Toom-3 returns the product full
This proof shows: correctness, termination.
Let a and b be nonnegative integers of n limbs in radix β=2w with w≥3, and let k=⌈n/3⌉ and x=βk. Line L2 writes a=a2x2+a1x+a0 and b=b2x2+b1x+b0 with 0≤ai,bi<x. Put A(t)=a2t2+a1t+a0, B(t)=b2t2+b1t+b0, and
C(t)=A(t)B(t)=r0+r1t+r2t2+r3t3+r4t4,rm=i+j=m∑aibj. Then ab=A(x)B(x)=C(x), and every rm≥0. For a polynomial, its value at ∞ means its leading coefficient: A(∞)=a2, B(∞)=b2, C(∞)=r4=a2b2.
Lemma 1 (evaluation). Line L3 computes A(0),A(1),A(−1),A(−2),A(∞), and the same for B, and each has absolute value less than 5x≤βk+1.
Proof. With p=a0+a2: p+a1=A(1), p−a1=A(−1), and 2(A(−1)+a2)−a0=2a0−2a1+4a2−a0=A(−2). The bounds: 0≤A(1)<3x, −x<A(−1)<2x, −2x<A(−2)<5x. Since w≥3, 5x<8x≤βx=βk+1, so each magnitude fits in k+1 limbs. □
Lemma 2 (termination and depth). For n≥3, k+1<n. Hence the recursion of line L4, whose operands have k+1 limbs, ends, and with n0=n and ni+1=⌈ni/3⌉+1 its depth is the least i with ni≤max(T,2), which is log3n+O(1).
Proof. ⌈n/3⌉≤(n+2)/3, so k+1≤(n+5)/3<n when 2n>5, that is for n≥3; the base case of line L1 covers n≤2 since T≥2. From ni+1≤(ni+5)/3 we get ni+1−5/2≤(ni−5/2)/3, so ni−5/2≤(n−5/2)/3i, and ni≤3 as soon as 3i≥2n, that is after at most ⌈log3(2n)⌉ steps; one more step reaches 2 if needed. Each step makes five calls, so the recursion tree is finite. □
Lemma 3 (products). By strong induction on n, every call returns the product of its operands. For the call on a and b, line L4 therefore gives v(t)=A(t)B(t)=C(t) for t∈{0,1,−1,−2}, where the sign of v(t) is the product of the signs of A(t) and B(t), and v(∞)=a2b2=r4.
Proof. The base case returns the schoolbook product. A recursive call multiplies the magnitudes ∣A(t)∣ and ∣B(t)∣, of k+1<n limbs by Lemmas 1 and 2, so by the induction hypothesis it returns ∣A(t)∣∣B(t)∣, and attaching the sign gives A(t)B(t). □
Lemma 4 (interpolation). The assignments of line L5 end with r0,r1,r2,r3,r4 equal to the coefficients of C, and the divisions in it are exact.
Proof. Evaluating C,
v(1)=r0+r1+r2+r3+r4,v(−1)=r0−r1+r2−r3+r4,v(−2)=r0−2r1+4r2−8r3+16r4. Follow line L5, writing t1,t2,t3 for the first values of r1,r2,r3:
- v(−2)−v(1)=−3r1+3r2−9r3+15r4, a multiple of 3, so t3=−r1+r2−3r3+5r4 exactly.
- v(1)−v(−1)=2r1+2r3, so t1=r1+r3 exactly.
- t2=v(−1)−v(0)=−r1+r2−r3+r4.
- t2−t3=2r3−4r4 is even, and (t2−t3)/2+2r4=r3.
- t2+t1−r4=r2.
- t1−r3=r1.
With r0=v(0) and r4=v(∞), all five coefficients are right. Every quotient is an integer combination of the rm, so the divisions are exact. □
Theorem. TOOM-3 returns ab as 2n limbs.
Proof. By Lemmas 3 and 4, line L6 computes ∑mrmxm=C(x)=ab. Each rm is nonnegative, so line L6 adds nonnegative shifted arrays, and every partial sum is at most ab<β2n; the result fits in 2n limbs. Termination is Lemma 2. ■