コード例 #1
0
ファイル: primes.c プロジェクト: toma2004/Crytography
/*
 * print all primes number from 2 to maxval using Sieve of Eratosthenes algorithm
 * @param: maxval with conditin that maxval is in range 2-2^24
 * @output: print all prime numbers from 2 to maxval
 */
void primes(uint32_t maxval)
{
	/*
	 * Construct an array of uint32_t, and each bit of this array represent an integer from 2 to maxval to save space
	 */
	int arr_size = ceil_func((double)maxval/32, (int)maxval/32);

	uint32_t *se = malloc(arr_size*sizeof(uint32_t));
	uint32_t *holder = malloc(sizeof(uint32_t));
	//initialize all bits to 0
	for(int k = 0; k < arr_size; k++)
	{
		se[k] = 0x00;
	}

	//Initialize byte and bit
	int byteTH = 0;
	int bitTH = 31;

	uint32_t p = 2;
	uint32_t j = 0;
	while (p*p <= maxval)
	{
		j = p*p;
		while (j <= maxval)
		{
			//Determine the byteTH and bitTH to be set based on j
			if (j < 8)
			{
				bitTH = 31-(int)j;
				byteTH = 0;
			}
			else
			{
				bitTH = 31 - ((int)j%32);
				byteTH = (int)j / 32;
			}
			//set all non-prime number bits to 1
			se[byteTH] |= ((0x01) << bitTH);
			j += p;
		}
		//Continue to increment p until se[p] = 0 which means that it has not been marked as non-prime numbers in previous rounds
		p += 1;
		bitTH = 31 - ((int)p%32);
		byteTH = (int)p / 32;
		while((se[byteTH] & ((0x01) << bitTH)) != 0)
		{
			p += 1;
			//re-calculate bitTH and byteTH
			bitTH = 31 - ((int)p%32);
			byteTH = (int)p / 32;
		}
	}

	//First to print out the maxval value
	*holder = htonl(maxval);
	fwrite(holder, 1, sizeof(uint32_t), stdout);

	//Then go through the array se and print out the index of those bits that are NOT set
	//Note that we will start at 29th bit of the first uint32_t number in se array since the first 2 bits represent 0 and 1
	byteTH = 0;
	bitTH = 29;
	uint32_t index = 2;
	while (index <= maxval)
	{
		if((se[byteTH] & ((0x01) << bitTH)) == 0)
		{
			*holder = htonl(index);
			fwrite(holder, 1, sizeof(uint32_t), stdout);
		}
		index += 1;
		bitTH -= 1;
		if(bitTH < 0)
		{
			byteTH += 1;
			bitTH = 31;
		}
	}

	free(holder);
	free(se);
}
コード例 #2
0
PIPE_ALIGN_STACK
static boolean
test_round(unsigned verbose, FILE *fp)
{
   LLVMModuleRef module = NULL;
   LLVMValueRef test_round = NULL, test_trunc, test_floor, test_ceil;
   LLVMExecutionEngineRef engine = lp_build_engine;
   LLVMPassManagerRef pass = NULL;
   char *error = NULL;
   test_round_t round_func, trunc_func, floor_func, ceil_func;
   float unpacked[4];
   unsigned packed;
   boolean success = TRUE;
   int i;

   module = LLVMModuleCreateWithName("test");

   test_round = add_test(module, "round", lp_build_round);
   test_trunc = add_test(module, "trunc", lp_build_trunc);
   test_floor = add_test(module, "floor", lp_build_floor);
   test_ceil = add_test(module, "ceil", lp_build_ceil);

   if(LLVMVerifyModule(module, LLVMPrintMessageAction, &error)) {
      printf("LLVMVerifyModule: %s\n", error);
      LLVMDumpModule(module);
      abort();
   }
   LLVMDisposeMessage(error);

#if 0
   pass = LLVMCreatePassManager();
   LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine), pass);
   /* These are the passes currently listed in llvm-c/Transforms/Scalar.h,
    * but there are more on SVN. */
   LLVMAddConstantPropagationPass(pass);
   LLVMAddInstructionCombiningPass(pass);
   LLVMAddPromoteMemoryToRegisterPass(pass);
   LLVMAddGVNPass(pass);
   LLVMAddCFGSimplificationPass(pass);
   LLVMRunPassManager(pass, module);
#else
   (void)pass;
#endif

   round_func = (test_round_t) pointer_to_func(LLVMGetPointerToGlobal(engine, test_round));
   trunc_func = (test_round_t) pointer_to_func(LLVMGetPointerToGlobal(engine, test_trunc));
   floor_func = (test_round_t) pointer_to_func(LLVMGetPointerToGlobal(engine, test_floor));
   ceil_func = (test_round_t) pointer_to_func(LLVMGetPointerToGlobal(engine, test_ceil));

   memset(unpacked, 0, sizeof unpacked);
   packed = 0;

   if (0)
      LLVMDumpModule(module);

   for (i = 0; i < 3; i++) {
      v4sf xvals[3] = {
         {-10.0, -1, 0, 12.0},
         {-1.5, -0.25, 1.25, 2.5},
         {-0.99, -0.01, 0.01, 0.99}
      };
      v4sf x = xvals[i];
      v4sf y, ref;
      float *xp = (float *) &x;
      float *refp = (float *) &ref;

      printf("\n");
      printv("x            ", x);

      refp[0] = round(xp[0]);
      refp[1] = round(xp[1]);
      refp[2] = round(xp[2]);
      refp[3] = round(xp[3]);
      y = round_func(x);
      printv("C round(x)   ", ref);
      printv("LLVM round(x)", y);
      compare(ref, y);

      refp[0] = trunc(xp[0]);
      refp[1] = trunc(xp[1]);
      refp[2] = trunc(xp[2]);
      refp[3] = trunc(xp[3]);
      y = trunc_func(x);
      printv("C trunc(x)   ", ref);
      printv("LLVM trunc(x)", y);
      compare(ref, y);

      refp[0] = floor(xp[0]);
      refp[1] = floor(xp[1]);
      refp[2] = floor(xp[2]);
      refp[3] = floor(xp[3]);
      y = floor_func(x);
      printv("C floor(x)   ", ref);
      printv("LLVM floor(x)", y);
      compare(ref, y);

      refp[0] = ceil(xp[0]);
      refp[1] = ceil(xp[1]);
      refp[2] = ceil(xp[2]);
      refp[3] = ceil(xp[3]);
      y = ceil_func(x);
      printv("C ceil(x)    ", ref);
      printv("LLVM ceil(x) ", y);
      compare(ref, y);
   }

   LLVMFreeMachineCodeForFunction(engine, test_round);
   LLVMFreeMachineCodeForFunction(engine, test_trunc);
   LLVMFreeMachineCodeForFunction(engine, test_floor);
   LLVMFreeMachineCodeForFunction(engine, test_ceil);

   LLVMDisposeExecutionEngine(engine);
   if(pass)
      LLVMDisposePassManager(pass);

   return success;
}
コード例 #3
0
ファイル: rndsearch.c プロジェクト: toma2004/Crytography
/*
 * rndsearch function to generate numbits-bit probable prime
 * @param: k numbits, maxitr, FILE pointer to primesfile, FILE pointer to rndfile
 * @output: Print out rndsearch process and generate a probable prime
 */
void rndsearch(int k, int maxitr, FILE *fp, FILE *fp_rndfile)
{
	int byte = 0;
	int x_byte = ceil_func((double)k/8, (int)k/8);

	char *temp = malloc(x_byte); //read in x_byte from rndfile
	int itr_count = 1;
	while((byte = fread(temp, 1, x_byte, fp_rndfile)) > 0)
	{
		BIGNUM *bn_n = RndOddNum(k, temp, x_byte);
		if(bn_n == NULL)
		{
			free(temp);
			return; //ERROR has happened in RndOddNum() function call
		}
		else
		{
			//Print itr
			fprintf(stdout, "RANDOM-SEARCH: iteration %d\n", itr_count);
			itr_count += 1;
			fprintf(stdout, "  n = %s\n", BN_bn2dec(bn_n));

			//Use trial division function to check if n passes trial division test
			int trialdiv_returnCode = trialdiv(bn_n, fp, 2);
			//reset fp pointer to beginning of primesfile for next time use
			rewind(fp);
			if(trialdiv_returnCode == -1)
			{
				//error has happened in trialdiv function. Terminate this function
				free(temp);
				BN_free(bn_n); //free from RndOddNum() function call
				return;
			}
			else if(trialdiv_returnCode == 0)
			{
				BN_free(bn_n);
				continue; //bn_n does not pass trial division test. Continue to generate next number
			}

			int millerrabin_returnCode = millerrabin(bn_n, maxitr, fp, 2);
			//reset fp pointer to beginning of primesfile for next time use
			rewind(fp);
			if(millerrabin_returnCode == -1)
			{
				//error has happened in millerrabin function. Terminate this function
				free(temp);
				BN_free(bn_n); //free from RndOddNum() function call
				return;
			}
			else if(millerrabin_returnCode == 1)
			{
				free(temp);
				BN_free(bn_n); //free from RndOddNum() function call
				return; //Prove n to be a prime number. End function
			}
			else
			{
				//continue generate the next probable prime
				BN_free(bn_n);
			}
		}
	}
	//Free in case we have never generated a bn_n that passes both tests
	free(temp);
	fprintf(stderr, "Run out of bytes in rndfile and we have not successfully generated a prime number\n");
}