Karatsuba multiplication computes the exact product full
This proof shows: correctness, termination.
Let β≥2 and T≥1. For a limb array x=[x0,…,xk−1] with 0≤xi<β write ⟨x⟩=∑ixiβi, so 0≤⟨x⟩<βk. We assume that SCHOOLBOOK(a,b) returns 2n limbs of value ⟨a⟩⟨b⟩ for n-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≥1 and n-limb arrays a and b, KARATSUBA(a,b) terminates and returns 2n limbs of value ⟨a⟩⟨b⟩.
Proof. By strong induction on n.
Base, n≤T. Line L1 returns the schoolbook product, which is correct by assumption.
Step, n>T, so n≥2. Assume the theorem for all sizes less than n. Let h=⌈n/2⌉. Since n≥2, h≤(n+1)/2<n. Line L2 sets a0=[a0,…,ah−1] and a1=[ah,…,an−1,0,…,0], padded to h limbs, and likewise for b. Writing A0=⟨a0⟩ and so on, we have ⟨a⟩=A1βh+A0 and ⟨b⟩=B1βh+B0 with 0≤A0,A1,B0,B1<βh.
Lines L3 and L4. The calls are on h-limb arrays, h<n, so by the induction hypothesis they terminate with z0=A0B0 and z2=A1B1.
Line L5. Let S=A0+A1 and U=B0+B1, both in [0,2βh−2]. Write S=s+cβh and U=t+dβh with 0≤s,t<βh and c,d∈{0,1}; the h-limb arrays of s and t are the low limbs of the sums. The call on h<n limbs terminates with st by the induction hypothesis. Then
SU=(s+cβh)(t+dβh)=st+(ct+ds)βh+cdβ2h, which is how the implementation forms the product of the sums from st, so it computes SU exactly, in 2h+2 limbs since SU<4β2h≤β2h+2. Expanding,
SU−A0B0−A1B1=(A0+A1)(B0+B1)−A0B0−A1B1=A0B1+A1B0≥0. So after the two subtractions, which never go below 0 because each intermediate value SU−A0B0=A0B1+A1B0+A1B1 is nonnegative, z1=A0B1+A1B0.
Line L6.
z0+z1βh+z2β2h=A0B0+(A0B1+A1B0)βh+A1B1β2h=(A1βh+A0)(B1βh+B0)=⟨a⟩⟨b⟩. All three terms are nonnegative, so every partial sum is at most ⟨a⟩⟨b⟩<β2n, and the result fits in 2n limbs; the limbs above position 2n of the buffer are 0 and are cut off.
Termination. Each call with n>T makes three calls on h<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⌉ needed to reach T, which is ⌈log2(n/T)⌉ for T≥1, plus one. ■
Corollary (cost). Let M(n) be the number of limb operations, with M(n)≤cn2 for n≤T and M(n)≤3M(⌈n/2⌉)+c′n for n>T. For n=2jT the recursion tree has 3i nodes of size n/2i at level i, with cost c′n(3/2)i, so
M(n)≤i=0∑j−1c′n(3/2)i+3jcT2≤2c′n(3/2)j+cT23j=O(3j)=O((n/T)log23), using (3/2)jn=3jT and 3j=(n/T)log23. Monotonicity extends this to all n, and the 3j leaf products give the matching lower bound, so M(n)=Θ(nlog23) for fixed T.