BROWN-GCD returns gcd(f, g) and stops after finitely many primes full
This proof shows: correctness.
Let f,g∈Z[x] be nonzero and not constant after line L1, so that f and g are primitive with positive leading coefficients. (If one input is zero or a constant, the implementation answers directly, and the answer is the gcd by definition.) Let G=gcd(f,g) in Z[x], primitive with lc(G)>0, and b=gcd(lcf,lcg). For a∈Z[x], ∥a∥1 and ∥a∥∞ are the sum and the largest absolute value of the coefficients. The primes come from a list of distinct primes, and only primes p∤b reach line L4.
Lemma 1 (contents). gcd(f0,g0)=gcd(contf0,contg0)⋅gcd(ppf0,ppg0) for the original inputs f0,g0.
Proof. This is a consequence of Gauss's lemma: the product of primitive polynomials is primitive, so contents and primitive parts of common divisors split independently. □
So it is enough to show that line L10 returns c⋅G with c=gcd(contf0,contg0).
Lemma 2 (images). Let p∤b and let vp be the monic gcd of fmodp and gmodp. Then degvp≥degG, and if equality holds, bvp≡(b/lcG)G(modp).
Proof. lc(G) divides lc(f) and lc(g), so it divides b, and p∤lc(G). Hence Gmodp has degree degG. It divides fmodp and gmodp, so it divides vp in (Z/pZ)[x], and degvp≥degG. If the degrees are equal, vp=G⋅lc(G)−1modp, since both are monic and one divides the other. Multiplying by b gives the claim, and b/lc(G) is an integer. □
Call p lucky if degvp=degG. Let H∘=(b/lcG)G, U∘=lc(G)f/G and W∘=lc(G)g/G. These are in Z[x], and bf=H∘U∘, bg=H∘W∘.
Invariant. After line L8, d is the smallest degree degvp seen so far, m is the product of the primes p seen so far with degvp=d since the last restart, and H,U,W have coefficients in the symmetric range modulo m with
H≡bvp,U≡f/vp,W≡g/vp(modp)for each such p,bf≡HU,bg≡HW(modm), and the coefficient of xd in H is ≡b≡0(modp) for every p∣m, so degH=d.
Proof. Line L5 discards primes of larger degree, line L6 restarts with one prime when the degree drops or at the first prime, and line L7 combines a new image of the same degree, so the first part follows by induction, using that the Chinese remainder theorem gives the unique residue modulo mp with the given residues modulo m and p. For each p∣m, bf≡bvp⋅(f/vp)≡HU(modp), and the same holds for g; the primes are distinct, so the congruences hold modulo m. Finally vp is monic of degree d, so the coefficient of xd in H is ≡b(modp), and higher coefficients are 0. □
Lemma 3 (the test is sound). If the test at line L9 passes, then pp(H)=G.
Proof. Every coefficient of HU has absolute value at most ∥H∥1∥U∥1<m/2, and every coefficient of bf has absolute value less than m/2. Two integers in (−m/2,m/2) that are congruent modulo m are equal, so bf=HU in Z[x], and in the same way bg=HW. So H divides bf and bg, and so does pp(H). As pp(H) is primitive, Gauss's lemma gives pp(H)∣f and pp(H)∣g, so pp(H) divides G. By Lemma 2, every image has degree at least degG, so degpp(H)=d≥degG. A divisor of G of degree at least degG is ±G, and both have positive leading coefficients, so pp(H)=G. □
With Lemma 1, the returned value c⋅pp(H) is gcd(f0,g0).
Lemma 4 (unlucky primes are finite). If p∤b is unlucky, then p divides the resultant r=res(f/G,g/G), which is a nonzero integer.
Proof. f/G and g/G are coprime in Q[x], so r=0. Since p∤b, p does not divide both leading coefficients, say p∤lc(f), hence p∤lc(f/G). Then rmodp equals, up to a power of lc(f/G) and a sign, the resultant of the reductions, and it is zero if the reductions have a common factor of positive degree. If p is unlucky, vp has degree greater than degG, so vp/(Gmodp) is a common factor of (f/G)modp and (g/G)modp of positive degree, and p∣r. □
Termination. Only finitely many primes are unlucky by Lemma 4, and each is either discarded at line L5 or causes a restart at line L6 that a later lucky prime undoes, since a lucky prime has the smallest possible degree degG. After the last unlucky prime in the list, every prime p∤b is lucky, d=degG, and by Lemma 2 and the invariant, H≡H∘, U≡U∘, W≡W∘(modm). Once m>2max(∥H∘∥1∥U∘∥1,∥H∘∥1∥W∘∥1,∥H∘∥∞,∥U∘∥∞,∥W∘∥∞,∥bf∥∞,∥bg∥∞), the symmetric residues at line L8 are H∘,U∘,W∘ themselves, and the test at line L9 passes. The list must contain enough primes; the implementation raises an error otherwise. ■