Miller–Rabin Primality Test

lean4-proofcryptographyvisualization
an1≢1(modn)    n compositea^{n-1} \not\equiv 1 \pmod{n} \implies n \text{ composite}

Statement

Fermat's compositeness witness. If n>2n > 2 is prime and gcd(a,n)=1\gcd(a, n) = 1, then Fermat's little theorem gives an11(modn)a^{n-1} \equiv 1 \pmod{n}. The contrapositive:

an1≢1(modn)    n is compositea^{n-1} \not\equiv 1 \pmod{n} \implies n \text{ is composite}

Such an aa is called a Fermat witness for the compositeness of nn.

Carmichael numbers. There exist composite nn (Carmichael numbers) for which an11(modn)a^{n-1} \equiv 1 \pmod{n} for every aa with gcd(a,n)=1\gcd(a, n) = 1. The smallest is n=561=3×11×17n = 561 = 3 \times 11 \times 17. The Miller–Rabin test strengthens Fermat's test to eliminate Carmichael numbers by additionally checking square roots of 11.

Visualization

Test n=561n = 561 (Carmichael number) with base a=2a = 2:

Write n1=560=16×35=24×35n - 1 = 560 = 16 \times 35 = 2^4 \times 35. Compute the sequence 235,270,2140,2280,25602^{35}, 2^{70}, 2^{140}, 2^{280}, 2^{560} modulo 561561:

2^35   mod 561 = 263
2^70   mod 561 = 166   (263^2 mod 561)
2^140  mod 561 = 67    (166^2 mod 561)
2^280  mod 561 = 1     (67^2 mod 561)
2^560  mod 561 = 1     (1^2 mod 561)

The final value is 11 (Fermat test passes), but 228012^{280} \equiv 1 and 214067±1(mod561)2^{140} \equiv 67 \ne \pm 1 \pmod{561}. This is a non-trivial square root of 11, impossible modulo a prime — so Miller–Rabin correctly rejects 561561.

For comparison, test n=13n = 13 (prime) with a=2a = 2:

kk2kmod132^k \bmod 13
1122
6664mod13=12164 \bmod 13 = 12 \equiv -1
121211

21212^{12} \equiv 1 and the sequence hits 1-1 before 11 — consistent with primality.

Carmichael witness table for n=561n = 561:

PropertyValue
n1=2sdn - 1 = 2^s \cdot ds=4s = 4, d=35d = 35
a=2a = 2, admodna^d \bmod n263263
Fermat test an1modna^{n-1} \bmod n11 (passes — false!)
Miller–Rabin verdictCOMPOSITE (non-trivial sqrt of 1)

Proof Sketch

  1. Fermat witness. If nn is prime and gcd(a,n)=1\gcd(a, n) = 1, Fermat's little theorem says an11(modn)a^{n-1} \equiv 1 \pmod{n}. Contrapositive: an1≢1a^{n-1} \not\equiv 1 certifies nn is composite.

  2. Square root argument. In Z/pZ\mathbb{Z}/p\mathbb{Z} for prime pp, the only solutions to x21x^2 \equiv 1 are x±1x \equiv \pm 1. So in the sequence ad,a2d,,a2sd=an1a^d, a^{2d}, \ldots, a^{2^s d} = a^{n-1}, the first 11 must be preceded by 1-1 (or the sequence starts at 11).

  3. Carmichael numbers fail this. Since nn is composite, Z/nZ\mathbb{Z}/n\mathbb{Z} may have more square roots of 11. Detecting them certifies compositeness.

  4. Probabilistic guarantee. For a random composite nn, at least 3/43/4 of bases aa are Miller–Rabin witnesses. Running kk rounds gives error probability (1/4)k\le (1/4)^k.

Connections

Miller–Rabin rests on Fermat's Little TheoremFermat's Little Theoremapa(modp)a^p \equiv a \pmod{p}For prime p, a^p is congruent to a mod pRead more → (the prime case). The square-root-of-11 argument uses that Z/pZ\mathbb{Z}/p\mathbb{Z} is a field; compare with Wilson's TheoremWilson's Theorem(p1)!1(modp)(p-1)! \equiv -1 \pmod{p}A natural number p > 1 is prime if and only if (p-1)! ≡ -1 mod pRead more → which characterises primes via (p1)!1(p-1)! \equiv -1. Large primes are generated for RSA CorrectnessRSA Correctnessmedm(modn)m^{ed} \equiv m \pmod{n}Decryption recovers the plaintext: m^(ed) ≡ m (mod n) whenever ed ≡ 1 (mod φ(n)).Read more → using exactly this test.

Lean4 Proof

/-- Fermat's compositeness witness: if n is prime then a^(n-1) ≡ 1 mod n
    (for gcd(a,n)=1). We state the contrapositive concretely. -/

-- In ZMod p (prime p), every nonzero element satisfies a^(p-1) = 1.
theorem fermat_prime_pow (p : ) [Fact p.Prime] {a : ZMod p} (ha : a  0) :
    a ^ (p - 1) = 1 :=
  ZMod.pow_card_sub_one_eq_one ha

/-- Concrete check: 561 is composite. -/
example : ¬ Nat.Prime 561 := by decide

/-- 2^560 mod 561 = 1 (Fermat test passes — 561 is a Carmichael number). -/
example : 2 ^ 560 % 561 = 1 := by decide

/-- But 2^140 mod 561 ≠ 1 and ≠ 560 (i.e. ≠ -1 mod 561).
    This is the non-trivial square root of 1 that Miller–Rabin detects. -/
example : 2 ^ 140 % 561 = 67 := by decide
example : (67 : )  1 := by decide
example : (67 : )  560 := by decide