/** * Return the product of the primes between i and j inclusive * as well as phi of the product of primes. * TODO: Build a vector of primes and use a tree based multiplication. */ void mpz_primorial_range( mpz_t primorial, mpz_t phi, const uint32_t i, const uint32_t j) { mpz_t p; mpz_t t; mpz_init_set_ui(p, i-1); mpz_init(t); mpz_nextprime(p, p); mpz_set_ui(primorial, 1); mpz_set_ui(phi, 1); while (mpz_cmp_ui(p, j) <= 0) { gmp_printf("%Zd\n", p); mpz_sub_ui(t, p, 1); mpz_mul(primorial, primorial, p); mpz_mul(phi, phi, t); mpz_nextprime(p, p); } mpz_clear(t); mpz_clear(p); }
struct rsa_key new_rsa_key(int bits) { init_rsa(); mpz_t* e = (mpz_t*)calloc(1, sizeof(mpz_t)); mpz_t* d = (mpz_t*)calloc(1, sizeof(mpz_t)); mpz_t* n = (mpz_t*)calloc(1, sizeof(mpz_t)); mpz_t p, q, et; int r = 0; mpz_init(*e); mpz_init(*d); mpz_init(*n); mpz_init(p); mpz_init(q); mpz_init(et); while (r == 0) { mpz_urandomb(p, rsa_rand_state, bits/2); mpz_nextprime(p, p); mpz_urandomb(q, rsa_rand_state, bits/2); mpz_nextprime(q, q); mpz_mul(*n, p, q); mpz_sub_ui(p, p, 1); mpz_sub_ui(q, q, 1); mpz_mul(et, p, q); mpz_set_ui(*e, 3); r = mpz_invert(*d, *e, et); } return (rsa_key) {.e = e, .d = d, .n = n}; }
int main(void) { mpz_t p, q, N; mpz_init(p); mpz_init(q); mpz_init(N); // In a real application, p and q must be stored somewhere safe. pbc_mpz_randomb(p, 512); pbc_mpz_randomb(q, 512); mpz_nextprime(p, p); mpz_nextprime(q, q); mpz_mul(N, p, q); pbc_param_t param; pbc_param_init_a1_gen(param, N); pbc_param_out_str(stdout, param); pbc_param_clear(param); mpz_clear(p); mpz_clear(q); mpz_clear(N); return 0; }
int rsa_createkey(PRSAKEY pkey){ /* inisialisasi variabel helper */ unsigned long int k_int = 65537; PPRIVATEATTRIB prsaattrib = create_private_attrib(); if(prsaattrib==0x00) return -1; /* pick two random prime number (p and q ) */ mpz_urandomb(prsaattrib->p,prsaattrib->hrandstate,PRIMESIZE); mpz_nextprime(prsaattrib->p,prsaattrib->p); //gmp_randseed_ui(prsaattrib->hrandstate,GetTickCount()); mpz_urandomb(prsaattrib->q,prsaattrib->hrandstate,PRIMESIZE); mpz_nextprime(prsaattrib->q,prsaattrib->q); /* calculate n = (p * q) */ mpz_mul(prsaattrib->n,prsaattrib->q,prsaattrib->p); /* calculate z = (p-1) * ( q - 1) */ mpz_sub_ui(prsaattrib->pminus,prsaattrib->p,(unsigned int)1); mpz_sub_ui(prsaattrib->qminus,prsaattrib->q,(unsigned int)1); mpz_mul(prsaattrib->z,prsaattrib->pminus,prsaattrib->qminus); /* choose k, such that k is co-prime to z, i.e z is not divisible by k or in other word gcd(k,z) = 1 */ while(1){ mpz_gcd_ui(prsaattrib->gcd,prsaattrib->z,k_int); if(mpz_cmp_ui(prsaattrib->gcd,(unsigned long)1) == 0) break; k_int +=1; } mpz_set_ui(prsaattrib->k,k_int); /* calculate j for ( k * j ) mod z = 1 */ if(mpz_invert(prsaattrib->j,prsaattrib->k,prsaattrib->z) == 0){ /* cannot find j (multiplicative inverse) */ destroy_private_attrib(prsaattrib); return -1; } /* then we have public key = [n,k] */ mpz_get_str(pkey->public_key.strkey_k,16,prsaattrib->k); mpz_get_str(pkey->public_key.strkey_n,16,prsaattrib->n); /* and private key [n,j] */ mpz_get_str(pkey->private_key.strkey_j,16,prsaattrib->j); mpz_get_str(pkey->private_key.strkey_n,16,prsaattrib->n); /* clean up everything */ destroy_private_attrib(prsaattrib); return 0; }
/** * Returns an array p^(floor(log_p B)) for all primes p <= B. * @return An array p^(floor(log_p B)) for all primes p <= B. * Caller must free the returned array. * @param w is the number of prime powers. */ uint32_t* mpz_prime_powers(int* w, const uint32_t B) { mpz_t p; // prime mpz_t pp; // prime power int ppsi; // prime powers index unsigned long e; // exponent unsigned long exps[32]; // exponents int b; int i; double dB; uint32_t* prime_powers; mpz_init(p); mpz_init(pp); *w = count_primes(B); prime_powers = (uint32_t*)malloc(*w * sizeof(uint32_t)); // check the base cases if (B == 0 || B == 1) { return prime_powers; } // size of B b = msb_u32(B) + 1; // compute the i^th root of B, from 2 to log2(B)-1 dB = (double)B; exps[0] = 0; exps[1] = 0; for (i = 2; i < b; i ++) { exps[i] = (unsigned long)floor(pow(dB, 1.0/((double)i))); } // start with p=2 prime_powers[0] = 1U << (b-1); ppsi = 1; // let p=3 be the first odd prime mpz_set_ui(p, 3); e = b-1; while (ppsi < *w) { // reduce the exponent as appropriate while (e >= 2 && mpz_cmp_ui(p, exps[e]) > 0) { e --; } // compute the prime power mpz_pow_ui(pp, p, e); prime_powers[ppsi] = mpz_get_ui(pp); ppsi ++; // move to the next prime mpz_nextprime(p, p); } mpz_clear(pp); mpz_clear(p); return prime_powers; }
mpz_class computeRandomPrime(gmp_randclass& randomGenerator, unsigned int security) { mpz_class prime = randomGenerator.get_z_bits(security - 1); mpz_setbit(prime.get_mpz_t(), security - 1); mpz_nextprime(prime.get_mpz_t(), prime.get_mpz_t()); return prime; }
void run (char *start, int reps, char *end, short diffs[]) { mpz_t x, y; int i; mpz_init_set_str (x, start, 0); mpz_init (y); for (i = 0; i < reps; i++) { mpz_nextprime (y, x); mpz_sub (x, y, x); if (diffs != NULL && diffs[i] != mpz_get_ui (x)) { gmp_printf ("diff list discrepancy\n"); abort (); } mpz_set (x, y); } mpz_set_str (y, end, 0); if (mpz_cmp (x, y) != 0) { gmp_printf ("got %Zx\n", x); gmp_printf ("want %Zx\n", y); abort (); } mpz_clear (y); mpz_clear (x); }
/** * Computes n primorials that are for i from 1 to n, the * product of the first i primes greater than or equal to * first_prime. * @return array should be cleared using mpz_clear_array(). */ mpz_t* mpz_primorials(const int n, const int first_prime) { mpz_t* res = mpz_init_array(n); mpz_t p; int i; mpz_init_set_ui(p, first_prime-1); mpz_nextprime(p, p); mpz_set(res[0], p); for (i = 1; i < n; i ++) { mpz_nextprime(p, p); mpz_mul(res[i], res[i-1], p); } mpz_clear(p); return res; }
void makeprime( mpz_t out, int bits ) { mpz_urandomb( out, random_state, bits ); mpz_setbit( out, bits-1 ); mpz_nextprime( out, out ); }
// get next prime where p mod 4 = 3 static void bbs_pseudo_next_prime ( mpz_t arg ) { mpz_t r; mpz_t mod; mpz_init(r); mpz_init(mod); mpz_set(r,arg); loop:; mpz_nextprime(r,r); mpz_mod_ui(mod,r,4); if ( 0 != mpz_cmp_ui(mod,3) ) { #if 0 && defined(CP_TEST) printf("%s: redo\n",__FUNCTION__); #endif goto loop; } mpz_set(arg,r); mpz_clear(r); mpz_clear(mod); }
/** * Computes the product of the first w primes such that the product <= B */ void mpz_bounded_primorial(int* w, mpz_t primorial, mpz_t phi, const mpz_t B) { mpz_t p; mpz_t t; if (mpz_cmp_ui(B, 1) <= 0) { mpz_set_ui(primorial, 0); mpz_set_ui(phi, 0); *w = 0; return; } mpz_init_set_ui(p, 1); mpz_init(t); mpz_set_ui(primorial, 1); mpz_set_ui(phi, 1); *w = 0; do { mpz_nextprime(p, p); (*w) ++; mpz_mul(primorial, primorial, p); mpz_sub_ui(t, p, 1); mpz_mul(phi, phi, t); } while (mpz_cmp(primorial, B) <= 0); mpz_divexact(primorial, primorial, p); mpz_divexact(phi, phi, t); (*w) --; mpz_clear(p); mpz_clear(t); }
/*** * 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); }
void *CalculationThread(void *context) { PCALC_THREAD_CONTEXT tcontext = (PCALC_THREAD_CONTEXT)context; mpz_t num, nextprime; unsigned long p, i; mpz_init_set_ui(num, tcontext->st); mpz_init(nextprime); mpz_nextprime(nextprime, num); while ((p = mpz_get_ui(nextprime)) < tcontext->end) { mpz_set_ui(num, 0); printf("P=%lu\n", p); fflush(stdout); for (i = 0; i < p; i++) { mpz_setbit(num, p + i - 1); } if (executeSumCalculation(num) != 0) { printf("Thread %d - Found one: ", tcontext->threadIndex); mpz_out_str(stdout, 10, num); printf(" (P: %lu)\n", p); fflush(stdout); } // Next p value mpz_nextprime(nextprime, nextprime); } mpz_clear(num); // Set the termination bit to notify the arbiter that this thread needs to be respawned // with more work. pthread_mutex_lock(&TerminationMutex); TerminationBits |= (1 << tcontext->threadIndex); pthread_cond_signal(&TerminationVariable); pthread_mutex_unlock(&TerminationMutex); pthread_exit(context); }
int main() { mpz_class maxcount(45); mpz_class found(0); mpz_class check(0); for( mpz_nextprime(check.get_mpz_t(), check.get_mpz_t()); found < maxcount; mpz_nextprime(check.get_mpz_t(), check.get_mpz_t())) { //std::cout << "P" << check << " " << std::flush; if( is_mersenne_prime(check) ) { ++found; std::cout << "M" << check << " " << std::flush; } } }
RCP<const Integer> nextprime(const Integer &a) { mpz_class c; mpz_nextprime(c.get_mpz_t(), a.as_mpz().get_mpz_t()); return integer(c); }
int VNRW_GMP_GenKeys( VNAsymCryptCtx_t * ctx, int keyBits ) { mpz_t zrand; gmp_randstate_t state; struct vn_iovec seed; VNRW_GMP_Ctx_t * gmpCtx = VN_CONTAINER_OF( ctx, VNRW_GMP_Ctx_t, mCtx ); assert( VN_TYPE_VNRWSign_GMP == ctx->mType || VN_TYPE_VNRWEnc_GMP == ctx->mType ); keyBits = ( keyBits + 1 ) / 2; mpz_init( zrand ); VNIovecGetRandomBuffer( &seed, ( keyBits / 8 + 1 ) ); mpz_import( zrand, seed.i.iov_len, 1, 1, 0, 0, seed.i.iov_base ); gmp_randinit_default( state ); gmp_randseed( state, zrand ); do { mpz_urandomb( zrand, state, keyBits ); } while( keyBits != mpz_sizeinbase( zrand, 2 ) ); mpz_set( gmpCtx->mP, zrand ); do { mpz_nextprime( gmpCtx->mP, gmpCtx->mP ); } while ( mpz_fdiv_ui( gmpCtx->mP, 8 ) != 3 ); mpz_set( gmpCtx->mQ, gmpCtx->mP ); do { mpz_nextprime( gmpCtx->mQ, gmpCtx->mQ ); } while ( mpz_fdiv_ui( gmpCtx->mQ, 8 ) != 7 ); /* compute public key n */ mpz_mul( gmpCtx->mN, gmpCtx->mP, gmpCtx->mQ ); VNRW_CalcD( ctx ); gmp_randclear( state ); VNIovecFreeBufferAndTail( &seed ); mpz_clear( zrand ); return 0; }
static val_ptr run_nextprime(val_ptr v[]) { element_ptr e = v[0]->elem; mpz_t z; mpz_init(z); element_to_mpz(z, e); mpz_nextprime(z, z); element_set_mpz(e, z); return v[0]; }
void random_prime(mpz_t prime, unsigned long num_bits) { if (!rand_initialized) { init_rand(); } mpz_urandomb(prime, randstate, num_bits); mpz_nextprime(prime, prime); }
void genPrime(mpz_t prime, size_t length, gmp_randstate_t rand_state) { mpz_init(prime); do { mpz_urandomb(prime, rand_state, length); mpz_nextprime(prime, prime); } while (mpz_sizeinbase(prime, 2) != length); }
int main (int argc, char **argv) { int i; int reps = 20; gmp_randstate_ptr rands; mpz_t bs, x, nxtp, ref_nxtp; unsigned long size_range; tests_start(); rands = RANDS; run ("2", 1000, "0x1ef7", diff1); run ("3", 1000 - 1, "0x1ef7", NULL); run ("0x8a43866f5776ccd5b02186e90d28946aeb0ed914", 50, "0x8a43866f5776ccd5b02186e90d28946aeb0eeec5", diff3); run ("0x10000000000000000000000000000000000000", 50, "0x100000000000000000000000000000000010ab", diff4); run ("0x1c2c26be55317530311facb648ea06b359b969715db83292ab8cf898d8b1b", 50, "0x1c2c26be55317530311facb648ea06b359b969715db83292ab8cf898da957", diff5); mpz_init (bs); mpz_init (x); mpz_init (nxtp); mpz_init (ref_nxtp); if (argc == 2) reps = atoi (argv[1]); for (i = 0; i < reps; i++) { mpz_urandomb (bs, rands, 32); size_range = mpz_get_ui (bs) % 8 + 2; /* 0..1024 bit operands */ mpz_urandomb (bs, rands, size_range); mpz_rrandomb (x, rands, mpz_get_ui (bs)); /* gmp_printf ("%ld: %Zd\n", mpz_sizeinbase (x, 2), x); */ mpz_nextprime (nxtp, x); refmpz_nextprime (ref_nxtp, x); if (mpz_cmp (nxtp, ref_nxtp) != 0) abort (); } mpz_clear (bs); mpz_clear (x); mpz_clear (nxtp); mpz_clear (ref_nxtp); tests_end (); return 0; }
void genreateKeys(mpz_t privateKey, mpz_t publicKey) { //generate two primes mpz_t prime1, prime2; mpz_init(prime1); mpz_init(prime2); genRandNum(prime1); if(!isPrime(prime1)) { mpz_nextprime(prime1, prime1); } genRandNum(prime2); if(!isPrime(prime2)) { mpz_nextprime(prime2, prime2); } getKeysWithPrimes(prime1, prime2, publicKey, privateKey); }
/* Attempt factor with trial division of 500 primes. VERY SLOW */ mpz_class primes::bigPrimeFactorer(mpz_class &n) { mpz_class tmp(211); for (int j = 0; j < 500; ++j) { if (n % tmp == 0) { return tmp; } mpz_nextprime(tmp.get_mpz_t(), tmp.get_mpz_t()); } return 1; }
void init_factorbase(size_t bits) { int i; mpz_init_set_ui(fb[0], 2); for(i = 1; i < NRPRIMES; i++) { mpz_init(fb[i]); mpz_nextprime(fb[i], fb[i-1]); } return; }
unsigned long nextprime (unsigned long n) { mpz_t p; mpz_init_set_ui (p, n); mpz_nextprime (p, p); n = mpz_get_ui (p); mpz_clear (p); return n; }
//RSA SETUP !!! RSA::RSA() { //https://gmplib.org/list-archives/gmp-bugs/2008-March/000972.html mpz_t randnum1; mpz_t randnum2; mpz_t one; //initialize the 1 mpz_init(one); mpz_set_ui(global_one); mpz_t rand_seed; unsigned long int i, seed; gmp_randstate_t r_state; seed = 123456; gmp_randinit_default(r_state); gmp_randseed_ui(r_state,seed); mpz_init(randnum1); mpz_init(randnum2); mpz_urandomb(randnum1,r_state,100); //2^100 -1 range mpz_urandomb(randnum2,r_state,100); //2^100 -1 range mpz_t p, q, n; mpz_t p_sub_1, q_sub_1; mpz_init(p); mpz_init(q); mpz_init(n); mpz_nextprime(p,randnum1); mpz_nextprime(q,randnum2); mpz_mul(n,p,q); //compute n = pq //phi(n) = (p-1)(q-1) mpz_init(p_sub_1); mpz_init(q_sub_1); mpz_sub(p_sub_1,p,one); mpz_sub(q_sub_1,q,one); //randomly choose e in range(1,phi(n)) //modular inversion magic }
void pm1_plan_init(pm1_plan_t *plan, u_int32_t B1, u_int32_t B2) { u_int64_t p, q, q_max; size_t tmp_E_n_words; mpz_t E, P; mpz_init(E); mpz_init(P); plan->B1 = B1; plan->exp2 = 0; for(p = 1; p <= B1 / 2; p <<= 1) plan->exp2++; mpz_set_ui(P, 2); mpz_set_ui(E, 1); for(mpz_nextprime(P, P); mpz_cmp_ui(P, B1) <= 0; mpz_nextprime(P, P)) { p = mpz_get_ui(P); q_max = B1 / (p - 1); for(q = 1; q <= q_max; q *= p) mpz_mul_ui(E, E, p); } #ifdef VERBOSE_PLAN gmp_printf("E: %Zd\n", E); #endif plan->E = mpz_export(NULL, &tmp_E_n_words, -1, sizeof(u_int64_t), 0, 0, E); plan->E_n_words = (u_int32_t) tmp_E_n_words; plan->E_mask = 1ULL << 63; while((plan->E[plan->E_n_words - 1] & plan->E_mask) == 0) plan->E_mask >>= 1; stage2_plan_init(&plan->stage2, B1, B2); mpz_clear(E); mpz_clear(P); return; }
void gen_q(fmpz_t q, long len) { mpz_t tmp, hold; mpz_init(tmp); mpz_init(hold); mpz_set_ui(hold, 1); mpz_mul_2exp(tmp, hold, len); mpz_nextprime(hold, tmp); fmpz_set_mpz(q, hold); mpz_clear(tmp); mpz_clear(hold); }
void MathUtils::GetRandomPrime(mpz_class& rand_prime, unsigned int num_bits) { gmp_randclass r1 (gmp_randinit_default); r1.seed(GetTimeStamp()); mpz_class rand = r1.get_z_bits(num_bits); mpz_t p; mpz_init(p); mpz_nextprime(p, rand.get_mpz_t()); rand_prime = mpz_class(p); mpz_clear(p); }
void test_big3(){ bj_ostream& os = bj_out; bj_big_int_t p1, p2; p1 = 0; p2 = 500; for(long aa = 0; aa < 100; aa++){ os << "p1=" << p1 << " p2=" << p2 << bj_eol; mpz_nextprime(p1.get_mpz_t(), p2.get_mpz_t()); p2 = p1; } }
static void generate_primes(private_key *ku) { mpz_t tmp; mpz_init(tmp); srand(time(NULL)); /* Select p and q */ /* Start with p */ generate_random_p(ku->p); /* Make sure this is a good choice*/ /* If p mod e == 1, gcd(phi, e) != 1 */ mpz_mod(tmp, ku->p, ku->e); while(!mpz_cmp_ui(tmp, 1)) { /* Nope. Choose the next prime */ mpz_nextprime(ku->p, ku->p); mpz_mod(tmp, ku->p, ku->e); } /* Now select q, where q!=p */ do { generate_random_p(ku->q); /* Make sure this is a good choice*/ /* If p mod e == 1, gcd(phi, e) != 1 */ mpz_mod(tmp, ku->q, ku->e); while(!mpz_cmp_ui(tmp, 1)) { /* Nope. Choose the next prime */ mpz_nextprime(ku->q, ku->q); mpz_mod(tmp, ku->q, ku->e); } } while(mpz_cmp(ku->p, ku->q) == 0); /* If we have identical primes (unlikely), try again */ mpz_clear(tmp); }