Miller-Rabin Primality Test
- •
What it is
- •
A probabilistic primality test — the practical, industry-standard way essentially every cryptography library decides "is this number prime?" — built on a stronger version of Fermat's little theorem. Write with odd. For a candidate "witness" base , compute ; if , repeatedly square it up to times looking for it to hit . If neither nor any squared value equals , is a definitive witness that is composite. Crucially, this catches numbers that fool the plain Fermat test entirely — Carmichael numbers like 561 pass Fermat's test () for the overwhelming majority of bases, but Miller-Rabin's stronger structural check (looking for a non-trivial square root of unity along the way, not just the final result) exposes most of them as composite anyway. No single witness base can be trusted 100% of the time (a "strong liar" base exists for every composite), but at most 1/4 of all bases can be strong liars for any given composite — so testing a handful of random bases drives the false-positive rate down to effectively zero for practical purposes.
- •
- •
When to apply
- •
Whenever you need to determine (or exploit an implementation's method of determining) whether a large number is prime — and specifically whenever a challenge's primality check tests against a small, fixed, disclosed set of bases rather than random ones, since that's a very different (and much weaker) trust model.
- •
- •
Symbols and assumptions
- •
is an odd candidate; write with odd . A tested base satisfies . Passing a base means probable prime for that test, not a proof of primality.
- •
- •
Math — step by step
- •
- If , a factor is already found. Otherwise compute . Accept this base if or .
- •
- Otherwise square at most times, accepting if any result is . If none is, reject as composite.
- •
- Why this is valid for primes: Fermat forces the squaring chain to end at 1, and the only square roots of 1 in a field are ±1. A chain that reaches 1 without a preceding −1 exposes composite behavior.
- •
- For an odd composite, at most one quarter of admissible bases are strong liars. The bound requires independent suitably random bases; fixed adversarially known bases are a different assumption.
- •
- •
- •
Python
- •
def miller_rabin_round(n, a): d, r = n - 1, 0 while d % 2 == 0: d //= 2; r += 1 x = pow(a, d, n) if x == 1 or x == n - 1: return True # probably prime, relative to this witness for _ in range(r - 1): x = pow(x, 2, n) if x == n - 1: return True return False # definitely composite; a is a witness
- •
- •
- •
- •
Cards
- •
What's the key structural check Miller-Rabin adds beyond the plain Fermat test?
- •
It examines the sequence of values while repeatedly squaring a^d, looking for a non-trivial square root of unity (not just checking the final result equals 1) — catching many Carmichael-number-style Fermat liars.
- •
- •
What fraction of bases can be "strong liars" for any given composite number, at most?
- •
At most 1/4 — which is why testing several random bases drives the overall false-positive probability down exponentially.
- •
- •