Lably
Skip to content

Miller–Rabin primality test

Probabilistic primality test

  • 19 min read
  • 6 chapters

Related on Lably

Through Primality test, Algorithm and Coprime integers

It is of historical significance in the search for a polynomial-time deterministic primality test. Its probabilistic variant remains widely used in practice, as one of the simplest and fastest tests known.

Gary L. Miller discovered the test in 1976. Miller's version of the test is deterministic, but its correctness relies on the unproven extended Riemann hypothesis. Michael O. Rabin modified it to obtain an unconditional probabilistic algorithm in 1980.

Mathematical concepts

Similarly to the Fermat and Solovay–Strassen tests, the Miller–Rabin primality test checks whether a specific property, which is known to hold for prime values, holds for the number under testing.

Strong probable primes

The property is the following. For a given odd integer n>2, let’s write n−1 as 2sd where s is a positive integer and d is an odd positive integer. Let’s consider an integer a, called a base, which is coprime to n. Then, n is said to be a strong probable prime to base a if one of these congruence relations holds:

  • ad≡1(modn), or
  • a2rd≡−1(modn) for some 0≤r<s.

This simplifies to first checking for ad≡1(modn) and then a2rd≡n−1(modn) for successive values of r. For each value of r, the value of the expression may be calculated using the value obtained for the previous value of r by squaring under the modulus of n.

The idea beneath this test is that when n is an odd prime, it passes the test because of two facts:

  • by Fermat's little theorem, an−1≡1(modn) (this property alone defines the weaker notion of probable prime to base a, on which the Fermat test is based);
  • the only square roots of 1 modulo n are 1 and −1.

Hence, by contraposition, if n is not a strong probable prime to base a, then n is definitely composite, and a is called a witness for the compositeness of n.

However, this property is not an exact characterization of prime numbers. If n is composite, it may nonetheless be a strong probable prime to base a, in which case it is called a strong pseudoprime, and a is a strong liar.

Choices of bases

No composite number is a strong pseudoprime to all bases at the same time (contrary to the Fermat primality test for which Fermat pseudoprimes to all bases exist: the Carmichael numbers). However no simple way of finding a witness is known. A naïve solution is to try all possible bases, which yields an inefficient deterministic algorithm. The Miller test is a more efficient variant of this (see section Miller test below).

Another solution is to pick a base at random. This yields a fast probabilistic test. When n is composite, most bases are witnesses, so the test will detect n as composite with a reasonably high probability (see section Accuracy below). We can quickly reduce the probability of a false positive to an arbitrarily small rate, by combining the outcome of as many independently chosen bases as necessary to achieve the said rate. This is the Miller–Rabin test. There seems to be diminishing returns in trying many bases, because if n is a pseudoprime to some base, then it seems more likely to be a pseudoprime to another base.

Note that ad ≡ 1 (mod n) holds trivially for a ≡ 1 (mod n), because the congruence relation is compatible with exponentiation. And ad = a20d ≡ −1 (mod n) holds trivially for a ≡ −1 (mod n) since d is odd, for the same reason. That is why random a are usually chosen in the interval 1 < a < n − 1.

For testing arbitrarily large n, choosing bases at random is essential, as we don't know the distribution of witnesses and strong liars among the numbers 2, 3, ..., n − 2.

However, a pre-selected set of a few small bases guarantees the identification of all composites up to a pre-computed maximum. This maximum is generally quite large compared to the bases. This gives very fast deterministic tests for small enough n (see section Testing against small sets of bases below).

Proofs

Here is a proof that, if n is a prime, then the only square roots of 1 modulo n are 1 and −1.

Proof

Certainly 1 and −1, when squared modulo n, always yield 1. It remains to show that there are no other square roots of 1 modulo n. This is a special case, here applied with the polynomial X2 − 1 over the finite field Z/nZ, of the more general fact that a polynomial over some field has no more roots than its degree (this theorem follows from the existence of an Euclidean division for polynomials). Here follows a more elementary proof. Suppose that x is a square root of 1 modulo n. Then:

(x−1)(x+1)=x2−1≡0(modn).

In other words, n divides the product (x − 1)(x + 1). By Euclid's lemma, since n is prime, it divides one of the factors x − 1 or x + 1, implying that x is congruent to either 1 or −1 modulo n.

Here is a proof that, if n is an odd prime, then it is a strong probable prime to base a.

Proof

If n is an odd prime and we write n − 1= 2sd where s is a positive integer and d is an odd positive integer, by Fermat's little theorem:

a2sd≡1(modn).

Each term of the sequence a2sd,a2s−1d,…,a2d,ad is a square root of the previous term. Since the first term is congruent to 1, the second term is a square root of 1 modulo n. By the previous lemma, it is congruent to either 1 or −1 modulo n. If it is congruent to −1, we are done. Otherwise, it is congruent to 1 and we can iterate the reasoning. At the end, either one of the terms is congruent to −1, or all of them are congruent to 1, and in particular the last term, ad, is.

Sign in to keep reading

5 more chapters are free with a Lably account.

More in Computing and communication