Lattice multiplication computes the exact product full
This proof shows: correctness, termination.
Let β≥2, and let a=[a0,…,am−1] and b=[b0,…,bn−1] be digit arrays with 0≤ai,bj≤β−1, of values A=∑iaiβi and B=∑jbjβj.
Theorem. LATTICE-MUL(a,b) terminates and returns digits c0,…,cm+n−1 in [0,β−1] with ∑kckβk=AB.
Proof. All loops have fixed bounds, so the procedure terminates.
Step 1: the grid. Line L2 uses division with remainder by β, so aibj=hijβ+lij with 0≤lij≤β−1 and hij≥0.
Step 2: the lanes. After lines L3 to L5, for each k with 0≤k≤m+n−1,
sk=i+j=k∑lij+i+j=k−1∑hij, where the sums run over 0≤i<m and 0≤j<n. The index i+j+1 of line L5 is at most (m−1)+(n−1)+1=m+n−1, so it is inside the array. Hence
k=0∑m+n−1skβk=i,j∑lijβi+j+i,j∑hijβi+j+1=i,j∑(hijβ+lij)βi+j=i,j∑aibjβi+j=AB. Step 3: the carry pass. We show the invariant
(C) before the step with index k of line L7, c0,…,ck−1 are digits in [0,β−1], carry≥0, and ∑k′<kck′βk′+carry⋅βk=∑k′<ksk′βk′.
For k=0, line L6 sets carry to 0, and both sides are 0. In the step with index k, line L8 forms t=sk+carry≥0, and line L9 writes t=ck+carry′β with 0≤ck≤β−1 and carry′=⌊t/β⌋≥0. Then
k′≤k∑ck′βk′+carry′βk+1=k′<k∑ck′βk′+tβk=k′<k∑ck′βk′+carry⋅βk+skβk=k′≤k∑sk′βk′, by (C) for k. So (C) holds for k+1.
Step 4: the end. After the step with k=m+n−1, (C) and Step 2 give ∑kckβk+carry⋅βm+n=AB. Since AB≤(βm−1)(βn−1)<βm+n and ∑kckβk≥0, the final carry is less than 1, so it is 0, and ∑kckβk=AB. ■
The size of the lane sums. Each lij≤β−1, and each hij≤⌊(β−1)2/β⌋=β−2, because (β−1)2=(β−2)β+1. Lane k has at most min(m,n) cells with i+j=k, and at most min(m,n) with i+j=k−1, since for fixed k the index i determines j, and both i<m and j<n. So sk≤2min(m,n)(β−1). Write p=min(m,n). If carry≤2p−1 before a step, then t≤2p(β−1)+2p−1=2pβ−1, and carry′=⌊t/β⌋≤2p−1. Since the first carry is 0, every carry is at most 2p−1, which gives the bound on the extra bits stated on the page.