2 #include "gcd_euclid.tcc"
3 #include "cra_garner.hpp"
4 #include <cln/random.h>
5 #include <cln/numtheory.h>
12 * @brief Remove the integer content from univariate polynomials A and B.
14 * As a byproduct compute the GCD of contents.
16 static void remove_content(upoly& A, upoly& B, upoly::value_type& c)
19 upoly::value_type acont, bcont;
20 normalize_in_ring(A, &acont);
21 normalize_in_ring(B, &bcont);
22 c = gcd(acont, bcont);
25 /// Check if @a candidate divides both @a A and @a B
27 do_division_check(const upoly& A, const upoly& B, const upoly& candidate)
30 remainder_in_ring(r1, A, candidate);
35 remainder_in_ring(r2, B, candidate);
43 * Given two GCD candidates H \in Z/q[x], and C \in Z/p[x] (where p is a prime)
44 * compute the candidate in Z/(q*p) with CRA (chinise remainder algorithm)
46 * @param H \in Z/q[x] GCD candidate, will be updated by this function
47 * @param q modulus of H, will NOT be updated by this function
48 * @param C \in Z/p[x] GCD candidate, will be updated by this function
49 * @param p modulus of C
52 update_the_candidate(upoly& H, const upoly::value_type& q,
54 const upoly::value_type& p,
55 const cln::cl_modint_ring& R)
57 typedef upoly::value_type ring_t;
58 std::vector<ring_t> moduli(2);
61 if (H.size() < C.size())
64 for (std::size_t i = C.size(); i-- != 0; ) {
65 std::vector<ring_t> coeffs(2);
67 coeffs[1] = R->retract(C[i]);
68 H[i] = integer_cra(coeffs, moduli);
72 /// Convert Z/p[x] -> Z[x]
73 static void retract(upoly& p, const umodpoly& cp,
74 const cln::cl_modint_ring& Rp)
77 for (std::size_t i = cp.size(); i-- != 0; )
78 p[i] = Rp->retract(cp[i]);
82 /// Find the prime which is > p, and does NOT divide g
83 static void find_next_prime(cln::cl_I& p, const cln::cl_I& g)
88 } while (zerop(mod(g, p)));
91 /// Compute the GCD of univariate polynomials A, B \in Z[x]
92 void mod_gcd(upoly& result, upoly A, upoly B)
94 typedef upoly::value_type ring_t;
96 // remove the integer content
97 remove_content(A, B, content_gcd);
99 // compute the coefficient bound for GCD(a, b)
100 ring_t g = gcd(lcoeff(A), lcoeff(B));
101 std::size_t max_gcd_degree = std::min(degree(A), degree(B));
102 ring_t limit = (ring_t(1) << max_gcd_degree)*g*
103 std::min(max_coeff(A), max_coeff(B));
109 find_next_prime(p, g);
111 // Map the polynomials onto Z/p[x]
112 cln::cl_modint_ring Rp = cln::find_modint_ring(p);
113 cln::cl_MI gp = Rp->canonhom(g);
114 umodpoly ap(A.size()), bp(B.size());
115 make_umodpoly(ap, A, Rp);
116 make_umodpoly(bp, B, Rp);
118 // Compute the GCD in Z/p[x]
120 gcd_euclid(cp, ap, bp);
121 bug_on(cp.size() == 0, "gcd(ap, bp) = 0, with ap = " <<
122 ap << ", and bp = " << bp);
125 // Normalize the candidate so that its leading coefficient
127 umodpoly::value_type norm_factor = gp*recip(lcoeff(cp));
128 bug_on(zerop(norm_factor), "division in a field give 0");
131 for (std::size_t k = cp.size() - 1; k-- != 0; )
132 cp[k] = cp[k]*norm_factor;
135 // check for unlucky homomorphisms
136 if (degree(cp) < max_gcd_degree) {
138 max_gcd_degree = degree(cp);
141 update_the_candidate(H, q, cp, p, Rp);
147 normalize_in_ring(result);
148 // if H divides both A and B it's a GCD
149 if (do_division_check(A, B, result)) {
150 result *= content_gcd;
153 // H does not divide A and/or B, look for
155 } else if (degree(cp) == 0) {
156 // Polynomials are relatively prime
158 result[0] = content_gcd;