コード例 #1
0
ファイル: baseOT.cpp プロジェクト: muelli/OTExtension
BOOL BaseOT::GMP_Init(int secparam, BYTE* seed) {
	m_SecParam = secparam;
	mpz_init(m_NPState.p);
	mpz_init(m_NPState.g);
	mpz_init(m_NPState.q);
	switch (secparam)
	{
	case 1024:
		mpz_set_str(m_NPState.p, ifcp1024, 16);	mpz_set_str(m_NPState.g, ifcg1024, 16);	mpz_set_str(m_NPState.q, ifcq1024, 16); break;
	case 2048:
		mpz_set_str(m_NPState.p, ifcp2048, 16);	mpz_set_str(m_NPState.g, ifcg2048, 16);	mpz_set_str(m_NPState.q, ifcq2048, 16); break;
	case 3072:
		mpz_set_str(m_NPState.p, ifcp3072, 10);	mpz_set_str(m_NPState.g, ifcg3072, 10);	mpz_set_str(m_NPState.q, ifcq3072, 10); break;
	default:
		mpz_set_str(m_NPState.p, ifcp1024, 16);	mpz_set_str(m_NPState.g, ifcg1024, 16);	mpz_set_str(m_NPState.q, ifcq1024, 16);	m_SecParam = 1024; break;
	}
	
	//TODO: Seed gmp from seed using gmp_randseed (gmp randstate t state, mpz t seed )
	gmp_randinit_mt (m_NPState.rnd_state ); 
	m_NPState.field_size = mpz_sizeinbase(m_NPState.p, 2)/8;
	/*if(secparam != 2048 || secparam != 1024)
	{
		mpz_cdiv_q_ui(m_NPState.q, m_NPState.p, 2);
		mpz_sub_ui(m_NPState.q, m_NPState.p, 1);
	}*/
	return true;
}
コード例 #2
0
void init_rand() {

   gmp_randinit_mt(randstate);
   gmp_randseed_ui(randstate, time(0));

   rand_initialized = 1;
}
コード例 #3
0
ファイル: main.c プロジェクト: BenjiG/SICA5_Crypto_subject2
int generate_key(ptr_curve_t E, ptr_point_t G, ptr_point_t Q, bases_t d_bases)
{
	mpz_t n;
	mpz_init(n);
	mpz_ui_pow_ui(n,2,M);

	if(DEBUG)gmp_printf("%Zd\n",n);

	gmp_randstate_t rand_stat;
	gmp_randinit_mt(rand_stat);
	gmp_randseed_ui(rand_stat,time(NULL));

	mpz_t d;
	mpz_init(d);
	mpz_urandomb(d,rand_stat,M);

	if(DEBUG)gmp_printf("%Zd\n",d);


	bases_init(d_bases);
	int_to_bases(d,d_bases);

	point_init(Q);

	multiple_point_CE(E,G,d_bases,Q);
	if(DEBUG)
	{
		bases_print(d_bases);
		printf("\n");
		point_print(Q,"Q");
	}
	char buffer [1024];
	FILE *pub, *priv;
	pub = fopen("./pub.key","w+");
	if(pub != NULL){
		memset(buffer, '\0', sizeof(buffer));
		bases_to_string(Q->_x,buffer);
		fputs(buffer,pub);
		fputs(";",pub);
		memset(buffer, '\0', sizeof(buffer));
		bases_to_string(Q->_y,buffer);
		fputs(buffer,pub);
		fclose(pub);
	}
	else
		return ERROR;

	priv = fopen("./priv.key","w+");
	if(priv != NULL){
		memset(buffer, '\0', sizeof(buffer));
		bases_to_string(d_bases,buffer);
		fputs(buffer,priv);
		fclose(priv);
	}
	else
		return ERROR;

	return NERROR;
}//generate_key()
コード例 #4
0
ファイル: utility.c プロジェクト: toddr/Math-Prime-Util-GMP
void init_randstate(unsigned long seed) {
#if (__GNU_MP_VERSION > 4) || (__GNU_MP_VERSION == 4 && __GNU_MP_VERSION_MINOR >= 2)
    /* MT was added in GMP 4.2 released in 2006. */
    gmp_randinit_mt(_randstate);
#else
    gmp_randinit_default(_randstate);
#endif
    gmp_randseed_ui(_randstate, seed);
}
コード例 #5
0
ファイル: SecretShare.cpp プロジェクト: PICCO-Team/picco
SecretShare::SecretShare(int p, int t, mpz_t mod){
	peers = p;
	threshold = t;
	mpz_init(fieldSize);
	//getPrime(fieldSize, bits);
	//gmp_printf("%Zd\n", fieldSize);
	mpz_set(fieldSize, mod);
	computeSharingMatrix();
	computeLagrangeWeight();
	gmp_randinit_mt(rstate); 
}
コード例 #6
0
ファイル: rmath.cpp プロジェクト: GilesBathgate/RapCAD
void r_rand_seed(int seed)
{
#if USE_CGAL
	gmp_randinit_mt(state);
	mpz_t n;
	mpz_init_set_ui(n,seed);
	gmp_randseed(state,n);
#else
	srand(seed);
#endif
}
コード例 #7
0
ファイル: t4.c プロジェクト: Stavrosnco/cudaRSA
void generateRandomPrime(mpz_t *num) {
   mpz_t temp;
   mp_bitcnt_t bits = 512;
   gmp_randstate_t state;

   seedOff++; 
   mpz_init(temp);

   time_t result = time(NULL);
   gmp_randinit_mt(state);
   gmp_randseed_ui(state, result + seedOff);

   mpz_urandomb(temp, state, bits);

   mpz_nextprime(*num, temp);
}
コード例 #8
0
ファイル: gmp_pbs_client.c プロジェクト: robgjansen/pbs
void gmp_pbs_client_init(gmp_pbs_client_state *state) {
	/* initiate the state for a signature */
	mpz_init(state->r);
	mpz_init(state->c);
	mpz_init(state->s);
	mpz_init(state->d);
	mpz_init(state->e);
	mpz_init(state->a);
	mpz_init(state->b);

	mpz_init(state->t1);
	mpz_init(state->t2);
	mpz_init(state->t3);
	mpz_init(state->t4);
	mpz_init(state->epsilon);

	mpz_init(state->signature.delta);
	mpz_init(state->signature.rho);
	mpz_init(state->signature.sigma);
	mpz_init(state->signature.omega);

	/* workspace used by the client */
	mpz_init(state->workspace.alpha);
	mpz_init(state->workspace.beta);
	mpz_init(state->workspace.z);
	mpz_init(state->workspace.work1);
	mpz_init(state->workspace.work2);
	mpz_init(state->workspace.work3);

	/* parameters and keys */
	mpz_init(state->parameters.p);
	mpz_init(state->parameters.q);
	mpz_init(state->parameters.g);
	mpz_init(state->pk.key);

	/* import our parameters and keys from file */
	gmp_pbs_import_parameters(&state->parameters, param_filename);
	gmp_pbs_import_key(&state->pk, pk_filename);

	/* our random generator, mersenne twister */
	gmp_randinit_mt(state->random);
	/* FIXME get random seed from true random source */
	gmp_randseed_ui(state->random, 987654321);

	gmp_pbs_client_reset(state);
}
コード例 #9
0
ファイル: gmp_pbs_bank.c プロジェクト: robgjansen/pbs
void gmp_pbs_bank_init(gmp_pbs_bank_state *state) {
	/* initiate the state for a signature */
	mpz_init(state->u);
	mpz_init(state->r);
	mpz_init(state->c);
	mpz_init(state->s);
	mpz_init(state->d);
	mpz_init(state->e);
	mpz_init(state->a);
	mpz_init(state->b);

	/* workspace used by the bank */
	mpz_init(state->workspace.alpha);
	mpz_init(state->workspace.beta);
	mpz_init(state->workspace.z);
	mpz_init(state->workspace.work1);
	mpz_init(state->workspace.work2);
	mpz_init(state->workspace.work3);

	/* parameters and keys */
	mpz_init(state->parameters.p);
	mpz_init(state->parameters.q);
	mpz_init(state->parameters.g);
	mpz_init(state->sk.key);
	mpz_init(state->pk.key);

	/* import our parameters and keys from file */
	gmp_pbs_import_parameters(&state->parameters, param_filename);
	gmp_pbs_import_key(&state->sk, sk_filename);
	gmp_pbs_import_key(&state->pk, pk_filename);

	/* our random generator, mersenne twister */
	gmp_randinit_mt(state->random);
	/* FIXME get random seed from true random source */
	gmp_randseed_ui(state->random, 123456789);

	/* sanity check, g^x =? y */
	mpz_powm(state->workspace.work1, state->parameters.g, state->sk.key, state->parameters.p);
	if (mpz_cmp(state->workspace.work1, state->pk.key) != 0) {
		printf("Failed sanity check during bank initialization: g^x != pk\n");
	}

	/* get our required random state values */
	gmp_pbs_bank_reset(state);
}
コード例 #10
0
BOOL GMPInit(SECLVL lvl, BYTE* seed, fparams* params) {
	mpz_init(params->ifcparams.p);
	mpz_init(params->ifcparams.g);
	mpz_init(params->ifcparams.q);

	if(lvl.ifcbits == ST.ifcbits)
	{
		mpz_set_str(params->ifcparams.p, ifcp1024, 16);	mpz_set_str(params->ifcparams.g, ifcg1024, 16);
		mpz_set_str(params->ifcparams.q, ifcq1024, 16); params->secparam = 1024;
	} else if(lvl.ifcbits == MT.ifcbits)
	{
		mpz_set_str(params->ifcparams.p, ifcp2048, 16);	mpz_set_str(params->ifcparams.g, ifcg2048, 16);
		mpz_set_str(params->ifcparams.q, ifcq2048, 16); params->secparam = 2048;
	} else if(lvl.ifcbits == LT.ifcbits)
	{
		mpz_set_str(params->ifcparams.p, ifcp3072, 10);	mpz_set_str(params->ifcparams.g, ifcg3072, 10);
		mpz_set_str(params->ifcparams.q, ifcq3072, 10); params->secparam = 3072;
	} else //Short term security
	{
		mpz_set_str(params->ifcparams.p, ifcp1024, 16);	mpz_set_str(params->ifcparams.g, ifcg1024, 16);
		mpz_set_str(params->ifcparams.q, ifcq1024, 16);	params->secparam = 1024;
	}

	/*switch (lvl.ifcbits)
	{
	case ST.ifcbits:
		mpz_set_str(m_NPState.p, ifcp1024, 16);	mpz_set_str(m_NPState.g, ifcg1024, 16);	mpz_set_str(m_NPState.q, ifcq1024, 16); m_nSecParam = 1024; break;
	case MT.ifcbits:
		mpz_set_str(m_NPState.p, ifcp2048, 16);	mpz_set_str(m_NPState.g, ifcg2048, 16);	mpz_set_str(m_NPState.q, ifcq2048, 16); m_nSecParam = 2048; break;
	case LT.ifcbits:
		mpz_set_str(m_NPState.p, ifcp3072, 10);	mpz_set_str(m_NPState.g, ifcg3072, 10);	mpz_set_str(m_NPState.q, ifcq3072, 10); m_nSecParam = 3072; break;
	default:
		mpz_set_str(m_NPState.p, ifcp1024, 16);	mpz_set_str(m_NPState.g, ifcg1024, 16);	mpz_set_str(m_NPState.q, ifcq1024, 16);	m_nSecParam = 1024; break;
	}*/

	//TODO: Seed gmp from seed using gmp_randseed (gmp randstate t state, mpz t seed )
	gmp_randinit_mt (params->ifcparams.rnd_state );
	params->elebytelen = (params->secparam+7)/8;//(mpz_sizeinbase(m_NPState.p, 2)+7)/8;
	//m_NPField.secparam  = m_nSecParam;

	return true;
}
コード例 #11
0
ファイル: misc.c プロジェクト: Cl3Kener/gmp
/* Call (*func)() with various random number generators. */
void
call_rand_algs (void (*func) (const char *, gmp_randstate_ptr))
{
  gmp_randstate_t  rstate;
  mpz_t            a;

  mpz_init (a);

  gmp_randinit_default (rstate);
  (*func) ("gmp_randinit_default", rstate);
  gmp_randclear (rstate);

  gmp_randinit_mt (rstate);
  (*func) ("gmp_randinit_mt", rstate);
  gmp_randclear (rstate);

  gmp_randinit_lc_2exp_size (rstate, 8L);
  (*func) ("gmp_randinit_lc_2exp_size 8", rstate);
  gmp_randclear (rstate);

  gmp_randinit_lc_2exp_size (rstate, 16L);
  (*func) ("gmp_randinit_lc_2exp_size 16", rstate);
  gmp_randclear (rstate);

  gmp_randinit_lc_2exp_size (rstate, 128L);
  (*func) ("gmp_randinit_lc_2exp_size 128", rstate);
  gmp_randclear (rstate);

  /* degenerate always zeros */
  mpz_set_ui (a, 0L);
  gmp_randinit_lc_2exp (rstate, a, 0L, 8L);
  (*func) ("gmp_randinit_lc_2exp a=0 c=0 m=8", rstate);
  gmp_randclear (rstate);

  /* degenerate always FFs */
  mpz_set_ui (a, 0L);
  gmp_randinit_lc_2exp (rstate, a, 0xFFL, 8L);
  (*func) ("gmp_randinit_lc_2exp a=0 c=0xFF m=8", rstate);
  gmp_randclear (rstate);

  mpz_clear (a);
}
コード例 #12
0
ファイル: abc.c プロジェクト: WeamMikkawi/IbnSinaTHing
void encrypt(int msg/*To be extended to other types !*/){
	// generate r between 0 and n-1 inclusive 
	unsigned long int m = msg;
	gmp_randstate_t state;
	gmp_randinit_mt (state); // initiallize state with Mersenne Twister which is basically fast one !
	mpz_urandomm(r, state, n);
//66666666666666666666666666666666666
	//mpz_init_set_str(r,"23",10);
//6666666666666666666666666666666666	
	gmp_printf ("\nr = %Zd\n", r);
	mpz_powm_ui(tmpI, g/*base*/, m/*exp*/, nn/*mod*/); // tmp = base^exp modulo mod
	gmp_printf ("\ng^m = %Zd\n", tmpI);
	mpz_powm(tmpII, r/*base*/, n/*exp*/, nn/*mod*/); // tmp = base^exp modulo mod
	gmp_printf ("\nr^n = %Zd\n", tmpII);
	// Remember : a.b mod n = ((a mod n).(b mod n)) mod n
	mpz_mul(c, tmpI, tmpII);
	mpz_mod(c, c, nn);

	gmp_printf ("\nc = %Zd\n", c);
}
コード例 #13
0
ファイル: hec.c プロジェクト: fason/stigmergy
static void
_hec_init (const mpz_t pub_n, const mpz_t pub_g)
{
    mpz_init (n);
    mpz_init (g);
    mpz_init (n2);
    
    mpz_set (n, pub_n);
    mpz_set (g, pub_g);
    mpz_mul (n2, n, n);
    
    gmp_randinit_mt (state);
    unsigned long seed;
    FILE* fp = fopen ("/dev/urandom", "rb");
    if (fp == NULL)
        seed = time(NULL);
    else {
        fread (&seed, sizeof(unsigned long), 1, fp);
        fclose (fp);
    }
    gmp_randseed_ui (state, seed);
}
コード例 #14
0
ファイル: rsa.cpp プロジェクト: devendradora/Programs-CSENITW
int main(){

	mpz_t p , q , N ,phi_N,e,d,M,C,res;
	mpz_inits(p,q,N,phi_N,e,d,M,C,res,NULL);

	gmp_randstate_t state ;
	gmp_randinit_mt(state);

 //Manual input of p and q
     //int repeats=5,primecheck=0;   
	// printf("Enter p \n");
	// gmp_scanf("%Zd",&p);
	// //mpz_inp_str(p,stdin,10);
	// primecheck = mpz_probab_prime_p(p,repeats);
	// if(primecheck != 2)
	// {printf(" p is not prime . Error code : %d \n",primecheck);exit(1);
 //    }
	// printf("Enter q \n");
	// gmp_scanf("%Zd",&q);
	// //mpz_inp_str(q,stdin,10);
	// primecheck = mpz_probab_prime_p(q,repeats);
	// if(primecheck!= 2)
	// {printf(" q is not prime . Error code : %d \n",primecheck);exit(1);
 //    }
    

    // Randomly generating p and q
	//b - bits uradomb ( 0- 2^n -1 ) urandom  (0 - n-1)
	mpz_urandomb(p,state,512);  
	mpz_nextprime(p,p);
	gmp_printf("p : %Zd \n",p );

	mpz_urandomb(q,state,512);
	mpz_nextprime(q,q);
	gmp_printf("q : %Zd \n",q );

	printf("Enter M \n");
	gmp_scanf("%Zd",&M);
	

	mpz_mul(N,p,q);
	gmp_printf("N : %Zd \n",N );

    mpz_sub_ui(p,p,1);
    mpz_sub_ui(q,q,1);
	mpz_mul(phi_N,p,q);
	gmp_printf("phi_N : %Zd \n",phi_N );


	// mpz_urandomm(e,state,phi_N);
	// mpz_nextprime(e,e);
	
	
	mpz_set_ui(e,2);
     mpz_gcd(res,e,phi_N);
	 while( mpz_cmp_ui(res,1) != 0 ){
	 	  mpz_add_ui(e,e,1);
	 	  mpz_gcd(res,e,phi_N);
                   
	 }


	gmp_printf("e : %Zd \n",e);

	gmp_printf("\n\nPublic key : { %Zd , %Zd \n }",e,N);

	mpz_invert(d,e,phi_N);

	gmp_printf("\n\nd : %Zd \n",d);
	gmp_printf("\n\nPrivate key : { %Zd , %Zd \n }",d,N);

	mpz_powm (C , M , e , N );

	gmp_printf("\n\nC : %Zd \n",C );

	mpz_powm (M , C , d, N );

	gmp_printf("\n\nM : %Zd \n",M );




}
コード例 #15
0
ファイル: abc.c プロジェクト: WeamMikkawi/IbnSinaTHing
/*
Generate Private Public Keys
*/
void keysGeneration(){
	//char* initNbChar = malloc(sizeof(char) * (initNbLength+1));
	char* initNbChar;
	//mpz_init(gcdResult);
	//mpz_init(nn);
	//mpz_init(lambda);
	do{
		 initNbChar = malloc(sizeof(char) * (initNbLength+1));
		generateInitNb(initNbChar, initNbLength);
		mpz_set_str(initNbMpz, initNbChar, 10/*decimal*/);
		mpz_nextprime (p, initNbMpz);
		
		initNbChar = malloc(sizeof(char) * (initNbLength+1));
		generateInitNb(initNbChar, initNbLength);
		mpz_set_str(initNbMpz, initNbChar, 10/*decimal*/);
		mpz_nextprime (q, initNbMpz);

//6666666666666666666666666666666666666
		//mpz_init_set_str(p,"7",10);
		//mpz_init_set_str(q,"11",10);
//6666666666666666666666666666




		mpz_sub_ui(p_1, p, 1);
		mpz_sub_ui(q_1, q, 1);
		mpz_mul(n, p, q);
		mpz_mul(tmpI, p_1, q_1);
		mpz_gcd(gcdResult, tmpI, n);
		
	}while(mpz_cmp_ui(gcdResult, 1) != 0); // check if (gcd(pq, (p-1)(q-1)) = 1
//	mpz_mul(n, p, q);
		mpz_mul(nn, n, n);
		mpz_lcm(lambda, p_1, q_1);
		
	// generate a number g within the range 0 to n*n-1 inclusive ! 
	// this way i can get rid of using mod n^2 on this step 
	gmp_randstate_t state;
	gmp_randinit_mt (state); // initiallize state with Mersenne Twister which is basically fast one !
	mpz_urandomm(g, state, nn);
//66666666666666666666666666666666
	//mpz_init_set_str(g,"5652",10);
//666666666666666666666666666666666666666
	// Now we check if g is good enough for us 
	mpz_powm(tmpI, g/*base*/, lambda/*exp*/, nn/*mod*/); // tmp = base^exp modulo mod 
	// now L(u) = (u-1)/n
	mpz_sub_ui(tmpI, tmpI, 1);
	mpz_tdiv_q(tmpI, tmpI, n);

	mpz_gcd(gcdResult, n, tmpI);
	if (mpz_cmp_ui(gcdResult, 1) != 0){
		printf("G is not proper !!!\n");
		exit(EXIT_FAILURE);
	}
	


	printf("Keys Generation Result : \n");
	printf("Start -- \n");
	printf("Public Key  : \n");
	gmp_printf ("\nn = %Zd\n", n);
	gmp_printf ("\ng= %Zd\n", g);
	printf("\nPrivate Key  : \n");
	gmp_printf ("\nlambda = %Zd\n", lambda);
	printf("Micro :3 \n");
	printf("End --  \n");
	
	
}
コード例 #16
0
ファイル: randdef.c プロジェクト: BrianGladman/mpir
void
gmp_randinit_default (gmp_randstate_t rstate)
{
  gmp_randinit_mt (rstate);
}
コード例 #17
0
/**
 * Start at a random md5 hash and search through all possible hashes to find
 * a string that hashes to itself.
 */
int main(void)
{
	/* there are 2^128 possible md5 hashes */
	mpz_t max;
	mpz_init(max);
	mpz_ui_pow_ui(max, 2, MD5_BITS);

	/* initialize PRNG with MT algorithm and seed with time */
	gmp_randstate_t rand_state;
	gmp_randinit_mt(rand_state);
	gmp_randseed_ui(rand_state, time(NULL));
	
	/* pick a random one to start with and free PRNG memory */
	mpz_t start;
	mpz_init(start);
	mpz_urandomb(start, rand_state, MD5_BITS);
	gmp_randclear(rand_state);

	printf("Starting with random string: \"");
	mpz_out_str(stdout, HEX_BASE, start);
	printf("\"\n");
	
	/* current hash */
	mpz_t current;
	mpz_init_set(current, start);

	/* get ready to calculate hashes */
	MD5_CTX md5_ctx;
	unsigned char input[MD5_BYTES];
	unsigned char digest[MD5_BYTES];
	char input_str[MD5_STR_LEN + 1];
	char digest_str[MD5_STR_LEN + 1];

	/* search from start until max */
	while(mpz_cmp(current, max) < 0)
	{
		/* initialize MD5 and get current as an unsigned char[] */
		MD5_Init(&md5_ctx);
		mpz_export(input, NULL, 1, 1, 0, 0, current);
		
		/* hash the input string */
		hex_str(input_str, input, MD5_BYTES);
		MD5_Update(&md5_ctx, input_str, MD5_STR_LEN);
		MD5_Final(digest, &md5_ctx);

		/* check for a match */
		if(strncmp(input, digest, MD5_BYTES) == 0)
		{
			/* we have a winner! */
			hex_str(digest_str, digest, MD5_BYTES);
			printf("md5(\"%s\") = \"%s\"\n", input_str, digest_str);
		}

		/* keep looking */
		mpz_add_ui(current, current, 1);
	}

	/* now search backwards from start */
	mpz_set(current, start);
	mpz_sub_ui(current, current, 1);

	while(mpz_cmp_ui(current, 0) >= 0)
	{
		/* initialize MD5 and get current as an unsigned char[] */
		MD5_Init(&md5_ctx);
		mpz_export(input, NULL, 1, 1, 0, 0, current);
		
		/* hash the input string */
		hex_str(input_str, input, MD5_BYTES);
		MD5_Update(&md5_ctx, input_str, MD5_STR_LEN);
		MD5_Final(digest, &md5_ctx);

		/* check for a match */
		if(strncmp(input, digest, MD5_BYTES) == 0)
		{
			/* we have a winner! */
			hex_str(digest_str, digest, MD5_BYTES);
			printf("md5(\"%s\") = \"%s\"\n", input_str, digest_str);
		}

		/* keep looking */
		mpz_sub_ui(current, current, 1);
	}

	/* clean up */
	mpz_clear(max);
	mpz_clear(start);
	mpz_clear(current);
}
コード例 #18
0
ファイル: interface.c プロジェクト: naa/SLE
int main(int argc, char **argv)
{
  lattice_neighbors=square_lattice_neighbors;
  lattice_dx=square_lattice_dx;
  lattice_dy=square_lattice_dy;
  
  gmp_randinit_mt(state);
  gmp_randseed_ui(state, time(NULL));
  mpf_init2(rop,256);
  //  wang_landau(5,25,1.0,20,0.8,5,1.5, 0.05, 30);
  //  return 0;

  
  double T=2.269185;
  double step=0.01;
  int M=50;
  int N=50;
  int steps = 100;
  long measures=10000;
  double p=1.0;
  char *method="";

  int flags, opt;
  int nsecs, tfnd;

  while ((opt = getopt(argc, argv, "M:N:T:S:s:p:m:a:L:")) != -1) {
    switch (opt) {
    case 'M':
      M=atoi(optarg);
      break;
    case 'N':
      N=atoi(optarg);
      break;
    case 'T':
      T = atof(optarg);
      break;
    case 'S':
      step = atof(optarg);
      break;
    case 's':
      steps = atoi(optarg);
      break;
    case 'p':
      p = atof(optarg);
      break;
    case 'm':
      measures=atol(optarg);
      break;
    case 'a':
      method=optarg;
      break;
    case 'L':
      if ((strlen(optarg)==6) && (strcmp("triang",optarg)==0)) {
	lattice_neighbors=triangular_lattice_neighbors;
	lattice_dx=triangular_lattice_dx;
	lattice_dy=triangular_lattice_dy;
      }
      break;
      
    default: /* '?' */
      fprintf(stderr, "Usage: %s [-M xsize] [-N ysize] [-T temperature] [-S temperature step] [-s number of steps] [-p non-empty probability] [-m measures] [-a algorithm:metropolis|wolf|interface|interface_cluster] [-L lattice:square|triang]  \n",
	      argv[0]);
      exit(EXIT_FAILURE);
    }
  }
  

  //  printf("M:[\n");
  if ((strlen(method)==4) && (strcmp("wolf",method)==0))
    for (int i=0;i<steps;i++) {
      T=T+step;
      //      printf("Temperature: %lf \n",T);
      measure_susceptibility_cluster(M,N, init_table_with_p(M,N,p),1/T,measures);
      //      printf("\n");
    }
  else if ((strlen(method)==10) && (strcmp("metropolis",method)==0))
    for (int i=0;i<steps;i++) {
      T=T+step;
      printf("Temperature: %lf \n",T);      
      measure_susceptibility(M,N, init_table_with_p(M,N,p),1/T,measures);
      printf("\n");
    } 
  else if  ((strlen(method)==9) && (strcmp("interface",method)==0))
    draw_interface(M,N,allocate_2d_rectangle(M,N),1/T);
  else
    generate_interfaces(M,N,allocate_2d_rectangle(M,N),1/T,measures);    
  //  printf("[%lf,%lf] ", T,M);
  //  printf("]\n");
  
  //  print_Ul(T,step,steps, measures, size,4*size);
  //  print_Ul(T,step,steps, measures, 2*size,3*size);
  mpf_clear(rop);
  return 0;
}
コード例 #19
0
ファイル: crypt.c プロジェクト: MFreeze/m2moca
// {{{ Random Number Management
void rnInit() {
    gmp_randinit_mt(seed);
    seed_initialized = 1;
}
コード例 #20
0
int generatePrime(mpz_t *p,mpz_t *q,mpz_t *n,int number)
{
	int i;

	gmp_randstate_t rstate_p[number],rstate_q[number];

	for(i = 0; i < number; i++)
	{
		gmp_randinit_mt(rstate_p[i]);
		gmp_randinit_mt(rstate_q[i]);
	}


	for(i = 0; i < number; i++)
	{

		//Seeds to generate random number
		gmp_randseed_ui(rstate_p[i],randomSeed());
		gmp_randseed_ui(rstate_q[i],randomSeed());

		do{
			do{
				mpz_urandomb(*(p+i),rstate_p[i],32); // generate random number p less than 2^32-1

			}while(mpz_sizeinbase(*(p+i),2)!=32); //checks whether p is 32 bit or not


		}while(!isPrimeNumber(*(p+i)) || (!mpz_congruent_ui_p(*(p+i),3,4))); //checks whether p is prime or not with 500 witnesses


		do{
			do{
				mpz_urandomb(*(q+i),rstate_q[i],32); // generate random number q less than 2^32-1

			}while(mpz_sizeinbase(*(q+i),2)!=32); //checks whether p is 32 bit or not


		}while(!isPrimeNumber(*(q+i)) || (!mpz_congruent_ui_p(*(q+i),3,4))); //checks whether q is prime or not with 500 witnesses


		mpz_mul (*(n+i),*(p+i),*(q+i));

	}

/*	for(i = 0; i < number; i++)
	{
		//Seeds to generate random number
		gmp_randseed_ui(rstate_q[i],randomSeed());

		do{
			do{
				mpz_urandomb(*(q+i),rstate_q[i],32); // generate random number q less than 2^32-1

			}while(mpz_sizeinbase(*(q+i),2)!=32); //checks whether p is 32 bit or not


		}while(!isPrimeNumber(*(q+i)) || (!mpz_congruent_ui_p(*(q+i),3,4))); //checks whether q is prime or not with 500 witnesses

	}*/


	return TRUE;
}
コード例 #21
0
ファイル: dhclient.c プロジェクト: fredoste101/diffie-hellman
int dh(unsigned char * k, int knum,unsigned char * n, int nnum, int s){

	if(VISIBLE){
		printf("\n\n****** Diffie-Hellman begin ******\n\n");
	}

	int i,j=0,itemp;	
	mpz_t p,g,a,A,B,S;
	char buf[BUF_SIZE],*string_A,*string_p, *string_g, *tempkey;
	unsigned long int rand;
	size_t count;
	unsigned int temp;
	FILE *fp;

	mpz_init(a);
	mpz_init(A);
	mpz_init(B);
	mpz_init(S);

	// Setts g and p according to RFC 3526 1536 bits. http://tools.ietf.org/html/rfc3526 
	mpz_init_set_str(g,"2",10);
	mpz_init_set_str(p,"FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1 \
      29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD \
      EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245 \
      E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED \
      EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE45B3D \
      C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8 FD24CF5F \
      83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D \
      670C354E 4ABC9804 F1746C08 CA237327 FFFFFFFF FFFFFFFF", 16);
	
	if(VISIBLE){
		printf("\np:\n");
		mpz_out_str(NULL,10,p);
		printf("\n\ng:\n");
		mpz_out_str(NULL,10,g);
		printf("\n");
	}

	//Randomizar a ( Bättre seed nu )
	gmp_randstate_t state;
	gmp_randinit_mt(state);	// Initializerar state random måstes för att urandomm ska funka

	fp = fopen("/dev/urandom", "r");
	fread(&rand, 8, 1, fp);
	fclose(fp);
	
	gmp_randseed_ui(state,rand);	

	mpz_urandomm(a,state,p);	// Ger a random värde mellan 0 <= a <= p-1

	if(VISIBLE){
		printf("\na:\n");
		mpz_out_str(NULL,10,a);
		printf("\n");
	}

	// ^Frågan är om urandom skall användas för att generara hela A??????

	mpz_powm_sec(A,g,a,p);	// g^a mod p = A

	if(VISIBLE){
		printf("\nA:\n");
		mpz_out_str(NULL,10,A);
		printf("\n");
	}

	string_p = (char *)mpz_export (NULL,&count,1,1,0,0, p); // Antal bytes i word är 1

	// Sätter första meddelandet till standard: (bokstav(1 byte(char)),storlek(4 byte(unsigned int)),content und so weiter...)


	i=0;
	buf[i++] = 'p';
	memcpy(&buf[i],(char *)&count,sizeof(unsigned int));		//Detta för att hålla någon slags struktur
	i = i+sizeof(unsigned int);

	itemp = i;
	for(;i<count+itemp;i++){
		buf[i]=string_p[i-itemp];
	}

	string_g = (char *)mpz_export(NULL,&count,1,1,0,0,g);

	buf[i++] = 'g';
	memcpy(&buf[i],(char *)&count,sizeof(unsigned int));
	i = i+sizeof(unsigned int);
	itemp = i;

	for(;i<count+itemp;i++){
		buf[i]=string_g[i-itemp];
	}
	
	string_A = (char *)mpz_export(NULL,&count,1,1,0,0,A);

	buf[i++] = 'A';
	memcpy(&buf[i],(char *)&count,sizeof(unsigned int));
	i = i+sizeof(unsigned int);
	itemp = i;
	for(;i<count+itemp;i++){
		buf[i]=string_A[i-itemp];
	}

	if(write(s,buf,sizeof(buf))==-1){
		return 0;		//Diffie-hellman fail
	}

	read(s,buf,BUF_SIZE);		//Read answer from server _!check!_

	if(buf[j]=='B'){
		j++;
		memcpy(&temp,(unsigned int *)&buf[j],sizeof(unsigned int));
		j+=4;
		mpz_import(B,temp,1,1,0,0,&buf[j]);
		mpz_powm_sec(S,B,a,p);							// B^a mod p = S
	}

	if(VISIBLE){
		printf("\nS:\n");
		mpz_out_str(NULL,10,S);
		printf("\n");	
	}	

	tempkey = (char *)mpz_export(NULL,&count,1,1,0,0,S);		//Fyllar sedan nyckeln med detta för det är mer random

	// Fyller riktig nyckel och nonce

	for(i=0;i<knum;i++){
		k[i] = tempkey[i];
	}
	itemp = i;
	for(;i<nnum+itemp;i++){
		n[i-itemp] = tempkey[i];
	}

	if(VISIBLE){
		printf("\n\n****** Diffie-Hellman end ******\n\n");
	}

	return 1;

}