Key idea of this section
If n is prime and n − 1 = 2ˢd with d odd, then ad ≡ 1 or a2ʳd ≡ −1 (mod n) for some 0 ≤ r < s.
The strong probable-prime test
Gauss wrote in the Disquisitiones that "the problem of distinguishing prime numbers from composite numbers and of resolving the latter into their prime factors is known to be one of the most important and useful in arithmetic." Trial division to √n is exponential in the number of digits. Fermat's theorem gives a fast necessary condition, but Carmichael numbers defeat it. The fix uses the fact that modulo a prime, 1 has only the square roots ±1.
Theorem 13.1. Let n be an odd prime, n − 1 = 2ˢd with d odd, and n ∤ a. Then either ad ≡ 1 (mod n), or a2ʳd ≡ −1 (mod n) for some r with 0 ≤ r < s.
Proof. Consider the sequence ad, a2d, a4d, …, a2ˢd = an−1 ≡ 1, each term the square of the previous. If the first term is not 1, let a2ʳd be the last term that is not 1; its square is 1, so it is a square root of 1 other than 1, hence −1 (Theorem 3.3). ∎
A composite n satisfying this condition for the base a is a strong pseudoprime to base a, and a is a strong liar for n. The Miller–Rabin test (Miller 1976, Rabin 1980) chooses random bases and declares n composite as soon as one base fails. Its power comes from the following theorem.
Theorem 13.2 (Monier; Rabin, 1980). If n > 9 is an odd composite, at most φ(n)/4 of the bases 1 ≤ a < n are strong liars.
We prove the easier fact that if n is not a Carmichael number, at most half of the units are even Fermat liars. The set H = {a ∈ (ℤ/nℤ)× : an−1 = 1} is a subgroup, and it is proper because n is not a Carmichael number. By Lagrange's theorem |H| divides φ(n) and is therefore at most φ(n)/2. The full theorem handles Carmichael numbers using the CRT: since such n has at least two distinct prime factors, one can construct units whose powers a2ʳd are ≡ −1 modulo one prime factor and ≡ 1 modulo another, and these are never strong liars. Consequently k independent random bases wrongly certify a composite with probability at most 4⁻ᵏ.
Worked example: n = 561, a = 2. Here 560 = 2⁴ · 35. Computing,
2³⁵ ≡ 263, 263² ≡ 166, 166² ≡ 67, 67² ≡ 1 (mod 561).
The sequence never reaches −1 ≡ 560 before reaching 1, so 561 is composite; 2 is a Fermat liar but not a strong liar. Moreover 67 is a non-trivial square root of 1, which reveals factors: gcd(67 − 1, 561) = 33 and gcd(67 + 1, 561) = 17.
Strong pseudoprimes exist: the smallest to base 2 is 2047 = 23 · 89, since 2046 = 2 · 1023 and 2¹¹ = 2048 ≡ 1 gives 2¹⁰²³ = (2¹¹)⁹³ ≡ 1. But base 3 exposes it (3¹⁰²³ ≡ 1565 ≢ ±1 (mod 2047)). Testing the bases 2, 3, 5, 7 is deterministic for n < 3 215 031 751, and the first twelve primes as bases suffice for n < 3.18 · 10²³. Assuming the generalised Riemann hypothesis, testing all bases a ≤ 2(log n)² is a deterministic polynomial-time test (Miller; Bach, 1990).
Deterministic and certifying methods
Agrawal, Kayal and Saxena (2002) found the first unconditional deterministic polynomial-time primality test, based on the polynomial congruence (x + a)ⁿ ≡ xⁿ + a modulo (n, xr − 1) for suitable small r; Lenstra and Pomerance reduced its running time to Õ((log n)⁶). In practice probabilistic tests are faster. For numbers of special form there are classical tests: Pépin's test for Fermat numbers (Lesson 8), and the Lucas–Lehmer test for Mersenne numbers: with s₀ = 4 and sₖ₊₁ = sₖ² − 2, the number Mₚ = 2ᵖ − 1 (p an odd prime) is prime if and only if Mₚ | sₚ₋₂. For p = 5, M₅ = 31 and s₁ = 14, s₂ = 194 ≡ 8, s₃ ≡ 62 ≡ 0 (mod 31). The largest known primes are Mersenne primes because of this test. Elliptic curve primality proving (ECPP) produces certificates that can be checked much faster than they are found.
The RSA cryptosystem
Rivest, Shamir and Adleman published their public-key system in 1977 (it had been found in 1973 by Clifford Cocks at GCHQ, but kept secret). Key generation: choose large primes p ≠ q, let n = pq, choose e with gcd(e, φ(n)) = 1, and compute d ≡ e⁻¹ (mod φ(n)) by the extended Euclidean algorithm. The public key is (n, e) and the private key is d. A message m ∈ ℤ/nℤ is encrypted as c = mᵉ mod n and decrypted as cᵈ mod n.
Theorem 13.3. For every m ∈ ℤ/nℤ (whether or not gcd(m, n) = 1), (mᵉ)ᵈ ≡ m (mod n).
Proof. Write ed = 1 + kφ(n) = 1 + k(p − 1)(q − 1). Modulo p: if p | m both sides are 0; otherwise med = m · (mp−1)k(q−1) ≡ m by Fermat. Similarly modulo q. By the CRT the congruence holds modulo n = pq. ∎
The same proof works with the Carmichael function λ(n) = lcm(p − 1, q − 1) in place of φ(n), giving a smaller decryption exponent.
Worked example: p = 61, q = 53, so n = 3233 and φ(n) = 60 · 52 = 3120. Take e = 17; the extended Euclidean algorithm gives d = 2753, since 17 · 2753 = 46801 = 15 · 3120 + 1. The message m = 65 encrypts to c = 65¹⁷ mod 3233 = 2790, and 2790²⁷⁵³ mod 3233 = 65.
Security
Anyone who can factor n can compute φ(n) and then d. Conversely, knowing φ(n) reveals the factors (p + q = n − φ(n) + 1 and pq = n, so p and q are the roots of a quadratic), and knowing d also allows n to be factored by a randomised algorithm that hunts for non-trivial square roots of 1, exactly as in the Miller–Rabin example above. Whether breaking RSA (recovering m from c without d) is as hard as factoring is open. The best known classical factoring algorithm is the general number field sieve, with heuristic running time
exp((64/9)1/3 (log n)1/3 (log log n)2/3 (1 + o(1))),
which has factored RSA-250, a number of 829 bits, in 2020. Practical RSA uses moduli of 2048 bits or more, randomised padding (textbook RSA is deterministic and malleable), and the CRT for fast decryption. Shor's quantum algorithm factors in polynomial time, which is why post-quantum schemes based on lattices were standardised by NIST in 2024.
Exercise
- (a)Run one round of Miller–Rabin on n = 91 with base 2.
- (b)In RSA with p = 5, q = 11 and e = 3, find d, encrypt m = 7, and decrypt.
- (c)An RSA modulus n = 3233 has φ(n) = 3120. Factor n.
Show solution
Answers: (a) 90 = 2 · 45. 2⁴⁵ ≡ 57 (mod 91), which is not ±1; squaring, 57² = 3249 = 35 · 91 + 64, so 2⁹⁰ ≡ 64 ≢ −1. Hence 91 is composite (indeed 91 = 7 · 13); here even the Fermat test detects it since 2⁹⁰ ≢ 1. (b) φ(55) = 40 and 3 · 27 = 81 ≡ 1 (mod 40), so d = 27. Encryption: 7³ = 343 = 6 · 55 + 13, so c = 13. Decryption: 13²⁷ ≡ 7 (mod 55). (c) p + q = 3233 − 3120 + 1 = 114 and pq = 3233, so p, q are the roots of t² − 114t + 3233 = 0: t = (114 ± √(12996 − 12932))/2 = (114 ± 8)/2, giving 61 and 53.