This post is about the Miller-Rabin primality test.
Fermat Primality Test
The calculation behind the Miller-Rabin primality test is closely related to that of the Fermat primality test. It is instructive to recall the calculation of the Fermat test. The Fermat test is based on the modular exponentiation modulo , where is the odd positive integer being tested and is a positive integer that is relatively prime to . According to the Fermat Little Theorem, if is prime, then modulo . This fact is the basis of the Fermat primality test. If modulo , this provides evidence that may be prime since this is a property that is shared by all prime numbers. Thus, any integer is called a probable prime to base if modulo . If is a probable prime to base and if is composite, then is said to be a pseudoprime to base . Furthermore, if is not a probable prime to base , i.e., modulo , then the base is said to be a Fermat witness for (the compositeness of) .
This is how the Fermat test is run. Given a positive integer whose “prime or composite” status is not known, choose several bases that are relatively prime to at random. Compute modulo for the chosen bases . If for one base , modulo , we have conclusive proof that is composite. If modulo for all the chosen bases , we have very strong evidence for the primality of . The Fermat test is discussed in the two preceding posts (here and here).
The Miller-Rabin Calculation
The Miller-Rabin test rests on the following idea.
Lemma 1 (Roots of Unity Modulo a Prime)
Let be a prime number. Suppose that modulo . Then modulo or modulo .
Clearly, both and modulo are solutions to the congruence equation modulo . These two solutions are the trivial solutions to the equation. We claim that there is no other solution (no non-trivial solution) when the modulus is prime. To this end, we show that if modulo , then or modulo . Let be such that modulo . Then divides . Since is prime, either divides or divides (due to Euclid’s Lemma). If divides , then modulo . If divides , then modulo .
The calculation for the Miller-Rabin test is the same modular exponentiation but built up in several steps. Let be an odd positive integer. Express where and is odd. Let be a base relatively prime to . Instead of computing the exponentiation directly, we compute and then square times to obtain . In other words, we calculate the terms in the following sequence modulo .
-
(1)…………….
The first term in the sequence is modulo . Each subsequent term is the square of the preceding term. This is done until we obtain . For all odd primes , the above sequence follows a certain pattern, which is described in the following theorem.
Theorem 2
Let be an odd prime number. Let be a positive integer that is relatively prime to . Then either one of the following holds.
- modulo .……………………………………………………………….(a)
- modulo for some with .………………….(b)
The first bullet point says that the first term in the sequence (1) is congruent to 1 modulo . The second bullet point says that the term immediately preceding the first 1 in the sequence is congruent to -1 modulo .
The proof of Theorem 2 relies on Lemma 1. The proof goes something like this. First, the last term in the sequence must be a 1 due to the Fermat Little Theorem. If modulo , then the entire sequence consists of 1’s. Then (a) is satisfied. Suppose the sequence is not entirely 1’s. Then the term that is the first 1 must have a preceding term. Then the square of the preceding term is congruent to 1 modulo . Since is prime, that preceding term can only be 1 or -1. However, the preceding term can only be a -1 since it precedes the term that is the first value of 1.
Two Pieces of Information About Primality
Theorem 2 describes a property possessed by all old primes. Whenever an odd positive integer satisfies this property with a certain base , i.e., the sequence (1) satisfied either (a) or (b), we consider that as evidence that may be a prime. Hence, we have this definition. Suppose is an odd positive integer with the “prime or composite” status not known. Let be a base that is relatively prime to . Compute the sequence (1). If either condition (a) or condition (b) holds, then is said to be a strong probable prime to base .
The underlying calculation in the sequence (1) attempts to capture two pieces of information about primality. The first piece is based on the Fermat Little Theorem (whether is congruent to 1). The second piece of information is the roots of unity modulo a prime (Lemma 1). If the Miller-Rabin calculation indicates a probable prime, the evidence is stronger than that from the Fermat test. Two pieces of evidence is more corroborative than just one piece of evidence.
The Millar-Rabin test will be more capable to differentiate composite numbers. This is because there are two ways to detect compositeness embedded in the calculation of the sequence (1) described above. The compositeness of a number can be revealed by the Fermat Little theorem (whether is congruent to 1 or not) and by whether there exists a non-trivial square root of 1 modulo . This is demonstrated in the examples in the next section.
Examples of the Millar-Rabin Calculation
In this section, we demonstrate how the Miller-Rabin calculation can detect the compositeness of Carmichael numbers, which the Fermat test fails to do.
Example 1
Consider 29,341. This is a composite number with 29,341 = 13 x 37 x 61. This is a Carmichael number. Thus, modulo 29341 for any coprime base . As a result, the Fermat test will not be able to detect the compositeness of this and other Carmichael numbers. Note that . We first perform base 2 calculation, which produces the sequence . The resulting sequence is:
-
26424, 29340, 1
The last term is 1 (due to 29341 being a Carmichael number). The term preceding the last term is 29340, which is congruence to -1. Thus, condition (b) is satisfied. This means that 29341 is a strong probable prime to base 2. Let’s try base 3 calculation, which is the sequence . The resulting sequence is:
-
22569, 1, 1
The term preceding the first 1 is 22569, which is neither 1 nor -1. This violates conditions (a) and (b) described above. This gives a conclusive proof that 29341 is a composite number. The number 22569 is a non-trivial square root of 1 modulo 29341. The existence of the non-trivial square root shows that the modulus 29341 cannot be prime.
Example 2
Consider 172,947,529. This is a Carmichael number with 172,947,529 =307 x 613 x 919. First, we have where 21,618,441. Let’s try the base 2 calculation, producing the sequence , resulting in:
-
40063806, 2257065, 1, 1
Note that the term preceding the first 1 is 2,257,065, which is neither 1 nor -1. This means that the conclusion of Theorem 2 is not satisfied. As a result, the Carmichael number 172,947,529 is shown to be composite.
The two examples demonstrate that the Miller-Rabin works correctly where the Fermat test fails. Referring back to the point about the two pieces of evidence for primality, the first way of detecting compositeness does not work in both Example 1 and Example 2, i.e., the congruence is 1. However, the second piece of evidence enables the Miller-Rabin calculation to detect the compositeness. Two pieces of evidence is better than just one. Therefore, the Miller-Rabin test is superior than the Fermat test.
For the Miller-Rabin calculation to work correctly, we need to find the right bases to use. In Example 1, using base 2 does not detect the compositeness. Luckily, trying base 3 works. How likely is it to find a base that will detect a composite number? If a number is composite, the calculation described in Theorem 2 will detect the compositeness of with at least 75% of the bases coprime to . See Theorem 3 below.
The Implementation
To implement the Miller-Rabin test, randomly select several bases that are relatively prime to , the number to be tested. For each chosen base , compute the sequence (1).
- If, for one chosen base , both conditions (a) and (b) in Theorem 2 do not hold, then is proven to be composite.
- If, for all chosen bases , one of the conditions (a) and (b) holds, then is a strong probable prime to all the chosen bases, which indicates that we have obtained strong evidence for primality and is likely a prime number.
Strong Pseudoprime and Miller-Rabin Witness
Additional terminology will help the discussion. For a given base , if neither condition (a) nor condition (b), as described in Theorem 2, holds, then is proven to be composite, in which case, the base is said to be a Miller-Rabin witness for (the compositeness of) . For simplicity, we use the term witness for Miller-Rabin witness in the remainder of this article. If is a strong probable prime to base and if is composite, then is said to be a strong pseudoprime to base .
Between the Fermat test and the Miller-Rabin test, there are two notions of probable primes, two notions of pseudoprimes and two notions of witnesses. How do they relate? We go back to the above section on the two pieces of evidence for primality. A probable prime under the Miller-Rabin test must also be a probable prime under the Fermat since having two pieces of evidence is stronger than one piece of evidence. Therefore, we have the following implications.
-
Strong probable prime to base Probable prime to base
-
Strong pseudoprime to base Pseudoprime to base
The above implications cannot be reversed. The number in Example 1 is a Carmichael number, hence is a pseudoprime to base 3. However, it is not a strong pseudoprime to base 3. In general, a number can be a weaker form of pseudoprime and not be a stronger form of pseudoprime.
- Strong pseudoprime to base Pseudoprime to base
How do the two notions of witnesses relate? We have the following implications.
-
Fermat witness Miller-Rabin witness
Fermat witness Miller-Rabin witness
A Fermat witness violates one piece of evidence of primality, namely that from the Fermat Little Theorem. It means that it would be a witness from the point of view of the Miller-Rabin test. On the other hand, a Miller-Rabin witness does not have to be a Fermat witness. It is possible that a Miller-Rabin witness is to violate the second piece of evidence by producing a non-trivial square root of 1 while satisfying the Fermat Little Theorem. In Example 1, 3 is a (Miller-Rabin) witness for the integer 29,341 and 3 is not a Fermat witness for 29,341. In Example 2, the base 2 is a witness for 172,947,529, while base 2 is not a Fermat witness. It is because in both examples, the Miller-Rabin calculation produces a non-trivial square root of unity.
Performance
The above discussion and examples indicate that the Miller-Rabin test is superior than the Fermat test in that it can detect the Carmichael numbers as composite. The advantage of the Miller-Rabin test is real. For any composite number , there is at least one base that is a witness for , even when is a Carmichael number (as shown in the above examples). In fact, if is composite, at least 75% of the possible choices for bases are (Miller-Rabin) witnesses for . Consider the following theorem.
Theorem 3
Let be an odd composite number. Then at least 75% of the numbers with are (Miller-Rabin) witnesses for . In other words, the composite number is a strong pseudoprime to at most 25% of the possible bases .
With Theorem 3, the Miller-Rabin test can be made a truly probabilistic test for primality. This means that the test can be implemented in such a way that the error probability can be made as small as we wish. When we randomly select a base out of all the bases coprime with the composite , there is at least a 0.75 probability that is a witness, which means that the probability of error is at most 0.25. If we choose 50 or so bases at random and if the test indicates prime, the error probability is practically zero. Thus, the Miller-Rabin test is an efficient and practical test for finding large numbers that are prime with high probability.
Dan Ma Fermat primality test
Dan Ma Miller-Rabin primality test
Daniel Ma Fermat primality test
Daniel Ma Miller-Rabin primality test
Dan Ma Carmichael numbers
Daniel Ma Carmichael numbers
2022 – Dan Ma