2 #include "collect_vargs.h"
3 #include "smod_helpers.h"
4 #include "euclid_gcd_wrap.h"
5 #include "eval_point_finder.h"
6 #include "newton_interpolate.h"
7 #include "divide_in_z_p.h"
13 primpart_content(ex& pp, ex& c, ex e, const exvector& vars, const long p);
15 // Computes the GCD of two polynomials over a prime field.
16 // Based on Algorithm 7.2 from "Algorithms for Computer Algebra"
17 // A and B are considered as Z_p[x_n][x_0, \ldots, x_{n-1}], that is,
18 // as a polynomials in variables x_0, \ldots x_{n-1} having coefficients
19 // from the ring Z_p[x_n]
20 ex pgcd(const ex& A, const ex& B, const exvector& vars, const long p)
22 static const ex ex1(1);
35 // Checks for univariate polynomial
36 if (vars.size() == 1) {
37 ex ret = euclid_gcd(A, B, vars[0], p); // Univariate GCD
40 const ex& mainvar(vars.back());
42 // gcd of the contents
43 ex H = 0, Hprev = 0; // GCD candidate
44 ex newton_poly = 1; // for Newton Interpolation
46 // Contents and primparts of A and B
48 primpart_content(Aprim, contA, A, vars, p);
50 primpart_content(Bprim, contB, B, vars, p);
51 // gcd of univariate polynomials
52 const ex cont_gcd = euclid_gcd(contA, contB, mainvar, p);
54 exvector restvars = vars;
56 const ex AL = lcoeff_wrt(Aprim, restvars);
57 const ex BL = lcoeff_wrt(Bprim, restvars);
58 // gcd of univariate polynomials
59 const ex lc_gcd = euclid_gcd(AL, BL, mainvar, p);
61 // The estimate of degree of the gcd of Ab and Bb
62 int gcd_deg = std::min(degree(Aprim, mainvar), degree(Bprim, mainvar));
63 eval_point_finder::value_type b;
65 eval_point_finder find_eval_point(p);
68 // Find a `good' evaluation point b.
69 bool has_more_pts = find_eval_point(b, lc_gcd, mainvar);
70 // If there are no more possible evaluation points, bail out
75 // Evaluate the polynomials in b
76 ex Ab = Aprim.subs(mainvar == bn).smod(pn);
77 ex Bb = Bprim.subs(mainvar == bn).smod(pn);
78 ex Cb = pgcd(Ab, Bb, restvars, p);
80 // Set the correct the leading coefficient
81 const cln::cl_I lcb_gcd =
82 smod(to_cl_I(lc_gcd.subs(mainvar == bn)), p);
83 const cln::cl_I Cblc = integer_lcoeff(Cb, restvars);
84 const cln::cl_I correct_lc = smod(lcb_gcd*recip(Cblc, p), p);
85 Cb = (Cb*numeric(correct_lc)).smod(pn);
87 // Test for unlucky homomorphisms
88 const int img_gcd_deg = Cb.degree(restvars.back());
89 if (img_gcd_deg < gcd_deg) {
90 // The degree decreased, previous homomorphisms were
91 // bad, so we have to start it all over.
93 newton_poly = mainvar - numeric(b);
95 gcd_deg = img_gcd_deg;
98 if (img_gcd_deg > gcd_deg) {
99 // The degree of images GCD is too high, this
100 // evaluation point is bad. Skip it.
104 // Image has the same degree as the previous one
105 // (or at least not higher than the limit)
107 H = newton_interp(Cb, b, H, newton_poly, mainvar, p);
108 newton_poly = newton_poly*(mainvar - b);
110 // try to reduce the number of division tests.
111 const ex H_lcoeff = lcoeff_wrt(H, restvars);
113 if (H_lcoeff.is_equal(lc_gcd)) {
114 if ((Hprev-H).expand().smod(pn).is_zero())
116 ex C /* primitive part of H */, contH /* dummy */;
117 primpart_content(C, contH, H, vars, p);
118 // Normalize GCD so that leading coefficient is 1
119 const cln::cl_I Clc = recip(integer_lcoeff(C, vars), p);
120 C = (C*numeric(Clc)).expand().smod(pn);
124 if (divide_in_z_p(Aprim, C, dummy1, vars, p) &&
125 divide_in_z_p(Bprim, C, dummy2, vars, p))
126 return (cont_gcd*C).expand().smod(pn);
127 else if (img_gcd_deg == 0)
129 // else continue building the candidate