Squaring halves the exponent
a^{2k} = (a^k)², so each squaring doubles the exponent reached. Reading b in binary tells you which squarings to multiply in.
Repeated squaring computes aᵇ mod m in about 2 log₂ b multiplications rather than b of them, reducing at every step so the numbers stay small. It is what makes RSA possible.
Core ideas
Two ideas, each essential: halve the exponent by squaring, and reduce the numbers by the modulus.
a^{2k} = (a^k)², so each squaring doubles the exponent reached. Reading b in binary tells you which squarings to multiply in.
Without reducing, a^b for a 1024-bit b has more digits than there are atoms in the universe. With reducing, every intermediate is below m².
Fast exponentiation without reduction produces unrepresentable numbers; reduction without fast exponentiation takes b multiplications.
The material
The algorithm, the cost, and why RSA depends on it.
The loop reads b in binary from the least significant bit. base holds a^{2^i} mod m at step i, and the result accumulates exactly those powers whose bit is set. Since b = Σ bᵢ2ⁱ, the product of the selected powers is a^b.
MODPOW returns a^b mod m.
The loop invariant is result · base^b ≡ a^{b₀} (mod m), where b₀ is the original exponent.
Initially result = 1 and base = a, so it holds. If b is even, base^b = (base²)^{b/2}, and the update preserves it. If b is odd, base^b = base · (base²)^{(b−1)/2}, and multiplying result by base before the halving preserves it.
At exit b = 0, so the invariant reads result ≡ a^{b₀}.
□
The loop runs ⌊log₂ b⌋ + 1 times, with one squaring per iteration and at most one extra multiplication. The last squaring is never used, so the useful count is ⌊log₂ b⌋ squarings plus one multiplication per set bit — at most about 2 log₂ b modular multiplications in all.
7² = 49 ≡ 10; 10² = 100 ≡ 9; 9² = 81 ≡ 3; 3² = 9; 9² = 81 ≡ 3; 3² ≡ 9; 9² ≡ 3. Since 128 = 2⁷, seven squarings give 7^128 ≡ 3 (mod 13). No number above 144 ever appeared.
That bound is the second essential point: every intermediate is a product of two numbers below m, hence below m². For a 2048-bit modulus the intermediates are 4096 bits, which is entirely routine, while a^b itself would have about 2^1024 bits.
It is worth appreciating the asymmetry the algorithm creates. Computing a^b mod m is fast; recovering b from a, a^b mod m and m — the discrete logarithm — is believed hard. A great deal of cryptography lives in that gap.
Interactive
Algorithms with their traces
Method
Binary exponent, reduce every step.
100 = 1100100₂. The set bits are the powers you will multiply in.
a, a², a⁴, a⁸, … each reduced mod m. Never let a value exceed m².
Reducing after every multiplication. The order does not matter.
If a number larger than m² appears, a reduction was skipped and the advantage is lost.
200 = 11001000₂, so the set bits are at positions 3, 6 and 7.
The powers began repeating — 3^4 ≡ 3^64 ≡ 31 — because the multiplicative order of 3 modulo 50 divides φ(50) = 20. “Fermat's Little Theorem and Euler's Theorem” explains that, and it is why the answer came out as 1.
Compute 5^117 mod 19 by square and multiply, writing 117 in binary first: 1110101₂, five set bits. Six squarings to reach 5^64, five factors to multiply together, and no intermediate above 361.
Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.
No recommendations yet for this lesson.