Algorithm Search⌘K

Proofs: Lattice multiplication

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

Lattice multiplication computes the exact product full

This proof shows: correctness, termination.

Let β≥2\beta \ge 2, and let a=[a0,…,am−1]a = [a_0, \ldots, a_{m-1}] and b=[b0,…,bn−1]b = [b_0, \ldots, b_{n-1}] be digit arrays with 0≤ai,bj≤β−10 \le a_i, b_j \le \beta - 1, of values A=∑iaiβiA = \sum_i a_i\beta^i and B=∑jbjβjB = \sum_j b_j\beta^j.

Theorem. LATTICE-MUL(a,b)(a, b) terminates and returns digits c0,…,cm+n−1c_0, \ldots, c_{m+n-1} in [0,β−1][0, \beta - 1] with ∑kckβk=AB\sum_k c_k\beta^k = AB.

Proof. All loops have fixed bounds, so the procedure terminates.

Step 1: the grid. Line L2 uses division with remainder by β\beta, so aibj=hijβ+lija_i b_j = h_{ij}\beta + l_{ij} with 0≤lij≤β−10 \le l_{ij} \le \beta - 1 and hij≥0h_{ij} \ge 0.

Step 2: the lanes. After lines L3 to L5, for each kk with 0≤k≤m+n−10 \le k \le m + n - 1,

sk=∑i+j=klij+∑i+j=k−1hij,s_k = \sum_{i + j = k} l_{ij} + \sum_{i + j = k - 1} h_{ij} ,

where the sums run over 0≤i<m0 \le i < m and 0≤j<n0 \le j < n. The index i+j+1i + j + 1 of line L5 is at most (m−1)+(n−1)+1=m+n−1(m - 1) + (n - 1) + 1 = m + n - 1, so it is inside the array. Hence

∑k=0m+n−1skβk=∑i,jlijβi+j+∑i,jhijβi+j+1=∑i,j(hijβ+lij)βi+j=∑i,jaibjβi+j=AB.\sum_{k=0}^{m+n-1} s_k\beta^k = \sum_{i,j} l_{ij}\beta^{i+j} + \sum_{i,j} h_{ij}\beta^{i+j+1} = \sum_{i,j} (h_{ij}\beta + l_{ij})\beta^{i+j} = \sum_{i,j} a_i b_j \beta^{i+j} = AB .

Step 3: the carry pass. We show the invariant

(C) before the step with index kk of line L7, c0,…,ck−1c_0, \ldots, c_{k-1} are digits in [0,β−1][0, \beta - 1], carry≥0\text{carry} \ge 0, and ∑k′<kck′βk′+carry⋅βk=∑k′<ksk′βk′\sum_{k'<k} c_{k'}\beta^{k'} + \text{carry}\cdot\beta^k = \sum_{k'<k} s_{k'}\beta^{k'}.

For k=0k = 0, line L6 sets carry to 0, and both sides are 0. In the step with index kk, line L8 forms t=sk+carry≥0t = s_k + \text{carry} \ge 0, and line L9 writes t=ck+carry′βt = c_k + \text{carry}'\beta with 0≤ck≤β−10 \le c_k \le \beta - 1 and carry′=⌊t/β⌋≥0\text{carry}' = \lfloor t/\beta \rfloor \ge 0. Then

∑k′≤kck′βk′+carry′βk+1=∑k′<kck′βk′+tβk=∑k′<kck′βk′+carry⋅βk+skβk=∑k′≤ksk′βk′,\sum_{k' \le k} c_{k'}\beta^{k'} + \text{carry}'\beta^{k+1} = \sum_{k'<k} c_{k'}\beta^{k'} + t\beta^k = \sum_{k'<k} c_{k'}\beta^{k'} + \text{carry}\cdot\beta^k + s_k\beta^k = \sum_{k' \le k} s_{k'}\beta^{k'} ,

by (C) for kk. So (C) holds for k+1k + 1.

Step 4: the end. After the step with k=m+n−1k = m + n - 1, (C) and Step 2 give ∑kckβk+carry⋅βm+n=AB\sum_{k} c_k\beta^k + \text{carry}\cdot\beta^{m+n} = AB. Since AB≤(βm−1)(βn−1)<βm+nAB \le (\beta^m - 1)(\beta^n - 1) < \beta^{m+n} and ∑kckβk≥0\sum_k c_k \beta^k \ge 0, the final carry is less than 1, so it is 0, and ∑kckβk=AB\sum_k c_k\beta^k = AB. ■\blacksquare

The size of the lane sums. Each lij≤β−1l_{ij} \le \beta - 1, and each hij≤⌊(β−1)2/β⌋=β−2h_{ij} \le \lfloor (\beta - 1)^2/\beta \rfloor = \beta - 2, because (β−1)2=(β−2)β+1(\beta - 1)^2 = (\beta - 2)\beta + 1. Lane kk has at most min⁡(m,n)\min(m, n) cells with i+j=ki + j = k, and at most min⁡(m,n)\min(m, n) with i+j=k−1i + j = k - 1, since for fixed kk the index ii determines jj, and both i<mi < m and j<nj < n. So sk≤2min⁡(m,n)(β−1)s_k \le 2\min(m, n)(\beta - 1). Write p=min⁡(m,n)p = \min(m, n). If carry≤2p−1\text{carry} \le 2p - 1 before a step, then t≤2p(β−1)+2p−1=2pβ−1t \le 2p(\beta - 1) + 2p - 1 = 2p\beta - 1, and carry′=⌊t/β⌋≤2p−1\text{carry}' = \lfloor t/\beta \rfloor \le 2p - 1. Since the first carry is 0, every carry is at most 2p−12p - 1, which gives the bound on the extra bits stated on the page.

Back to Lattice multiplication