Miller-Rabin Primality Test Calculator
Miller-Rabin primality test: multi-round probabilistic with (1/4)^k error, deterministic for n<3.3e24, Fermat comparison and Carmichael numbers handling.
Report a calculation error
Help us improve. Describe what is wrong with the calculation.
Result
| Voce | Valore |
|---|
Show full details
Share result
Disclaimer: this calculation is for informational purposes only. For important decisions, consult a qualified professional.
The Miller-Rabin test
The Miller-Rabin primality test is the world's most-used algorithm for deciding whether a natural number is prime. Developed by Gary Miller (1976) as a deterministic algorithm conditional on the generalised Riemann hypothesis, modified by Michael O. Rabin (1980) into an unconditional probabilistic form, it underlies every key generation in RSA, DSA, Diffie-Hellman and the primality tests in OpenSSL, GnuPG, Mathematica, Python (sympy.isprime), Java (BigInteger.isProbablePrime).
The algorithm
For odd n > 3, write n − 1 = d · 2^s with d odd. To test with base a:
- Compute x0 = ad mod n.
- If x0 = 1 or x0 = n − 1, n is probably prime.
- Otherwise compute xi+1 = xi2 mod n for i = 0, ..., s − 2. If any xi = n − 1, probably prime.
- Otherwise n is definitely composite and a is a witness.
The test relies on the fact that in ℤ/pℤ with p prime, the only non-trivial square roots of 1 are ±1. Finding b with b² ≡ 1 (mod n) but b ≠ ±1 proves n composite.
The four modes
- Single test: Miller-Rabin with a specific base a, showing all intermediate steps.
- Multi-round random: k random-base rounds. False-positive probability <= (1/4)^k. For k = 20: < 10^-12.
- Deterministic: uses fixed bases {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37} for deterministic result on n < 3.317·10^24 (Sorenson-Webster 2015). Faster than AKS.
- Fermat comparison: shows how Miller-Rabin distinguishes Carmichael numbers from true primes.
Error probability
Rabin (1980) proved: for any composite n, at least 3/4 of bases in [2, n − 2] are witnesses. So a single round with a random base has error probability <= 1/4. k independent rounds give error 99% of bases are witnesses.
Deterministic variant
With enough fixed bases, Miller-Rabin becomes deterministic for bounded n:
- n < 2,047: test a = 2.
- n < 1,373,653: {2, 3}.
- n < 9,080,191: {31, 73}.
- n < 4,759,123,141: {2, 7, 61}.
- n < 3.317·10^24: {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37} (Sorenson-Webster 2015).
For larger n, Miller-Rabin remains probabilistic but with practically null error at k = 20-40 rounds. For absolute certainty there is AKS (Agrawal-Kayal-Saxena 2002, O((log n)^(6+ε))), but it is much slower in practice.
Carmichael numbers
Carmichael numbers (discovered 1910) are composites that pass the Fermat test for every coprime base. First few:
561, 1105, 1729, 2465, 2821, 6601, 8911, 10585, 15841, 29341, 41041, ...
1729 is the Hardy-Ramanujan number; 561 = 3·11·17 is the smallest. Alford-Granville-Pomerance (1994) proved there are infinitely many, but they are rare: only 2,163 below 10^9. Miller-Rabin detects them correctly via the non-trivial-root-of-1 check.
Practical applications
- RSA key generation: large primes (1024-2048 bits) tested with k = 40 rounds. Error < 10^-24.
- TLS handshake: Diffie-Hellman parameters and elliptic curves need verified primes.
- Blockchain: Bitcoin, Ethereum use Miller-Rabin on elliptic-curve primes (secp256k1).
- OpenSSL:
BN_is_prime_ex()combines trial division + Miller-Rabin. - Python:
sympy.isprime()andgmpy2.is_prime()implement Miller-Rabin with BPSW enhancements. - Digital certificates: CAs verify primality in RSA signatures of X.509 certs.
Comparison with other tests
- Trial division: exact but O(√n), too slow for n > 10^12.
- Fermat: O(log n) per round but fooled by Carmichael.
- Solovay-Strassen (1977): similar but worse error (1/2 per round).
- Baillie-PSW (BPSW): Miller-Rabin base 2 + Lucas test. No known counterexample up to 2^64.
- AKS (2002): first deterministic polynomial-time test. O((log n)^(6+ε)).
- ECPP: produces verifiable primality certificate. O((log n)^4), used for world records.
Updated July 2026
Comments (0)
Login to leave a comment.
No comments yet. Be the first!