Suppose . Using Theorem 2.4.1 and Algorithm 2.3.13, we can either quickly prove that is not prime, or convince ourselves that is likely prime (but not quickly prove that is prime). For example, if , then we have proved that is not prime. On the other hand, if for a few , it ``seems likely'' that is prime, and we loosely refer to such a number that seems prime for several bases as a pseudoprime.
There are composite numbers (called Carmichael numbers) with the amazing property that for all with . The first Carmichael number is , and it is a theorem that there are infinitely many such numbers ([#!carmichael!#]).
mod 323 | |||
0 | 322 | 0 | 2 |
1 | 161 | 1 | 4 |
2 | 80 | 0 | 16 |
3 | 40 | 0 | 256 |
4 | 20 | 0 | 290 |
5 | 10 | 0 | 120 |
6 | 5 | 1 | 188 |
7 | 2 | 0 | 137 |
8 | 1 | 1 | 35 |
so is not prime, though this computation gives no information about how factors as a product of primes. In fact, one finds that .
then , so is composite.
sage: n = 95468093486093450983409583409850934850938459083 sage: Mod(2,n)^(n-1) 34173444139265553870830266378598407069248687241Note that factoring actually takes much longer than the above computation (which was essentially instant).
sage: factor(n) # takes up to a few seconds. 1610302526747 * 59285812386415488446397191791023889
Another practical primality test is the Miller-Rabin test, which has the property that each time it is run on a number it either correctly asserts that the number is definitely not prime, or that it is probably prime, and the probability of correctness goes up with each successive call. If Miller-Rabin is called times on and in each case claims that is probably prime, then one can in a precise sense bound the probability that is composite in terms of .
We state the Miller-Rabin algorithm precisely, but do not prove anything about the probability that it will succeed.
Until recently it was an open problem to give an algorithm (with proof) that decides whether or not any integer is prime in time bounded by a polynomial in the number of digits of the integer. Agrawal, Kayal, and Saxena recently found the first polynomial-time primality test (see [#!agrawal:primes!#]). We will not discuss their algorithm further, because for our applications to cryptography Miller-Rabin or pseudoprimality tests will be sufficient.
sage: is_prime(95468093486093450983409583409850934850938459083) FalseWe use the is_prime function to make a small table of the first few Mersenne primes (see Section 1.2.3).
sage: for p in primes(100): ... if is_prime(2^p - 1): ... print p, 2^p - 1 2 3 3 7 5 31 7 127 13 8191 17 131071 19 524287 31 2147483647 61 2305843009213693951 89 618970019642690137449562111There is a specialized tests for primality of Mersenne numbers called the Lucas-Lehmer test. This remarkably simple algorithm determines provably correctly whether or not a number is prime. We implement it in a few lines of code and use the Lucas-Lehmer test to check for primality of two Mersenne numbers:
sage: def is_prime_lucas_lehmer(p): ... s = Mod(4, 2^p - 1) ... for i in range(3, p+1): ... s = s^2 - 2 ... return s == 0 sage: is_prime_lucas_lehmer(next_prime(1000)) False sage: is_prime_lucas_lehmer(9941) TrueFor more on searching for Mersenne primes, see the Great Internet Mersenne Prime Search (GIMPS) project at http://www.mersenne.org/.
William 2007-06-01