computational number theory
Modular Exponentiation
You should know: number representations
Overview
Modular exponentiation computes bᵉ mod m — a base raised to an exponent, reduced modulo m — without ever forming the (potentially astronomically large) full number bᵉ. Naively multiplying b by itself e times and reducing mod m at the end would require storing a number with roughly e·log(b) digits, which is completely inferactable for the exponents used in real cryptography (hundreds or thousands of bits, as in RSA). The key trick, exponentiation by squaring (also called fast or binary exponentiation), reduces the number of multiplications from O(e) to O(log e) by writing e in binary and repeatedly squaring the base while reducing modulo m at every step, so intermediate values never grow larger than m². This single technique — squaring plus modular reduction at every step — is what makes RSA encryption, Diffie–Hellman key exchange, and the Miller–Rabin primality test computationally feasible: they all require raising numbers to exponents with hundreds of digits, modulo similarly huge numbers.
Intuition
Instead of multiplying by b one factor at a time (e multiplications total), repeated squaring builds up powers of b that double at each step: b¹, b², b⁴, b⁸, b¹⁶, … After k squarings you already have b^(2^k) — exponential growth in the exponent from only k multiplications. Any exponent e can be assembled as a sum of distinct powers of 2 (its binary representation), so bᵉ is just the product of the b^(2^i) terms where bit i of e is 1. Reducing modulo m after every single multiplication (not just at the very end) keeps every number in the computation smaller than m², so the whole algorithm runs in O(log e) multiplications of numbers no bigger than m — this is precisely why an RSA server can compute a 2048-bit modular exponentiation in milliseconds instead of needing to store a number with hundreds of digits.
Formal Definition
Write the exponent e in binary as e = Σ bᵢ 2ⁱ (bᵢ ∈ {0,1}). The right-to-left binary method maintains a running result and a running squared base:
Worked Examples
Write 13 in binary: 13 = 8+4+1 = 1101₂. Build up squares of the base mod 7.
13 = 8+4+1, so multiply the base powers corresponding to the 1-bits (3^8, 3^4, 3^1).
Reduce the product modulo 7.
Answer: 3^13 mod 7 = 3.
Practice Problems
Compute 5^117 mod 19 (5^117 is far too large to compute directly, so use properties of modular exponentiation).
Using repeated squaring, compute 2^10 mod 100.
An RSA-style computation needs c = 7^560 mod 561, where 561 is a Carmichael number (561 = 3·11·17). Given that Fermat-like behavior can be misleading for Carmichael numbers, what does direct modular exponentiation give?
Quiz
Summary
- Modular exponentiation computes bᵉ mod m efficiently, without ever forming the full (astronomically large) number bᵉ.
- Exponentiation by squaring writes e in binary and repeatedly squares the base modulo m, reducing the multiplication count from O(e) to O(log e).
- Reducing modulo m after every multiplication (not just at the end) keeps all intermediate values bounded below m², which is essential for cryptographic-scale numbers.
- This technique underlies RSA encryption, Diffie–Hellman key exchange, and primality tests like Miller–Rabin, all of which require raising huge numbers to huge exponents modulo huge moduli.
- Carmichael numbers (like 561 = 3·11·17) satisfy Fermat's congruence aᵐ⁻¹ ≡ 1 (mod m) for all coprime bases despite being composite, which is why Fermat's test alone is unreliable for primality.
Mathematics