Ejemplo n.º 1
0
// The main execution
int main(void)
{	
	int bla;
	bla = 0;
	
Start:
	srand(time(0));
	//srand(0);

    char filestring[500];
    snprintf(filestring, 500, "dim%usd%u-LLL.txt", N, SEED);
	
	importBasis(filestring);
	gramSchmidt();
	initKlein();
	initParams();
	initHashes();
	initStack();
    
	printf("=====  HashSieve  ======\n" );
	printf("Dimension (N):  %8d\n", N);
	printf("Hash length (K): %7d\n", K);
	printf("Hash tables (T): %7d\n", T);
	printf("Random seed:    %8d\n", SEED);
	//printf("Probe level:    %8d\n", PROBE);
	//printf("Hypersimplex:   %8d\n", SIMPLEX);
	printf("Target norm^2:  %8d\n", Target);
	printf("------------------------\n");
	
	// Some dummy variables used in the algorithm
	int i,j,k,m,n,t;
	LatticeVector** Candidates;
	LatticeVector* v;
	LatticeVector* w;
	int vHash[T];			// "Temporary" variable for hashes of a target vector v and its rotations
	LatticeVector vrot[N];
	int vHashp;				// Shifted hashes of v used for probing only
	int wHash[T];			// "Temporary" variable for hashes of a candidate vector w
	long long int vw;
	long long int vwAbs;
	long long int vwAbsDouble;
	long long int NCandidates;
	int vReduced;
	time_t start = time(NULL);
	time_t now;
	time_t end;
	int rot;
	
	LatticeVector *shortest;
	
	// The main algorithm loop
	while(Iteration < MAX_ITERATIONS && Collisions2 < MAX_COLLISIONS_2){
		Iteration++;
		
		// Get vector from stack, or sample a new one if the stack is empty
		if(StackLength == 0){
			if(VectorsLength == MAX_VECTORS){
				perror("Vector list overflow...\n");
        		goto End;
			}
			sampleKlein(&Vectors[VectorsLength++]);
			//sampleSimple(&Vectors[VectorsLength++]);
			while(Vectors[VectorsLength-1].Norm2 == 0 && Collisions1 < MAX_COLLISIONS_1){
				Collisions1++;
				sampleKlein(&Vectors[VectorsLength-1]);
				//sampleSimple(&Vectors[VectorsLength-1]);
			}
			v = &Vectors[VectorsLength-1];
		}
		else{
			v = Stack[--StackLength];
		}
		
		vReduced = 0;
		// Check each table for candidate near list vectors
		for(t = 0; t < T; t++){
				
			// Compute v's hash value
			vHash[t] = lshash(v, t);
			Hashes += K;
			Candidates = HashTables[t][vHash[t]].Pointers;
			NCandidates = HashTables[t][vHash[t]].Length;
			
			// Go through the list to find reducing vectors
			for(j = NCandidates - 1; j >= 0; j--){
				w = Candidates[j];
				vw = ip(v, w);				
				Comparisons++;
				vwAbs = (vw > 0 ? vw : -vw);
				vwAbsDouble = (vwAbs << 1);
			
				// Reduce v with w if possible
				if(vwAbsDouble > w->Norm2){
					add(v, w, vw);
					Reductions1++;
					vReduced = 1;
					goto vEnd;
				}
			
				// Reduce w with v if possible
				if(vwAbsDouble > v->Norm2){
					
					// Remove w from the hash tables
					for(int tt = 0; tt < T; tt++){
						wHash[tt] = lshash(w, tt);
						Hashes += K;
						bucketRemove(&HashTables[tt][wHash[tt]], w);
					}
					
					// Reduce w with v
					add(w, v, vw);
					
					Reductions2++;
					if(w->Norm2 > 0)
						Stack[StackLength++] = w;
					else
						Collisions2++;
				}
			}	
		}
		
vEnd:	// We have reached a decision for vector v
		// Push v to stack, list, or delete altogether
		if(vReduced == 0){
			if(v->Norm2 > 0){
				
				// Push v to the hash tables
				for(t = 0; t < T; t++)
					bucketAdd(&HashTables[t][vHash[t]], v);

				// Check for new minimum
				if(v->Norm2 < MinNorm2){
					now = time(NULL);
					printf("New minimum: %11llu (%5d sec)\n", v->Norm2, (now - start)); 
					MinNorm2 = v->Norm2;
				}
				if(v->Norm2 <= Target){
					now = time(NULL);
					printf("Target found: %10llu (%5d sec)\n", v->Norm2, (now - start));
					break;
				}
			}
			else{
				Collisions2++;
			}
		}
		else{
			// Append v to the stack
			Stack[StackLength++] = v;
		}
	}
	
End:
	end = time(NULL);
	printf("------------------------\n");
	
	// Formatting the time taken
	int Time = end - start;
	int Timesec = Time % 60;
	int Timemin = (Time / 60) % 60;
	int Timehr = (Time / 3600) % 24;
	int Timeday = (Time / 86400);
	if(Timeday == 0)					printf("Time:           %02u:%02u:%02u (hh:mm:ss)\n", Timehr, Timemin, Timesec);
	else								printf("Time:    (%3ud) %02u:%02u:%02u (hh:mm:ss)\n", Timeday, Timehr, Timemin, Timesec);
	
	// Formatting the main space costs
	double Space = (T * VectorsLength * sizeof(void*)) + (VectorsLength * N * sizeof(Vectors[0].Crd[0]));
	if(Space < 1000)					printf("Est. space: %12f (bytes)\n", Space);
	else if (Space < 1000000)			printf("Est. space: %12f (kB)\n", Space / 1000);
	else if (Space < 1000000000)		printf("Est. space: %12f (MB)\n", Space / 1000000);
	else if (Space < 1000000000000)		printf("Est. space: %12f (GB)\n", Space / 1000000000);
	else								printf("Est. space: %12f (TB)\n", Space / 1000000000000);
	
	printf("------------------------\n");
	printf("Iterations:   %10llu\n", Iteration);
	printf("Inner products\n");
	printf("- Comparing:%12llu\n", Comparisons);
	printf("- Hashing: %13llu\n", Hashes);
	printf("- Total:   %13llu\n", (Comparisons + Hashes));
	printf("Reductions v: %10llu\n", Reductions1);
	printf("Reductions w: %10llu\n", Reductions2);
	printf("Vectors:      %10llu\n", VectorsLength);
	printf("List length:  %10llu\n", VectorsLength - Collisions2 - StackLength);
	printf("Stack length: %10llu\n", StackLength);
	printf("Collisions\n");
	printf("- Sampling 0: %10llu\n", Collisions1);
	printf("- Reducing:   %10llu\n", Collisions2);
	printf("- Total:      %10llu\n", Collisions1 + Collisions2);
	printf("Shortest:     %10llu\n\n", MinNorm2);	
	
	bla++;
	if(bla < 1){
		goto Start;
	}

}
Ejemplo n.º 2
0
// All the setup stuff goes here
void setup() {
	initHashes();
	openCometSocket();
	openManagerSocket();
}