Correctness of Williams' p+1 algorithm full
This proof shows: correctness, termination.
Fix an integer A. The Lucas sequence is V0=2, V1=A, Vj+1=AVj−Vj−1, and we write Vj(A) for its terms. Work in the ring R=Z[t]/(t2−At+1), a free Z-module with basis 1,t, so that Z is a subring of R. Put τ=t and σ=A−t. Then τ+σ=A and τσ=At−t2=1.
Lemma 1. Vj(A)=τj+σj in R for all j≥0.
Proof. Both τ and σ are roots of X2−AX+1 (for σ: σ2−Aσ+1=σ(σ−A)+1=−στ+1=0). So Wj=τj+σj satisfies Wj+1=AWj−Wj−1, and W0=2, W1=A. By induction Wj=Vj(A). □
Lemma 2 (doubling rules). For j≥1: V2j=Vj2−2 and V2j+1=VjVj+1−A.
Proof. By Lemma 1 and τσ=1: (τj+σj)2=τ2j+σ2j+2, and (τj+σj)(τj+1+σj+1)=τ2j+1+σ2j+1+τjσj(σ+τ)=V2j+1+A. □
Lemma 3 (composition). For k,m≥1: Vm(Vk(A))=Vmk(A).
Proof. Let w=Vk(A)=τk+σk. The elements τk,σk satisfy τkσk=1 and τk+σk=w, so both are roots of X2−wX+1. As in Lemma 1, τkj+σkj satisfies the recurrence of the Lucas sequence with seed w and the same first two terms 2,w; so it equals Vj(w). With j=m and Lemma 1, Vm(w)=τkm+σkm=Vkm(A). □
All these are identities between integers built from A by sums and products, so they also hold modulo N, with A replaced by any integer congruent to it.
Lemma 4 (LUCAS-V). LUCAS-V(v,m,N) returns Vm(v)modN for m≥1.
Proof. Write m in binary with leading digit 1, and let j be the number formed by the digits read so far, so j=1 before the loop of line K2. The invariant is x≡Vj(v) and y≡Vj+1(v)(modN). It holds after line K1, since V2=v2−2. If the next digit is 1, the new j is 2j+1, and line K3 sets x to VjVj+1−v=V2j+1 and y to Vj+12−2=V2j+2, by Lemma 2 (for y, applied to j+1), both from the old values. If the digit is 0, the new j is 2j, and line K4 sets x to Vj2−2=V2j and y to VjVj+1−v=V2j+1. After all digits, j=m, and line K5 returns Vm(v)modN. The loop has one pass per digit, so it ends. □
Lemma 5 (the main loop). After line L4 has run for the primes q1<⋯<qi, with powers Q1,…,Qi, v≡VQ1⋯Qi(A)(modN). At the end, v≡VM(A).
Proof. Before the loop, v=A=V1(A). If v≡Vk(A), then line L4 makes v≡VQ(Vk(A))=VQk(A) by Lemmas 4 and 3. Induction on i gives the claim, and the product of all Q is M. The loop is over the finitely many primes up to B, so it ends. □
Lemma 6 (the key congruence). Let p be an odd prime that does not divide D=A2−4, and let ε=(D/p). Then Vj(A)≡2(modp) for every multiple j of p−ε.
Proof. Let f=X2−AX+1 over Fp. Its discriminant is D≡0.
If ε=1, then D≡s2 for some s≡0, and f has the roots α=(A+s)/2 and β=(A−s)/2 in Fp (here p is odd, so 2 is invertible). They satisfy αβ=(A2−s2)/4=1, so both are nonzero, and by Fermat's little theorem αp−1=βp−1=1.
If ε=−1, then f has no root in Fp, so it is irreducible and K=Fp[X]/(f) is a field with p2 elements. Let α be the class of X and β=A−α; then αβ=1. The map z↦zp is a ring homomorphism of K that fixes Fp, so f(αp)=f(α)p=0, and αp is a root of f. The roots of f in the field K are α and β. If αp=α, then α would be a root of Zp−Z, whose p roots in K are the elements of Fp, and α∈/Fp. So αp=β and αp+1=αβ=1, and also βp+1=(α−1)p+1=1.
In both cases there is a ring homomorphism from R to a field of characteristic p that sends t to α (since f(α)=0), and so σ=A−t to β. By Lemma 1, Vj(A)modp is αj+βj. With αp−ε=βp−ε=1, every multiple j of p−ε gives αj+βj=2. □
Theorem. WILLIAMS-P-PLUS-1(N,B,A) ends and returns a divisor g of N. If an odd prime p∣N does not divide A2−4 and p−(D/p) divides M, then p∣g. If moreover some prime power rf that divides N has VM(A)≡2(modrf), then 1<g<N.
Proof. The loops end by Lemmas 4 and 5. The value g=gcd(v−2,N) divides N. By Lemma 5, v≡VM(A)(modN), so v−2≡VM(A)−2(modp) for every p∣N. Under the hypothesis on p, Lemma 6 with j=M gives p∣v−2, so p∣g and g>1. If rf∣N and rf∤VM(A)−2, then rf∤v−2, so rf∤g and g=N. ■