Fast doubling returns the pair of Fibonacci numbers F(n) and F(n + 1) full
This proof shows: correctness, termination.
Let F0=0, F1=1 and Fm=Fm−1+Fm−2 for m≥2. Extend the sequence one step back by F−1=1, so that F1=F0+F−1 also holds. Let Q=(1110).
Lemma 1 (matrix form). For each k≥0, Qk=(Fk+1FkFkFk−1).
Proof. By induction on k. For k=0, Q0=I=(F1F0F0F−1). If the claim holds for k, then
Qk+1=QkQ=(Fk+1+FkFk+Fk−1Fk+1Fk)=(Fk+2Fk+1Fk+1Fk) by the recurrence, valid for the indices k+2≥2 and k+1≥1. □
Lemma 2 (doubling). For each k≥0,
F2k=Fk(2Fk+1−Fk),F2k+1=Fk2+Fk+12. Proof. By Lemma 1, Q2k=(Qk)2. The top right entry of (Qk)2 is Fk+1Fk+FkFk−1=Fk(Fk+1+Fk−1), and the top left entry is Fk+12+Fk2. The top right and top left entries of Q2k are F2k and F2k+1. Finally Fk−1=Fk+1−Fk by the recurrence (for k=0 by the definition of F−1), so Fk+1+Fk−1=2Fk+1−Fk. □
Invariant. Let tL−1tL−2⋯t0 be the binary digits of n read by line L2, most significant first. After j passes, let kj=∑i=0j−1tL−1−i2j−1−i be the number formed by the first j digits, so k0=0, kj+1=2kj+tL−1−j and kL=n. The invariant is
(a,b)=(Fkj,Fkj+1). Initialization. Line L1 sets (a,b)=(0,1)=(F0,F1), and k0=0.
Maintenance. Assume (a,b)=(Fk,Fk+1) with k=kj, and let t be the next digit. By Lemma 2, line L3 sets c=Fk(2Fk+1−Fk)=F2k and line L4 sets d=Fk2+Fk+12=F2k+1. If t=0, then kj+1=2k, and line L5 sets (a,b)=(F2k,F2k+1). If t=1, then kj+1=2k+1, and line L6 sets (a,b)=(F2k+1,F2k+F2k+1)=(F2k+1,F2k+2) by the recurrence. In both cases (a,b)=(Fkj+1,Fkj+1+1).
Termination. The loop runs once for each of the L binary digits of n.
Theorem. FAST-DOUBLING(n) returns (Fn,Fn+1) for each n≥0.
Proof. After the L passes, the invariant gives (a,b)=(FkL,FkL+1)=(Fn,Fn+1). For n=0 the single digit 0 gives c=0, d=1 and (a,b)=(0,1). ■
Modular version. Lemma 2 is an identity of integers, so it also holds modulo m: if a≡Fk and b≡Fk+1(modm), then c≡F2k and d≡F2k+1(modm), because sums, differences and products respect congruences. Reducing a and b after each pass keeps the invariant modulo m, and the result is Fnmodm.