Short answer: The Miller-Rabin test writes an odd number n as d·2^s + 1 and looks at a^d mod n. The test passes for a base a when that power is 1, or when some a^{d·2^r} mod n equals n−1 for an r before s. A prime passes for every base. A composite that passes for a base is a strong pseudoprime to that base. k random bases miss all of them with probability at most 4^{−k} under the usual analysis. A fixed set of bases makes the test deterministic for every integer below a published bound, including all 64-bit integers.
What the problem actually asks
Fermat’s test checks a^{n−1} ≡ 1 (mod n). Carmichael numbers pass it for every base coprime to n and are composite. Miller-Rabin checks a stronger condition that comes from the fact that, modulo a prime, the equation x² ≡ 1 has only the solutions 1 and n−1. A nontrivial square root of 1 is a proof of compositeness. The sequence of squarings from a^d up to a^{n−1} must hit n−1 before it hits 1, or start at 1.
Random bases make the test Monte Carlo. Deterministic base sets are finite lists that have been checked for a range. They are not a new general proof that a handful of bases work for every integer.
What the algorithm guarantees
If the test fails for any base, n is composite, and the witness is unconditional. If n is prime, every base passes. If n is an odd composite, a random base in 2..n−2 passes with probability at most 1/4, so k independent bases err with probability at most 4^{−k}. Even n other than 2 is composite and does not need the test. Extra memory is a few integers the size of n. Modular exponentiation is the cost, O(k log³ n) with schoolbook multiplication.
For integers below 2^64, a known finite list of bases makes the test deterministic. Above that range the same short list is only a probabilistic test. AKS is a deterministic polynomial-time test with a much larger constant. Miller-Rabin is the test that is actually run.
The working steps
- Handle n < 3 and even n directly. Write n−1 = d·2^s with d odd.
- Choose a base a. Compute x = a^d mod n by binary exponentiation.
- If x is 1 or n−1, this base passes. Otherwise square x repeatedly, s−1 times. If a square equals n−1, the base passes. If a square equals 1 earlier, or the loop ends without n−1, the base fails and n is composite.
- Repeat for the chosen bases. Any failure proves composite. All passes mean “probably prime” unless the base set is a deterministic set for the range that contains n.
- Do not conclude that a failing base yields a factor. The witness proves compositeness. Factoring is a different algorithm.
A passing probabilistic test is not a proof of primality. A failing test is a proof of compositeness. Deterministic base lists are range-limited. The test does not factor n. Pollard’s rho factors. The 1/4 bound is the standard Miller-Rabin analysis for the strong test, not the weaker Fermat test.
Approaches that look simpler and fail
| Approach | Why it fails when a Fermat test replaces the strong condition |
|---|---|
| Check only a^{n−1} ≡ 1 | Carmichael numbers pass for every coprime base and are composite. The squaring sequence is what catches them. |
| Treat k passing random bases as a theorem | The error probability is at most 4^{−k}, which is small and is not zero. Say probably prime, or use a deterministic base set inside its range. |
| Use a 64-bit base list on an arbitrary-precision integer | The list was verified up to a bound. Past that bound it is just another set of random-looking bases. |
| Return a factor from a failing witness without an extra construction | The witness shows compositeness. Extracting a factor needs a different step, such as a gcd involving a nontrivial square root of 1, and not every failure yields that root in the form the caller expects. |
Enumerating small primes is a sieve. Factoring a composite the test has caught is Pollard’s rho or a heavier factorisation algorithm. A fully general deterministic polynomial test is AKS. Miller-Rabin is the practical primality test.
A working example
This Python fragment writes n−1 = d·2^s and looks for a strong liar. A witness that fails the strong-pseudoprime conditions proves compositeness. 91 is composite and 97 is prime under the deterministic bases used for small n. It prints composite prime.
def miller_rabin(n, bases):
if n < 2:
return False
d, s = n - 1, 0
while d % 2 == 0:
d //= 2
s += 1
for a in bases:
if a % n == 0:
continue
x = pow(a, d, n)
if x == 1 or x == n - 1:
continue
for _ in range(s - 1):
x = pow(x, 2, n)
if x == n - 1:
break
else:
return False
return True
small_bases = [2, 3, 5, 7]
assert miller_rabin(91, small_bases) is False
assert miller_rabin(97, small_bases) is True
print("composite", "prime")
Where it is used
Miller-Rabin is the probable-prime test in RSA key generation: OpenSSL, Java BigInteger.isProbablePrime, and every library that draws primes. A composite that passes is a strong pseudoprime to the bases that were tried. AKS proves primality in polynomial time and is not the test those libraries run.
When you can drop this algorithm
Drop probabilistic language when the number lies under a bound covered by a published deterministic base set. Use that set and call the result a proof. Keep random bases, and state the error bound, when the integer is larger.
Why this stays published
We publish this so a search for Miller-Rabin returns the strong pseudoprime condition and the difference between a witness of compositeness and a probabilistic pass. Miller-Rabin proves compositeness when a base fails the strong pseudoprime test, and k passing random bases are wrong with probability at most 4 to the minus k.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
