Primality tests

One way to prove that a given number mm is prime is to find one of its primitive roots.

Choose a random aa between 22 and m2m-2.

If gcd(a.m)1gcd(a.m) \ne 1, then mm is not prime. Better yet, the greatest common divisor allows us to partially factor mm.

By Fermat's little theorem, we know that am11modma^{m-1} \equiv 1 mod m. If this is not so, then definitely mm is not prime.

Furthermore, when mm is an odd number, we must have either a(m1)/21modma^{(m-1)/2} \equiv 1 mod m or a(m1)/21modma^{(m-1)/2} \equiv -1modm.

Now, it can be shown that aa is a primitive root modulo mm if, for every prime divisor dd of m1m-1, we have a(m1)/dmodm1a^{(m-1)/d} mod m \ne 1. If aa satisfies these conditions then the order of aa modulo mm must be m1m-1, and thus mm must be prime.

If not, try another aa.

These exist composite numbers, called Carmichael numbers, for which am1modm=1a^{m-1}mod m = 1 for all aa which are relatively prime to mm. For these numbes, λ(m)(m1)\lambda(m) \mid (m-1).

The Miller-Rabin primality test

Goal: to test if the odd number nn, with n>3n \gt 3, is a prime number or not.

Result of the test: either nn is not prime or it may be prime (a probable primer number); in the second case, the probability that the test fails to identify a composite number is at most 0.25.

How it is done (to increase confidence on the result, do steps 2 to 6 several times):

  1. Let n1n-1 be written as n1=2rdn-1 = 2^rd, with dd an odd number (so rr is as large as possible).

  2. Select at random an integer aa uniformly distributed in the interval 2an22\le a \le n-2.

  3. If gcd(a,n)1gcd(a,n) \ne 1, then nn is definitely a composite number.

  4. Compute x0=admodnx_0 = a^d mod n. If x0=1x_0 = 1 or x0=n1x_0 = n -1 then nn is a probale prime.

  5. Otherwise, for k=1,2,...,d1k = 1, 2, ..., d-1, compute xk=xk12modnx_k = x²_{k-1} mod n. If xk=n1x_k = n - 1 then nn is a probable prime.

  6. Finally, if we get here, say that nn is definitely a composite number (because Fermat's little theorem failed because a(m1)/2±1modma^{(m-1)/2} \equiv \pm 1 modm).

The composite numbers that pass this test (meaning that the algorithm above says that they are probable primes) for a given aa are called base-aa strong pseudo-primes.

Last updated