static void dm_ddh_setup_dec(struct dm_ddh_crs *crs, struct params *p) { mpz_t x, y; int file; unsigned long seed; mpz_inits(x, y, NULL); /* fix seed of random number generator */ gmp_randseed_ui(p->rnd, 0UL); find_generator(crs->g0, p); random_element(y, p); mpz_powm(crs->g1, crs->g0, y, p->p); mpz_powm(crs->h0, crs->g0, x, p->p); mpz_powm(crs->h1, crs->g1, x, p->p); mpz_clears(x, y, NULL); /* re-seed random number generator */ if ((file = open("/dev/urandom", O_RDONLY)) == -1) { (void) fprintf(stderr, "Error opening /dev/urandom\n"); } else { if (read(file, &seed, sizeof seed) == -1) { (void) fprintf(stderr, "Error reading from /dev/urandom\n"); (void) close(file); } } gmp_randseed_ui(p->rnd, seed); (void) close(file); }
void tests_rand_start (void) { gmp_randstate_ptr rands; char *perform_seed; unsigned long seed; if (__gmp_rands_initialized) { printf ("Please let tests_start() initialize the global __gmp_rands.\n"); printf ("ie. ensure that function is called before the first use of RANDS.\n"); abort (); } gmp_randinit_default (__gmp_rands); __gmp_rands_initialized = 1; rands = __gmp_rands; perform_seed = getenv ("GMP_CHECK_RANDOMIZE"); if (perform_seed != NULL) { #ifdef HAVE_STRTOUL seed = strtoul (perform_seed, 0, 0); #else /* This will not work right for seeds >= 2^31 on 64-bit machines. Perhaps use atol unconditionally? Is that ubiquitous? */ seed = atoi (perform_seed); #endif if (! (seed == 0 || seed == 1)) { printf ("Re-seeding with GMP_CHECK_RANDOMIZE=%lu\n", seed); gmp_randseed_ui (rands, seed); } else { #if HAVE_GETTIMEOFDAY struct timeval tv; gettimeofday (&tv, NULL); seed = tv.tv_sec ^ (tv.tv_usec << 12); seed &= 0xffffffff; #else time_t tv; time (&tv); seed = tv; #endif gmp_randseed_ui (rands, seed); printf ("Seed GMP_CHECK_RANDOMIZE=%lu (include this in bug reports)\n", seed); } fflush (stdout); } }
static PyObject * GMPy_RandomState_Factory(PyObject *self, PyObject *args) { RandomState_Object *result; MPZ_Object *temp; if (!(result = GMPy_RandomState_New())) { return NULL; } if (PyTuple_GET_SIZE(args) == 0) { gmp_randseed_ui(result->state, 0); } else if (PyTuple_GET_SIZE(args) == 1) { if (!(temp = GMPy_MPZ_From_Integer(PyTuple_GET_ITEM(args,0), NULL))) { Py_DECREF((PyObject*)result); TYPE_ERROR("seed must be an integer"); return NULL; } gmp_randseed(result->state, temp->z); Py_DECREF((PyObject*)temp); } else { Py_DECREF((PyObject*)result); TYPE_ERROR("random_state() requires 0 or 1 integer arguments"); return NULL; } return (PyObject*)result; }
static object make_random_state(object rs) { object z; if (rs==Cnil) rs=symbol_value(Vrandom_state); if (rs!=Ct && type_of(rs) != t_random) { FEwrong_type_argument(sLrandom_state, rs); return(Cnil); } z = alloc_object(t_random); init_gmp_rnd_state(&z->rnd.rnd_state); if (rs == Ct) gmp_randseed_ui(&z->rnd.rnd_state,RS_DEF_INIT); else memcpy(z->rnd.rnd_state._mp_seed->_mp_d,rs->rnd.rnd_state._mp_seed->_mp_d, rs->rnd.rnd_state._mp_seed->_mp_alloc*sizeof(*z->rnd.rnd_state._mp_seed->_mp_d)); #if __GNU_MP_VERSION > 4 || (__GNU_MP_VERSION == 4 && __GNU_MP_VERSION_MINOR >= 2) z->rnd.rnd_state._mp_algdata._mp_lc=&Mersenne_Twister_Generator_Noseed; #endif return(z); }
long long testPerformance(long long tests, int bit_count, int repetitions) { gmp_randstate_t RAND; gmp_randinit_default(RAND); gmp_randseed_ui(RAND, getTime()); mpz_t base; mpz_init_set_ui(base, 1); mpz_mul_2exp(base, base, bit_count-1); // base = 2^(bit_count-1) mpz_t n; mpz_init(n); long long time = 0; int test; for (test = 0; test < tests; test++) { mpz_urandomb(n, RAND, bit_count-1); mpz_add(n, n, base); // n has bit_count bits time += measureTime(n, repetitions); } mpz_clear(n); mpz_clear(base); gmp_randclear(RAND); return time / tests; // Average }
static pk_t * pkMatrix_rand(long n, long k) { long i, j; mpzMatrix_t * A = mpzMatrix_init(n, n); pk_t * Z; mpz_t * data; gmp_randstate_t state; gmp_randinit_default(state); gmp_randseed_ui(state, rand()); data = mpzMatrix_data(A); for (i = 0; i < A->nrows; ++i) { mpz_set_ui(data[i*A->nrows + i], 1); } for (i = 0; i < k; ++i) { long c = rand() % n; for (j = 0; j <= c; ++j) { mpz_urandomb(data[j*n+c], state, 10); } } gmp_randclear(state); Z = pkMatrix_fromFull(A); mpzMatrix_fini(A); return Z; }
void init_rsa() { if (rsa_inited) return; rsa_inited = 1; gmp_randinit_default(rsa_rand_state); gmp_randseed_ui(rsa_rand_state, 5128); }
void init_rand() { gmp_randinit_mt(randstate); gmp_randseed_ui(randstate, time(0)); rand_initialized = 1; }
/* Test operands from a table of seed data. This variant creates the operands using a division chain. This is a hack for better coverage of the gcd code, which depends on that the random number generators give the exact numbers we expect. */ void check_kolmo2 (void) { static const struct { unsigned int seed; int nb, chain_len; } data[] = { { 917, 15, 5 }, { 1032, 18, 6 }, { 1167, 18, 6 }, { 1174, 18, 6 }, { 1192, 18, 6 }, }; gmp_randstate_t rs; mpz_t bs, a, b, want; int i; gmp_randinit_default (rs); mpz_inits (bs, a, b, want, NULL); for (i = 0; i < numberof (data); i++) { gmp_randseed_ui (rs, data[i].seed); make_chain_operands (want, a, b, rs, data[i].nb, data[i].nb, data[i].chain_len); one_test (a, b, want, -1); } mpz_clears (bs, a, b, want, NULL); gmp_randclear (rs); }
void SRPGlobalInit() { mpz_init2(N, 256); mpz_import(N, 32, -1, sizeof(modulus[0]), 0, 0, modulus); gmp_randinit_default(gmprand); gmp_randseed_ui(gmprand, RandomGenSecure()); }
static void tests_rand_start (void) { gmp_randstate_ptr rands; char *perform_seed; unsigned long seed; if (__gmp_rands_initialized) { printf ( "Please let tests_start() initialize the global __gmp_rands, i.e.\n" "ensure that function is called before the first use of RANDS.\n"); exit (1); } gmp_randinit_default (__gmp_rands); __gmp_rands_initialized = 1; rands = __gmp_rands; perform_seed = getenv ("GMP_CHECK_RANDOMIZE"); if (perform_seed != NULL) { seed = strtoul (perform_seed, NULL, 10); if (! (seed == 0 || seed == 1)) { printf ("Re-seeding with GMP_CHECK_RANDOMIZE=%lu\n", seed); gmp_randseed_ui (rands, seed); } else { #ifdef HAVE_GETTIMEOFDAY struct timeval tv; gettimeofday (&tv, NULL); seed = tv.tv_sec + tv.tv_usec; #else time_t tv; time (&tv); seed = tv; #endif gmp_randseed_ui (rands, seed); printf ("Seed GMP_CHECK_RANDOMIZE=%lu " "(include this in bug reports)\n", seed); } } else gmp_randseed_ui (rands, 0x2143FEDC); }
int BnetSRP3::init( const char* username_, const char* password_, const byte * salt_ ) { if(!srp3_inited) { gmp_randinit_default(rand); gmp_randseed_ui(rand, (unsigned long)time(NULL)); mpz_init2(N, 256); mpz_import(N, 32, -1, 1, 0, 0, SRP3_N); mpz_init_set_ui(g, SRP3_g); srp3_inited = true; } uint i; const char* source; if (!username_) { return -1; } uint len = strlen(username_); username.resize(len); source = username_; for(i = 0; i < len; ++ i) { username[i] = toupper(*(source++)); } if (!((password_ == NULL) ^ (salt_ == NULL))) { return -1; } if (password_!=NULL) { len = strlen(password_); password.resize(len); source = password_; for(i = 0; i < len; ++ i) { password[i] = toupper(*(source++)); } mpz_init2(a, 256); mpz_init(b); mpz_urandomm(a, rand, N); /* generates the private key */ mpz_t s; mpz_init2(s, 256); mpz_urandomb(s, rand, 256); mpz_export(raw_salt, NULL, 1, 4, -1, 0, s); } else { password = ""; mpz_init(a); mpz_init2(b, 256); mpz_urandomm(b, rand, N); setSalt(salt_); } B_inited = false; return 0; }
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()
MEXP(nls_t*) nls_init_l(const char* username, unsigned long username_length, const char* password, unsigned long password_length) { unsigned int i; char* d; /* destination */ const char* o; /* original */ nls_t* nls; nls = (nls_t*) malloc(sizeof(nls_t)); if (!nls) return (nls_t*) 0; nls->username_len = username_length; nls->password_len = password_length; nls->username = (char*) malloc(nls->username_len + 1); nls->password = (char*) malloc(nls->password_len + 1); if (!nls->username || !nls->password) { free(nls); return (nls_t*) 0; } d = (char*) nls->username; o = username; for (i = 0; i < nls->username_len; i++) { *d = (char) toupper(*o); d++; o++; } *((char*) nls->username + username_length) = 0; *((char*) nls->password + password_length) = 0; d = (char*) nls->password; o = password; for (i = 0; i < nls->password_len; i++) { *d = (char) toupper(*o); d++; o++; } mpz_init_set_str(nls->n, NLS_VAR_N_STR, 16); gmp_randinit_default(nls->rand); gmp_randseed_ui(nls->rand, nls_pre_seed()); mpz_init2(nls->a, 256); mpz_urandomm(nls->a, nls->rand, nls->n); /* generates the private key */ /* The following line replaces preceding 2 lines during testing. */ /*mpz_init_set_str(nls->a, "1234", 10);*/ nls->A = (char*) 0; nls->S = (char*) 0; nls->K = (char*) 0; nls->M1 = (char*) 0; nls->M2 = (char*) 0; return nls; }
/*** * get premier number */ void getNumberPremier(mpz_t nbP,int seed) { gmp_randstate_t state; gmp_randinit_default (state); gmp_randseed_ui(state,seed); mpz_urandomb(nbP,state,KEY_LENGTH / 2); mpz_nextprime(nbP, nbP); }
RandomState::RandomState(RandomState * rs) : TypedObject(WIDETAG_RANDOM_STATE) { gmp_randinit_default(_state); if (rs) gmp_randseed(_state, rs->_state->_mp_seed); else gmp_randseed_ui(_state, time(NULL)); }
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); }
/* * It is a function that generate random number * input: variable to be returned(mpz_t) and the bitsize(int) of the number * output: variable where argument in main function must be void e.g. int main(void) */ void RandomNumber(mpz_t num, int bitsize){ unsigned long int seed; gmp_randstate_t state; srand(time(NULL)); seed = rand(); gmp_randinit_default (state); gmp_randseed_ui(state, seed); mpz_urandomb(num, state, bitsize); }
int main(int argc, char const *argv[]) { gmp_randstate_t state; unsigned long int i, seed; // Get a timestamp and use it as our seed // (this is generally regarded as a 'bad idea') time_t nowTime = time(0); seed = (unsigned long int) nowTime; // Initiate random state gmp_randinit_default(state); gmp_randseed_ui(state, seed); // Generate p, q, and n mpz_t p; mpz_init(p); stupidPrimeGenerator(p, BIT_LENGTH, state); mpz_t q; mpz_init(q); stupidPrimeGenerator(q, BIT_LENGTH, state); mpz_t n; mpz_init(n); mpz_mul(n, p, q); // Generate e and totient mpz_sub_ui(p, p, 1); mpz_sub_ui(q, q, 1); mpz_t e; mpz_init(e); mpz_set_ui(e, 65537); mpz_t totient; mpz_init(totient); mpz_mul(totient, p, q); // Debug: // gmp_printf("p=%Zd\n\nq=%Zd\n\ne=%Zd\n\n", p, q, e); // Calculate d mpz_t negativeOne; mpz_init(negativeOne); mpz_set_si(negativeOne, -1); mpz_t d; mpz_init(d); mpz_powm(d, e, negativeOne, totient); // Debug: // gmp_printf("d=%Zd\n\nn=%Zd\n\n", d, n); mpz_t inputMessage; mpz_init(inputMessage); mpz_t encryptedMessage; mpz_init(encryptedMessage); mpz_t decryptedMessage; mpz_init(decryptedMessage); // Read in message printf("Input message: "); mpz_inp_str(inputMessage, stdin, 10); // Encrpyt message mpz_powm(encryptedMessage, inputMessage, e, n); gmp_printf("\nEncrypted message: %Zd\n\n", encryptedMessage); // Decrypt message mpz_powm(decryptedMessage, encryptedMessage, d, n); gmp_printf("Decrypted message: %Zd\n\n", decryptedMessage); // size_t count = 1024; // char *outputMessage = (char*) calloc(count, sizeof(char)); // mpz_export(outputMessage, &count, 1, sizeof(char), 1, 0, decryptedMessage); // gmp_printf("Decrypted message:%d\n", outputMessage); }
void tests_rand_start (void) { gmp_randstate_ptr rands; char *perform_seed; unsigned long seed; if (__gmp_rands_initialized) { printf ("Please let tests_start() initialize the global __gmp_rands.\n"); printf ("ie. ensure that function is called before the first use of RANDS.\n"); abort (); } rands = RANDS; perform_seed = getenv ("GMP_CHECK_RANDOMIZE"); if (perform_seed != NULL) { seed = atoi (perform_seed); if (! (seed == 0 || seed == 1)) { printf ("Re-seeding with GMP_CHECK_RANDOMIZE=%lu\n", seed); gmp_randseed_ui (rands, seed); } else { #if HAVE_GETTIMEOFDAY struct timeval tv; gettimeofday (&tv, NULL); seed = tv.tv_sec ^ (tv.tv_usec << 12); #else time_t tv; time (&tv); seed = tv; #endif gmp_randseed_ui (rands, seed); printf ("Seed GMP_CHECK_RANDOMIZE=%lu (include this in bug reports)\n", seed); } fflush (stdout); } }
void mpfr_bench(mpfr_prec_t prec_a, mpfr_prec_t prec_b, mpfr_prec_t prec_c, const char *b_str, const char *c_str, unsigned long seed) { mpfr_t a,b,c; mpf_t x,y,z; mpz_t zz; gmp_randstate_t state; gmp_randinit_lc_2exp_size (state, 128); gmp_randseed_ui (state, seed); mpfr_init2(a, prec_a); mpfr_init2(b, prec_b); mpfr_init2(c, prec_c); mpf_init2(x, prec_a); mpf_init2(y, prec_b); mpf_init2(z, prec_c); if (b_str) mpf_set_str(y, b_str, 10); else mpf_urandomb(y, state, prec_b); if (c_str) mpf_set_str(z, c_str, 10); else mpf_urandomb(z, state, prec_c); mpfr_set_f(b, y, MPFR_RNDN); mpfr_set_f(c, z, MPFR_RNDN); mpz_init (zz); mpz_urandomb (zz, state, 2*prec_b); if (verbose) { printf("B="); mpfr_out_str(stdout, 10, 0, b, MPFR_RNDD); printf("\nC="); mpfr_out_str(stdout, 10, 0, c, MPFR_RNDD); putchar('\n'); } TIMP_OVERHEAD (); #undef BENCH #define BENCH(TEST_STR, TEST) printf(" "TEST_STR": %Lu\n", TIMP_MEASURE(TEST)) TEST_LIST; mpz_clear (zz); mpfr_clear (a); mpfr_clear (b); mpfr_clear (c); mpf_clear (x); mpf_clear (y); mpf_clear (z); gmp_randclear (state); }
void PerlCryptDHGMP_mpz_rand_set(pTHX_ mpz_t *v, unsigned int bits) { gmp_randstate_t state; gmp_randinit_default(state); /* Perl_seed should come with Perl 5.8.1. You shouldn't be using Perl older than that, or at least you should be supplying me with a patch */ gmp_randseed_ui(state, Perl_seed(aTHX)); mpz_urandomb(*v, state, bits); gmp_randclear(state); }
void init(){ mpz_init(big_temp); mpz_init(n); mpz_init(n_plus_1); mpz_init(n_square); mpz_init(r); mpz_init(r_pow_n); mpz_init(d); mpz_init(d_inverse); gmp_randinit_default(state); gmp_randseed_ui(state, time(NULL)); #if 0 //if (mpz_set_str(n, "179681631915977638526315179067310074434153390395025087607016290555239821629901731559598243352941859391381209211619271844002852733873844383750232911574662592776713675341534697696513241904324622555691981004726000585832862539270063589746625628692671893634789450932536008307903467370375372903436564465076676639793", 10) == -1) { if (mpz_set_str(n, "32317006071311007300714876688666765257611171752763855809160912665177570453236751584543954797165007338356871062761077928870875400023524429983317970103631801129940018920824479704435252236861111449159643484346382578371909996991024782612350354546687409434034812409194215016861565205286780300229046771688880430612167916071628041661162278649907703501859979953765149466990620201855101883306321620552981581118638956530490592258880907404676403950212619825592177687668726740525457667131084835164607889060249698382116887240122647424904709577375839097384133219410477128893432015573232511359202702215050502392962065602621373646633", 10) == -1) { printf("\n n = %s is invalid \n", n); exit(0); } #endif // Get the values of n and d from already generated file get_n_and_d_from_file(); gmp_printf("n read = %Zd\n", n); gmp_printf("d read = %Zd\n", d); mpz_add_ui(n_plus_1, n, 1); mpz_pow_ui(n_square, n, 2); //d=lcm(p-1, q-1) #if 0 //if (mpz_set_str(d, "11230101994748602407894698691706879652134586899689067975438518159702488851868858222474890209558866211961325575726204490250178295867115273984389556973416410372977387708241492548657648934473794873887265114170151559636690542947614482279486573108720489183236783924737117351777821606184702946528449106783160617728", 10) == -1) { if (mpz_set_str(d, "16158503035655503650357438344333382628805585876381927904580456332588785226618375792271977398582503669178435531380538964435437700011762214991658985051815900564970009460412239852217626118430555724579821742173191289185954998495512391306175177273343704717017406204597107508430782602643390150114523385844440215305904188722327789239808208805874958140879652760769485822638556957710893419557319777578361302813366793271881989825323106333296234499565420424474500540721018071974424406358805685133447529623729447991492181271325655526833481571159446598626059774013428343748622306000136795074246791265885436988193283384961017822594", 10) == -1) { printf("\n d = %s is invalid \n", d); exit(0); } #endif if (mpz_invert (d_inverse, d, n_square) == 0) { printf("\n%s\n", "d^-1 does not exist!"); exit(0); } }
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); }
static void seed_from_tod (gmp_randstate_ptr rands) { unsigned long seed; #if HAVE_GETTIMEOFDAY struct timeval tv; gettimeofday (&tv, NULL); seed = tv.tv_sec ^ (tv.tv_usec << 12); seed &= 0xffffffff; #else time_t tv; time (&tv); seed = tv; #endif gmp_randseed_ui (rands, seed); printf ("Seed GMP_CHECK_RANDOMIZE=%lu (include this in bug reports)\n", seed); }
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); }
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); }
/* Test operands from a table of seed data. This variant creates the operands using plain ol' mpz_rrandomb. This is a hack for better coverage of the gcd code, which depends on that the random number generators give the exact numbers we expect. */ void check_kolmo1 (void) { static const struct { unsigned int seed; int nb; const char *want; } data[] = { { 59618, 38208, "5"}, { 76521, 49024, "3"}, { 85869, 54976, "1"}, { 99449, 63680, "1"}, {112453, 72000, "1"} }; gmp_randstate_t rs; mpz_t bs, a, b, want; int i, unb, vnb, nb; gmp_randinit_default (rs); mpz_inits (bs, a, b, want, NULL); for (i = 0; i < numberof (data); i++) { nb = data[i].nb; gmp_randseed_ui (rs, data[i].seed); mpz_urandomb (bs, rs, 32); unb = mpz_get_ui (bs) % nb; mpz_urandomb (bs, rs, 32); vnb = mpz_get_ui (bs) % nb; mpz_rrandomb (a, rs, unb); mpz_rrandomb (b, rs, vnb); mpz_set_str_or_abort (want, data[i].want, 0); one_test (a, b, want, -1); } mpz_clears (bs, a, b, want, NULL); gmp_randclear (rs); }
/* * It is a function that generates random prime numbers * input: prime (mpz_t) for retrieving the prime and bitUsed for determining the bits size * output: mpz_t prime will be captured in host if parameter of main is void, e.g. main(void) */ void generatePrime(mpz_t prime, int bitUsed){ int check; mpz_t rop; unsigned long int seed; gmp_randstate_t state; srand(time(NULL)); seed = rand(); gmp_randinit_default (state); gmp_randseed_ui(state, seed); mpz_init(rop); int cond = 1; for(check = 0; check != 1;) { switch(cond){ case 1: mpz_urandomb(rop, state, bitUsed); printf("."); if(CheckOdd(rop) == 1){ cond = 2; }else{ cond = 1; } break; case 2: if(TrialDivision(rop) == 1){ cond = 3; }else{ cond = 1; } break; case 3: if(Miller(rop,10) == 1){ check = 1; }else{ cond = 1; } break; } } printf("\n"); mpz_set(prime, rop); gmp_randclear(state); mpz_clear(rop); return 0; }
void gen_prime(unsigned int b_size, mpz_t res) { gmp_randstate_t state; struct timeval r_time; gettimeofday(&r_time,NULL); gmp_randinit_default(state); gmp_randseed_ui(state,(r_time.tv_sec * 1000) + (r_time.tv_usec / 1000)); do { mpz_urandomb(res,state,b_size); mpz_setbit(res,0); mpz_setbit(res,b_size - 1); mpz_setbit(res,b_size - 2); } while(rabin_miller(res,20,state) == 0); gmp_randclear(state); }