Primality Tests
Given an integer , how can we tell if is prime? The most obvious way is to look for factors of , but no efficient factoring algorithm is known.
From now let us assume is odd, since deciding the primality of an even number is trivial.
By Fermat's Theorem, if is prime, then for any we have . This suggests the Fermat test for a prime: pick a random and see if . If not, then must be composite.
However we may still get equality even when is not prime. For example, take . By the Chinese Remainder Theorem
thus each corresponds to some
By Fermat's Theorem, , , and . Since 2, 10, and 16 all divide 560, this means , in other words, for any .
Thus no matter what we pick, always passes the Fermat test despite being composite so long as is coprime to . Such numbers are called Carmichael numbers, and it turns out there are infinitely many of them.
Note if is not coprime to then the Fermat test fails. But if this happened often for large , then we could easily recover a factor by computing and we have have an efficient factoring algorithm!
The Miller-Rabin Test
We can improve things by using the fact that there are no nontrival square roots of unity modulo a prime.
One promising idea is to first check , then check that , because is a square root of 1.
Unfortunately, this is still not sufficient, for example the third Carmichael number still passes. To defeat numbers like this, we iterate this idea as follows.
Let be the largest power of 2 dividing , thus we have for some odd number . Consider the sequence
We have set this sequence up so that each member of the sequence is a square root of the preceding member.
Then if is prime, by Fermat's theorem this sequence must start with 1. Also from our notes on polynomials, when is prime, the only square roots of are . Hence either every element of the sequence is 1, or the first member of the sequence not equal to must be .
The Miller-Rabin test works by picking a random then checking that the above sequence has the correct form. If the sequence does not begin with , or the first member of the sequence that is not is also not then is not prime.
It turns out that for any composite (including Carmichael numbers), the probability passes the Miller-Rabin test is at most . On average it is significantly less. Thus the probability passes several runs decreases exponentially.
If fails the Miller-Rabin test with a sequence starting with 1, then we have a nontrivial square root of modulo . Again from our work on polynomials, this means we can factor .
When run on numbers of the form where are random primes of a certain size, the Miller-Rabin test fails almost always because the sequence does not start with 1. Thus we cannot break RSA in this fashion. However, note that Carmichael numbers are always easy to factor since the sequence proving the compositeness starts with 1.
In practice, we implement the Miller-Rabin test as follows:
Given , pick a random and find so that for some odd . If then passes (and exit). For see if . If so, passes (and exit). Otherwise is composite.
Also one usually performs a few trial divisions by small primes before running the Miller-Rabin test.
Strictly speaking, these tests are compositeness tests since they do not prove the input is prime. They only ever prove that an input is composite.
There exist deterministic polynomial-time algorithms for deciding whether a number is prime or not (see Agrawal, Kayal and Saxena), though at present they are impractical as probabilistic tests are much faster.