← LibraryProbabilistic Primality TestingEngineering · MathematicsLesson 25/32← PrevNext →
ArticlePublished 6 Aug 2026Updated 5 Aug 20268 min readBy Kevin Jogin
KEVOS® Knowledge Library · Engineering → Mathematics

Engineering/Mathematics/Algorithm engineering

Probabilistic Primality Testing

Deciding primality is easy; producing a factorization is hard. Miller–Rabin exploits that gap by looking for a non-trivial square root of one — a structure that a prime modulus cannot supply and a composite one usually can.

  • Core algorithm
  • Computing
  • Key generation
  • ≈17 min read
  • Runs in every TLS handshake
≤ 1/4Error per roundWorst-case probability that a composite passes one Miller–Rabin round with a random base.
O(kℓ³)Costk rounds, each one modular exponentiation of -bit values.
13 basesDeterministic to 3.3×10²⁴Verified base sets make the test deterministic below explicit thresholds.
561Smallest Carmichael numberPasses the Fermat test for every base coprime to it — the failure Miller–Rabin repairs.

01

Executive summary

Trial division settles primality for small inputs and is hopeless beyond about twenty digits. The Fermat test uses an−1 ≡ 1 as a necessary condition, but Carmichael numbers satisfy it for every coprime base, so it can never be made reliable.

Miller–Rabin repairs this by examining the sequence of squarings that produces an−1. For a prime modulus the only square roots of 1 are ±1; for a composite with at least two distinct prime factors there are others, and observing one is a proof of compositeness. The probability that a random base fails to expose a composite is at most 1/4, so repetition drives the error down geometrically.

Necessary conditiona^{n−1} ≡ 1 (mod n)

Fermat's little theorem — necessary but not sufficient.

Failure modeCarmichael numbers

Composites passing for every coprime base; infinitely many exist.

RepairTrack square roots of 1

A prime modulus admits only ±1; a non-trivial root certifies compositeness.

Guarantee≥ 3/4 of bases are witnesses

So k independent rounds bound the error by 4^{-k}.

Contents

02

Trial division and the Fermat test

Trial division by primes up to √n is complete and certain, and costs O(√n / ln n) divisions — exponential in the bit length. It remains indispensable as a pre-filter: dividing by the primes below 1000 rejects about 80% of random odd candidates at trivial cost.

Definition D1

Fermat test

Pick a with 1 < a < n−1. If an−1 ≢ 1 (mod n), output composite — this is certain by Fermat's little theorem. Otherwise output probably prime. A composite passing the test for base a is a Fermat pseudoprime to that base.

Carmichael numbers make the Fermat test unrepairable

A Carmichael number is a composite n with an−1 ≡ 1 (mod n) for every a coprime to n. The smallest are 561, 1105, 1729, 2465 and 2821, and Alford, Granville and Pomerance proved in 1994 that there are infinitely many. Increasing the number of Fermat rounds does not help: every coprime base is fooled. The test can only fail by stumbling on a base sharing a factor with n, which is no more likely than trial division succeeding.

Contents

03

The Miller–Rabin test

Theorem T1

The structural fact behind the test

If n is an odd prime then x2 ≡ 1 (mod n) implies x ≡ ±1, because n is a field and X2−1 has at most two roots. If n has at least two distinct odd prime factors then, by the Chinese remainder theorem, there are at least four square roots of 1, and a non-trivial one yields a factor via gcd(x−1, n).

Miller–Rabin, one round

  1. write n − 1 = 2^s·m with m odd
  2. choose a uniformly from {2, …, n−2}
  3. y ← a^m mod n
  4. if y = 1 or y = n−1: return probably prime
  5. for i = 1 .. s−1:
  6. y ← y² mod n
  7. if y = n−1: return probably prime
  8. if y = 1: return composite // non-trivial square root of 1 found
  9. return composite

One modular exponentiation: O(ℓ³) bit operations classically, or Õ(ℓ²) with fast arithmetic. The squaring loop reuses the exponentiation's own intermediate values.

Theorem T2

Error bound

For odd composite n > 9, at least 3/4 of the bases in {1,…,n−1} are witnesses to compositeness. Hence a single round errs with probability at most 1/4, and k independent rounds err with probability at most 4−k. The bound is tight only for a sparse family of composites; for random inputs the true error is vastly smaller.

Failure probability by round count (worst case, adversarial input)
k = 12.5 × 10⁻¹
k = 81.5 × 10⁻⁵
k = 162.3 × 10⁻¹⁰
k = 325.4 × 10⁻²⁰
k = 642.9 × 10⁻³⁹

Two distinct threat models

For a candidate you generated yourself from a good random source, a small number of rounds suffices: the average-case error for random -bit candidates falls far below 4−k. For a candidate supplied by someone else — a modulus in a certificate, a parameter in a protocol message — the worst case applies, because the value may have been constructed to pass. Use 64 rounds, or a proof-based test, on untrusted input.

Contents

04

Related tests and practical choices

Primality tests in use
TestTypeErrorWhere used
Trial divisionDeterministicNonePre-filter; small inputs
FermatMonte CarloFails on Carmichael numbersHistoric; not recommended alone
Solovay–StrassenMonte Carlo≤ 1/2 per roundSuperseded by Miller–Rabin
Miller–RabinMonte Carlo≤ 1/4 per roundThe default everywhere
Miller–Rabin, fixed verified basesDeterministic below a thresholdNone below the threshold64-bit integers: first 12–13 primes suffice
Baillie–PSWHeuristicNo counterexample known below 264Widely used in computer algebra systems
Pocklington certificateProofNoneWhen the factorization of p−1 is known
ECPPProofNoneCertified primes of thousands of digits
AKSDeterministic, unconditionalNoneTheoretical landmark; impractical at scale

Baillie–PSW combines a base-2 strong test with a Lucas test; the two have complementary failure modes, and no composite is known to pass both.

  • Always include base 2 first when using random bases: it is the cheapest possible round on many implementations and eliminates the overwhelming majority of composites immediately.
  • Use verified base sets for small inputs. Below 3.3 × 1024 the first thirteen primes as bases give a deterministic answer, exhaustively verified. This is both faster and stronger than random rounds for 64-bit work.
  • Certify when the application demands it. Pocklington's criterion turns a partial factorization of p−1 into a proof of primality, which is why safe-prime generation naturally yields certified primes.
Contents

06

Implementation checklist

Practical requirements
ItemRequirementConsequence if ignored
Pre-sievingTrial divide by primes below ~1000Roughly five times more exponentiations
Base selectionUniform in [2, n−2], fresh per roundCompounding argument invalid
Round countSmall for self-generated, 64 for untrusted inputFalse confidence on adversarial input
ExponentiationMontgomery arithmetic, windowedSeveral times slower
Constant timeRequired if the candidate itself is secretTiming leakage of prime factors during key generation
Candidate shapeFix the top bits for exact modulus sizeModulus one bit short of the nominal size
Extra conditionsgcd(e, p−1) = 1 for RSAKey generation fails later, or a weak key
EntropyCryptographic source, well seededColliding primes across devices

Order of operations that works

Draw a random odd candidate with the top two bits set → sieve against a table of small primes → one Miller–Rabin round with base 2 → the remaining rounds with random bases → check the application-specific side conditions. Most candidates die at the sieve, and almost all survivors of the base-2 round are prime, so the expensive rounds run only a handful of times per key.

Contents

07

Quick reference and FAQ

Key facts
FactStatement
Fermat conditionan−1 ≡ 1 (mod n) for prime n, n ∤ a
Carmichael numbersInfinitely many; the smallest is 561
Witness density≥ 3/4 of bases witness compositeness
Error after k rounds≤ 4−k worst case
CostO(kℓ3) bit operations
Deterministic thresholdFirst 13 prime bases correct below 3.3 × 1024
Compositeness certificateA non-trivial square root of 1, or a Fermat failure
Prime density≈ 2/(k ln 2) among odd k-bit integers
Does Miller–Rabin ever declare a prime composite?
Never. Compositeness output is accompanied by an implicit certificate — either a Fermat failure or a non-trivial square root of 1 — and neither can occur for a prime modulus. The error is strictly one-sided.
Why is the worst-case bound 1/4 when most composites do far worse?
The bound must hold for every composite, including carefully constructed ones such as products of two primes with matched structure. Ordinary composites have witness densities close to 1, which is why a single base-2 round rejects nearly everything in practice.
Should the test be constant time?
Yes when the candidate is a secret being generated, since timing variation can leak information about the prime under construction. When testing a public value such as a certificate modulus, constant time is unnecessary.
What if a certified prime is required rather than a probable prime?
Use Pocklington's criterion when a partial factorization of p−1 is available — which is automatic when generating safe primes — or ECPP for the general case. Both produce a short certificate that a third party can verify quickly.
Contents

09

References and further reading

  • V. Shoup, A Computational Introduction to Number Theory and Algebra, Cambridge University Press, 2005 — Chapter 10.
  • M. O. Rabin, 'Probabilistic algorithm for testing primality', J. Number Theory 12 (1980) 128–138.
  • W. R. Alford, A. Granville and C. Pomerance, 'There are infinitely many Carmichael numbers', Annals of Mathematics 139 (1994) 703–722.
  • R. Crandall and C. Pomerance, Prime Numbers: A Computational Perspective, 2nd ed., Springer, 2005 — Chapter 3.
  • NIST FIPS 186-5, Digital Signature Standard, 2023 — Appendix A on prime generation and required round counts.

KEVOS® Knowledge LibraryEngineering → MathematicsTaxonomy ID: ENG-MATHPage ID: primality-testing-miller-rabinReview cycle: annual


Continue learning

Probabilistic AlgorithmsArticle · MathematicsNEXT LESSON →Deterministic Primality Testing: the AKS AlgorithmArticle · MathematicsEuclid's Algorithm and Modular ComputationArticle · MathematicsGenerators and Discrete Logarithms in ℤ*pArticle · Mathematics