01 / 20
a n mod m

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.

Discrete Math

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 \).

0 1 2 3 4 5 6 mod 7 17 17 mod 7 = 3

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\} \).

Base \( a \)
The number being raised to a power
Exponent \( n \)
How many times we multiply the base
Modulus \( m \)
The "wrap-around" value for the result

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.

Naive Method
Multiply \( a \) by itself \( n-1 \) times: \( O(n) \) multiplications. Impossibly slow for large \( n \).
Fast Powering
Use repeated squaring: only \( O(\log n) \) multiplications. Exponentially faster.

The Key Property of Modular Arithmetic

The foundation of fast modular exponentiation is this identity, which lets us reduce intermediate results at every step:

\( (a \cdot b) \bmod m = [(a \bmod m) \cdot (b \bmod m)] \bmod m \)
The multiplication property of modular arithmetic

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 \).

Exponent: 13 1 \( 2^3 = 8 \) 1 \( 2^2 = 4 \) 0 \( 2^1 = 2 \) 1 \( 2^0 = 1 \) \( 13 = 8 + 4 + 1 = 2^3 + 2^2 + 2^0 \) \( a^{13} = a^8 \cdot a^4 \cdot a^1 \)

The Fast Powering Algorithm

Here is the repeated squaring (binary exponentiation) algorithm to compute \( a^n \bmod m \):

1
Initialize: Set \( \text{result} = 1 \) and \( \text{base} = a \bmod m \).
2
Loop while \( n > 0 \):
2a
If the last bit of \( n \) is 1 (i.e., \( n \) is odd): set \( \text{result} = (\text{result} \times \text{base}) \bmod m \).
2b
Square the base: \( \text{base} = (\text{base} \times \text{base}) \bmod m \).
2c
Right-shift the exponent: \( n = \lfloor n / 2 \rfloor \).
3
Return \( \text{result} \).

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

1
Convert the exponent to binary. Write \( n \) in base 2. For example, \( 13 = (1101)_2 \).
2
Build a squaring table. Compute \( a^1, a^2, a^4, a^8, \ldots \pmod{m} \) by squaring each time.
3
Multiply the powers whose binary bit is 1. Combine only the powers corresponding to 1-bits in the binary representation.
4
Reduce mod \( m \) at every step. After each multiplication and squaring, take the remainder.

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^1 \equiv \) 2
\( 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^1 \equiv \) 3 \( \pmod{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^1 \equiv 5 \pmod{19} \)
\( 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

✕ Computing the full power first
Never compute \( a^n \) then reduce mod \( m \). The intermediate value can have millions of digits, causing overflow or extreme slowness.
✕ Forgetting to reduce after squaring
You must apply \( \bmod m \) after every multiplication and squaring, not just at the end. Skipping causes numbers to blow up.
✕ Wrong binary conversion
Errors in converting \( n \) to binary lead to wrong products. Double-check by summing the powers of 2 back to \( n \).

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 \).