Caloogy Article
中文

Where Primes Come From: The Fermat Test, Carmichael Numbers and Miller–Rabin

Date
Nov 18, 2025
Series
Cryptography
Reading
5 min
Topics
PrimalityFermatCarmichaelMiller–Rabin

The one-sentence version

Generating an RSA key means finding two primes hundreds of digits long. The method is "pick a random odd number, test it, and if it fails, pick another". Fermat's little theorem gives the simplest test: if pp is prime, then ap−1≡1a^{p-1} \equiv 1. But one class of composites (Carmichael numbers, the smallest being 561561) fools every base. Miller–Rabin closes this hole using the fact that modulo a prime, x2=1x^2 = 1 has only the two roots ±1\pm 1.

This article has three layers:

  1. The intuition: why primality can be decided without factoring, and the idea of "random spot checks"
  2. Real-world use: RSA key generation, OpenSSL and accidents in prime generation
  3. The mathematics: the Fermat test, Carmichael numbers, why Miller–Rabin is correct and a worked example with 561561

Layer 1: The Intuition

1.1 Deciding primality is far easier than factoring

"Is NN prime?" and "which two numbers multiply to NN?" sound like the same question, but their difficulty is worlds apart:

The remarkable part is that a primality test can declare a number composite without finding any factor. It is like a counterfeit detector: it doesn't need to know who printed the fake, only that some feature is wrong.

1.2 Are there enough primes?

The prime number theorem says that near NN, about one number in ln⁡N\ln N is prime. For a 1024-bit number, ln⁡21024≈710\ln 2^{1024} \approx 710; picking only odd numbers, you hit a prime after about 355 tries on average. Each test is fast, so generating primes isn't slow.

1.3 The "spot check" idea

Primes always satisfy certain properties (such as Fermat's little theorem). We pick some random bases aa and check:

The key is designing a check where a composite's chance of slipping through is low enough. Each round of Miller–Rabin is fooled with probability at most 1/41/4. After 40 rounds, the chance of being fooled is at most 4−40=2−804^{-40} = 2^{-80}, lower than the chance of a hardware fault.


Layer 2: Real-World Use

What happens when prime generation goes wrong

  • Randomness that isn't random: a 2012 scan of RSA public keys across the internet found that many routers and firewalls, short of entropy at boot, generated keys with shared prime factors, which a single gcd⁡\gcd factors (see "Why RSA Decrypts Correctly").
  • Primes with structure: in the ROCA vulnerability (2017), a chip used primes of a special form for speed, and the results could be factored.
  • "Pseudoprimes" built by an adversary: the 2018 study Prime and Prejudice found that some cryptographic libraries, when testing numbers supplied from outside, used too few rounds or fixed bases, so attackers could build composites that fool them and inject insecure DH parameters.

Layer 3: The Mathematics

3.1 The Fermat test

Fermat's little theorem: if pp is prime and gcd⁡(a,p)=1\gcd(a, p) = 1, then ap−1≡1(modp)a^{p-1} \equiv 1 \pmod p. (This is Lagrange's theorem for Zp×\mathbb{Z}_p^\times, whose order is p−1p-1.)

The test: pick a random aa and compute aN−1 mod Na^{N-1} \bmod N. If it isn't 11, NN is definitely composite.

Most composites fail right here. For example with N=15N = 15, 214=16384≡4(mod15)2^{14} = 16384 \equiv 4 \pmod{15}, and it is exposed at once.

3.2 Carmichael numbers

There is a class of composites satisfying aN−1≡1a^{N-1} \equiv 1 for every aa coprime to them, called Carmichael numbers.

Korselt's criterion: a composite NN is a Carmichael number if and only if NN is squarefree and (p−1)∣(N−1)(p - 1) \mid (N - 1) for every prime factor p∣Np \mid N.

The smallest example is 561=3×11×17561 = 3 \times 11 \times 17: 560560 is divisible by 22, 1010 and 1616. By the Chinese Remainder Theorem, a560a^{560} is 11 modulo each of 33, 1111 and 1717, and so also 11 modulo 561561.

In 1994 Alford, Granville and Pomerance proved there are infinitely many Carmichael numbers. So the Fermat test alone won't do.

3.3 The key lemma: modulo a prime, 11 has only two square roots

If pp is an odd prime and x2≡1(modp)x^2 \equiv 1 \pmod p, then p∣(x−1)(x+1)p \mid (x-1)(x+1). Because Zp\mathbb{Z}_p is a field (it has no zero divisors), either x≡1x \equiv 1 or x≡−1x \equiv -1.

Conversely, if you find x≢±1x \not\equiv \pm 1 with x2≡1(modN)x^2 \equiv 1 \pmod N, then ZN\mathbb{Z}_N has zero divisors and NN must be composite (and gcd⁡(x−1,N)\gcd(x - 1, N) is a nontrivial factor).

3.4 Miller–Rabin

Write N−1N - 1 as 2s⋅d2^s \cdot d with dd odd. Pick a random aa and look at this sequence:

ad, a2d, a4d, …, a2sd=aN−1(modN).a^d,\ a^{2d},\ a^{4d},\ \dots,\ a^{2^s d} = a^{N-1} \pmod N.

Each term is the square of the previous one. If NN is prime, the last term is 11 by Fermat's little theorem; and by the lemma in 3.3, taking square roots backwards, the first departure from 11 must be −1-1. So a prime always satisfies:

Either ad≡1a^d \equiv 1, or some term of the sequence equals −1-1.

If this fails, the number is declared composite. Rabin proved that for a composite NN, at most 1/41/4 of the bases aa can fool this test.

3.5 A worked example: exposing 561561

N=561N = 561, base a=2a = 2

Fermat test: 2560 mod 561=12^{560} \bmod 561 = 1. It passes, and the test is fooled.

Miller–Rabin: 560=24×35560 = 2^4 \times 35, so s=4s = 4 and d=35d = 35.

Term Value  mod 561\bmod 561
2352^{35} 263263
270=26322^{70} = 263^2 166166
2140=16622^{140} = 166^2 6767
2280=6722^{280} = 67^2 11
25602^{560} 11

The sequence jumps straight from 6767 to 11 without passing through −1=560-1 = 560. That is, 672≡167^2 \equiv 1 but 67≢±167 \not\equiv \pm 1, which can't happen modulo a prime, so 561 is composite.

We even get factors along the way: gcd⁡(67−1,561)=gcd⁡(66,561)=33=3×11\gcd(67 - 1, 561) = \gcd(66, 561) = 33 = 3 \times 11 and gcd⁡(67+1,561)=17\gcd(67 + 1, 561) = 17. (Checked with Python.)

3.6 Deterministic algorithms

In 2002 Agrawal, Kayal and Saxena gave the AKS algorithm, the first proof that primality can be tested in deterministic polynomial time (the paper is titled PRIMES is in P). It is much slower than Miller–Rabin, though, so probabilistic algorithms are still used in practice.


Summary

Layer Key points
Intuition Deciding primality is far easier than factoring; spot check at random, and one failure means composite
Algebra Fermat's little theorem comes from Lagrange's theorem; Carmichael numbers fool it
The key In a field, x2=1x^2 = 1 has only the roots ±1\pm 1; any other square root proves compositeness
Engineering Trial division + many rounds of Miller–Rabin; neither randomness quality nor round count can be skimped