/*-------------------------------------------------------------------------*/ static void xydata_init(xydata_t *xydata, uint32 num_lattice_primes, lattice_t *lattice_xyz, int64 z_base) { uint32 i, j, k, m, n; for (i = 0; i < num_lattice_primes; i++) { xydata_t *curr_xydata = xydata + i; uint32 num_powers = curr_xydata->num_powers; for (j = 0; j < num_powers; j++) { xypower_t *curr_xypower = curr_xydata->powers + j; uint32 num_roots = curr_xypower->num_roots; uint32 p = curr_xypower->power; uint32 latsize_mod = curr_xypower->latsize_mod; uint32 y_mod_p = lattice_xyz->y % p; int64 z_start = z_base + lattice_xyz->z; int32 z_start_mod = z_start % p; uint32 z_mod_p = (z_start_mod < 0) ? (z_start_mod + (int32)p) : z_start_mod; for (k = 0; k < num_roots; k++) { xyprog_t *curr_xyprog = curr_xypower->roots + k; uint8 *invtable_y = curr_xyprog->invtable_y; uint32 start = curr_xyprog->base_start; uint32 resclass = curr_xyprog->resclass; uint32 resclass2 = mp_modmul_1(resclass, resclass, p); uint32 ytmp = y_mod_p; uint32 stride_y = mp_modmul_1(resclass, latsize_mod, p); curr_xyprog->stride_z = mp_modmul_1(resclass2, latsize_mod, p); start = mp_modsub_1(start, mp_modmul_1(resclass, y_mod_p, p), p); curr_xyprog->start = mp_modsub_1(start, mp_modmul_1(resclass2, z_mod_p, p), p); for (m = n = 0; m < p; m++) { invtable_y[ytmp] = n; ytmp = mp_modadd_1(ytmp, latsize_mod, p); n = mp_modadd_1(n, stride_y, p); } } } } }
/*------------------------------------------------------------------*/ u_int32_t poly_get_zeros_and_mult(u_int32_t *zeros, u_int32_t *mult, mpzpoly_t _f, u_int32_t p) { u_int32_t i; u_int32_t num_roots; poly_t f; num_roots = poly_get_zeros(zeros, _f, p, 0); if (num_roots == 0) return num_roots; poly_reduce_mod_p(f, _f, p); for (i = 0; i < num_roots; i++) mult[i] = 0; if (f->degree == num_roots) return num_roots; for (i = 0; i < num_roots; i++) { poly_t g, r; u_int32_t root = zeros[i]; g->degree = 2; g->coef[0] = mp_modmul_1(root, root, p); g->coef[1] = p - mp_modadd_1(root, root, p); g->coef[2] = 1; poly_mod(r, f, g, p); if (r->degree == 0) mult[i] = 1; } return num_roots; }
static void poly_modmul(poly_t res, poly_t a, poly_t b, poly_t mod, u_int32_t p) { u_int32_t i, j; poly_t prod; for (i = 0; i <= a->degree; i++) prod->coef[i] = mp_modmul_1(a->coef[i], b->coef[0], p); for (i = 1; i <= b->degree; i++) { for (j = 0; j < a->degree; j++) { u_int32_t c = mp_modmul_1(a->coef[j], b->coef[i], p); prod->coef[i+j] = mp_modadd_1(prod->coef[i+j], c, p); } prod->coef[i+j] = mp_modmul_1(a->coef[j], b->coef[i], p); } prod->degree = a->degree + b->degree; poly_fix_degree(prod); poly_mod(res, prod, mod, p); return; }
/*-------------------------------------------------------------------*/ static uint32 verify_product(gmp_poly_t *gmp_prod, abpair_t *abpairs, uint32 num_relations, uint32 q, mp_t *c, mp_poly_t *alg_poly) { /* a sanity check on the computed value of S(x): for a small prime q for which alg_poly is irreducible, verify that gmp_prod mod q equals the product mod q of the relations in abpairs[]. The latter can be computed very quickly */ uint32 i, j; uint32 c_mod_q = mp_mod_1(c, q); uint32 d = alg_poly->degree; uint32 ref_prod[MAX_POLY_DEGREE]; uint32 prod[MAX_POLY_DEGREE]; uint32 mod[MAX_POLY_DEGREE]; uint32 accum[MAX_POLY_DEGREE + 1]; /* compute the product mod q directly. First initialize and reduce the coefficients of alg_poly and gmp_prod mod q */ for (i = 0; i < d; i++) { prod[i] = 0; ref_prod[i] = mpz_fdiv_ui(gmp_prod->coeff[i], (unsigned long)q); mod[i] = mp_mod_1(&alg_poly->coeff[i].num, q); if (alg_poly->coeff[i].sign == NEGATIVE && mod[i] > 0) { mod[i] = q - mod[i]; } } prod[0] = 1; /* multiply the product by each relation in turn, modulo q */ for (i = 0; i < num_relations; i++) { int64 a = abpairs[i].a; uint32 b = q - (abpairs[i].b % q); uint32 ac; a = a % (int64)q; if (a < 0) a += q; ac = mp_modmul_1((uint32)a, c_mod_q, q); for (j = accum[0] = 0; j < d; j++) { accum[j+1] = mp_modmul_1(prod[j], b, q); accum[j] = mp_modadd_1(accum[j], mp_modmul_1(ac, prod[j], q), q); } for (j = 0; j < d; j++) { prod[j] = mp_modsub_1(accum[j], mp_modmul_1(accum[d], mod[j], q), q); } } /* do the polynomial compare */ for (i = 0; i < d; i++) { if (ref_prod[i] != prod[i]) break; } if (i == d) return 1; return 0; }
/*------------------------------------------------------------------*/ static void get_zeros_rec(u_int32_t *zeros, u_int32_t shift, u_int32_t *num_zeros, poly_t f, u_int32_t p) { /* get the zeros of a poly, f, that is known to split completely over Z/pZ. Many thanks to Bob Silverman for a neat implementation of Cantor-Zassenhaus splitting */ poly_t g, xpow; u_int32_t degree1, degree2; /* base cases of the recursion: we can find the roots of linear and quadratic polynomials immediately */ if (f->degree == 1) { u_int32_t w = f->coef[1]; if (w != 1) { w = mp_modinv_1(w, p); zeros[(*num_zeros)++] = mp_modmul_1(p - f->coef[0],w,p); } else { zeros[(*num_zeros)++] = (f->coef[0] == 0 ? 0 : p - f->coef[0]); } return; } else if (f->degree == 2) { /* if f is a quadratic polynomial, then it will always have two distinct nonzero roots or else we wouldn't have gotten to this point. The two roots are the solution of a general quadratic equation, mod p */ u_int32_t d = mp_modmul_1(f->coef[0], f->coef[2], p); u_int32_t root1 = p - f->coef[1]; u_int32_t root2 = root1; u_int32_t ainv = mp_modinv_1( mp_modadd_1(f->coef[2], f->coef[2], p), p); d = mp_modsub_1(mp_modmul_1(f->coef[1], f->coef[1], p), mp_modmul_1(4, d, p), p); d = mp_modsqrt_1(d, p); root1 = mp_modadd_1(root1, d, p); root2 = mp_modsub_1(root2, d, p); zeros[(*num_zeros)++] = mp_modmul_1(root1, ainv, p); zeros[(*num_zeros)++] = mp_modmul_1(root2, ainv, p); return; } /* For an increasing sequence of integers 's', compute the polynomial gcd((x-s)^(p-1)/2 - 1, f). If the result is not g = 1 or g = f, this is a nontrivial splitting of f. References require choosing s randomly, but however s is chosen there is a 50% chance that it will split f. Since only 0 <= s < p is valid, we choose each s in turn; choosing random s allows the possibility that the same s gets chosen twice (mod p), which would waste time */ while (shift < p) { poly_xpow(xpow, shift, (p-1)/2, f, p); poly_cp(g, xpow); g->coef[0] = mp_modsub_1(g->coef[0], 1, p); poly_fix_degree(g); poly_gcd(g, f, p); if (g->degree > 0) break; shift++; } /* f was split; repeat the splitting process on the two halves of f. The linear factors of f are either somewhere in x^((p-1)/2) - 1, in x^((p-1)/2) + 1, or 'shift' itself is a linear factor. Test each of these possibilities in turn. In the first two cases, begin trying values of s strictly greater than have been tried thus far */ degree1 = g->degree; get_zeros_rec(zeros, shift + 1, num_zeros, g, p); poly_cp(g, xpow); g->coef[0] = mp_modadd_1(g->coef[0], 1, p); poly_fix_degree(g); poly_gcd(g, f, p); degree2 = g->degree; if (degree2 > 0) get_zeros_rec(zeros, shift + 1, num_zeros, g, p); if (degree1 + degree2 < f->degree) zeros[(*num_zeros)++] = (shift == 0 ? 0 : p - shift); }