Primality: Miller-Rabin
17 Dec 2021
§ Motivation
The existence of Carmichael numbers makes it difficult to create a primality testing algorithm based on Fermat tests.
§ The Miller-Rabin Algorithm
The Miller-Rabin algorithm is a non-deterministic test for compositeness; i.e. if Miller-Rabin fails on a number then we say that is a probable prime. While non-deterministic, the probablity of failure is given by where is the number of rounds of Miller-Rabin done, which means the failure probablity approaches zero very quickly.
There are Carmichael numbers that are strongly pseudoprime to a multitude of bases, but the occurrence of such numbers are extremely rare.
Let be an odd prime and let . Let be values that satisfy , and let be any integer not divisible by .
Then, one of the following conditions are true
- .
- One of is congruent to modulo .
We know that by Fermat's. Hence, . If this value is , then is also and so on. If holds for all non-negative strictly less than , then the first statement holds. Otherwise, there must exist an such that the second statement holds.
Miller-Rabin Test
Suppose we want to factor an odd composite integer .
- Find with odd such that .
- Choose a random integer .
- Compute . If , stop. The test fails, is a probable prime to .
- Let .
- If the value is , stop. is a witness to the compositeness of .
- If the value is , stop. is a probable prime to base .
- Repeat step 4 until .
- If the test has not terminated by this point, then is composite.