Algorithm Search⌘K

Proofs: Williams' p+1 algorithm

These are the human proofs of Williams' p+1 algorithm, at full length.

Correctness of Williams' p+1 algorithm full

This proof shows: correctness, termination.

Fix an integer AA. The Lucas sequence is V0=2V_0 = 2, V1=AV_1 = A, Vj+1=AVj−Vj−1V_{j+1} = A V_j - V_{j-1}, and we write Vj(A)V_j(A) for its terms. Work in the ring R=Z[t]/(t2−At+1)R = \mathbb Z[t]/(t^2 - At + 1), a free Z\mathbb Z-module with basis 1,t1, t, so that Z\mathbb Z is a subring of RR. Put τ=t\tau = t and σ=A−t\sigma = A - t. Then τ+σ=A\tau + \sigma = A and τσ=At−t2=1\tau\sigma = At - t^2 = 1.

Lemma 1. Vj(A)=τj+σjV_j(A) = \tau^j + \sigma^j in RR for all j≥0j \ge 0.

Proof. Both τ\tau and σ\sigma are roots of X2−AX+1X^2 - AX + 1 (for σ\sigma: σ2−Aσ+1=σ(σ−A)+1=−στ+1=0\sigma^2 - A\sigma + 1 = \sigma(\sigma - A) + 1 = -\sigma\tau + 1 = 0). So Wj=τj+σjW_j = \tau^j + \sigma^j satisfies Wj+1=AWj−Wj−1W_{j+1} = A W_j - W_{j-1}, and W0=2W_0 = 2, W1=AW_1 = A. By induction Wj=Vj(A)W_j = V_j(A). □\square

Lemma 2 (doubling rules). For j≥1j \ge 1: V2j=Vj2−2V_{2j} = V_j^2 - 2 and V2j+1=VjVj+1−AV_{2j+1} = V_j V_{j+1} - A.

Proof. By Lemma 1 and τσ=1\tau\sigma = 1: (τj+σj)2=τ2j+σ2j+2(\tau^j + \sigma^j)^2 = \tau^{2j} + \sigma^{2j} + 2, and (τj+σj)(τj+1+σj+1)=τ2j+1+σ2j+1+τjσj(σ+τ)=V2j+1+A(\tau^j + \sigma^j)(\tau^{j+1} + \sigma^{j+1}) = \tau^{2j+1} + \sigma^{2j+1} + \tau^j\sigma^j(\sigma + \tau) = V_{2j+1} + A. □\square

Lemma 3 (composition). For k,m≥1k, m \ge 1: Vm(Vk(A))=Vmk(A)V_m(V_k(A)) = V_{mk}(A).

Proof. Let w=Vk(A)=τk+σkw = V_k(A) = \tau^k + \sigma^k. The elements τk,σk\tau^k, \sigma^k satisfy τkσk=1\tau^k\sigma^k = 1 and τk+σk=w\tau^k + \sigma^k = w, so both are roots of X2−wX+1X^2 - wX + 1. As in Lemma 1, τkj+σkj\tau^{kj} + \sigma^{kj} satisfies the recurrence of the Lucas sequence with seed ww and the same first two terms 2,w2, w; so it equals Vj(w)V_j(w). With j=mj = m and Lemma 1, Vm(w)=τkm+σkm=Vkm(A)V_m(w) = \tau^{km} + \sigma^{km} = V_{km}(A). □\square

All these are identities between integers built from AA by sums and products, so they also hold modulo NN, with AA replaced by any integer congruent to it.

Lemma 4 (LUCAS-V). LUCAS-V(v,m,N)(v, m, N) returns Vm(v) mod NV_m(v) \bmod N for m≥1m \ge 1.

Proof. Write mm in binary with leading digit 11, and let jj be the number formed by the digits read so far, so j=1j = 1 before the loop of line K2. The invariant is x≡Vj(v)x \equiv V_j(v) and y≡Vj+1(v)(modN)y \equiv V_{j+1}(v) \pmod N. It holds after line K1, since V2=v2−2V_2 = v^2 - 2. If the next digit is 11, the new jj is 2j+12j + 1, and line K3 sets xx to VjVj+1−v=V2j+1V_j V_{j+1} - v = V_{2j+1} and yy to Vj+12−2=V2j+2V_{j+1}^2 - 2 = V_{2j+2}, by Lemma 2 (for yy, applied to j+1j + 1), both from the old values. If the digit is 00, the new jj is 2j2j, and line K4 sets xx to Vj2−2=V2jV_j^2 - 2 = V_{2j} and yy to VjVj+1−v=V2j+1V_j V_{j+1} - v = V_{2j+1}. After all digits, j=mj = m, and line K5 returns Vm(v) mod NV_m(v) \bmod N. The loop has one pass per digit, so it ends. □\square

Lemma 5 (the main loop). After line L4 has run for the primes q1<⋯<qiq_1 < \dots < q_i, with powers Q1,…,QiQ_1, \dots, Q_i, v≡VQ1⋯Qi(A)(modN)v \equiv V_{Q_1 \cdots Q_i}(A) \pmod N. At the end, v≡VM(A)v \equiv V_M(A).

Proof. Before the loop, v=A=V1(A)v = A = V_1(A). If v≡Vk(A)v \equiv V_k(A), then line L4 makes v≡VQ(Vk(A))=VQk(A)v \equiv V_{Q}(V_k(A)) = V_{Qk}(A) by Lemmas 4 and 3. Induction on ii gives the claim, and the product of all QQ is MM. The loop is over the finitely many primes up to BB, so it ends. □\square

Lemma 6 (the key congruence). Let pp be an odd prime that does not divide D=A2−4D = A^2 - 4, and let ε=(D/p)\varepsilon = (D/p). Then Vj(A)≡2(modp)V_j(A) \equiv 2 \pmod p for every multiple jj of p−εp - \varepsilon.

Proof. Let f=X2−AX+1f = X^2 - AX + 1 over Fp\mathbb F_p. Its discriminant is D≢0D \not\equiv 0.

If ε=1\varepsilon = 1, then D≡s2D \equiv s^2 for some s≢0s \not\equiv 0, and ff has the roots α=(A+s)/2\alpha = (A + s)/2 and β=(A−s)/2\beta = (A - s)/2 in Fp\mathbb F_p (here pp is odd, so 22 is invertible). They satisfy αβ=(A2−s2)/4=1\alpha\beta = (A^2 - s^2)/4 = 1, so both are nonzero, and by Fermat's little theorem αp−1=βp−1=1\alpha^{p-1} = \beta^{p-1} = 1.

If ε=−1\varepsilon = -1, then ff has no root in Fp\mathbb F_p, so it is irreducible and K=Fp[X]/(f)K = \mathbb F_p[X]/(f) is a field with p2p^2 elements. Let α\alpha be the class of XX and β=A−α\beta = A - \alpha; then αβ=1\alpha\beta = 1. The map z↦zpz \mapsto z^p is a ring homomorphism of KK that fixes Fp\mathbb F_p, so f(αp)=f(α)p=0f(\alpha^p) = f(\alpha)^p = 0, and αp\alpha^p is a root of ff. The roots of ff in the field KK are α\alpha and β\beta. If αp=α\alpha^p = \alpha, then α\alpha would be a root of Zp−ZZ^p - Z, whose pp roots in KK are the elements of Fp\mathbb F_p, and α∉Fp\alpha \notin \mathbb F_p. So αp=β\alpha^p = \beta and αp+1=αβ=1\alpha^{p+1} = \alpha\beta = 1, and also βp+1=(α−1)p+1=1\beta^{p+1} = (\alpha^{-1})^{p+1} = 1.

In both cases there is a ring homomorphism from RR to a field of characteristic pp that sends tt to α\alpha (since f(α)=0f(\alpha) = 0), and so σ=A−t\sigma = A - t to β\beta. By Lemma 1, Vj(A) mod pV_j(A) \bmod p is αj+βj\alpha^j + \beta^j. With αp−ε=βp−ε=1\alpha^{p-\varepsilon} = \beta^{p-\varepsilon} = 1, every multiple jj of p−εp - \varepsilon gives αj+βj=2\alpha^j + \beta^j = 2. □\square

Theorem. WILLIAMS-P-PLUS-1(N,B,A)(N, B, A) ends and returns a divisor gg of NN. If an odd prime p∣Np \mid N does not divide A2−4A^2 - 4 and p−(D/p)p - (D/p) divides MM, then p∣gp \mid g. If moreover some prime power rfr^f that divides NN has VM(A)≢2(modrf)V_M(A) \not\equiv 2 \pmod{r^f}, then 1<g<N1 < g < N.

Proof. The loops end by Lemmas 4 and 5. The value g=gcd⁡(v−2,N)g = \gcd(v - 2, N) divides NN. By Lemma 5, v≡VM(A)(modN)v \equiv V_M(A) \pmod N, so v−2≡VM(A)−2(modp)v - 2 \equiv V_M(A) - 2 \pmod p for every p∣Np \mid N. Under the hypothesis on pp, Lemma 6 with j=Mj = M gives p∣v−2p \mid v - 2, so p∣gp \mid g and g>1g > 1. If rf∣Nr^f \mid N and rf∤VM(A)−2r^f \nmid V_M(A) - 2, then rf∤v−2r^f \nmid v - 2, so rf∤gr^f \nmid g and g≠Ng \ne N. ■\blacksquare

Back to Williams' p+1 algorithm