// Refine the random square mpfr_prec_t Refine(gmp_randstate_t r, mpfr_prec_t prec, long num = 1) const { if (num <= 0) return prec; // Use _vx as scratch prec += num * chunk_; mpfr_div_2ui(_eps, _eps, num * chunk_, MPFR_RNDN); mpz_urandomb(_ui, r, num * chunk_); mpfr_set_prec(_up, prec); mpfr_set_z_2exp(_up, _ui, -prec, MPFR_RNDN); mpfr_set_prec(_vx, prec); mpfr_add(_vx, _u, _up, MPFR_RNDN); mpfr_swap(_u, _vx); // u = vx mpfr_add(_up, _u, _eps, MPFR_RNDN); mpz_urandomb(_vi, r, num * chunk_); mpfr_set_prec(_vp, prec); mpfr_set_z_2exp(_vp, _vi, -prec, MPFR_RNDN); mpfr_set_prec(_vx, prec); mpfr_add(_vx, _v, _vp, MPFR_RNDN); mpfr_swap(_v, _vx); // v = vx mpfr_add(_vp, _v, _eps, MPFR_RNDN); return prec; }
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}; }
void check_random () { gmp_randstate_ptr rands; mpz_t bs; unsigned long arg_size, size_range; mpq_t q, r; int i; mp_bitcnt_t shift; int reps = 10000; rands = RANDS; mpz_init (bs); mpq_init (q); mpq_init (r); for (i = 0; i < reps; i++) { mpz_urandomb (bs, rands, 32); size_range = mpz_get_ui (bs) % 11 + 2; /* 0..4096 bit operands */ mpz_urandomb (bs, rands, size_range); arg_size = mpz_get_ui (bs); mpz_rrandomb (mpq_numref (q), rands, arg_size); do { mpz_urandomb (bs, rands, size_range); arg_size = mpz_get_ui (bs); mpz_rrandomb (mpq_denref (q), rands, arg_size); } while (mpz_sgn (mpq_denref (q)) == 0); /* We now have a random rational in q, albeit an unnormalised one. The lack of normalisation should not matter here, so let's save the time a gcd would require. */ mpz_urandomb (bs, rands, 32); shift = mpz_get_ui (bs) % 4096; mpq_mul_2exp (r, q, shift); if (mpq_cmp (r, q) < 0) { printf ("mpq_mul_2exp wrong on random\n"); abort (); } mpq_div_2exp (r, r, shift); if (mpq_cmp (r, q) != 0) { printf ("mpq_mul_2exp or mpq_div_2exp wrong on random\n"); abort (); } } mpq_clear (q); mpq_clear (r); mpz_clear (bs); }
/* expect after a gmp_randinit_set that the new and old generators will produce the same sequence of numbers */ void check_one (const char *name, gmp_randstate_ptr src) { gmp_randstate_t dst; mpz_t sz, dz; int i; gmp_randinit_set (dst, src); mpz_init (sz); mpz_init (dz); for (i = 0; i < 20; i++) { mpz_urandomb (sz, src, 123); mpz_urandomb (dz, dst, 123); if (mpz_cmp (sz, dz) != 0) { printf ("gmp_randinit_set didn't duplicate randstate\n"); printf (" algorithm: %s\n", name); gmp_printf (" from src: %#Zx\n", sz); gmp_printf (" from dst: %#Zx\n", dz); abort (); } } mpz_clear (sz); mpz_clear (dz); gmp_randclear (dst); }
void paillier_keygen( int modulusbits, paillier_pubkey_t** pub, paillier_prvkey_t** prv, paillier_get_rand_t get_rand ) { mpz_t p; mpz_t q; gmp_randstate_t rand; /* allocate the new key structures */ *pub = (paillier_pubkey_t*) malloc(sizeof(paillier_pubkey_t)); *prv = (paillier_prvkey_t*) malloc(sizeof(paillier_prvkey_t)); /* initialize our integers */ mpz_init((*pub)->n); mpz_init((*pub)->n_squared); mpz_init((*pub)->n_plusone); mpz_init((*prv)->lambda); mpz_init((*prv)->x); mpz_init(p); mpz_init(q); /* pick random (modulusbits/2)-bit primes p and q */ init_rand(rand, get_rand, modulusbits / 8 + 1); do { do mpz_urandomb(p, rand, modulusbits / 2); while( !mpz_probab_prime_p(p, 10) ); do mpz_urandomb(q, rand, modulusbits / 2); while( !mpz_probab_prime_p(q, 10) ); /* compute the public modulus n = p q */ mpz_mul((*pub)->n, p, q); } while( !mpz_tstbit((*pub)->n, modulusbits - 1) ); complete_pubkey(*pub); (*pub)->bits = modulusbits; /* compute the private key lambda = lcm(p-1,q-1) */ mpz_sub_ui(p, p, 1); mpz_sub_ui(q, q, 1); mpz_lcm((*prv)->lambda, p, q); complete_prvkey(*prv, *pub); /* clear temporary integers and randstate */ mpz_clear(p); mpz_clear(q); gmp_randclear(rand); }
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 target(void* y, unsigned long count) { arg_t* arg = (arg_t*) y; mpz_t x; mpz_init(x); mpz_poly_t in1, in2, out; mpz_poly_init(in1); mpz_poly_init(in2); mpz_poly_init(out); fmpz_poly_t in1f, in2f, outf; fmpz_poly_init2(in1f, arg->length1, (arg->bits1-1)/FLINT_BITS+1); fmpz_poly_init2(in2f, arg->length2, (arg->bits2-1)/FLINT_BITS+1); _fmpz_poly_stack_init(outf, arg->length1 + arg->length2 - 1, in1f->limbs + in2f->limbs + 1); for (unsigned long i = 0; i < arg->length1; i++) { mpz_urandomb(x, randstate, arg->bits1); mpz_poly_set_coeff(in1, i, x); } mpz_poly_to_fmpz_poly(in1f, in1); for (unsigned long i = 0; i < arg->length2; i++) { mpz_urandomb(x, randstate, arg->bits2); mpz_poly_set_coeff(in2, i, x); } mpz_poly_to_fmpz_poly(in2f, in2); start_clock(0); if (arg->which) { for (unsigned long i = 0; i < count; i++) _fmpz_poly_mul_karatsuba(outf, in1f, in2f); } else { for (unsigned long i = 0; i < count; i++) mpz_poly_mul_karatsuba(out, in1, in2); } stop_clock(0); _fmpz_poly_stack_clear(outf); fmpz_poly_clear(in2f); fmpz_poly_clear(in1f); mpz_poly_clear(out); mpz_poly_clear(in2); mpz_poly_clear(in1); mpz_clear(x); }
int main (int argc, char *argv[]) { gmp_randstate_t rs; mpz_t x, y, z; unsigned long int m, n, i, niter, t0, ti; double t, f, ops_per_sec; int decimals; if (argc != 3) { fprintf (stderr, "usage: %s m n\n", argv[0]); fprintf (stderr, " where m is number of dividend bits\n"); fprintf (stderr, " and n is number of divisor bits\n"); return -1; } m = atoi (argv[1]); n = atoi (argv[2]); gmp_randinit_default (rs); mpz_init (x); mpz_init (y); mpz_init (z); mpz_urandomb (x, rs, m); mpz_urandomb (y, rs, n); printf ("Calibrating CPU speed..."); fflush (stdout); TIME (t, mpz_tdiv_q (z, x, y)); printf ("done\n"); niter = 1 + (unsigned long) (1e4 / t); printf ("Dividing an %lu-bit number by an %lu-bit number %lu times...", m, n, niter); fflush (stdout); t0 = cputime (); for (i = niter; i > 0; i--) { mpz_tdiv_q (z, x, y); } ti = cputime () - t0; printf ("done!\n"); ops_per_sec = 1000.0 * niter / ti; f = 100.0; for (decimals = 0;; decimals++) { if (ops_per_sec > f) break; f = f * 0.1; } printf ("RESULT: %.*f operations per second\n", decimals, ops_per_sec); return 0; }
void random(ElementType& result) const { rawSetRandomQQ(&result, 0); #if 0 mpz_urandomb(mpq_numref(&result), mRandomState, mMaxHeight); mpz_urandomb(mpq_denref(&result), mRandomState, mMaxHeight); mpz_add_ui(mpq_numref(&result), mpq_numref(&result), 1); mpz_add_ui(mpq_denref(&result), mpq_denref(&result), 1); mpq_canonicalize(&result); #endif }
int main (int argc, char **argv) { mpz_t x2; mpz_t root1; mp_size_t x2_size; int i; int reps = 5000; unsigned long nth; gmp_randstate_ptr rands; mpz_t bs; unsigned long bsi, size_range; tests_start (); rands = RANDS; mpz_init (bs); if (argc == 2) reps = atoi (argv[1]); mpz_init (x2); mpz_init (root1); /* This triggers a gcc 4.3.2 bug */ mpz_set_str (x2, "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000000000000000000000000000000000000000000000000000000000002", 16); mpz_root (root1, x2, 2); check_one (root1, x2, 2, -1); for (i = 0; i < reps; i++) { mpz_urandomb (bs, rands, 32); size_range = mpz_get_ui (bs) % 12 + 2; mpz_urandomb (bs, rands, size_range); x2_size = mpz_get_ui (bs) + 10; mpz_rrandomb (x2, rands, x2_size); mpz_urandomb (bs, rands, 15); nth = mpz_getlimbn (bs, 0) % mpz_sizeinbase (x2, 2) + 2; mpz_root (root1, x2, nth); mpz_urandomb (bs, rands, 4); bsi = mpz_get_ui (bs); if ((bsi & 1) != 0) { /* With 50% probability, set x2 near a perfect power. */ mpz_pow_ui (x2, root1, nth); if ((bsi & 2) != 0) { mpz_sub_ui (x2, x2, bsi >> 2); mpz_abs (x2, x2); } else
int main (int argc, char **argv) { mpz_t x2; mpz_t x; mpz_t temp, temp2; mp_size_t x2_size; int i; int reps = 1000; unsigned long nth; gmp_randstate_ptr rands; mpz_t bs; unsigned long bsi, size_range; tests_start (); rands = RANDS; mpz_init (bs); if (argc == 2) reps = atoi (argv[1]); mpz_init (x2); mpz_init (x); mpz_init (temp); mpz_init (temp2); for (i = 0; i < reps; i++) { mpz_urandomb (bs, rands, 32); size_range = mpz_get_ui (bs) % 12 + 2; mpz_urandomb (bs, rands, size_range); x2_size = mpz_get_ui (bs) + 10; mpz_rrandomb (x2, rands, x2_size); mpz_urandomb (bs, rands, 15); nth = mpz_getlimbn (bs, 0) % mpz_sizeinbase (x2, 2) + 2; mpz_root (x, x2, nth); mpz_urandomb (bs, rands, 4); bsi = mpz_get_ui (bs); if ((bsi & 1) != 0) { /* With 50% probability, set x2 near a perfect power. */ mpz_pow_ui (x2, x, nth); if ((bsi & 2) != 0) { mpz_sub_ui (x2, x2, bsi >> 2); mpz_abs (x2, x2); } else
void rsaKeyGen(rsapvk_t *pvk, rsapbk_t *pbk) { // Temporary variable mpz_t tmp; mpz_init(tmp); // Integer used for random numbers if (!seed_initialized) rnInit(); do { mpz_urandomb(pvk->p, seed, RSA_KEY_SIZE); } while (!mpz_probab_prime_p(pvk->p, NB_TURN)); do { mpz_urandomb(pvk->q, seed, RSA_KEY_SIZE); } while (!mpz_probab_prime_p(pvk->q, NB_TURN)); // Initialize keys mpz_mul(pbk->n, pvk->p, pvk->q); mpz_set(pvk->n, pbk->n); // Compute phi = (p-1)(q-1) = q(p-1) - (p-1) // I don't compute p-1 and q-1 to avoid many initializations mpz_sub_ui(tmp, pvk->p, 1); mpz_mul(pvk->phi, pvk->q, tmp); mpz_sub(pvk->phi, pvk->phi, tmp); // Find a random number prime with phi do { mpz_urandomb(pbk->pbk, seed, RSA_KEY_SIZE); mpz_gcd(tmp, pbk->pbk, pvk->phi); } while (mpz_cmp_ui(tmp, 1)); // Compute pvk mpz_invert(pvk->pvk, pbk->pbk, pvk->phi); // Compute k mpz_set_ui(tmp, NB_CHAR); pvk->k = 0; while (mpz_cmp(tmp, pbk->n) < 0) { mpz_mul_ui(tmp, tmp, NB_CHAR); pvk->k ++; } pvk->k --; pbk->k = pvk->k; // Clearing variables mpz_clear(tmp); }
void damgard_jurik::key_gen(int modulusbits) { mpz_t p, q; gmp_randstate_t rand; mpz_init(pubkey->n); mpz_init(pubkey->g); mpz_init(prvkey->lambda); mpz_init(p); mpz_init(q); init_rand(rand, modulusbits / 8 + 1); do { do mpz_urandomb(p, rand, modulusbits / 2); while( !mpz_probab_prime_p(p, 10) ); do mpz_urandomb(q, rand, modulusbits / 2); while( !mpz_probab_prime_p(q, 10) ); /* compute the public modulus n = p q */ mpz_mul(pubkey->n, p, q); } while( !mpz_tstbit(pubkey->n, modulusbits - 1)); pubkey->bits = modulusbits; mpz_add_ui(pubkey->g, pubkey->n, 1); pubkey->n_j = new mpz_t[s_max + 2]; pubkey->k_n = new mpz_t[s_max + 2]; mpz_init(pubkey->n_j[0]); mpz_set_ui(pubkey->n_j[0], 1); mpz_init(pubkey->k_n[0]); mpz_set_ui(pubkey->k_n[0], 1); for (int i = 1;i <= s_max + 1;i++) { mpz_init(pubkey->n_j[i]); mpz_mul(pubkey->n_j[i], pubkey->n_j[i - 1], pubkey->n); mpz_init(pubkey->k_n[i]); mpz_mul_ui(pubkey->k_n[i], pubkey->k_n[i - 1], i); } /* Compute Private Key */ mpz_sub_ui(p, p, 1); mpz_sub_ui(q, q, 1); mpz_lcm(prvkey->lambda, p, q); mpz_clear(p); mpz_clear(q); gmp_randclear(rand); }
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; }
/** * Private encrypt methode. * Params : * - paillier_pubkey* pub : Paillier public key pointer ; * - mpz_t m : data to encrypt ; * - unsigned int s : recursion level ; * - mpz_t c : result operande . **/ void PaillierAdapter::enc( paillier_pubkey* pub, mpz_t m, unsigned int s, mpz_t c ) { mpz_t gm, rns, r; mpz_inits( gm, rns, r, NULL); unsigned int modulusbits = publicParameters.getKeyBitsize(); do { mpz_urandomb(r, rand, (s+1)*modulusbits); } while( mpz_cmp(r, *pub->getnj(s+1)) >= 0 ); mpz_powm(gm, *pub->getg(), m, *pub->getnj(s+1)); mpz_powm(rns, r, *pub->getnj(s), *pub->getnj(s+1)); mpz_mul(c, gm, rns); mpz_mod(c,c, *pub->getnj(s+1)); #ifdef DEBUG_ENCRYPT mpz_t test; mpz_init(test); dec(pub, privateParameters.getPrvKey(), c, s, test); gmp_printf("Decrypting %Zd into %Zd\n\n", c, test); #endif mpz_clears(gm, rns, r, NULL); }
static PyObject * GMPy_MPZ_urandomb_Function(PyObject *self, PyObject *args) { MPZ_Object *result; mp_bitcnt_t len; if (PyTuple_GET_SIZE(args) != 2) { TYPE_ERROR("mpz_urandomb() requires 2 arguments"); return NULL; } if (!RandomState_Check(PyTuple_GET_ITEM(args, 0))) { TYPE_ERROR("mpz_urandomb() requires 'random_state' and 'bit_count' arguments"); return NULL; } len = mp_bitcnt_t_From_Integer(PyTuple_GET_ITEM(args, 1)); if (len == (mp_bitcnt_t)(-1) && PyErr_Occurred()) { TYPE_ERROR("mpz_urandomb() requires 'random_state' and 'bit_count' arguments"); return NULL; } if ((result = GMPy_MPZ_New(NULL))) { mpz_urandomb(result->z, RANDOM_STATE(PyTuple_GET_ITEM(args, 0)), len); } return (PyObject*)result; }
guint32 srp_generate_salt_and_v(srp_t *srp, gchar *out) { mpz_t s; /* salt */ mpz_t v; mpz_t x; size_t count; if (!srp) return 0; mpz_init2(s, 256); mpz_urandomb(s, srp->rand, 256); mpz_export(out, &count, -1, 1, 0, 0, s); /*memset(buf, 0, 32);*/ srp_get_x(srp, x, out); srp_get_v_mpz(srp, v, x); mpz_export(out + 32, &count, -1, 1, 0, 0, v); mpz_clear(x); mpz_clear(v); mpz_clear(s); return 64; }
int main(int argc,char* argv[]) { if(argc!=3) { printf("Wrong number of arguments: %d",argc-1); return 1; } int bits; int N; bits = atoi(argv[1]); N = atoi(argv[2]); mpz_t number; gmp_randstate_t rnd; gmp_randinit_default(rnd); mpz_init(number); int i; for(i=0;i<N;++i) { mpz_urandomb(number,rnd,bits); gmp_printf("%Zd\n",number); } return 0; }
void egEncrypt(egpbk_t k, egdm_t m, egem_t *c) { // Useful for random generation if (!seed_initialized) rnInit(); // Temporary variables mpz_t tmp; mpz_init(tmp); mpz_urandomb(tmp, seed, EG_KEY_SIZE); // Counters and size of encrypted message int i = 0, l = 0; // Memory initialization mpz_init(c->y1); c->y2 = convertToInt(m.m, m.size, k.k, &l); c->size = l; mpz_powm(c->y1, k.alpha, tmp, k.p); mpz_powm(tmp, k.beta, tmp, k.p); while(i < c->size) { // Encryption mpz_mul(c->y2[i], c->y2[i], tmp); mpz_mod(c->y2[i], c->y2[i], k.p); i++; } mpz_clear(tmp); }
void makeprime( mpz_t out, int bits ) { mpz_urandomb( out, random_state, bits ); mpz_setbit( out, bits-1 ); mpz_nextprime( out, out ); }
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; }
MEXP(nls_t*) nls_account_change_proof(nls_t* nls, char* buf, const char* new_password, const char* B, const char* salt) { nls_t* nouveau; mpz_t s; /* salt */ if (!nls) return (nls_t*) 0; /* create new nls_t */ nouveau = nls_init_l(nls->username, nls->username_len, new_password, (unsigned long) strlen(new_password)); if (!nouveau) return (nls_t*) 0; /* fill buf */ nls_get_M1(nls, buf, B, salt); mpz_init2(s, 256); mpz_urandomb(s, nouveau->rand, 256); mpz_export(buf + 20, (size_t*) 0, -1, 1, 0, 0, s); nls_get_v(nouveau, buf + 52, buf + 20); mpz_clear(s); return nouveau; }
void PaillierAdapter::getRandInteger(mpz_t rop, unsigned int length, gmp_randstate_t prng) { do { mpz_urandomb(rop, prng, length); }while(mpz_sizeinbase(rop, 2) != length); }
MEXP(unsigned long) nls_account_create(nls_t* nls, char* buf, unsigned long bufSize) { mpz_t s; /* salt */ mpz_t v; mpz_t x; if (!nls) return 0; if (bufSize < nls->username_len + 65) return 0; mpz_init2(s, 256); mpz_urandomb(s, nls->rand, 256); mpz_export(buf, (size_t*) 0, -1, 1, 0, 0, s); /*memset(buf, 0, 32);*/ nls_get_x(nls, x, buf); nls_get_v_mpz(nls, v, x); mpz_export(buf + 32, (size_t*) 0, -1, 1, 0, 0, v); mpz_clear(x); mpz_clear(v); mpz_clear(s); strcpy(buf + 64, nls->username); return nls->username_len + 65; }
static void select_point(mpz_t x, mpz_t y, mpz_t a, mpz_t b, mpz_t N, mpz_t t, mpz_t t2) { mpz_t Q, t3, t4; gmp_randstate_t* p_randstate = get_randstate(); mpz_init(Q); mpz_init(t3); mpz_init(t4); mpz_set_ui(y, 0); while (mpz_sgn(y) == 0) { /* select a Q s.t. (Q,N) != -1 */ do { do { /* mpz_urandomm(x, *p_randstate, N); */ mpz_urandomb(x, *p_randstate, 32); /* May as well make x small */ mpz_mod(x, x, N); } while (mpz_sgn(x) == 0); mpz_mul(t, x, x); mpz_add(t, t, a); mpz_mul(t, t, x); mpz_add(t, t, b); mpz_mod(Q, t, N); } while (mpz_jacobi(Q, N) == -1); /* Select Y */ sqrtmod(y, Q, N, t, t2, t3, t4); /* TODO: if y^2 mod Ni != t, return composite */ if (mpz_sgn(y) == 0) croak("y == 0 in point selection\n"); } mpz_clear(Q); mpz_clear(t3); mpz_clear(t4); }
void mpz_random (mpz_ptr x, mp_size_t size) { mpz_urandomb (x, RANDS, (unsigned long) (ABS (size) * GMP_NUMB_BITS)); if (size < 0) SIZ(x) = -SIZ(x); }
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 }
damgard_jurik_ciphertext_t* damgard_jurik::encrypt(damgard_jurik_plaintext_t* pt, unsigned long s) { mpz_t r; gmp_randstate_t rand; mpz_t x; mpz_t gc; damgard_jurik_ciphertext_t *res = new damgard_jurik_ciphertext_t(); /* pick random blinding factor */ mpz_init(r); mpz_init(gc); init_rand(rand, pubkey->bits / 8 + 1); do { mpz_urandomb(r, rand, pubkey->bits); mpz_gcd(gc, r, pubkey->n); }while( mpz_cmp(r, pubkey->n) >= 0 || mpz_cmp_ui(gc, 1) > 0 || mpz_cmp_ui(r, 0) == 0); mpz_init(x); // mpz_powm(res->text, pubkey->g, pt->text, pubkey->n_j[s + 1]); compute_exp(&res->text, &pt->text, s); mpz_powm(x, r, pubkey->n_j[s], pubkey->n_j[s + 1]); mpz_mul(res->text, res->text, x); mpz_mod(res->text, res->text, pubkey->n_j[s + 1]); mpz_set(res->n_s, pubkey->n_j[s + 1]); res->s = s; mpz_clear(x); mpz_clear(r); gmp_randclear(rand); return res; }
void damgard_jurik::encrypt(damgard_jurik_ciphertext_t **list, damgard_jurik_plaintext_t **text, unsigned long s, int size) { mpz_t r; gmp_randstate_t rand; mpz_t x; mpz_t gc; /* pick random blinding factor */ mpz_init(r); mpz_init(gc); init_rand(rand, pubkey->bits / 8 + 1); do { mpz_urandomb(r, rand, pubkey->bits); mpz_gcd(gc, r, pubkey->n); }while (mpz_cmp(r, pubkey->n) >= 0 || mpz_cmp_ui(gc, 1) > 0 || mpz_cmp_ui(r, 0) == 0); mpz_init(x); mpz_powm(x, r, pubkey->n_j[s], pubkey->n_j[s + 1]); for (int i = 0; i < size; i++) { list[i] = new damgard_jurik_ciphertext_t(); // mpz_powm(list[i]->text, pubkey->g, text[i]->text, pubkey->n_j[s + 1]); compute_exp(&list[i]->text, &text[i]->text, s); mpz_mul(list[i]->text, list[i]->text, x); mpz_mod(list[i]->text, list[i]->text, pubkey->n_j[s + 1]); mpz_set(list[i]->n_s, pubkey->n_j[s + 1]); list[i]->s = s; } mpz_clear(x); mpz_clear(r); gmp_randclear(rand); }
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; }