Error, error, unsupported BITS_PER_MP_LIMB #endif int mpz_kronecker_ui (mpz_srcptr a, unsigned long b) { mp_srcptr a_ptr = PTR(a); mp_size_t a_size; mp_limb_t a_rem; int result_bit1; a_size = SIZ(a); if (a_size == 0) return JACOBI_0U (b); if (b > GMP_NUMB_MAX) { mp_limb_t blimbs[2]; mpz_t bz; ALLOC(bz) = numberof (blimbs); PTR(bz) = blimbs; mpz_set_ui (bz, b); return mpz_kronecker (a, bz); } if ((b & 1) != 0) { result_bit1 = JACOBI_ASGN_SU_BIT1 (a_size, b); } else { mp_limb_t a_low = a_ptr[0]; int twos; if (b == 0) return JACOBI_LS0 (a_low, a_size); /* (a/0) */ if (! (a_low & 1)) return 0; /* (even/even)=0 */ /* (a/2)=(2/a) for a odd */ count_trailing_zeros (twos, b); b >>= twos; result_bit1 = (JACOBI_TWOS_U_BIT1 (twos, a_low) ^ JACOBI_ASGN_SU_BIT1 (a_size, b)); } if (b == 1) return JACOBI_BIT1_TO_PN (result_bit1); /* (a/1)=1 for any a */ a_size = ABS(a_size); /* (a/b) = (a mod b / b) */ JACOBI_MOD_OR_MODEXACT_1_ODD (result_bit1, a_rem, a_ptr, a_size, b); return mpn_jacobi_base (a_rem, (mp_limb_t) b, result_bit1); }
int mpz_si_kronecker (long a, mpz_srcptr b) { mp_srcptr b_ptr; mp_limb_t b_low; mp_size_t b_size; mp_size_t b_abs_size; mp_limb_t a_limb, b_rem; unsigned twos; int result_bit1; #if GMP_NUMB_BITS < BITS_PER_ULONG if (a > GMP_NUMB_MAX || a < -GMP_NUMB_MAX) { mp_limb_t alimbs[2]; mpz_t az; ALLOC(az) = numberof (alimbs); PTR(az) = alimbs; mpz_set_si (az, a); return mpz_kronecker (az, b); } #endif b_size = SIZ (b); if (b_size == 0) return JACOBI_S0 (a); /* (a/0) */ /* account for the effect of the sign of b, then ignore it */ result_bit1 = JACOBI_BSGN_SS_BIT1 (a, b_size); b_ptr = PTR(b); b_low = b_ptr[0]; b_abs_size = ABS (b_size); if ((b_low & 1) != 0) { /* b odd */ result_bit1 ^= JACOBI_ASGN_SU_BIT1 (a, b_low); a_limb = (unsigned long) ABS(a); if ((a_limb & 1) == 0) { /* (0/b)=1 for b=+/-1, 0 otherwise */ if (a_limb == 0) return (b_abs_size == 1 && b_low == 1); /* a even, b odd */ count_trailing_zeros (twos, a_limb); a_limb >>= twos; /* (a*2^n/b) = (a/b) * twos(n,a) */ result_bit1 ^= JACOBI_TWOS_U_BIT1 (twos, b_low); } }
int mpz_kronecker_si (mpz_srcptr a, long b) { mp_srcptr a_ptr; mp_size_t a_size; mp_limb_t a_rem, b_limb; int result_bit1; a_size = SIZ(a); if (a_size == 0) return JACOBI_0S (b); #if GMP_NUMB_BITS < BITS_PER_ULONG if (b > GMP_NUMB_MAX || b < -GMP_NUMB_MAX) { mp_limb_t blimbs[2]; mpz_t bz; ALLOC(bz) = numberof (blimbs); PTR(bz) = blimbs; mpz_set_si (bz, b); return mpz_kronecker (a, bz); } #endif result_bit1 = JACOBI_BSGN_SS_BIT1 (a_size, b); b_limb = (unsigned long) ABS (b); a_ptr = PTR(a); if ((b_limb & 1) == 0) { mp_limb_t a_low = a_ptr[0]; int twos; if (b_limb == 0) return JACOBI_LS0 (a_low, a_size); /* (a/0) */ if (! (a_low & 1)) return 0; /* (even/even)=0 */ /* (a/2)=(2/a) for a odd */ count_trailing_zeros (twos, b_limb); b_limb >>= twos; result_bit1 ^= JACOBI_TWOS_U_BIT1 (twos, a_low); }
int checkpoly_siqs(siqs_poly *poly, mpz_t n) { //check that b^2 == N mod a //and that c == (b*b - n)/a mpz_t t1,t2,t3,t4; mpz_init(t1); mpz_init(t2); mpz_init(t3); mpz_init(t4); mpz_set(t1, n); mpz_tdiv_r(t3, t1, poly->mpz_poly_a); //zDiv(&t1,&poly->poly_a,&t2,&t3); mpz_mul(t2, poly->mpz_poly_b, poly->mpz_poly_b); //zMul(&poly->poly_b,&poly->poly_b,&t2); mpz_tdiv_r(t4, t2, poly->mpz_poly_a); //zDiv(&t2,&poly->poly_a,&t1,&t4); if (mpz_cmp(t3,t4) != 0) { printf("\nError in checkpoly: %s^2 !== N mod %s\n", mpz_conv2str(&gstr1.s, 10, poly->mpz_poly_b), mpz_conv2str(&gstr2.s, 10, poly->mpz_poly_a)); if (mpz_sgn(poly->mpz_poly_b) < 0) printf("b is negative\n"); } if (mpz_kronecker(n, poly->mpz_poly_a) != 1) printf("\nError in checkpoly: (a|N) != 1\n"); mpz_mul(t2, poly->mpz_poly_b, poly->mpz_poly_b); //zMul(&poly->poly_b,&poly->poly_b,&t2); mpz_sub(t2, t2, n); mpz_tdiv_q(t4, t2, poly->mpz_poly_a); //zDiv(&t2,&poly->poly_a,&t1,&t4); if (mpz_cmp(t4,poly->mpz_poly_c) != 0) printf("\nError in checkpoly: c != (b^2 - n)/a\n"); mpz_clear(t1); mpz_clear(t2); mpz_clear(t3); mpz_clear(t4); return 0; }
static PyObject * GMPy_MPZ_Function_Kronecker(PyObject *self, PyObject *args) { MPZ_Object *tempx = NULL, *tempy = NULL; long res; if (PyTuple_GET_SIZE(args) != 2) { TYPE_ERROR("kronecker() requires 'mpz','mpz' arguments"); return NULL; } if (!(tempx = GMPy_MPZ_From_Integer(PyTuple_GET_ITEM(args, 0), NULL)) || !(tempy = GMPy_MPZ_From_Integer(PyTuple_GET_ITEM(args, 1), NULL))) { Py_XDECREF((PyObject*)tempx); Py_XDECREF((PyObject*)tempy); return NULL; } res = (long)(mpz_kronecker(tempx->z, tempy->z)); Py_DECREF((PyObject*)tempx); Py_DECREF((PyObject*)tempy); return PyIntOrLong_FromLong(res); }
int mpz_ui_kronecker (unsigned long a, mpz_srcptr b) { mp_srcptr b_ptr; mp_limb_t b_low; int b_abs_size; mp_limb_t b_rem; int twos; int result_bit1; /* (a/-1)=1 when a>=0, so the sign of b is ignored */ b_abs_size = ABSIZ (b); if (b_abs_size == 0) return JACOBI_U0 (a); /* (a/0) */ if (a > GMP_NUMB_MAX) { mp_limb_t alimbs[2]; mpz_t az; ALLOC(az) = numberof (alimbs); PTR(az) = alimbs; mpz_set_ui (az, a); return mpz_kronecker (az, b); } b_ptr = PTR(b); b_low = b_ptr[0]; result_bit1 = 0; if (! (b_low & 1)) { /* (0/b)=0 for b!=+/-1; and (even/even)=0 */ if (! (a & 1)) return 0; /* a odd, b even Establish shifted b_low with valid bit1 for the RECIP below. Zero limbs stripped are accounted for, but zero bits on b_low are not because they remain in {b_ptr,b_abs_size} for JACOBI_MOD_OR_MODEXACT_1_ODD. */ JACOBI_STRIP_LOW_ZEROS (result_bit1, a, b_ptr, b_abs_size, b_low); if (! (b_low & 1)) { if (UNLIKELY (b_low == GMP_NUMB_HIGHBIT)) { /* need b_ptr[1] to get bit1 in b_low */ if (b_abs_size == 1) { /* (a/0x80...00) == (a/2)^(NUMB-1) */ if ((GMP_NUMB_BITS % 2) == 0) { /* JACOBI_STRIP_LOW_ZEROS does nothing to result_bit1 when GMP_NUMB_BITS is even, so it's still 0. */ ASSERT (result_bit1 == 0); result_bit1 = JACOBI_TWO_U_BIT1 (a); } return JACOBI_BIT1_TO_PN (result_bit1); } /* b_abs_size > 1 */ b_low = b_ptr[1] << 1; } else { count_trailing_zeros (twos, b_low); b_low >>= twos; } } } else { if (a == 0) /* (0/b)=1 for b=+/-1, 0 otherwise */
/* See Cohen section 1.5. * See http://www.math.vt.edu/people/brown/doc/sqrts.pdf */ int sqrtmod(mpz_t x, mpz_t a, mpz_t p, mpz_t t, mpz_t q, mpz_t b, mpz_t z) /* 4 temp variables */ { int r, e, m; /* Easy cases from page 31 (or Menezes 3.36, 3.37) */ if (mpz_congruent_ui_p(p, 3, 4)) { mpz_add_ui(t, p, 1); mpz_tdiv_q_2exp(t, t, 2); mpz_powm(x, a, t, p); return verify_sqrt(x, a, p, t, q); } if (mpz_congruent_ui_p(p, 5, 8)) { mpz_sub_ui(t, p, 1); mpz_tdiv_q_2exp(t, t, 2); mpz_powm(q, a, t, p); if (mpz_cmp_si(q, 1) == 0) { /* s = a^((p+3)/8) mod p */ mpz_add_ui(t, p, 3); mpz_tdiv_q_2exp(t, t, 3); mpz_powm(x, a, t, p); } else { /* s = 2a * (4a)^((p-5)/8) mod p */ mpz_sub_ui(t, p, 5); mpz_tdiv_q_2exp(t, t, 3); mpz_mul_ui(q, a, 4); mpz_powm(x, q, t, p); mpz_mul_ui(x, x, 2); mpz_mulmod(x, x, a, p, x); } return verify_sqrt(x, a, p, t, q); } if (mpz_kronecker(a, p) != 1) { /* Possible no solution exists. Check Euler criterion. */ mpz_sub_ui(t, p, 1); mpz_tdiv_q_2exp(t, t, 1); mpz_powm(x, a, t, p); if (mpz_cmp_si(x, 1) != 0) { mpz_set_ui(x, 0); return 0; } } mpz_sub_ui(q, p, 1); e = mpz_scan1(q, 0); /* Remove 2^e from q */ mpz_tdiv_q_2exp(q, q, e); mpz_set_ui(t, 2); while (mpz_legendre(t, p) != -1) /* choose t "at random" */ mpz_add_ui(t, t, 1); mpz_powm(z, t, q, p); /* Step 1 complete */ r = e; mpz_powm(b, a, q, p); mpz_add_ui(q, q, 1); mpz_divexact_ui(q, q, 2); mpz_powm(x, a, q, p); /* Done with q, will use it for y now */ while (mpz_cmp_ui(b, 1)) { /* calculate how many times b^2 mod p == 1 */ mpz_set(t, b); m = 0; do { mpz_powm_ui(t, t, 2, p); m++; } while (m < r && mpz_cmp_ui(t, 1)); if (m >= r) break; mpz_ui_pow_ui(t, 2, r-m-1); mpz_powm(t, z, t, p); mpz_mulmod(x, x, t, p, x); mpz_powm_ui(z, t, 2, p); mpz_mulmod(b, b, z, p, b); r = m; } return verify_sqrt(x, a, p, t, q); }