 is prime, then the statement follows from 
Proposition 2.1.21.  If
 is prime, then the statement follows from 
Proposition 2.1.21.  If  is composite,
then there is a divisor
 is composite,
then there is a divisor  of
 of  with
 with 
 .
If
.
If 
 , then
, then 
 .
Since
.
Since  ,  we have
,  we have 
 hence 
there exists an integer
 hence 
there exists an integer  such that
 such that 
 .
Subtracting we see that
.
Subtracting we see that 
 , so
, so 
 .
This implies that
.
This implies that  , which is 
a contradiction since
, which is 
a contradiction since  .
.
  
Suppose 
 .  Using Theorem 2.4.1 and
Algorithm 2.3.13, we can either quickly prove that
.  Using Theorem 2.4.1 and
Algorithm 2.3.13, we can either quickly prove that  is not
prime, or convince ourselves that
 is not
prime, or convince ourselves that  is likely prime (but not quickly
prove that
 is likely prime (but not quickly
prove that  is prime).  For example, if
 is prime).  For example, if 
 , then we have proved that
, then we have proved that  is not prime.  On the other
hand, if
 is not prime.  On the other
hand, if 
 for a few
 for a few  , it ``seems likely''
that
, it ``seems likely''
that  is prime, and we loosely refer to such a number that seems
prime for several bases as a pseudoprime.
 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
 (called Carmichael numbers) 
with the amazing property that
 for all
 for all  with
 with 
 .  The
first Carmichael number is
.  The
first Carmichael number is  , and it is a theorem that there
are infinitely many such numbers ([#!carmichael!#]).
, and it is a theorem that there
are infinitely many such numbers ([#!carmichael!#]).
 prime?
We compute
 prime?
We compute 
 .  
Making a table as above, we have
.  
Making a table as above, we have
|   |   |   |  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
 is not prime, though this computation gives no information
about how  factors as a product of primes. 
In fact, one finds that
 factors as a product of primes. 
In fact, one finds that 
 .
.
 
then
 , so
, so  is composite.
 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).
 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
 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
 times on  and in
each case claims that
 and in
each case claims that  is probably prime, then 
one can in a precise sense bound the probability 
that
 is probably prime, then 
one can in a precise sense bound the probability 
that  is composite in terms of
 is composite in terms of  .
.
We state the Miller-Rabin algorithm precisely, but do not prove anything about the probability that it will succeed.
 this algorithm 
outputs either true or false.  If it outputs
true, then
 this algorithm 
outputs either true or false.  If it outputs
true, then  is ``probably prime'', and if
it outputs false, then
 is ``probably prime'', and if
it outputs false, then  is definitely composite.
 is definitely composite.
 ] 
Compute the unique integers
] 
Compute the unique integers  and
 and  such that
 such that  is
odd  and
 is
odd  and 
 .
.
 with
 with  .
.
 .
If
.
If 
 output true and terminate.
 
output true and terminate.
 for any
 for any
 with
 with 
 , output true and terminate.
Otherwise output false.
, output true and terminate.
Otherwise output false.
 , we can call it again
with
, we can call it again
with  and if it again outputs true then the probability
that
 and if it again outputs true then the probability
that  is prime increases.
 is prime increases.   
 composite, then
 composite, then  really is composite.  Thus
  suppose
 really is composite.  Thus
  suppose  is prime, yet the algorithm pronounces
 is prime, yet the algorithm pronounces  composite.
  Then
 composite.
  Then 
 , and for all
, and for all  with
 with 
 we have
 we have 
 .  Since
.  Since  is prime
  and
 is prime
  and 
 , Proposition 4.2.1 implies that
, Proposition 4.2.1 implies that
  
 , so by our hypothesis
, so by our hypothesis
  
 .  But then
.  But then 
 , so by Proposition 2.5.3 (which is proved
  below, and whose proof does not depend on this argument), we have
, so by Proposition 2.5.3 (which is proved
  below, and whose proof does not depend on this argument), we have
  
 .  Again, by our hypothesis, this
  implies
.  Again, by our hypothesis, this
  implies 
 .  Repeating this argument
  inductively we see that
.  Repeating this argument
  inductively we see that 
 , which contradicts
  our hypothesis on
, which contradicts
  our hypothesis on  .
.
  
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:
 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