Goal
Given a number , decide whether is prime, i.e., whether it has no divisor in .
Application: Generating large random prime numbers for RSA cryptography (e.g. 1000 bits).
Prime Number Theorem:
Naive algorithm
For all , test whether divides .
This is too slow for , since then .
We want an algorithm whose running time is polynomial in (the input size of ).
Euclid Primality Test
Test
- Choose uniformly at random.
- If , return “prime”. (
gcd(n,m)can be calculated in ) - Otherwise, return “not prime”.
- If is prime: the output is always correct.
- If is not prime: the incorrect output “prime” occurs with probability (see Euler’s Totient Function)
Fermat Primality Test
Test
- Choose uniformly at random.
- If then return “prime”. (see Little Theorem of Fermat)
- Otherwise, return “not prime”.
-
If is prime: the output is always correct.
-
If is not prime: the incorrect output “prime” occurs with probability unless is a Carmichael number.
- If (i.e. is not a Carmichael number), then is a proper divisor of , hence
By repeating the test times (in the case the result is “prime”), we can reduce the error probability to
Pseudoprime base
We make an error if is not prime and
Such an is called a pseudoprime base of . Define
Equivalently,
The elements of are called pseudoprime bases of .
NOTE
is a subgroup of because: If then
Carmichael number
We say that is a Carmichael number if
- is not prime, and
Smallest examples:
The Miller-Rabin Primality Test
The Miller-Rabin test is a stronger randomized variant of the Fermat primality test. It also tests whether behaves like a prime with respect to modular exponentiation, but it additionally checks the intermediate squarings, so the Carmichael-number problem of the Fermat test no longer occurs.
Write
with odd. ( is always odd)
Test
- Choose uniformly at random.
- Compute .
- If or , return “prime”.
- Repeat times:
- Set .
- If , return “prime”.
- Return “not prime”.
- If is prime: the output is always correct.
- If is not prime: the incorrect output “prime” occurs with probability at most .
By repeating the test times (in the case the result is “prime”), we can reduce the error probability to at most
Proof
Assume that is prime and write
with odd.
Fix a base and define
Then
by Fermat’s little theorem.
If , then the test already returns “prime” in step 3. So assume . Since , there is a smallest index such that
Then
Because is a field, the congruence
has only the two solutions
Hence . By minimality of , we cannot have , so necessarily
Therefore one of the following always happens for a prime :
- , or
- for some we have .
These are exactly the cases in which the algorithm returns “prime”. So every prime passes the Miller-Rabin test.
NOTE
If is composite and the test still returns “prime”, then is called a strong pseudoprime base (or strong liar) for . A standard theorem of Rabin and Monier says that for every odd composite , at most one quarter of the possible bases are strong liars. This gives the error bound