Algorithm Search⌘K

Proofs: Multiprecision integer representation

These are the human proofs of Multiprecision integer representation, at full length.

The operations on multiprecision integers are exact and keep the representation normalized full

This proof shows: correctness.

We write β=2w\beta = 2^w, and for a list of limbs aa we write mag⁡(a)=∑i=0∣a∣−1aiβi\operatorname{mag}(a) = \sum_{i=0}^{|a|-1} a_i \beta^i. A pair (s,a)(s, a) is normalized when it meets (N): a∣a∣−1≠0a_{|a|-1} \ne 0 if ∣a∣>0|a| > 0, and s=+s = + if ∣a∣=0|a| = 0. A list cc with c∣c∣−1≠0c_{|c|-1} \ne 0 or ∣c∣=0|c| = 0 is a normalized list.

Fact 1 (length and size). If aa is a normalized list with ∣a∣=k≥1|a| = k \ge 1, then βk−1≤mag⁡(a)≤βk−1\beta^{k-1} \le \operatorname{mag}(a) \le \beta^k - 1. Proof. The top term is at least βk−1\beta^{k-1} and the other terms are nonnegative. Each limb is at most β−1\beta - 1, so mag⁡(a)≤(β−1)∑i<kβi=βk−1\operatorname{mag}(a) \le (\beta - 1)\sum_{i<k} \beta^i = \beta^k - 1. □\square

Lemma 1 (CMP-MAG). For normalized lists aa and bb, CMP-MAG(a,b)(a, b) returns the sign of mag⁡(a)−mag⁡(b)\operatorname{mag}(a) - \operatorname{mag}(b).

Proof. If ∣a∣>∣b∣|a| > |b|, then ∣a∣≥1|a| \ge 1 and, by Fact 1, mag⁡(a)≥β∣a∣−1≥β∣b∣>mag⁡(b)\operatorname{mag}(a) \ge \beta^{|a|-1} \ge \beta^{|b|} > \operatorname{mag}(b); the empty list bb has magnitude 0<mag⁡(a)0 < \operatorname{mag}(a) too. So line C1 is right, and the case ∣a∣<∣b∣|a| < |b| is symmetric. Let ∣a∣=∣b∣=k|a| = |b| = k. If a=ba = b, the loop of lines C2 and C3 finds no difference and line C4 returns 0. Otherwise let ii be the largest index with ai≠bia_i \ne b_i; the loop finds this ii first, because it runs down from k−1k - 1. Say ai>bia_i > b_i, so ai−bi≥1a_i - b_i \ge 1. Then

mag⁡(a)−mag⁡(b)=(ai−bi)βi+∑j<i(aj−bj)βj≥βi−(β−1)∑j<iβj=βi−(βi−1)=1,\operatorname{mag}(a) - \operatorname{mag}(b) = (a_i - b_i)\beta^i + \sum_{j<i} (a_j - b_j)\beta^j \ge \beta^i - (\beta - 1)\sum_{j<i}\beta^j = \beta^i - (\beta^i - 1) = 1 ,

and line C3 returns 1. The case ai<bia_i < b_i is symmetric. □\square

Lemma 2 (ADD-MAG). For lists aa and bb of limbs with m=∣a∣≥∣b∣=nm = |a| \ge |b| = n, ADD-MAG returns a list cc of limbs with mag⁡(c)=mag⁡(a)+mag⁡(b)\operatorname{mag}(c) = \operatorname{mag}(a) + \operatorname{mag}(b). If aa is a normalized list, so is cc.

Proof. Write bi=0b_i = 0 for i≥ni \ge n. We prove the invariant

(A) at the start of step ii of the loop of line L8, c0,…,ci−1c_0, \ldots, c_{i-1} are limbs, carry∈{0,1}\text{carry} \in \{0, 1\}, and ∑j<icjβj+carry⋅βi=∑j<i(aj+bj)βj\sum_{j<i} c_j \beta^j + \text{carry} \cdot \beta^i = \sum_{j<i} (a_j + b_j)\beta^j.

Initialization. For i=0i = 0, line L7 sets carry to 0, and both sides are 0.

Maintenance. In step ii, line L9 sets t=ai+bi+carryt = a_i + b_i + \text{carry}, so 0≤t≤2(β−1)+1=2β−10 \le t \le 2(\beta - 1) + 1 = 2\beta - 1. Line L10 writes t=ci+carry′βt = c_i + \text{carry}' \beta with carry′∈{0,1}\text{carry}' \in \{0, 1\} and 0≤ci<β0 \le c_i < \beta: if t≥βt \ge \beta then ci=t−β≤β−1c_i = t - \beta \le \beta - 1, else ci=tc_i = t. Then

∑j≤icjβj+carry′βi+1=∑j<icjβj+tβi=∑j<icjβj+carry⋅βi+(ai+bi)βi,\sum_{j \le i} c_j \beta^j + \text{carry}' \beta^{i+1} = \sum_{j<i} c_j\beta^j + t \beta^i = \sum_{j<i} c_j\beta^j + \text{carry}\cdot\beta^i + (a_i + b_i)\beta^i ,

which by (A) is ∑j≤i(aj+bj)βj\sum_{j \le i} (a_j + b_j) \beta^j. So (A) holds for i+1i + 1.

Termination. The loop runs mm times. At its end, (A) for i=mi = m says ∑j<mcjβj+carry⋅βm=mag⁡(a)+mag⁡(b)\sum_{j<m} c_j\beta^j + \text{carry}\cdot \beta^m = \operatorname{mag}(a) + \operatorname{mag}(b). Line L11 appends the limb 1 exactly when carry is 1, so mag⁡(c)=mag⁡(a)+mag⁡(b)\operatorname{mag}(c) = \operatorname{mag}(a) + \operatorname{mag}(b).

Normalized. If carry is 1, the top limb of cc is 1. If carry is 0 and m≥1m \ge 1, then mag⁡(c)≥mag⁡(a)≥βm−1\operatorname{mag}(c) \ge \operatorname{mag}(a) \ge \beta^{m-1} by Fact 1, while ∑j<m−1cjβj≤βm−1−1\sum_{j < m-1} c_j \beta^j \le \beta^{m-1} - 1; so cm−1≠0c_{m-1} \ne 0. If m=0m = 0, then cc is empty. □\square

Lemma 3 (SUB-MAG). For lists aa and bb of limbs with m=∣a∣≥∣b∣=nm = |a| \ge |b| = n and mag⁡(a)≥mag⁡(b)\operatorname{mag}(a) \ge \operatorname{mag}(b), SUB-MAG returns a normalized list cc with mag⁡(c)=mag⁡(a)−mag⁡(b)\operatorname{mag}(c) = \operatorname{mag}(a) - \operatorname{mag}(b).

Proof. Again let bi=0b_i = 0 for i≥ni \ge n. The invariant of the loop of line L14 is

(S) at the start of step ii, c0,…,ci−1c_0, \ldots, c_{i-1} are limbs, borrow∈{0,1}\text{borrow} \in \{0, 1\}, and ∑j<icjβj−borrow⋅βi=∑j<i(aj−bj)βj\sum_{j<i} c_j\beta^j - \text{borrow}\cdot\beta^i = \sum_{j<i}(a_j - b_j)\beta^j.

Initialization. Line L13 sets borrow to 0; both sides are 0 for i=0i = 0.

Maintenance. Line L15 gives t=ai−bi−borrowt = a_i - b_i - \text{borrow}, so −β≤t≤β−1-\beta \le t \le \beta - 1. Line L16 writes t=ci−borrow′βt = c_i - \text{borrow}'\beta with borrow′∈{0,1}\text{borrow}' \in \{0, 1\} and 0≤ci<β0 \le c_i < \beta: if t<0t < 0 then ci=t+β∈[0,β−1]c_i = t + \beta \in [0, \beta - 1], else ci=tc_i = t. The same computation as in Lemma 2, with −borrow-\text{borrow} in place of carry, gives (S) for i+1i + 1.

Termination. After mm steps, mag⁡(c)−borrow⋅βm=mag⁡(a)−mag⁡(b)≥0\operatorname{mag}(c) - \text{borrow}\cdot\beta^m = \operatorname{mag}(a) - \operatorname{mag}(b) \ge 0. Since mag⁡(c)≤βm−1<βm\operatorname{mag}(c) \le \beta^m - 1 < \beta^m, the borrow must be 0, and mag⁡(c)=mag⁡(a)−mag⁡(b)\operatorname{mag}(c) = \operatorname{mag}(a) - \operatorname{mag}(b). Line L17 removes only zero limbs at the top, which do not change mag⁡(c)\operatorname{mag}(c), and it stops with an empty list or a nonzero top limb, so cc is normalized. □\square

Theorem (ADD). For normalized pairs x=(s,a)x = (s, a) and y=(t,b)y = (t, b), ADD(x,y)(x, y) returns a normalized pair with the value val⁡(x)+val⁡(y)\operatorname{val}(x) + \operatorname{val}(y).

Proof. Write σ(+)=1\sigma(+) = 1 and σ(−)=−1\sigma(-) = -1, so val⁡(x)=σ(s)mag⁡(a)\operatorname{val}(x) = \sigma(s)\operatorname{mag}(a). ADD-MAG and SUB-MAG are called with the longer list first: ADD-MAG swaps its arguments when needed, and SUB-MAG is called only after CMP-MAG has shown that its first argument has the larger or equal magnitude, which by Fact 1 for normalized lists means at least as many limbs.

Case s=ts = t. Line L2 returns (s,c)(s, c) with mag⁡(c)=mag⁡(a)+mag⁡(b)\operatorname{mag}(c) = \operatorname{mag}(a) + \operatorname{mag}(b) by Lemma 2, so the value is σ(s)(mag⁡(a)+mag⁡(b))=val⁡(x)+val⁡(y)\sigma(s)(\operatorname{mag}(a) + \operatorname{mag}(b)) = \operatorname{val}(x) + \operatorname{val}(y). The longer of aa and bb is a normalized list, so cc is a normalized list by Lemma 2. If cc is empty, then aa and bb are both empty, so s=t=+s = t = + by (N) for xx, and the pair is normalized.

Case s≠ts \ne t. Then val⁡(x)+val⁡(y)=σ(s)(mag⁡(a)−mag⁡(b))\operatorname{val}(x) + \operatorname{val}(y) = \sigma(s)(\operatorname{mag}(a) - \operatorname{mag}(b)). By Lemma 1, line L3 gets the sign cc of mag⁡(a)−mag⁡(b)\operatorname{mag}(a) - \operatorname{mag}(b). If c=0c = 0, the sum is 0, and line L4 returns (+,[ ])(+, [\,]), which is normalized and has the value 0. If c>0c > 0, line L5 returns (s,d)(s, d) with mag⁡(d)=mag⁡(a)−mag⁡(b)>0\operatorname{mag}(d) = \operatorname{mag}(a) - \operatorname{mag}(b) > 0 by Lemma 3; dd is a normalized list and is not empty, so the pair is normalized, and its value is σ(s)(mag⁡(a)−mag⁡(b))\sigma(s)(\operatorname{mag}(a) - \operatorname{mag}(b)), the sum. If c<0c < 0, line L6 returns (t,d)(t, d) with mag⁡(d)=mag⁡(b)−mag⁡(a)>0\operatorname{mag}(d) = \operatorname{mag}(b) - \operatorname{mag}(a) > 0, and σ(t)(mag⁡(b)−mag⁡(a))=σ(s)(mag⁡(a)−mag⁡(b))\sigma(t)(\operatorname{mag}(b) - \operatorname{mag}(a)) = \sigma(s)(\operatorname{mag}(a) - \operatorname{mag}(b)) since σ(t)=−σ(s)\sigma(t) = -\sigma(s). □\square

Subtraction and negation. Negation maps (s,a)(s, a) to (sˉ,a)(\bar s, a) for aa nonempty and keeps (+,[ ])(+, [\,]); the result is normalized and has the value −val⁡(x)-\operatorname{val}(x). Then SUB(x,y)(x, y) = ADD(x,−y)(x, -y) is correct by the theorem.

Lemma 4 (MUL). For normalized pairs, MUL returns a normalized pair with the value val⁡(x)val⁡(y)\operatorname{val}(x)\operatorname{val}(y).

Proof. If m=0m = 0 or n=0n = 0 the product is 0, and line M1 returns (+,[ ])(+, [\,]). Otherwise the schoolbook multiplication proof shows that after lines M2 to M7 the list cc of m+nm + n limbs has mag⁡(c)=mag⁡(a)mag⁡(b)\operatorname{mag}(c) = \operatorname{mag}(a)\operatorname{mag}(b). By Fact 1, mag⁡(a)mag⁡(b)≥βm−1βn−1=βm+n−2\operatorname{mag}(a)\operatorname{mag}(b) \ge \beta^{m-1}\beta^{n-1} = \beta^{m+n-2}, so the limbs cm+n−2c_{m+n-2} and cm+n−1c_{m+n-1} are not both zero: if both were zero, mag⁡(c)≤βm+n−2−1\operatorname{mag}(c) \le \beta^{m+n-2} - 1. Hence removing one zero top limb at line M8 leaves a normalized nonempty list. The sign is ++ exactly when the signs agree, and σ(s)σ(t)\sigma(s)\sigma(t) is the sign of the product. □\square

Lemma 5 (DIV-LIMB). For a normalized pair (s,a)(s, a) and a limb 0<d<β0 < d < \beta, DIV-LIMB returns a normalized pair (s′,q)(s', q) and an rr with mag⁡(a)=mag⁡(q) d+r\operatorname{mag}(a) = \operatorname{mag}(q)\, d + r and 0≤r<d0 \le r < d.

Proof. Let k=∣a∣k = |a| and Pi=∑j≥iajβj−iP_i = \sum_{j \ge i} a_j \beta^{j-i}, the number formed by the limbs ak−1,…,aia_{k-1}, \ldots, a_i; so Pk=0P_k = 0, Pi=Pi+1β+aiP_i = P_{i+1}\beta + a_i and P0=mag⁡(a)P_0 = \operatorname{mag}(a). The invariant before the step with index ii (from k−1k - 1 down) is Pi+1=∑j>iqjβj−i−1⋅d+rP_{i+1} = \sum_{j > i} q_j \beta^{j-i-1} \cdot d + r with 0≤r<d0 \le r < d. It holds at the start with r=0r = 0 and no digits. In the step, u=rβ+aiu = r\beta + a_i satisfies 0≤u≤(d−1)β+β−1<dβ0 \le u \le (d - 1)\beta + \beta - 1 < d\beta, so qi=⌊u/d⌋<βq_i = \lfloor u/d \rfloor < \beta is a limb, and u=qid+r′u = q_i d + r' with 0≤r′<d0 \le r' < d. Then Pi=Pi+1β+ai=(∑j>iqjβj−i)d+u=(∑j≥iqjβj−i)d+r′P_i = P_{i+1}\beta + a_i = \big(\sum_{j>i} q_j \beta^{j-i}\big) d + u = \big(\sum_{j \ge i} q_j\beta^{j-i}\big) d + r', the invariant for i−1i - 1. After the step with i=0i = 0 this is the claim. Line D4 removes the zero top limbs and sets the sign to ++ for an empty quotient, so the pair is normalized. ■\blacksquare

Back to Multiprecision integer representation