Algorithm Search⌘K

Proofs: Montgomery multiplication

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

Correctness of Montgomery multiplication full

This proof shows: correctness, termination.

Let n≥1n \ge 1 be odd, let rr be the number of binary digits of nn, and let R=2rR = 2^r. Then 2r−1≤n<2r2^{r-1} \le n < 2^r, so R>nR > n, and gcd⁡(n,R)=1\gcd(n, R) = 1 because nn is odd. So RR has an inverse R−1R^{-1} modulo nn. (The same proof works for any rr with 2r>n2^r > n.)

Lemma 1 (setup, lines L2 to L6). The loop of lines L4 and L5 terminates, and afterwards nx≡1(modR)nx \equiv 1 \pmod R. Hence n′=(−x) mod Rn' = (-x) \bmod R satisfies nn′≡−1(modR)n n' \equiv -1 \pmod R and 0≤n′<R0 \le n' < R.

Proof. We show the invariant nx≡1(mod2j)nx \equiv 1 \pmod{2^j}. At the start, x=1x = 1, j=1j = 1, and n⋅1≡1(mod2)n \cdot 1 \equiv 1 \pmod 2 because nn is odd. Suppose nx≡1(mod2j)nx \equiv 1 \pmod{2^j} before a pass, and let j′=min⁡(2j,r)j' = \min(2j, r) and x′=x(2−nx) mod 2j′x' = x(2 - nx) \bmod 2^{j'}. Then

1−nx(2−nx)=1−2nx+(nx)2=(1−nx)2,1 - n x(2 - nx) = 1 - 2nx + (nx)^2 = (1 - nx)^2,

and 2j∣1−nx2^j \mid 1 - nx gives 22j∣(1−nx)22^{2j} \mid (1 - nx)^2, so nx(2−nx)≡1(mod22j)nx(2 - nx) \equiv 1 \pmod{2^{2j}} and hence modulo 2j′2^{j'}, since j′≤2jj' \le 2j. Reducing x(2−nx)x(2 - nx) modulo 2j′2^{j'} does not change this congruence. So the invariant holds after the pass. Each pass strictly increases jj, because j<rj < r and j≥1j \ge 1 give min⁡(2j,r)>j\min(2j, r) > j, and jj never exceeds rr. So the loop stops, with j=rj = r, and then nx≡1(mod2r)nx \equiv 1 \pmod{2^r}. Line L6 gives nn′≡−nx≡−1(modR)n n' \equiv -nx \equiv -1 \pmod R. □\square

Line L7 sets R2=R2 mod nR2 = R^2 \bmod n by ordinary division with remainder.

Lemma 2 (REDC, lines L8 to L11). For every integer TT with 0≤T<nR0 \le T < nR, REDC(T)(T) returns tt with 0≤t<n0 \le t < n and t≡TR−1(modn)t \equiv T R^{-1} \pmod n.

Proof. Line L8 sets m=((T mod R)⋅n′) mod Rm = ((T \bmod R) \cdot n') \bmod R, so 0≤m<R0 \le m < R and m≡Tn′(modR)m \equiv T n' \pmod R. Then

T+mn≡T+Tn′n≡T+T⋅(−1)=0(modR)T + mn \equiv T + T n' n \equiv T + T \cdot (-1) = 0 \pmod R

by Lemma 1. So RR divides T+mnT + mn, and t=(T+mn)/Rt = (T + mn)/R in line L9 is an integer. The equation tR=T+mntR = T + mn gives tR≡T(modn)tR \equiv T \pmod n, and multiplying by R−1R^{-1} gives t≡TR−1(modn)t \equiv T R^{-1} \pmod n.

Bounds: T+mn≥0T + mn \ge 0, so t≥0t \ge 0. And T<nRT < nR and m≤R−1m \le R - 1 give T+mn<nR+(R−1)n<2nRT + mn < nR + (R - 1) n < 2nR, so t<2nt < 2n. If t≥nt \ge n, line L10 replaces tt by t−nt - n, which lies in [0,n)[0, n) and is congruent to tt modulo nn. If t<nt < n, it is already in [0,n)[0, n). In both cases the returned value is the unique residue in [0,n)[0, n) congruent to TR−1TR^{-1}, that is, TR−1 mod nTR^{-1} \bmod n. □\square

Theorem. For a,b∈{0,…,n−1}a, b \in \{0, \ldots, n - 1\}, write a~=aR mod n\tilde a = aR \bmod n for the Montgomery form. Then:

  1. TO-MONT(a)=a~(a) = \tilde a;
  2. FROM-MONT(a~)=a(\tilde a) = a;
  3. MONT-MUL(a~,b~)=ab mod n~(\tilde a, \tilde b) = \widetilde{ab \bmod n};
  4. FROM-MONT((MONT-MUL((TO-MONT(a)(a), TO-MONT(b)))=ab mod n(b))) = ab \bmod n.

Proof. (1) The argument of REDC in line L13 is T=a⋅R2T = a \cdot R2 with 0≤a<n0 \le a < n and 0≤R2<n<R0 \le R2 < n < R, so 0≤T<nR0 \le T < nR. By Lemma 2 the result is ≡aR2R−1=aR(modn)\equiv a R^2 R^{-1} = aR \pmod n and lies in [0,n)[0, n), so it equals a~\tilde a.

(2) In line L14, T=a~<n<nRT = \tilde a < n < nR, so Lemma 2 gives a~R−1≡aRR−1=a\tilde a R^{-1} \equiv aRR^{-1} = a, and the result in [0,n)[0, n) is aa.

(3) In line L12, T=a~b~T = \tilde a \tilde b with 0≤a~,b~<n0 \le \tilde a, \tilde b < n, so 0≤T<n2<nR0 \le T < n^2 < nR. Lemma 2 gives a~b~R−1≡aR⋅bR⋅R−1=abR(modn)\tilde a \tilde b R^{-1} \equiv aR \cdot bR \cdot R^{-1} = abR \pmod n, and the result in [0,n)[0, n) is (ab mod n)R mod n(ab \bmod n) R \bmod n.

(4) follows from (1), (3) and (2), applied in this order to ab mod nab \bmod n. ■\blacksquare

Termination. The setup loop terminates by Lemma 1, and REDC, MONT-MUL, TO-MONT and FROM-MONT have no loops. So every call returns.

Exponentiation. By (3), every MONT-MUL in square-and-multiply maps forms to forms; so if the accumulator starts as the form of 11 and the base as the form of xx, then after processing the exponent ee the accumulator is the form of xe mod nx^e \bmod n, and FROM-MONT returns xe mod nx^e \bmod n by (2).

Back to Montgomery multiplication