Module 8 : Primality Testing

Lecture 2 : Fermat Primality Test

 

Fermat Primality Test: Let n >1 be an integer. Choose a random integer a with 1 < a < n-1.
If a n-1 1(mod n) then n is composite. If an-1 1 (mod n), then n is probably prime.

 If we are careful about how we do this successive squaring, the Fermat test can be combined with the basic principle to yield the following stronger result.

Miller- Rabin Primality test:

Let n >1 be an odd integer. Write a-1 =2km with m odd. Choose a random integer a with 1< a< n-1.  Compute b0 ≡ am (mod n). If b0 ≡ ±1 (mod n), then stop and declare that n is probably prime. Otherwise, let b1 ≡ b02 (mod n). If b1 1 (mod n), then n is composite (and gcd (b0 -1,n) gives a nontrivial factor of n ). If b1 ≡ -1 (mod n), then stop and declare that n is probably prime. Otherwise, let b2 ≡ b12 (mod n). If b21 (mod n), then n is composite. If b2 ≡ -1 (mod n), then stop and declare that n is probably prime. Continue in this way until stopping and reaching  bk-1. If bk-1 -1 (mod n), then n is composite.

The reason why the test works is- suppose, for example that b31 (mod n). This means that b221 (mod n). This means that b2212 (mod n). Apply the basic principle from before. Either b2±1 (mod n), or b2≢ ±1 (mod n) and n is composite. In the latter case, gcd (b2-1, n) give a nontrivial factor of n. In the former case, the algorithm would have stopped by the previous step.

MILLER-RABIN (n, s)

  1. For j← 1 to s
  2.      do a ← RANDOM(1,n-1)
  3.            If WITNESS (a,n)
  4.                 then return COMPOSITE
  5. return PRIME

WITNESS (a, n)