The Problem
Modular arithmetic often requires computing powers such as
If is large, direct multiplication is inefficient. For example, computing
by multiplying one thousand times is unnecessary.
Fast modular exponentiation computes powers by repeatedly squaring and reducing modulo . 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:
Each term is obtained by squaring the previous one:
Modulo , we reduce after each step:
This keeps the numbers small while preserving the final residue.
Binary Expansion of the Exponent
Every nonnegative integer can be written in binary:
where each digit satisfies
Then
So one only needs the powers
The terms corresponding to binary digit are multiplied together.
Example
Compute
First write
Thus
Now compute successive squares modulo :
Then
Next,
Then
Next,
Finally,
Therefore
Compute
Since
we obtain
Algorithmic Form
Fast modular exponentiation can be written as an iterative algorithm.
Given integers , with and , set
While :
If is odd, replace
by
Then replace
by
Finally replace
by
When the process ends, is congruent to
The algorithm reads the binary digits of 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
Since congruence respects multiplication, reducing after each multiplication does not change the final residue modulo .
Efficiency
The direct method requires roughly multiplications.
Fast modular exponentiation requires only about
squaring steps, plus one multiplication for each -digit in the binary expansion of .
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 .
For example,
can be computed using
Thus
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
where and 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.