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

  1. Choose uniformly at random.
  2. If , return “prime”. (gcd(n,m) can be calculated in )
  3. 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

  1. Choose uniformly at random.
  2. If then return “prime”. (see Little Theorem of Fermat)
  3. 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

  1. Choose uniformly at random.
  2. Compute .
  3. If or , return “prime”.
  4. Repeat times:
    1. Set .
    2. If , return “prime”.
  5. 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