Correctness of Pollard's rho algorithm full
This proof shows: correctness, termination.
Let N≥2, c an integer and 0≤s<N. Let f(x)=(x2+c)modN and xi=f(i)(s), so x0=s.
Lemma 1 (the passes). After i passes of the loop of lines L2 to L5, x=xi and y=x2i.
Proof. Line L1 gives x=y=s=x0. If x=xi and y=x2i before a pass, then line L3 gives x=f(xi)=xi+1 and line L4 gives y=f(f(x2i))=x2i+2. □
Lemma 2 (reduction). For each divisor m≥1 of N, the residues ui=ximodm satisfy ui+1=(ui2+c)modm. So ui is the orbit of u0 under the map h(u)=(u2+c)modm on the finite set {0,…,m−1}.
Proof. xi+1=xi2+c−kN for some integer k, and m∣N, so xi+1≡xi2+c≡ui2+c(modm). □
Lemma 3 (Floyd). Let u0,u1,… be the orbit of a map h on a finite set of size m. There are integers μ≥0 and λ≥1 with μ+λ≤m such that u0,…,uμ+λ−1 are distinct and ui+λ=ui for all i≥μ. For i≥1, ui=u2i holds if and only if i≥μ and λ∣i. The least such i≥1 is at most μ+λ≤m.
Proof. Among u0,…,um there are m+1 values in a set of size m, so some value repeats. Let j be the least index such that uj=uk for some k<j, and let μ=k, λ=j−k. Then u0,…,uj−1 are distinct, j=μ+λ≤m, and uμ+λ=uμ. Applying h repeatedly gives ui+λ=ui for every i≥μ, and so ui+tλ=ui for t≥0.
If i≥μ and λ∣i, write i=tλ; then u2i=ui+tλ=ui. Conversely, let ui=u2i with i≥1. If i<μ, then the value ui lies on the tail, but every uk with k≥μ is on the cycle {uμ,…,uμ+λ−1}, which is disjoint from the tail since u0,…,uμ+λ−1 are distinct; and if 2i<μ then ui and u2i are two distinct tail values. Either way ui=u2i, a contradiction, so i≥μ. Then u2i=ui with both indices at least μ, and on the cycle uk=ul for k,l≥μ holds only if λ∣k−l, because uμ,…,uμ+λ−1 are distinct. So λ∣i. Among μ,μ+1,…,μ+λ−1 there is a multiple of λ, and if μ=0 then λ itself works; so the least i≥1 with ui=u2i is at most μ+λ. □
Termination. Apply Lemmas 2 and 3 with m=N. There is an i∗≤N with xi∗=x2i∗. If the loop has not stopped before pass i∗, then at pass i∗, by Lemma 1, line L5 computes gcd(0,N)=N=1, and the loop stops. So the loop runs at most N passes.
Correctness. The loop stops only when g=1, and g is a gcd with N≥2, so the returned g is a divisor of N with g>1.
When it succeeds. Let the loop stop at pass i. For each prime power rk∥N, rk∣g if and only if xi≡x2i(modrk). So g<N exactly when some prime power part of N has not yet closed its cycle in the sense of Lemma 3 at pass i, and g>1 shows that some prime r∣N has xi≡x2i(modr). In particular, if a prime p∣N has its first i≥1 with xi≡x2i(modp) strictly smaller than the first such i for N, then the algorithm returns a proper factor of N that is a multiple of p. ■