Primes & Composites
For a number n, the input length is its number of bits: k = \lceil \log_2(n+1) \rceil
An algorithm running in time polynomial in n is exponential in the input size k (O(n) = O(2^k)).
An efficient algorithm must run in time \text{poly}(k) = \text{poly}(\log n)!
Let a, b be k-bit integers (k \approx \log_2 n):
| Operation | Naive Cost | Best Known Cost |
|---|---|---|
| Addition / Subtraction (a \pm b) | O(k) | O(k) |
| Multiplication (a \cdot b) | O(k^2) | O(k \log k) (Harvey–Hoeven 2019) |
| Division (a = qb + r) | O(k^2) | O(k \log k) |
| Modular Exponentiation (a^b \bmod m) | O(k^3) (Repeated squaring: O(k) mults) |
\tilde{O}(k^2) |
| GCD (\gcd(a, b)) | O(k^2) (Euclid: O(k) divisions) |
\tilde{O}(k) (Half-GCD) |
Subquadratic arithmetic is what makes 2048-bit modular exponentiation in RSA practical in milliseconds!
Split each number into halves: x = x_1 2^{n/2} + x_0, y = y_1 2^{n/2} + y_0.
Standard recursion needs 4 multiplications: x_1 y_1, \; x_1 y_0, \; x_0 y_1, \; x_0 y_0
Only recurse three times instead of four!
Recurrence & Depth
. . .
Euclid’s Theorem (~300 BCE)
There exist infinitely many primes.
Proof: If \{p_1, \dots, p_k\} were all primes, N = p_1 \cdots p_k + 1 has a prime factor not in the list.
Prime Number Theorem (Hadamard & de la Vallée Poussin, 1896): \pi(x) \sim \frac{x}{\ln x} \implies \Pr_{n \in [1, x]}[n \text{ is prime}] \approx \frac{1}{\ln x}
For a 2048-bit integer (x \approx 2^{2048}): \Pr[n \text{ is prime}] \approx \frac{1}{2048 \ln 2} \approx \frac{1}{1419}
Takeaway for Key Generation: A random 2048-bit odd number has a 1 / 710 chance of being prime. We only need \approx 700 trials! Crucial requirement: We need an ultrafast primality test!
Invariant of the Euclidean Algorithm
For integers a \ge b > 0: \gcd(a, b) = \gcd(b, a \bmod b)
Fermat’s Little Theorem (1640)
If p is prime and \gcd(a, p) = 1, then: a^{p-1} \equiv 1 \pmod p
The Fatal Flaw
Are there composite numbers where a^{n-1} \equiv 1 \pmod n for all coprime bases a?
YES: Carmichael Numbers!
Definition: Carmichael Numbers
A composite integer n is a Carmichael number if for every base a with \gcd(a, n) = 1: a^{n-1} \equiv 1 \pmod n
Definition: Group (G, \circ)
A set G equipped with a binary operation \circ : G \times G \to G is a group if it satisfies four axioms:
Closure: For all a, b \in G, a \circ b \in G.
Associativity: For all a, b, c \in G, (a \circ b) \circ c = a \circ (b \circ c).
Identity: There exists e \in G such that for all a \in G: a \circ e = e \circ a = a
Inverse: For each a \in G, there exists a^{-1} \in G such that: a \circ a^{-1} = a^{-1} \circ a = e
If a \circ b = b \circ a for all a, b \in G, the group is called Abelian (commutative).
1. Additive Group (\mathbb{Z}_n, +)
Set: \{0, 1, 2, \dots, n-1\}.
Operation: Addition modulo n.
Identity: e = 0.
Inverse: -a = (n - a) \bmod n.
Group Size (Order): |(\mathbb{Z}_n, +)| = n
Holds for any integer n \ge 1.
2. Multiplicative Group (\mathbb{Z}_n^*, \cdot)
Set: Elements coprime to n: \mathbb{Z}_n^* = \{a \in \{1, \dots, n-1\} : \gcd(a, n) = 1\}
Operation: Multiplication modulo n.
Identity: e = 1.
Inverse: Modular inverse via Euclid!
Group Size: Euler’s totient \phi(n).
Definition: Subgroup
A subset H \subseteq G is a subgroup (written H \le G) if H is itself a group under the same operation \circ:
Lagrange’s Theorem (1777)
If G is a finite group and H \le G is a subgroup, then the size of H divides the size of G: \frac{|G|}{|H|} = [G : H] \in \mathbb{Z}^+ \implies |H| \text{ divides } |G|
Immediate Consequence: If H is a proper subgroup (H \ne G), then: |H| \le \frac{1}{2}|G|
Why this powers randomized primality: If “bad” witness bases form a proper subgroup of \mathbb{Z}_n^*, then at most half the elements can fool us!
How does the multiplicative group \mathbb{Z}_n^* behave when n = p \cdot q is composite (\gcd(p, q) = 1)?
The CRT Bijection
The mapping: f : \mathbb{Z}_n^* \longrightarrow \mathbb{Z}_p^* \times \mathbb{Z}_q^* \quad \text{defined by} \quad f(x) = (x \bmod p, \; x \bmod q) is a bijective group homomorphism (an isomorphism): \mathbb{Z}_n^* \cong \mathbb{Z}_p^* \times \mathbb{Z}_q^*
One-to-One Correspondence:
Multiplication is Component-Wise: f(x \cdot y) = (xy \bmod p, \; xy \bmod q) = (x \bmod p, \; x \bmod q) \cdot (y \bmod p, \; y \bmod q)
Why This Unlocks Non-Trivial Square Roots:
What happens when we take the square root of 1 modulo n?
x^2 \equiv 1 \pmod n \iff x^2 - 1 \equiv (x - 1)(x + 1) \equiv 0 \pmod n
If n = p is Prime:
\mathbb{Z}_p is a field. A polynomial of degree 2 has at most 2 roots: x \equiv +1 \quad \text{or} \quad x \equiv -1 \pmod p There are no other roots!
If n = p \cdot q is Composite:
By the CRT bijection \mathbb{Z}_n^* \cong \mathbb{Z}_p^* \times \mathbb{Z}_q^*: (x \bmod p, \; x \bmod q) \in \{(\pm 1, \; \pm 1)\}
Consider n = 65 = 5 \times 13:
x = 14 \pmod{65} x^2 = 14^2 = 196 = 3 \times 65 + 1 \equiv \mathbf{1 \pmod{65}}
The Discovery
14 \not\equiv \pm 1 \pmod{65}, yet 14^2 \equiv 1 \pmod{65}!
14 is an irrefutable proof that 65 is composite.
How do we systematically search for nontrivial square roots of 1?
Write n - 1 as: n - 1 = 2^s \cdot d \quad (d \text{ odd})
For any base a \in \mathbb{Z}_n^*, consider the sequence obtained by repeated squaring: \langle x_0, \; x_1, \; x_2, \; \dots, \; x_s \rangle x_0 = a^d \bmod n, \quad x_1 = a^{2d} \bmod n, \quad x_2 = a^{4d} \bmod n, \quad \dots, \quad x_s = a^{2^s d} = a^{n-1} \bmod n
The Prime Invariant
If n is prime, then:
def miller_rabin(n, k=40):
# Write n - 1 = 2^s * d with d odd
s, d = 0, n - 1
while d % 2 == 0:
s += 1
d //= 2
for _ in range(k):
a = random.randint(2, n - 2)
x = pow(a, d, n)
if x == 1 or x == n - 1:
continue # Passes this round
for r in range(s - 1):
x = pow(x, 2, n)
if x == n - 1:
break # Safely reached -1 before 1
if x == 1:
return "COMPOSITE" # Caught nontrivial square root of 1!
else:
return "COMPOSITE" # Never reached -1; a^(n-1) != 1
return "PROBABLY PRIME"Factor n - 1 = 64 = 2^6 \cdot 1 \implies s = 6, \; d = 1.
Base a = 8 (Fails to catch 65)
x_0 = 8^1 = 8 \pmod{65}
x_1 = 8^2 = 64 \equiv \mathbf{-1} \pmod{65}
Hits -1 immediately!
Subsequent terms will be: (-1)^2 = 1, \; 1, \; 1, \dots
a = 8 is a liar (does not witness compositeness).
Base a = 12 (Catches 65!)
x_0 = 12^1 = 12 \pmod{65}
x_1 = 12^2 = 144 \equiv 14 \pmod{65}
x_2 = 14^2 = 196 \equiv \mathbf{1} \pmod{65}
x_2 = 1, but predecessor was: x_1 = 14 \not\equiv -1 \pmod{65}
Nontrivial root of 1 detected!
Outputs: COMPOSITE.
Could there be a composite number where 99.9\% of bases are liars?
Rabin’s Subgroup Theorem (1980)
If n is an odd composite number, then at most \frac{1}{4} of the bases a \in \{1, 2, \dots, n-1\} fail to witness the compositeness of n: \Pr_{a \sim \text{Unif}}[a \text{ is a liar for } n] \le \frac{1}{4}
Amplification via k Independent Rounds: \Pr[\text{Error on composite } n] \le \left(\frac{1}{4}\right)^k = 2^{-2k}
For k = 40 rounds: \text{Error} \le 2^{-80} \approx 10^{-24} (Far lower than the probability of cosmic ray hardware memory corruption!)
For any Carmichael number n = p \cdot q (\gcd(p, q) = 1), every base satisfies a^{n-1} \equiv 1 \pmod n. Why does repeated squaring catch them so easily?
The Component View via CRT
By the isomorphism \mathbb{Z}_n^* \cong \mathbb{Z}_p^* \times \mathbb{Z}_q^*, each element in the chain is a pair: x_t \longleftrightarrow (x_t \bmod p, \; x_t \bmod q) Both components square independently at every step!
What can precede +1 \equiv (1, 1)? Just before hitting (1, 1), each component must be either +1 or -1: (+1, +1) \leftrightarrow \mathbf{+1} \quad \text{and} \quad (-1, -1) \leftrightarrow \mathbf{-1} \quad \text{— Only 2 Trivial Roots!}
What happens when components split signs? (+1, -1)^2 = (1, 1) \equiv \mathbf{1} \quad \text{and} \quad (-1, +1)^2 = (1, 1) \equiv \mathbf{1 \pmod n} Squares to +1, but the predecessor is neither +1 nor -1 \pmod n! \mathbf{A \; non\text{-}trivial \; square \; root \; of \; 1 \; is \; exposed!}
Why can at most a small fraction of bases be liars on Carmichael numbers?
Trivial Roots (Liars)
Signs match across all components: (+1, +1, \dots, +1) \leftrightarrow \mathbf{+1} (-1, -1, \dots, -1) \leftrightarrow \mathbf{-1} Only 2 combinations can safely hit +1 without revealing a root!
Split Signs (Witnesses)
Signs differ across components: (+1, -1, \dots), \; (-1, +1, \dots) Every split sign combination exposes a non-trivial square root of 1!
The Math
Where does Miller–Rabin situate the computational complexity of primality?
Asymmetric One-Sided Error
Therefore, the language \text{COMPOSITES} \in \mathbf{RP}.
By complement: \mathbf{PRIMES \in co\text{-}RP \subseteq BPP}
Pratt’s Certificate (1975): \text{PRIMES} \in \mathbf{NP} \cap \mathbf{co\text{-}NP}.
Agrawal, Kayal, Saxena (AKS, 2002): Proved that \mathbf{PRIMES \in P} deterministically in \tilde{O}(\log^6 n) time!
Practical Reality: AKS runtime is \approx O(\log^6 n) vs Miller–Rabin’s O(\log^3 n). For 2048-bit keys, Miller–Rabin takes fractions of a millisecond; AKS takes minutes. Miller–Rabin powers all modern cryptography.
CS G526: Advanced Algorithms & ComplexityTulasimohan Molli