void generate (int limb_bits, int nail_bits, int base) { int numb_bits = limb_bits - nail_bits; mpz_set_ui (t, 1L); mpz_mul_2exp (t, t, numb_bits); mpz_set_ui (big_base, 1L); chars_per_limb = 0; for (;;) { mpz_mul_ui (big_base, big_base, (long) base); if (mpz_cmp (big_base, t) > 0) break; chars_per_limb++; } chars_per_bit_exactly = 0.69314718055994530942 / log ((double) base); mpz_ui_pow_ui (big_base, (long) base, (long) chars_per_limb); normalization_steps = limb_bits - mpz_sizeinbase (big_base, 2); mpz_set_ui (t, 1L); mpz_mul_2exp (t, t, 2*limb_bits - normalization_steps); mpz_tdiv_q (big_base_inverted, t, big_base); mpz_set_ui (t, 1L); mpz_mul_2exp (t, t, limb_bits); mpz_sub (big_base_inverted, big_base_inverted, t); }
void LLLoperations::REDI(int k, int ell, MutableMatrix *A, MutableMatrix *Achange, // can be NULL MutableMatrix *lambda) { // set q = ... // negate q. ring_elem Dl, mkl, q; if (!lambda->get_entry(ell,k,mkl)) return; lambda->get_entry(ell,ell,Dl); mpz_ptr a = mkl.get_mpz(); mpz_ptr b = Dl.get_mpz(); // b = D#ell mpz_t c, d; mpz_init(c); mpz_init(d); mpz_mul_2exp(c,a,1); // c = 2*lambda#(k,ell) mpz_abs(d,c); // d = abs(2*lambda#(k,ell) mpz_add(c,c,b); // c = 2*lambda#(k,ell) + D#ell mpz_mul_2exp(d,b,1); // d = 2*D#ell mpz_fdiv_q(c,c,d); // c = (almost) final q mpz_neg(c,c); q = c; //A->addColumnMultiple(ell,q,k); //lambda->addColumnMultiple(ell,q,k); A->column_op(k,q,ell); if (Achange) Achange->column_op(k,q,ell); lambda->column_op(k,q,ell); mpz_clear(c); mpz_clear(d); }
void BigNum::add(BigNum & r, BigNum & x, BigNum & y) // result of x + y will be stored in r, x and y will be changed { //r.denominator = x.denominator + y.denominator; if (x.denominator >= y.denominator) { r.denominator = x.denominator; mpz_mul_2exp(y.numerator, y.numerator, x.denominator - y.denominator); } else { r.denominator = y.denominator; mpz_mul_2exp(x.numerator, x.numerator, y.denominator - x.denominator); } mpz_add(r.numerator, x.numerator, y.numerator); r.zero_flag = false; // any result should be > 0 unsigned index = 0; while(!mpz_tstbit(r.numerator, index)) // mpz_tstbit(r.numerator, index) == 0, test 0 bit by bit ++index; if (index > 0) // reduce numerator and denominator as much as possible { mpz_tdiv_q_2exp(r.numerator, r.numerator, index); r.denominator = r.denominator - index; } return; }
static PyObject * GMPy_MPZ_Lshift_Slot(PyObject *self, PyObject *other) { mp_bitcnt_t count; MPZ_Object *result, *tempx; count = mp_bitcnt_t_From_Integer(other); if ((count == (mp_bitcnt_t)(-1)) && PyErr_Occurred()) return NULL; if (!(result = GMPy_MPZ_New(NULL))) return NULL; if (CHECK_MPZANY(self)) { mpz_mul_2exp(result->z, MPZ(self), count); return (PyObject*)result; } else { if (!(tempx = GMPy_MPZ_From_Integer(self, NULL))) { Py_XDECREF((PyObject*)result); Py_XDECREF((PyObject*)tempx); return NULL; } mpz_mul_2exp(result->z, tempx->z, count); Py_DECREF((PyObject*)tempx); return (PyObject*)result; } }
int main(void) { mpz_t a, exp, temp, p, res; mpz_init(a); mpz_init(exp); mpz_init(p); mpz_init(temp); mpz_init(res); mpz_set_ui(p, 1); mpz_mul_2exp(p, p, 29440); mpz_set_ui(temp, 1); mpz_mul_2exp(temp, temp, 27392); mpz_sub(p, p, temp); mpz_add_ui(p, p, 1); mpz_set_ui(a, 1); mpz_mul_2exp(a, a, 32); mpz_sub_ui(exp, p, 1); mpz_fdiv_q_2exp(exp, exp, 10); F_mpz_expmod_BZ(res, a, exp, p); //F_mpz_expmod_mont(res, a, exp, p); //mpz_powm(res, a, exp, p); mpz_clear(a); mpz_clear(exp); mpz_clear(p); mpz_init(temp); mpz_clear(res); }
void rat_gmp_shift(const mpq_t z1, mpq_t z, int n) { if (n > 0) mpz_mul_2exp(mpq_numref(z), mpq_numref(z1), n); else if (n < 0) mpz_mul_2exp(mpq_denref(z), mpq_denref(z1), (unsigned)-n); mpq_canonicalize(z); }
/* Called when g is supposed to be gcd(a,b), and g = s a + t b, for some t. Uses temp1, temp2 and temp3. */ static int gcdext_valid_p (const mpz_t a, const mpz_t b, const mpz_t g, const mpz_t s) { /* It's not clear that gcd(0,0) is well defined, but we allow it and require that gcd(0,0) = 0. */ if (mpz_sgn (g) < 0) return 0; if (mpz_sgn (a) == 0) { /* Must have g == abs (b). Any value for s is in some sense "correct", but it makes sense to require that s == 0. */ return mpz_cmpabs (g, b) == 0 && mpz_sgn (s) == 0; } else if (mpz_sgn (b) == 0) { /* Must have g == abs (a), s == sign (a) */ return mpz_cmpabs (g, a) == 0 && mpz_cmp_si (s, mpz_sgn (a)) == 0; } if (mpz_sgn (g) <= 0) return 0; mpz_tdiv_qr (temp1, temp3, a, g); if (mpz_sgn (temp3) != 0) return 0; mpz_tdiv_qr (temp2, temp3, b, g); if (mpz_sgn (temp3) != 0) return 0; /* Require that 2 |s| < |b/g|, or |s| == 1. */ if (mpz_cmpabs_ui (s, 1) > 0) { mpz_mul_2exp (temp3, s, 1); if (mpz_cmpabs (temp3, temp2) >= 0) return 0; } /* Compute the other cofactor. */ mpz_mul(temp2, s, a); mpz_sub(temp2, g, temp2); mpz_tdiv_qr(temp2, temp3, temp2, b); if (mpz_sgn (temp3) != 0) return 0; /* Require that 2 |t| < |a/g| or |t| == 1*/ if (mpz_cmpabs_ui (temp2, 1) > 0) { mpz_mul_2exp (temp2, temp2, 1); if (mpz_cmpabs (temp2, temp1) >= 0) return 0; } return 1; }
/* See: * (1) Kaltofen, Valente, Yui 1989 * (2) Valente 1992 (Thesis) * (3) Konstantinou, Stamatiou, and Zaroliagis (CHES 2002) * This code is performing table 1 of reference 3. */ static void weber_root_to_hilbert_root(mpz_t r, mpz_t N, long D) { mpz_t A, t; if (D < 0) D = -D; D = ((D % 4) == 0) ? D/4 : D; if ( (D % 8) == 0 ) return; mpz_init(A); mpz_init(t); switch (D % 8) { case 1: if ((D % 3) != 0) mpz_powm_ui(t, r, 12, N); else mpz_powm_ui(t, r, 4, N); mpz_mul_ui(A, t, 64); mpz_sub_ui(t, A, 16); break; case 2: case 6: if ((D % 3) != 0) mpz_powm_ui(t, r, 12, N); else mpz_powm_ui(t, r, 4, N); mpz_mul_ui(A, t, 64); mpz_add_ui(t, A, 16); break; case 5: if ((D % 3) != 0) mpz_powm_ui(t, r, 6, N); else mpz_powm_ui(t, r, 2, N); mpz_mul_ui(A, t, 64); mpz_sub_ui(t, A, 16); break; case 7: if (!mpz_invert(t, r, N)) mpz_set_ui(t, 0); if ((D % 3) != 0) mpz_powm_ui(A, t, 24, N); else mpz_powm_ui(A, t, 8, N); mpz_sub_ui(t, A, 16); break; /* Results in degree 3x Hilbert, so typically not used */ case 3: if (!mpz_invert(t, r, N)) mpz_set_ui(t, 0); if ((D % 3) != 0) { mpz_powm_ui(t, t, 24, N); mpz_mul_2exp(A, t, 12); } else { mpz_powm_ui(t, t, 8, N); mpz_mul_2exp(A, t, 4); } mpz_sub_ui(t, A, 16); break; default: break; } /* r = t^3 / A */ mpz_powm_ui(t, t, 3, N); if ( ! mpz_divmod(r, t, A, N, r) ) mpz_set_ui(r, 0); mpz_clear(A); mpz_clear(t); }
/* exercise the case where mpz_clrbit or mpz_combit ends up extending a value like -2^(k*GMP_NUMB_BITS-1) when clearing bit k*GMP_NUMB_BITS-1. */ void check_clr_extend (void) { mpz_t got, want; unsigned long i; int f; mpz_init (got); mpz_init (want); for (i = 1; i < 5; i++) { for (f = 0; f <= 1; f++) { /* lots of 1 bits in _mp_d */ mpz_set_ui (got, 1L); mpz_mul_2exp (got, got, 10*GMP_NUMB_BITS); mpz_sub_ui (got, got, 1L); /* value -2^(n-1) representing ..11100..00 */ mpz_set_si (got, -1L); mpz_mul_2exp (got, got, i*GMP_NUMB_BITS-1); /* complement bit n, giving ..11000..00 which is -2^n */ if (f == 0) mpz_clrbit (got, i*GMP_NUMB_BITS-1); else mpz_combit (got, i*GMP_NUMB_BITS-1); MPZ_CHECK_FORMAT (got); mpz_set_si (want, -1L); mpz_mul_2exp (want, want, i*GMP_NUMB_BITS); if (mpz_cmp (got, want) != 0) { if (f == 0) printf ("mpz_clrbit: "); else printf ("mpz_combit: "); printf ("wrong after extension\n"); mpz_trace ("got ", got); mpz_trace ("want", want); abort (); } } } mpz_clear (got); mpz_clear (want); }
void alder_gmp_test() { mpz_t integ1, integ2; mpz_init2 (integ1, 54); mpz_init2 (integ2, 54); mpz_set_ui(integ1, 1); mpz_mul_2exp(integ1, integ1, 65); mpz_out_str (stdout, 2, integ1); printf("\n"); // mpz_add_ui(integ1,integ1,1); mpz_out_str (stdout, 10, integ1); printf("\n"); // mpz_mul_2exp(integ1, integ1, 2); // mpz_out_str (stdout, 2, integ1); printf("\n"); uint64_t x2[2]; x2[0] = 0; x2[1] = 0; uint64_t x = 0; size_t countp = 0; mpz_export(x2, &countp, -1, 8, 0, 0, integ1); printf("x[0]: %llu\n", x2[0]); printf("x[1]: %llu\n", x2[1]); x = UINT64_MAX; printf("x: %llu\n", x); mpz_set_ui(integ1,0); // mpz_mul_2exp(integ1, integ1, ); // mpz_import(b, 1, 1, sizeof(a), 0, 0, &a); // mpz_export mpz_out_str (stdout, 2, integ1); printf("\n"); mpz_out_str (stdout, 2, integ2); printf("\n"); mpz_swap(integ1, integ2); mpz_mul_2exp(integ2, integ1, 2); mpz_setbit(integ2,0); mpz_setbit(integ2,1); mpz_out_str (stdout, 2, integ2); printf("\n"); mpz_clear (integ1); mpz_clear (integ2); }
static void check_onebit (void) { static const unsigned long data[] = { 1, 32, 52, 53, 54, 63, 64, 65, 128, 256, 511, 512, 513 }; mpz_t z; double got, want; long got_exp, want_exp; int i; mpz_init (z); for (i = 0; i < numberof (data); i++) { mpz_set_ui (z, 1L); mpz_mul_2exp (z, z, data[i]); want = 0.5; want_exp = data[i] + 1; got = mpz_get_d_2exp (&got_exp, z); if (got != want || got_exp != want_exp) { printf ("mpz_get_d_2exp wrong on 2**%ld\n", data[i]); mpz_trace (" z ", z); d_trace (" want ", want); d_trace (" got ", got); printf (" want exp %ld\n", want_exp); printf (" got exp %ld\n", got_exp); abort(); } mpz_set_si (z, -1L); mpz_mul_2exp (z, z, data[i]); want = -0.5; want_exp = data[i] + 1; got = mpz_get_d_2exp (&got_exp, z); if (got != want || got_exp != want_exp) { printf ("mpz_get_d_2exp wrong on -2**%ld\n", data[i]); mpz_trace (" z ", z); d_trace (" want ", want); d_trace (" got ", got); printf (" want exp %ld\n", want_exp); printf (" got exp %ld\n", got_exp); abort(); } } mpz_clear (z); }
void check_various (void) { mpf_t got; mpq_t q; mpf_init (got); mpq_init (q); /* 1/1 == 1 */ mpf_set_prec (got, 20L); mpq_set_ui (q, 1L, 1L); mpf_set_q (got, q); MPF_CHECK_FORMAT (got); ASSERT_ALWAYS (mpf_cmp_ui (got, 1L) == 0); /* 1/(2^n+1), a case where truncating the divisor would be wrong */ mpf_set_prec (got, 500L); mpq_set_ui (q, 1L, 1L); mpz_mul_2exp (mpq_denref(q), mpq_denref(q), 800L); mpz_add_ui (mpq_denref(q), mpq_denref(q), 1L); check_one (got, q); mpf_clear (got); mpq_clear (q); }
static void zp_double(element_ptr n, element_ptr a) { //mpz_add(n->data, a->data, a->data); mpz_mul_2exp(n->data, a->data, 1); if (mpz_cmp(n->data, n->field->order) >= 0) { mpz_sub(n->data, n->data, n->field->order); } }
/* Equality of integers with up to 53 bits */ void check_onebits (void) { mpz_t x, x2; double y; int i; mpz_init_set_ui (x, 0L); mpz_init (x2); for (i = 0; i < 512; i++) { mpz_mul_2exp (x, x, 1); mpz_add_ui (x, x, 1L); y = mpz_get_d (x); mpz_set_d (x2, y); /* stop if any truncation is occurring */ if (mpz_cmp (x, x2) != 0) break; check_one ("check_onebits", x, y, 0, 0); check_one ("check_onebits", x, -y, 1, 0); mpz_neg (x, x); check_one ("check_onebits", x, y, -1, 0); check_one ("check_onebits", x, -y, 0, 0); mpz_neg (x, x); } mpz_clear (x); mpz_clear (x2); }
void check_limb (void) { int i; mp_limb_t limb; mpz_t z; char *s; check_one ("0", "%Md", CNST_LIMB(0)); check_one ("1", "%Md", CNST_LIMB(1)); /* "i" many 1 bits, tested against mpz_get_str in decimal and hex */ limb = 1; mpz_init_set_ui (z, 1L); for (i = 1; i <= GMP_LIMB_BITS; i++) { s = mpz_get_str (NULL, 10, z); check_one (s, "%Mu", limb); (*__gmp_free_func) (s, strlen (s) + 1); s = mpz_get_str (NULL, 16, z); check_one (s, "%Mx", limb); (*__gmp_free_func) (s, strlen (s) + 1); s = mpz_get_str (NULL, -16, z); check_one (s, "%MX", limb); (*__gmp_free_func) (s, strlen (s) + 1); limb = 2*limb + 1; mpz_mul_2exp (z, z, 1L); mpz_add_ui (z, z, 1L); } mpz_clear (z); }
static void test_fixed (void) { mpz_t r; mpz_init (r); /* Triggered a bug reported by Hanno Böck. */ mpz_set_str (r, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFF001C2C00", 16); mpz_mul_2exp (r, r, 256); test_one ("p", &nettle_secp_256r1.p, r); test_one ("q", &nettle_secp_256r1.q, r); mpz_set_str (r, "ffffffff00000001fffffffeffffffffffffffffffffffffffffffc0000000000007ffffffffffffffffffffffffffff00000000000000000fffffffffffffff", 16); test_one ("p", &nettle_secp_256r1.p, r); test_one ("q", &nettle_secp_256r1.q, r); /* Triggered a bug reported by Hanno Böck. */ mpz_set_str (r, "4c9000000000000000000000000000000000000000000000004a604db486e000000000000000000000000000000000000000121025be29575adb2c8ffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16); test_one ("p", &nettle_secp_384r1.p, r); test_one ("q", &nettle_secp_384r1.q, r); /* Triggered a carry bug in development version. */ mpz_set_str (r, "e64a84643150260640e4677c19ffc4faef06042132b86af6e9ee33fe1850222e57a514d5f1d6d444008bb896a96a43d5629945e57548f5e12f66be132b24110cbb2df6d7d3dd3aaadc98b0bbf29573843ad72e57f59fc5d4f56cc599da18bb99", 16); test_one ("p", &nettle_secp_384r1.p, r); test_one ("q", &nettle_secp_384r1.q, r); mpz_clear (r); }
//------------------------------------------------------------------------------ void IDDContent::TO_MPZ(mpz_t &rop, const IDDC &n) { if(n == zero) mpz_set_ui(rop, 0); else if(n == one) mpz_set_ui(rop, 1); else { if(n->ul_fit()) mpz_set_ui(rop,n->val()); else { mpz_t _1; mpz_t g,p,d; mpz_init_set_ui(_1, 1); mpz_init(g); mpz_init(p); mpz_init(d); TO_MPZ(g,n->g); unsigned int pui = TO(n->p, 32, 0); TO_MPZ(d,n->d); pui = 1<<pui; mpz_mul_2exp(p, _1, pui); mpz_mul(rop,p,d); mpz_add(rop,g,rop); } } }
void gmp_twice(mpz_ptr out, mpz_ptr order) { mpz_mul_2exp(out, out, 1); if (mpz_cmp(out, order) >= 0) { mpz_sub(out, out, order); } }
static PyObject * GMPy_XMPZ_Function_XbitMask(PyObject *self, PyObject *other) { Py_ssize_t i = 0; XMPZ_Object* result; CTXT_Object *context = NULL; CHECK_CONTEXT(context); i = ssize_t_From_Integer(other); if (i == -1 && PyErr_Occurred()) { TYPE_ERROR("xbit_mask() requires 'int' argument"); return NULL; } if (i < 0) { VALUE_ERROR("mask length must be >= 0"); return NULL; } if (!(result = GMPy_XMPZ_New(context))) { return NULL; } mpz_set_ui(result->z, 1); mpz_mul_2exp(result->z, result->z, i); mpz_sub_ui(result->z, result->z, 1); return (PyObject*)result; }
/* if expz > target, shift z by (expz-target) bits to the left. if expz < target, shift z by (target-expz) bits to the right. Returns target. */ static int mpz_normalize2 (mpz_t rop, mpz_t z, int expz, int target) { if (target > expz) mpz_div_2exp(rop, z, target-expz); else mpz_mul_2exp(rop, z, expz-target); return target; }
void mpz_true_random (mpz_t s, unsigned long int nbits) { #if __FreeBSD__ FILE *fs; char c[1]; int i; mpz_set_ui (s, 0); for (i = 0; i < nbits; i += 8) { for (;;) { int nread; fs = fopen ("/dev/random", "r"); nread = fread (c, 1, 1, fs); fclose (fs); if (nread != 0) break; sleep (1); } mpz_mul_2exp (s, s, 8); mpz_add_ui (s, s, ((unsigned long int) c[0]) & 0xff); printf ("%d random bits\n", i + 8); } if (nbits % 8 != 0) mpz_mod_2exp (s, s, nbits); #endif }
bool FermatProbablePrimalityTestFast(const mpz_class &n, unsigned int& nLength, CPrimalityTestParams &testParams, bool fFastFail) { static const mpz_class mpzTwo = 2; mpz_t& mpzE = testParams.mpzE; mpz_t& mpzR = testParams.mpzR; mpz_sub_ui(mpzE, n.get_mpz_t(), 1); mpz_powm(mpzR, mpzTwo.get_mpz_t(), mpzE, n.get_mpz_t()); if (mpz_cmp_ui(mpzR, 1) == 0) return true; if (fFastFail) return false; // Fermat test failed, calculate chain length (integer and fractional part) mpz_sub(mpzE, n.get_mpz_t(), mpzR); mpz_mul_2exp(mpzR, mpzE, DifficultyFractionalBits); mpz_tdiv_q(mpzE, mpzR, n.get_mpz_t()); unsigned int fractionalLength = mpz_get_ui(mpzE); nLength = (nLength & DifficultyChainLengthMask) | fractionalLength; return false; }
/* Set the random seed from /dev/random */ void setrndseed() { FILE *rnd; mpz_t rndtmp; unsigned long int idx; time_t t1; if (!fastseed) { mpz_init(rndtmp); rnd = fopen("/dev/urandom", "r"); for (idx = 0; idx < 128; idx++) { mpz_set_ui(rndtmp, (unsigned long int) getc(rnd)); mpz_mul_2exp(rndtmp, rndtmp, idx * 8); /* left shift */ mpz_add(rndseed, rndseed, rndtmp); } fclose(rnd); mpz_clear(rndtmp); } else { /* Set a faster seed. Do not use this for cryptographic purposes! */ mpz_set_ui(rndseed, (unsigned long int) time(&t1)); mpz_mul_ui(rndseed, rndseed, (unsigned long int) getpid()); mpz_mul_ui(rndseed, rndseed, (unsigned long int) getppid()); } }
static PyObject * Pyxmpz_xbit_mask(PyObject *self, PyObject *other) { Py_ssize_t i = 0; PyxmpzObject* result; i = ssize_t_From_Integer(other); if (i == -1 && PyErr_Occurred()) { TYPE_ERROR("xbit_mask() requires 'int' argument"); return NULL; } if (i < 0) { VALUE_ERROR("mask length must be >= 0"); return NULL; } if (!(result = (PyxmpzObject*)Pyxmpz_new())) return NULL; mpz_set_ui(result->z, 1); mpz_mul_2exp(result->z, result->z, i); mpz_sub_ui(result->z, result->z, 1); return (PyObject*)result; }
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 }
void mp_unlinearize_msb_first(MP_INT * value, const unsigned char *buf, unsigned int len) { unsigned int i; mpz_set_ui(value, 0); for (i = 0; i + 4 <= len; i += 4) { unsigned long limb = GET_32BIT(buf + i); mpz_mul_2exp(value, value, 32); mpz_add_ui(value, value, limb); } for (; i < len; i++) { mpz_mul_2exp(value, value, 8); mpz_add_ui(value, value, buf[i]); } }
// Get integer part, starting search from given lower bound. void binary_search(mpz_ptr lower) { while (!mpz_sgn(p->c0)) move_right(); for (;;) { mpz_set(z0, lower); mpz_set(z, lower); int sign = sign_quad(); mpz_set_ui(pow2, 1); for (;;) { mpz_add(z, z0, pow2); if (sign_quad() != sign) break; mpz_mul_2exp(pow2, pow2, 1); } mpz_set(z1, z); for (;;) { mpz_add(z, z0, z1); mpz_div_2exp(z, z, 1); if (!mpz_cmp(z, z0)) break; if (sign_quad() == sign) { mpz_set(z0, z); } else { mpz_set(z1, z); } } sign = sign_quad1(); mpz_set(z, z1); if (sign_quad1() != sign) break; move_right(); } }
static Py_hash_t Pympq_hash(PympqObject *self) { #ifdef _PyHASH_MODULUS Py_hash_t hash = 0; mpz_t temp, temp1, mask; if (self->hash_cache != -1) return self->hash_cache; mpz_inoc(temp); mpz_inoc(temp1); mpz_inoc(mask); mpz_set_si(mask, 1); mpz_mul_2exp(mask, mask, _PyHASH_BITS); mpz_sub_ui(mask, mask, 1); if (!mpz_invert(temp, mpq_denref(self->q), mask)) { mpz_cloc(temp); mpz_cloc(temp1); mpz_cloc(mask); hash = _PyHASH_INF; if (mpz_sgn(mpq_numref(self->q))<0) hash = -hash; self->hash_cache = hash; return hash; } mpz_set(temp1, mask); mpz_sub_ui(temp1, temp1, 2); mpz_powm(temp, mpq_denref(self->q), temp1, mask); mpz_tdiv_r(temp1, mpq_numref(self->q), mask); mpz_mul(temp, temp, temp1); hash = (Py_hash_t)mpn_mod_1(temp->_mp_d, mpz_size(temp), _PyHASH_MODULUS); if (mpz_sgn(mpq_numref(self->q))<0) hash = -hash; if (hash==-1) hash = -2; mpz_cloc(temp); mpz_cloc(temp1); mpz_cloc(mask); self->hash_cache = hash; return hash; #else PyObject *temp; if (self->hash_cache != -1) return self->hash_cache; if (!(temp = Pympq_To_PyFloat(self))) { SYSTEM_ERROR("Could not convert 'mpq' to float."); return -1; } self->hash_cache = PyObject_Hash(temp); Py_DECREF(temp); return self->hash_cache; #endif }
static obj bignum_shl( obj a, INT_32 b ) { mpz_t r, a1; OBJ_TO_MPZ(a1, a); mpz_init(r); mpz_mul_2exp(r, a1, b); return bignum_compact(r); }
/* Set inv to the inverse of d, in the style of invert_limb, ie. for udiv_qrnnd_preinv. */ void mpz_preinv_invert (mpz_t inv, mpz_t d, int numb_bits) { mpz_t t; int norm; //ASSERT (SIZ(d) > 0); norm = numb_bits - mpz_sizeinbase (d, 2); //ASSERT (norm >= 0); mpz_init_set_ui (t, 1L); mpz_mul_2exp (t, t, 2*numb_bits - norm); mpz_tdiv_q (inv, t, d); mpz_set_ui (t, 1L); mpz_mul_2exp (t, t, numb_bits); mpz_sub (inv, inv, t); mpz_clear (t); }