Algorithm Search⌘K

Proofs: Toom-Cook multiplication

These are the human proofs of Toom-Cook multiplication, at full length.

Toom-3 returns the product full

This proof shows: correctness, termination.

Let aa and bb be nonnegative integers of nn limbs in radix β=2w\beta = 2^w with w≥3w \ge 3, and let k=⌈n/3⌉k = \lceil n/3 \rceil and x=βkx = \beta^k. Line L2 writes a=a2x2+a1x+a0a = a_2x^2 + a_1x + a_0 and b=b2x2+b1x+b0b = b_2x^2 + b_1x + b_0 with 0≤ai,bi<x0 \le a_i, b_i < x. Put A(t)=a2t2+a1t+a0A(t) = a_2t^2 + a_1t + a_0, B(t)=b2t2+b1t+b0B(t) = b_2t^2 + b_1t + b_0, and

C(t)=A(t)B(t)=r0+r1t+r2t2+r3t3+r4t4,rm=∑i+j=maibj.C(t) = A(t)B(t) = r_0 + r_1t + r_2t^2 + r_3t^3 + r_4t^4, \qquad r_m = \sum_{i + j = m} a_ib_j .

Then ab=A(x)B(x)=C(x)ab = A(x)B(x) = C(x), and every rm≥0r_m \ge 0. For a polynomial, its value at ∞\infty means its leading coefficient: A(∞)=a2A(\infty) = a_2, B(∞)=b2B(\infty) = b_2, C(∞)=r4=a2b2C(\infty) = r_4 = a_2b_2.

Lemma 1 (evaluation). Line L3 computes A(0),A(1),A(−1),A(−2),A(∞)A(0), A(1), A(-1), A(-2), A(\infty), and the same for BB, and each has absolute value less than 5x≤βk+15x \le \beta^{k+1}.

Proof. With p=a0+a2p = a_0 + a_2: p+a1=A(1)p + a_1 = A(1), p−a1=A(−1)p - a_1 = A(-1), and 2(A(−1)+a2)−a0=2a0−2a1+4a2−a0=A(−2)2(A(-1) + a_2) - a_0 = 2a_0 - 2a_1 + 4a_2 - a_0 = A(-2). The bounds: 0≤A(1)<3x0 \le A(1) < 3x, −x<A(−1)<2x-x < A(-1) < 2x, −2x<A(−2)<5x-2x < A(-2) < 5x. Since w≥3w \ge 3, 5x<8x≤βx=βk+15x < 8x \le \beta x = \beta^{k+1}, so each magnitude fits in k+1k + 1 limbs. □\square

Lemma 2 (termination and depth). For n≥3n \ge 3, k+1<nk + 1 < n. Hence the recursion of line L4, whose operands have k+1k + 1 limbs, ends, and with n0=nn_0 = n and ni+1=⌈ni/3⌉+1n_{i+1} = \lceil n_i/3 \rceil + 1 its depth is the least ii with ni≤max⁡(T,2)n_i \le \max(T, 2), which is log⁡3n+O(1)\log_3 n + O(1).

Proof. ⌈n/3⌉≤(n+2)/3\lceil n/3 \rceil \le (n + 2)/3, so k+1≤(n+5)/3<nk + 1 \le (n + 5)/3 < n when 2n>52n > 5, that is for n≥3n \ge 3; the base case of line L1 covers n≤2n \le 2 since T≥2T \ge 2. From ni+1≤(ni+5)/3n_{i+1} \le (n_i + 5)/3 we get ni+1−5/2≤(ni−5/2)/3n_{i+1} - 5/2 \le (n_i - 5/2)/3, so ni−5/2≤(n−5/2)/3in_i - 5/2 \le (n - 5/2)/3^i, and ni≤3n_i \le 3 as soon as 3i≥2n3^i \ge 2n, that is after at most ⌈log⁡3(2n)⌉\lceil \log_3 (2n) \rceil steps; one more step reaches 2 if needed. Each step makes five calls, so the recursion tree is finite. □\square

Lemma 3 (products). By strong induction on nn, every call returns the product of its operands. For the call on aa and bb, line L4 therefore gives v(t)=A(t)B(t)=C(t)v(t) = A(t)B(t) = C(t) for t∈{0,1,−1,−2}t \in \{0, 1, -1, -2\}, where the sign of v(t)v(t) is the product of the signs of A(t)A(t) and B(t)B(t), and v(∞)=a2b2=r4v(\infty) = a_2 b_2 = r_4.

Proof. The base case returns the schoolbook product. A recursive call multiplies the magnitudes ∣A(t)∣|A(t)| and ∣B(t)∣|B(t)|, of k+1<nk + 1 < n limbs by Lemmas 1 and 2, so by the induction hypothesis it returns ∣A(t)∣∣B(t)∣|A(t)||B(t)|, and attaching the sign gives A(t)B(t)A(t)B(t). □\square

Lemma 4 (interpolation). The assignments of line L5 end with r0,r1,r2,r3,r4r_0, r_1, r_2, r_3, r_4 equal to the coefficients of CC, and the divisions in it are exact.

Proof. Evaluating CC,

v(1)=r0+r1+r2+r3+r4,v(−1)=r0−r1+r2−r3+r4,v(−2)=r0−2r1+4r2−8r3+16r4.v(1) = r_0 + r_1 + r_2 + r_3 + r_4, \quad v(-1) = r_0 - r_1 + r_2 - r_3 + r_4, \quad v(-2) = r_0 - 2r_1 + 4r_2 - 8r_3 + 16r_4 .

Follow line L5, writing t1,t2,t3t_1, t_2, t_3 for the first values of r1,r2,r3r_1, r_2, r_3:

  • v(−2)−v(1)=−3r1+3r2−9r3+15r4v(-2) - v(1) = -3r_1 + 3r_2 - 9r_3 + 15r_4, a multiple of 3, so t3=−r1+r2−3r3+5r4t_3 = -r_1 + r_2 - 3r_3 + 5r_4 exactly.
  • v(1)−v(−1)=2r1+2r3v(1) - v(-1) = 2r_1 + 2r_3, so t1=r1+r3t_1 = r_1 + r_3 exactly.
  • t2=v(−1)−v(0)=−r1+r2−r3+r4t_2 = v(-1) - v(0) = -r_1 + r_2 - r_3 + r_4.
  • t2−t3=2r3−4r4t_2 - t_3 = 2r_3 - 4r_4 is even, and (t2−t3)/2+2r4=r3(t_2 - t_3)/2 + 2r_4 = r_3.
  • t2+t1−r4=r2t_2 + t_1 - r_4 = r_2.
  • t1−r3=r1t_1 - r_3 = r_1.

With r0=v(0)r_0 = v(0) and r4=v(∞)r_4 = v(\infty), all five coefficients are right. Every quotient is an integer combination of the rmr_m, so the divisions are exact. □\square

Theorem. TOOM-3 returns abab as 2n2n limbs.

Proof. By Lemmas 3 and 4, line L6 computes ∑mrmxm=C(x)=ab\sum_m r_m x^m = C(x) = ab. Each rmr_m is nonnegative, so line L6 adds nonnegative shifted arrays, and every partial sum is at most ab<β2nab < \beta^{2n}; the result fits in 2n2n limbs. Termination is Lemma 2. ■\blacksquare

Back to Toom-Cook multiplication