Algorithm Search⌘K

Proofs: Binary exponentiation

These are the human proofs of Binary exponentiation, at full length.

Binary exponentiation returns x^e mod m full

This proof shows: correctness, termination, multiplications.worst.

Claim. Let xx be an integer, e0≥0e_0 \ge 0 an integer and m≥1m \ge 1 an integer. BINARY-EXP(x,e0,m)(x, e_0, m) stops after ⌊lg⁡e0⌋+1\lfloor \lg e_0 \rfloor + 1 passes of the loop when e0≥1e_0 \ge 1, and after none when e0=0e_0 = 0. It returns the integer rr with 0≤r<m0 \le r < m and r≡xe0(modm)r \equiv x^{e_0} \pmod m. It makes ⌊lg⁡e0⌋\lfloor \lg e_0 \rfloor squarings and ν(e0)\nu(e_0) multiplications into rr, where ν(e0)\nu(e_0) is the number of digits 11 in the binary form of e0e_0.

Setup. We use the rules of congruences: if a≡a′a \equiv a' and c≡c′(modm)c \equiv c' \pmod m, then ac≡a′c′(modm)ac \equiv a'c' \pmod m, and a mod m≡aa \bmod m \equiv a with 0≤a mod m<m0 \le a \bmod m < m. Write rj,bj,ejr_j, b_j, e_j for the values of r,b,er, b, e at the start of pass j+1j + 1 (so r0=1 mod mr_0 = 1 \bmod m, b0=x mod mb_0 = x \bmod m, and e0e_0 is the input).

Invariant. At the start of each pass, and at the exit of the loop:

(I) r⋅be≡xe0(modm)r \cdot b^{e} \equiv x^{e_0} \pmod m, and 0≤r<m0 \le r < m and 0≤b<m0 \le b < m.

Step 1: initialization. After lines L1 and L2, r=1 mod m≡1r = 1 \bmod m \equiv 1 and b=x mod m≡xb = x \bmod m \equiv x, so rbe0≡1⋅xe0r b^{e_0} \equiv 1 \cdot x^{e_0}. Both values are results of a reduction modulo mm, so they lie in 00 to m−1m - 1. This covers m=1m = 1, where r=b=0r = b = 0; then each integer is congruent to 00 and (I) holds trivially.

Step 2: maintenance. Assume (I) at the start of a pass, with e≥1e \ge 1. Write e=2q+de = 2q + d with q=⌊e/2⌋q = \lfloor e/2 \rfloor and d=e mod 2∈{0,1}d = e \bmod 2 \in \{0, 1\}. Then be=bd⋅(b2)qb^{e} = b^{d} \cdot (b^2)^{q}.

  • Line L4 sets r′=rb mod mr' = r b \bmod m if d=1d = 1 and leaves r′=rr' = r if d=0d = 0. In both cases r′≡rbdr' \equiv r b^{d} and 0≤r′<m0 \le r' < m.
  • Line L5 sets e′=qe' = q.
  • If q≥1q \ge 1, line L6 sets b′=b2 mod m≡b2b' = b^2 \bmod m \equiv b^2, with 0≤b′<m0 \le b' < m. Then r′b′e′≡rbd(b2)q=rbe≡xe0r' b'^{e'} \equiv r b^{d} (b^2)^{q} = r b^{e} \equiv x^{e_0}.
  • If q=0q = 0, line L6 leaves b′=bb' = b. Then b′e′=1=(b2)0b'^{e'} = 1 = (b^2)^0, so again r′b′e′=r′≡rbd=rbd(b2)q=rbe≡xe0r' b'^{e'} = r' \equiv r b^{d} = r b^{d} (b^2)^{q} = r b^{e} \equiv x^{e_0}.

So (I) holds at the start of the next pass.

Step 3: termination. In each pass e′=⌊e/2⌋e' = \lfloor e/2 \rfloor. For e≥1e \ge 1 with LL binary digits, ⌊e/2⌋\lfloor e/2 \rfloor has L−1L - 1 binary digits (and is 00 when L=1L = 1). So the number of binary digits drops by exactly 11 in each pass, and the loop stops after exactly L=⌊lg⁡e0⌋+1L = \lfloor \lg e_0 \rfloor + 1 passes when e0≥1e_0 \ge 1. When e0=0e_0 = 0, the test of line L3 fails at once.

Step 4: the result. At exit e=0e = 0, so (I) gives r=r⋅b0≡xe0(modm)r = r \cdot b^0 \equiv x^{e_0} \pmod m with 0≤r<m0 \le r < m. This rr is unique, because two integers in 00 to m−1m - 1 that are congruent modulo mm are equal. Line L7 returns it.

Step 5: the count of multiplications. In pass j+1j + 1 (for j=0,1,…,L−1j = 0, 1, \ldots, L - 1), the value of ee is ⌊e0/2j⌋\lfloor e_0 / 2^j \rfloor, and its lowest digit is digit jj of e0e_0. Line L4 multiplies exactly when this digit is 11, so it runs ν(e0)\nu(e_0) times in total. Line L6 squares exactly when the new ee is non-zero, that is in each pass except the last, so it runs L−1=⌊lg⁡e0⌋L - 1 = \lfloor \lg e_0 \rfloor times. ■\blacksquare

The count is at most 2⌊lg⁡e0⌋+12 \lfloor \lg e_0 \rfloor + 1. Since ν(e0)≤L=⌊lg⁡e0⌋+1\nu(e_0) \le L = \lfloor \lg e_0 \rfloor + 1, the total ⌊lg⁡e0⌋+ν(e0)\lfloor \lg e_0 \rfloor + \nu(e_0) is at most 2⌊lg⁡e0⌋+12 \lfloor \lg e_0 \rfloor + 1, with equality exactly when all LL digits are 11, that is e0=2L−1e_0 = 2^L - 1.

Back to Binary exponentiation