Skip to content

Fast Modular Exponentiation

Modular arithmetic often requires computing powers such as

The Problem

Modular arithmetic often requires computing powers such as

ak(modn). a^k \pmod n.

If kk is large, direct multiplication is inefficient. For example, computing

71000(mod13) 7^{1000}\pmod{13}

by multiplying 77 one thousand times is unnecessary.

Fast modular exponentiation computes powers by repeatedly squaring and reducing modulo nn. The method is efficient because it uses the binary expansion of the exponent.

Repeated Squaring

The basic idea is that powers can be built from squares:

a2,a4,a8,a16, a^2,\quad a^4,\quad a^8,\quad a^{16},\ldots

Each term is obtained by squaring the previous one:

a2m=(am)2. a^{2m}=(a^m)^2.

Modulo nn, we reduce after each step:

a2mmodn=(ammodn)2modn. a^{2m}\bmod n = (a^m\bmod n)^2\bmod n.

This keeps the numbers small while preserving the final residue.

Binary Expansion of the Exponent

Every nonnegative integer kk can be written in binary:

k=ε0+ε12+ε222++εr2r, k=\varepsilon_0+\varepsilon_1 2+\varepsilon_2 2^2+\cdots+\varepsilon_r2^r,

where each digit satisfies

εi{0,1}. \varepsilon_i\in\{0,1\}.

Then

ak=aε0a2ε1a4ε2a2rεr. a^k = a^{\varepsilon_0} a^{2\varepsilon_1} a^{4\varepsilon_2} \cdots a^{2^r\varepsilon_r}.

So one only needs the powers

a,a2,a4,a8,,a2r. a,a^2,a^4,a^8,\ldots,a^{2^r}.

The terms corresponding to binary digit 11 are multiplied together.

Example

Compute

7100(mod13). 7^{100}\pmod{13}.

First write

100=64+32+4. 100=64+32+4.

Thus

7100=76473274. 7^{100}=7^{64}7^{32}7^4.

Now compute successive squares modulo 1313:

72=4910(mod13). 7^2=49\equiv10\pmod{13}.

Then

74102=1009(mod13). 7^4\equiv10^2=100\equiv9\pmod{13}.

Next,

7892=813(mod13). 7^8\equiv9^2=81\equiv3\pmod{13}.

Then

71632=9(mod13). 7^{16}\equiv3^2=9\pmod{13}.

Next,

73292=813(mod13). 7^{32}\equiv9^2=81\equiv3\pmod{13}.

Finally,

76432=9(mod13). 7^{64}\equiv3^2=9\pmod{13}.

Therefore

710076473274939(mod13). 7^{100} \equiv 7^{64}7^{32}7^4 \equiv 9\cdot3\cdot9 \pmod{13}.

Compute

939=243. 9\cdot3\cdot9=243.

Since

2439(mod13), 243\equiv9\pmod{13},

we obtain

71009(mod13). 7^{100}\equiv9\pmod{13}.

Algorithmic Form

Fast modular exponentiation can be written as an iterative algorithm.

Given integers a,k,na,k,n, with k0k\ge0 and n2n\ge2, set

r=1,ba(modn). r=1, \qquad b\equiv a\pmod n.

While k>0k>0:

If kk is odd, replace

r r

by

rbmodn. rb\bmod n.

Then replace

b b

by

b2modn. b^2\bmod n.

Finally replace

k k

by

k/2. \lfloor k/2\rfloor.

When the process ends, rr is congruent to

ak(modn). a^k\pmod n.

The algorithm reads the binary digits of kk from right to left.

Correctness

At each stage, the algorithm maintains the invariant that the original power is represented by the current accumulated result and the remaining exponent.

Informally, when the current exponent is odd, one factor of the current base must be moved into the result. When the exponent is even, the base is squared and the exponent is halved.

The identity behind the method is

bk={(b2)k/2,k even,b(b2)(k1)/2,k odd. b^k= \begin{cases} (b^2)^{k/2}, & k\text{ even},\\ b(b^2)^{(k-1)/2}, & k\text{ odd}. \end{cases}

Since congruence respects multiplication, reducing after each multiplication does not change the final residue modulo nn.

Efficiency

The direct method requires roughly kk multiplications.

Fast modular exponentiation requires only about

log2k \log_2 k

squaring steps, plus one multiplication for each 11-digit in the binary expansion of kk.

Thus the number of arithmetic steps grows logarithmically in the exponent, rather than linearly.

This is a major improvement. For a very large exponent, such as one with hundreds or thousands of bits, repeated squaring is essential.

Negative Bases

Negative bases cause no special difficulty. One first reduces the base modulo nn.

For example,

(3)20(mod11) (-3)^{20}\pmod{11}

can be computed using

38(mod11). -3\equiv8\pmod{11}.

Thus

(3)20820(mod11). (-3)^{20}\equiv8^{20}\pmod{11}.

The same repeated squaring method applies.

Large Exponents in Number Theory

Fast modular exponentiation is essential in many number-theoretic algorithms.

It is used in Fermat primality tests, Miller-Rabin primality tests, RSA encryption and decryption, Diffie-Hellman key exchange, elliptic curve methods, and computations with finite fields.

For example, RSA requires computing expressions of the form

me(modN) m^e\pmod N

where ee and NN may be very large. Without fast modular exponentiation, such computations would be impractical.

Role in Number Theory

Fast modular exponentiation is the computational counterpart of modular arithmetic. It turns theoretical congruence laws into efficient algorithms.

The central idea is simple: use the binary expansion of the exponent, square repeatedly, and reduce after every step.

This method shows how arithmetic structure can produce efficient computation. In modern number theory, this connection between algebra and algorithms is indispensable.