Algorithm Search⌘K

Proofs: Karatsuba multiplication

These are the human proofs of Karatsuba multiplication, at full length.

Karatsuba multiplication computes the exact product full

This proof shows: correctness, termination.

Let β≥2\beta \ge 2 and T≥1T \ge 1. For a limb array x=[x0,…,xk−1]x = [x_0, \ldots, x_{k-1}] with 0≤xi<β0 \le x_i < \beta write ⟨x⟩=∑ixiβi\langle x \rangle = \sum_i x_i\beta^i, so 0≤⟨x⟩<βk0 \le \langle x \rangle < \beta^k. We assume that SCHOOLBOOK(a,b)(a, b) returns 2n2n limbs of value ⟨a⟩⟨b⟩\langle a \rangle\langle b \rangle for nn-limb inputs (this is proved on the page of the schoolbook method), and that the additions and subtractions of limb arrays used in lines L5 and L6 are exact whenever their results are nonnegative and fit in the arrays.

Theorem. For every n≥1n \ge 1 and nn-limb arrays aa and bb, KARATSUBA(a,b)(a, b) terminates and returns 2n2n limbs of value ⟨a⟩⟨b⟩\langle a \rangle \langle b \rangle.

Proof. By strong induction on nn.

Base, n≤Tn \le T. Line L1 returns the schoolbook product, which is correct by assumption.

Step, n>Tn > T, so n≥2n \ge 2. Assume the theorem for all sizes less than nn. Let h=⌈n/2⌉h = \lceil n/2 \rceil. Since n≥2n \ge 2, h≤(n+1)/2<nh \le (n + 1)/2 < n. Line L2 sets a0=[a0,…,ah−1]a_0 = [a_0, \ldots, a_{h-1}] and a1=[ah,…,an−1,0,…,0]a_1 = [a_h, \ldots, a_{n-1}, 0, \ldots, 0], padded to hh limbs, and likewise for bb. Writing A0=⟨a0⟩A_0 = \langle a_0 \rangle and so on, we have ⟨a⟩=A1βh+A0\langle a \rangle = A_1\beta^h + A_0 and ⟨b⟩=B1βh+B0\langle b \rangle = B_1\beta^h + B_0 with 0≤A0,A1,B0,B1<βh0 \le A_0, A_1, B_0, B_1 < \beta^h.

Lines L3 and L4. The calls are on hh-limb arrays, h<nh < n, so by the induction hypothesis they terminate with z0=A0B0z_0 = A_0B_0 and z2=A1B1z_2 = A_1B_1.

Line L5. Let S=A0+A1S = A_0 + A_1 and U=B0+B1U = B_0 + B_1, both in [0,2βh−2][0, 2\beta^h - 2]. Write S=s+cβhS = s + c\beta^h and U=t+dβhU = t + d\beta^h with 0≤s,t<βh0 \le s, t < \beta^h and c,d∈{0,1}c, d \in \{0, 1\}; the hh-limb arrays of ss and tt are the low limbs of the sums. The call on h<nh < n limbs terminates with stst by the induction hypothesis. Then

SU=(s+cβh)(t+dβh)=st+(ct+ds)βh+cdβ2h,SU = (s + c\beta^h)(t + d\beta^h) = st + (ct + ds)\beta^h + cd\beta^{2h},

which is how the implementation forms the product of the sums from stst, so it computes SUSU exactly, in 2h+22h + 2 limbs since SU<4β2h≤β2h+2SU < 4\beta^{2h} \le \beta^{2h + 2}. Expanding,

SU−A0B0−A1B1=(A0+A1)(B0+B1)−A0B0−A1B1=A0B1+A1B0≥0.SU - A_0B_0 - A_1B_1 = (A_0 + A_1)(B_0 + B_1) - A_0B_0 - A_1B_1 = A_0B_1 + A_1B_0 \ge 0 .

So after the two subtractions, which never go below 0 because each intermediate value SU−A0B0=A0B1+A1B0+A1B1SU - A_0B_0 = A_0B_1 + A_1B_0 + A_1B_1 is nonnegative, z1=A0B1+A1B0z_1 = A_0B_1 + A_1B_0.

Line L6.

z0+z1βh+z2β2h=A0B0+(A0B1+A1B0)βh+A1B1β2h=(A1βh+A0)(B1βh+B0)=⟨a⟩⟨b⟩.z_0 + z_1\beta^h + z_2\beta^{2h} = A_0B_0 + (A_0B_1 + A_1B_0)\beta^h + A_1B_1\beta^{2h} = (A_1\beta^h + A_0)(B_1\beta^h + B_0) = \langle a \rangle\langle b \rangle .

All three terms are nonnegative, so every partial sum is at most ⟨a⟩⟨b⟩<β2n\langle a \rangle\langle b \rangle < \beta^{2n}, and the result fits in 2n2n limbs; the limbs above position 2n2n of the buffer are 0 and are cut off.

Termination. Each call with n>Tn > T makes three calls on h<nh < n limbs and otherwise a finite number of operations, so by induction every call terminates. The depth of the recursion is at most the number of halvings n↦⌈n/2⌉n \mapsto \lceil n/2 \rceil needed to reach TT, which is ⌈log⁡2(n/T)⌉\lceil \log_2 (n/T) \rceil for T≥1T \ge 1, plus one. ■\blacksquare

Corollary (cost). Let M(n)M(n) be the number of limb operations, with M(n)≤cn2M(n) \le cn^2 for n≤Tn \le T and M(n)≤3M(⌈n/2⌉)+c′nM(n) \le 3M(\lceil n/2 \rceil) + c'n for n>Tn > T. For n=2jTn = 2^jT the recursion tree has 3i3^i nodes of size n/2in/2^i at level ii, with cost c′n(3/2)ic'n(3/2)^i, so

M(n)≤∑i=0j−1c′n(3/2)i+3jcT2≤2c′n(3/2)j+cT23j=O(3j)=O((n/T)log⁡23),M(n) \le \sum_{i=0}^{j-1} c'n(3/2)^i + 3^j cT^2 \le 2c'n(3/2)^j + cT^2 3^j = O(3^j) = O\big((n/T)^{\log_2 3}\big),

using (3/2)jn=3jT(3/2)^j n = 3^j T and 3j=(n/T)log⁡233^j = (n/T)^{\log_2 3}. Monotonicity extends this to all nn, and the 3j3^j leaf products give the matching lower bound, so M(n)=Θ(nlog⁡23)M(n) = \Theta(n^{\log_2 3}) for fixed TT.

Back to Karatsuba multiplication