Modular Exponentiation
& Fast Powering
Learn how to compute large powers modulo a number efficiently using the binary exponentiation algorithm: a cornerstone of cryptography and number theory.
What Is Modular Arithmetic?
Modular arithmetic is a system where numbers "wrap around" after reaching a certain value called the modulus. We write \( a \equiv b \pmod{m} \) to mean that \( m \) divides \( a - b \).
What Is Modular Exponentiation?
Modular exponentiation means computing \( a^n \bmod m \): raising a base \( a \) to a power \( n \), then taking the remainder when divided by \( m \). The result is always in the range \( \{0, 1, \ldots, m-1\} \).
Why Does It Matter?
In real-world cryptography (RSA, Diffie-Hellman), we compute expressions like \( a^n \bmod m \) where \( n \) can have hundreds of digits. Naive multiplication would take longer than the age of the universe.
The Key Property of Modular Arithmetic
The foundation of fast modular exponentiation is this identity, which lets us reduce intermediate results at every step:
Binary Representation of the Exponent
The fast powering algorithm works by writing the exponent \( n \) in binary. Each bit tells us whether to multiply by the corresponding power of \( a \).
The Fast Powering Algorithm
Here is the repeated squaring (binary exponentiation) algorithm to compute \( a^n \bmod m \):
Naive vs. Fast Powering
| Naive Method | Fast Powering | |
|---|---|---|
| Approach | Multiply \( a \) one at a time | Repeated squaring + binary decomposition |
| Multiplications | \( n - 1 \) | \( \leq 2 \lfloor \log_2 n \rfloor \) |
| For \( n = 1000 \) | 999 multiplications | ~20 multiplications |
| For \( n = 2^{64} \) | \( \approx 1.8 \times 10^{19} \) | 128 multiplications |
| Intermediate values | Can grow astronomically large | Always \( < m^2 \), then reduced |
How to Apply Fast Powering
Example 1: \( 2^{10} \bmod 1000 \)
Step 1: Convert exponent. \( 10 = (1010)_2 = 2^3 + 2^1 \).
Step 2: Build squaring table mod 1000:
\( 2^2 \equiv 2^2 = \) 4
\( 2^4 \equiv 4^2 = \) 16
\( 2^8 \equiv 16^2 = \) 256
Step 3: \( 10 = (1010)_2 \), so bits 3 and 1 are set. Multiply those powers:
Only 4 squarings + 1 multiplication instead of 9 multiplications.
Example 2: \( 3^{13} \bmod 7 \)
Step 1: \( 13 = (1101)_2 = 2^3 + 2^2 + 2^0 \).
Step 2: Squaring table mod 7:
\( 3^2 \equiv 9 \equiv \) 2 \( \pmod{7} \)
\( 3^4 \equiv 2^2 = 4 \equiv \) 4 \( \pmod{7} \)
\( 3^8 \equiv 4^2 = 16 \equiv \) 2 \( \pmod{7} \)
Step 3: Bits 3, 2, and 0 are set:
We never computed \( 3^{13} = 1{,}594{,}323 \). Every intermediate value stayed below 49.
Example 3: \( 5^{117} \bmod 19 \)
Step 1: \( 117 = (1110101)_2 = 64 + 32 + 16 + 4 + 1 \).
\( 5^2 \equiv 25 \equiv \) 6
\( 5^4 \equiv 6^2 = 36 \equiv \) 17
\( 5^8 \equiv 17^2 = 289 \equiv 289 - 15 \times 19 = 289 - 285 = \) 4
\( 5^{16} \equiv 4^2 = 16 \equiv \) 16
\( 5^{32} \equiv 16^2 = 256 \equiv 256 - 13 \times 19 = 256 - 247 = \) 9
\( 5^{64} \equiv 9^2 = 81 \equiv 81 - 4 \times 19 = 81 - 76 = \) 5
Tracing the Algorithm Loop
Let us trace \( 2^{10} \bmod 1000 \) through each iteration of the loop:
| Step | \( n \) | Bit | base | result |
|---|---|---|---|---|
| Init | 10 | - | 2 | 1 |
| 1 | 10 | 0 | 4 | 1 (skip) |
| 2 | 5 | 1 | 16 | \( 1 \times 4 = 4 \) |
| 3 | 2 | 0 | 256 | 4 (skip) |
| 4 | 1 | 1 | - | \( 4 \times 256 = 1024 \equiv \) 24 |
Common Mistakes to Avoid
Summary & Connections
| Concept | Key Idea | Application |
|---|---|---|
| Modular arithmetic | \( (a \cdot b) \bmod m \) = reduce-then-multiply | Keeps values small |
| Binary decomposition | \( n = \sum 2^{k_i} \), so \( a^n = \prod a^{2^{k_i}} \) | Reduces multiplications to \( O(\log n) \) |
| Repeated squaring | \( a^{2^k} = (a^{2^{k-1}})^2 \) | Builds power table cheaply |
| RSA encryption | \( c = m^e \bmod n \) | Uses fast powering for security |
| Fermat's Little Theorem | \( a^{p-1} \equiv 1 \pmod{p} \) | Simplifies exponent before fast powering |
Question 1 of 5
What is the time complexity of the fast powering (binary exponentiation) algorithm for computing \( a^n \bmod m \)?
Question 2 of 5
Compute \( 2^{11} \bmod 13 \).
Question 3 of 5
Which property of modular arithmetic makes fast powering possible?
Question 4 of 5
When computing \( 7^{20} \bmod 11 \) via fast powering, which powers of 7 do you multiply together?
Question 5 of 5
Compute \( 3^{7} \bmod 11 \).