It Is Easy To Determine Whether A Given Integer Is Prime
It Is Easy To Determine Whether A Given Integer Is Prime
It Is Easy To Determine Whether A Given Integer Is Prime
There are few better known or more easily understood problems in pure mathematics than the question of rapidly determining whether a given integer is prime.
As we read above, the young Gauss in his rst book Disquisitiones Arithmeticae
regarded this as a problem that needs to be explored for the dignity of our subject. However it was not until the modern era, when questions about primality
testing and factoring became a central part of applied mathematics,1 that there
was a large group of researchers endeavoring to solve these questions. As we shall
see, most of the key ideas in recent work can be traced back to Gauss, Fermat and
other mathematicians from times long gone by, and yet there is also a modern spin:
With the growth of computer science and a need to understand the true diculty
of a computation, Gausss vague assessment intolerable labor was only recently
Received by the editors January 27, 2004, and, in revised form, August 19, 2004.
2000 Mathematics Subject Classication. Primary 11A51, 11Y11; Secondary 11A07, 11A41,
11B50, 11N25, 11T06.
Lauteur est partiellement soutenu par une bourse du Conseil de recherches en sciences naturelles et en genie du Canada.
1
Because of their use in the data encryption employed by public key cryptographic schemes;
see section 3a.
c
2004
American Mathematical Society
Reverts to public domain 28 years from publication
ANDREW GRANVILLE
claried by running time estimates, and of course our desktop computers are
todays indefatigable calculators.
Fast factoring remains a dicult problem. Although we can now factor an arbitrary large integer far faster than in Gausss day, still a 400 digit integer which is
the product of two 200 digit primes is typically beyond practical reach.2 This is just
as well since the safety of electronic business transactions, such as when you use
your ATM card or purchase something with your credit card over the Web depends
on the intractability of such problems!
On the other hand we have been able to rapidly determine whether quite large
numbers are prime for some time now. For instance recent algorithms can test an
arbitrary integer with several thousand digits for primality within a month on a
Pentium IV,3 which allows us to easily create the cryptographic schemes referred to
above. However the modern interpretation of Gausss dream was not realized until
August 2002, when three Indian computer scientistsManindra Agrawal, Neeraj
Kayal and Nitin Saxenaconstructed a polynomial time deterministic primality
test, a much sought-after but elusive goal of researchers in the algorithmic number
theory world. Most shocking was the simplicity and originality of their test ...
whereas the experts had made complicated modications on existing tests to
gain improvements (often involving great ingenuity), these authors rethought the
direction in which to push the usual ideas with stunning success.4 Their algorithm
is based on the following elegant characterization of prime numbers.
Agrawal, Kayal and Saxena. For given integer n 2, let r be a positive integer
< n, for which n has order > (log n)2 modulo r. Then n is prime if and only if
n is not a perfect power,
n does not have any prime factor r,
My denition will stretch the usual use of the word practical by erring on the cautious
side: I mean a calculation that can be done using all computers that have or will be built for the
next century, assuming that improvements in technology do not happen much faster than we have
seen in the last couple of decades (during which time computer technology has, by any standards,
developed spectacularly rapidly).
3
This is not to be confused with algorithms that test the primality of integers of a special
shape. For example, the GIMPS project (the Great Internet Mersenne Prime Search) routinely
tests Mersenne numbers, numbers of the form 2p 1, for primality which have millions of digits.
However these algorithms have very limited applicability.
4
Though some experts had actually developed many of the same ideas; see section 6.5 for
example.
like n in the worst case (when n is prime). Note that n is roughly 2d/2 where d
is the number of digits of n when written in binary (and d is roughly log n where,
here and throughout, we will take logarithms in base 2).
The objective in this area has been to come up with an algorithm which works
in no more than cdA steps in the worst case, where c and A are some xed positive constants, that is, an algorithm which works in Polynomial Time (which is
often abbreviated as P). With such an algorithm one expects that one can rapidly
determine whether any reasonably sized integer is prime. This is the modern
interpretation of Gausss dream.
Before the work of Agrawal, Kayal and Saxena the fastest that we could prove
any primality testing algorithm worked was something like dc log log d steps,5 for
some constant c > 0. At rst sight this might seem far away from a polynomial
time algorithm, since log log d goes to innity as d . However log log d goes to
innity with great dignity (as Dan Shanks put it), and in fact never gets larger
than 7 in practice!6 By January 2004 such algorithms were able7 to prove the
primality of 10,000 digit primes (in base 10), an extraordinary achievement.
The algorithm of Agrawal, Kayal and Saxena works in about d7.5 steps;8 and
a modication by Lenstra and Pomerance in about d6 steps. This realizes Gausss
5Though it is believed, but unproven, that some of these tests always work in polynomial time.
6By this I mean that if all computers on earth were to focus simply on writing down as many
7
digits as possible for the next century, they would write down far less than 22 digits.
7
Using a version of the Elliptic Curve Primality Proving routine of Francois Morain and his
collaborators.
8And thus the legend Seven and a half logs should do it! on the Larry Gonick cartoon above.
Printed with permission.
ANDREW GRANVILLE
dream, or, in modern language, implies that Primes are in P. No one has yet
written a computer program implementing these algorithms which comes close to
proving primality of primes as large as those 10,000 digit primes discussed above.
Building on a clever idea of Berrizbeitia, Bernstein (and, independently, Mihailescu and Avanzi) gave a modication that will almost certainly run in around
d4 bit operations. There is hope that this can be used to write a computer program
which will prove the primality of 10,000 digit primes rapidly. This is a challenge
for the near future.
1.2. This article is an elaboration of a lecture given at the Current Events special session during the 2004 annual meeting of the American Mathematical Society.
The purpose is to explain the AKS9 primality test, with complete proofs, and to
put the result and ideas in appropriate historical context.
To start with I would like to discuss simpler ideas from the subject of primality testing, focusing on some that are closely related to the AKS algorithm. In
the process we will discuss the notions of complexity classes from theoretical computer science10 and in particular introduce the P=NP problem, one of the great
challenges of mathematics for the new millennium.
We will see that most of the key ideas used to prove the theorem above were
already in broad circulation, and so it is surprising that such an approach was not
successfully completed earlier. I believe that there were two reasons for this
rst, the way in which these classical ideas were combined was clever and original
in several aspects. Second the authors are not number theorists and came at it
from a little bit of a dierent angle; in particular not being so aware of what was
supposedly too dicult, they trod where number theorists fear to tread.
In the third section we will discuss running time of algorithms in some detail
and how they are determined, and so analyze the AKS algorithm.
In the fourth, and perhaps most interesting, section, we give the proof of the
main results. In fact Agrawal et al. have produced two manuscripts, the second
giving an even easier proof than the rst, and we shall discuss both these proofs
and relevant background information.
To prove the best running times for the algorithm it is necessary to employ tools
of analytic number theory. In section 5 we introduce the reader to some beautiful
theorems about the distribution of primes that should be better known and use
them to prove the claimed running times.
In section 6, we discuss the modied AKS algorithm of Berrizbeitia and Bernstein, as well as Lenstras nite eld primality test,11 and then, in section 7, the
AKS-inspired algorithm of Lenstra and Pomerance.
Since the rst announcement of this result in August 2002 there have been more
than a dozen preprints circulating containing interesting ideas concerning the AKS
algorithm, though none have yet appeared in print. I have thus succumbed to the
temptation to include several of these ideas in the nal section, in part because
they are quite accessible, and in part because they are too elegant to leave out.
9An abbreviation for Agrawal, Kayal and Saxena.
10The cost of conveying the essence, rather than the details, of these notions is that our
denitions will be a little awry, but not in a way that eects the key considerations in our context.
11Since this twenty-year-old test has much in common with the AKS test and has a running
time that is not far from polynomial.
Bernstein reckons that these and other ideas for improving the AKS algorithm
result in a speed up by a factor of about two million, although, he cautions, two
million times faster is not necessarily fast.
1.3. Undergraduate research experiences. Manindra Agrawal is a faculty
member in the Computer Science Department at the Indian Institute of Technology in Kanpur, India. The fundamental approach taken here to primality testing
was developed by Agrawal in conjunction with two bachelors theses which we will
discuss in section 8.5, the rst completed by Pashant Pandey and Rajat Bhattacharjee in 2001, the second completed by Neeraj Kayal and Nitin Saxena in 2002.
Later that summer they developed what they had done into a rst version of the
characterization of primes given above. There can have been few undergraduate
research experiences with such a successful outcome!
2. Primality testing and the Childs Binomial Theorem
2.1. Recognizing primes. To nd a primality test that works faster than trial
division we look for other simple characterizations of prime numbers which might
be used in a more ecient algorithm. If you have studied a little number theory,
then a simple characterization of the primes that comes to mind is:
Wilsons Theorem (1770). Integer n 2 is prime if and only if n divides
(n 1)! + 1.
In trying to convert this elegant characterization into a fast algorithm we run into
the problem that there is no obvious way to compute (n 1)! rapidly (or even
(n 1)! (mod n)).
Another idea is to use Matijasevics unbelievable polynomial (1970) which was
essential in resolving Hilberts Tenth Problem. He showed how to construct a
polynomial with integer coecients (in several variables) such that whenever one
substitutes in integers for the variables and gets a positive value, then that value is a
prime number; moreover, every prime number will be such a value of the polynomial.
(In fact one can construct such a polynomial of degree 10 in 26 variables.) However
it is far from evident how to quickly determine whether a given integer is a value
taken by this polynomial (at least no one to date has found a nice way to do so),
and so this seems to be a hopeless approach.
Primes come up in many dierent places in the mathematical literature, and
some of these suggest ways to distinguish primes from composites. Those of us who
are interested in primality testing always look at anything new with one eye open
to this application, and yet nding a fast primality testing algorithm has remained
remarkably elusive. The advent of the AKS algorithm makes me wonder whether
we have missed some such algorithm, something that one could perform in a few
minutes, by hand, on any enormous number.
Such speculation brings me to a passage from Oliver Sacks The man who mistook
his wife for a hat, in which he tells us of a pair of severely autistic twins with a
phenomenal memory for numbers and a surprising aesthetic. Sacks discovered the
twins holding a purely numerical conversation, in which one would mention a sixdigit number, the other would listen, think for a moment and then beam a smile of
contented pleasure before responding with another six-digit number for his brother.
After listening for a while, Sacks wrote the numbers down and, following a hunch,
determined that all of the numbers exchanged were primes. The next day, armed
ANDREW GRANVILLE
with a table of primes, Sacks butted into their conversation, venturing an eightdigit prime and eliciting, after a short pause, enthusiastic smiles from the twins.
Now the twins kept on going, increasing the number of digits at each turn, until
they were trading (as far as Sacks could tell) twenty-digit prime numbers. So how
did the twins do it? Perhaps we will never know, since the twins were eventually
separated, became socialized and forgot their amazing algorithm!
Since we do not know of any such shortcuts, I wish to move on to a property
of prime numbers that has fascinated mathematicians since antiquity and has been
developed into several key approaches to primality testing.
2.2. The Childs Binomial Theorem. The binomial theorem gives a formula
for expanding (x + y)n as a sum of multiples of terms xi y ni , namely
n
n i ni
(2.1)
(x + y)n =
xy
i
i=0
n!
where ni = i!(ni)!
and m! = m(m 1)(m 2) . . . 3.2.1. This is one of the rst
signicant formulas that students learn in high school, though, perhaps due to
its complicated structure, all too often we nd university undergraduates who are
unable to recall this formula and indeed who write down the (generally) incorrect
(2.2)
(x + y)n = xn + y n .
This is called, by some, the Childs Binomial Theorem. Despite (2.2) being wrong
in general, we shall be interested in those (unlikely) circumstances in which this
formula is actually correct!
One of the most amazing properties of prime numbers, discovered12 by Fermat
around 1637, is that if n is prime, then n divides an a for all integers a. This may
be rewritten13 as
(2.3)
an a
mod n
(x + y)n x + y xn + y n
(mod n)
advantage of this notation is that we can do most regular arithmetic operations (mod m).
(mod 341),
10
ANDREW GRANVILLE
square roots (as often as 2 divides n 1), we call integer a a witness to n being
composite if the nite sequence
an1
(mod n)
11
can be veried in polynomial time, and we say that such problems are in class NP
(non-deterministic polynomial time).15 We are not suggesting that the proof can be
found in polynomial time, only that the proof can be checked in polynomial time;
indeed we have no idea whether it is possible to factor numbers in polynomial time,
and this is now the outstanding problem of this area.
What about primality testing? If someone gives you an integer and asserts that
it is prime, can you check that this is so in polynomial time? Can they give you
better evidence than their say-so that it is a prime number? Can they provide some
sort of certicate that gives you all the information you need to verify that the
number is indeed a prime? It is not, as far as I can see, obvious how to do so,
certainly not so obvious as with the factoring problem. It turns out that some old
remarks of Lucas from the 1870s can be modied for this purpose:
First note that n is prime if there are precisely n 1 integers a in the range
1 a n 1 which are coprime to n. Therefore if we can show the existence
of n 1 distinct values mod n which are coprime to n, then we have a proof that
n is prime. In fact if n is prime, then these values form a cyclic group under
multiplication and so have a generator g; that is, there exists an integer g for which
1, g, g 2, . . . , g n2 are all coprime to n and distinct mod n, so these are the n 1
distinct values mod n that we are looking for (note that g n1 1 (mod n) by
Fermats little theorem). Thus to show that n is prime we need simply exhibit g
and prove that g has order16 n 1 (mod n). It can be shown that any such order
must divide n1, and so one can show that if g is not a generator, then g (n1)/q 1
(mod n) for some prime q dividing n 1. Thus a certicate to show that n is
prime would consist of g and {q prime : q divides n 1}, and the checker would
need to verify that g n1 1 (mod n) whereas g (n1)/q 1 (mod n) for all primes
q dividing n 1, something that can be accomplished in polynomial time using fast
exponentiation.
There is a problem though: One needs certication that each such q is prime.
The solution is to iterate the above algorithm, and one can show that no more
than log n odd primes need to be certied prime in the process of proving that n
is prime. Thus we have a polynomial time certicate (short proof) that n is prime,
and so primality testing is in the class NP.
But isnt this the algorithm we seek? Doesnt this give a polynomial time algorithm for determining whether a given integer n is prime? The answer is no,
because along the way we would have had to factor n 1 quickly, something no one
knows how to do in general.
2.6. Is P=NP? The set of problems that are in the complexity class P are those
for which one can nd the solution, with proof, in polynomial time, while the set
of problems that are in the complexity class NP are those for which one can check
the proof of the solution in polynomial time. By denition PNP, and of course
we believe that there are problems, for example the factoring problem, which are
in NP but not in P; however this has not been proved, and it is now perhaps the
outstanding unresolved question of theoretical computer science. This is another
of the Clay Mathematics Institutes million dollar problems and perhaps the most
15Note that NP is not non-polynomial time, a common source of confusion. In fact it is
non-deterministic, because the method for discovering the proof is not necessarily determined.
16 The order of h (mod n) is the least positive integer k for which hk 1 (mod n).
12
ANDREW GRANVILLE
likely to be resolved by someone with less formal training, since the experts seem
to have few plausible ideas for attacking this question.
It had better be the case that P=NP, else there is little chance that one can have
safe public key cryptography (see section 3a) or that one could build a highly unpredictable (pseudo-)random number generator17 or that we could have any one of
several other necessary software tools for computers. Notice that one implication of
the P=NP question remaining unresolved is that no fast public key cryptographic
protocol is, as yet, provably safe!
2.7. Random polynomial time algorithms. In section 2.4 we saw that if n is
composite, then there is a probability of at least 1/2 that a random integer a is a
witness for the compositeness of n, and if so, then it provides a short certicate
verifying that n is composite. Such a test is called a random polynomial time test
for compositeness (denoted RP). As noted, if n is composite, then the randomized
witness test is almost certain to provide a short proof of that fact in 100 runs of the
test. If 100 runs of the test do not produce a witness, then we can be almost certain
that n is prime, but we cannot be absolutely certain since no proof is provided.
Short of nding a polynomial time test for primality, we might try to nd a
random polynomial time test for primality (in addition to the one we already have
for compositeness). This was achieved by Adleman and Huang in 1992 using a
method of counting points on elliptic and hyperelliptic curves over nite elds
(based on ideas of Goldwasser and Kilian). Although beautiful in structure, their
test is very complicated and almost certainly impractical, as well as being rather
dicult to justify theoretically in all its details. It does however provide a short
certicate verifying that a given prime is prime and proves that primality testing
is also in complexity class RP.
If this last test were practical, then you could program your computer to run
the witness test by day and the Adleman-Huang test by night and expect that you
would not only quickly distinguish whether given integer n is prime or composite,
but also rapidly obtain a proof of that fact. However you could not be certain that
this would workyou might after all be very unluckyso mathematicians would
still wish to nd a polynomial time test that would always work no matter how
unlucky you are!
2.8. The new work of Agrawal, Kayal and Saxena starts from an old beginning,
the Childs Binomial Theorem, in fact from the following result, which is a good
exercise for an elementary number theory course.
Theorem 1. Integer n is prime if and only if (x + 1)n xn + 1 (mod n) in Z[x].
Proof. Since (x + 1)n (xn + 1) = 1jn1 nj xj , we have that xn + 1 (x + 1)n
(mod n) if and only if n divides nj for all j in the range 1 j n 1.
If n = p is prime, then p appears in the numerator of pj but is larger than,
and so does not divide, any term in the denominator, and hence p divides pj for
1 j p 1.
17So-called random number generators written in computer software are not random since
they need to work on a computer where everything is designed to be determined! Thus what are
called random numbers are typically a sequence of numbers, determined in a totally predictable
manner but which appear to be random when subjected to randomness tests in which the tester
does not know how the sequence was generated.
13
(x + 1)n xn + 1
This can be computed rapidly (as we will discuss in section 3b.2), and it is true for
any prime n (as a consequence of the theorem above), but it is unclear whether this
fails to hold for all composite n and thus provides a true primality test. However,
the main theorem of Agrawal, Kayal and Saxena provides a modication of this
congruence, which can be shown to succeed for primes and fail for composites, thus
providing a polynomial time primality test. In section 4 we shall show that this is
so, but rst we discuss various computational issues.
3a. Computational issues: Factoring and primality testing as applied
to cryptography
In cryptography we seek to transmit a secret message m from Alice to Bob in
such a way that Oscar, who intercepts the transmission, cannot read the message.
The idea is to come up with an encryption key , an easily described mathematical
function, which transforms m into r := (m) for transmission. The number r
should be a seemingly meaningless jumble of symbols that Oscar cannot interpret
and yet Bob can decipher by computing (r), where = 1 . Up until recently,
knowledge of the encryption key would allow the astute Oscar to determine the
decryption key , and thus it was extremely important to keep the encryption key
secret, often a dicult task.
It seems obvious that if Oscar is given an encryption key, then it should be easy
for him to determine the decryption key by simply reversing what was done to
encrypt. However, in 1976 Die and Hellman postulated the seemingly impossible
idea of creating a public key , which Oscar can see yet which gives no hint in and
of itself as to how to determine = 1 . If feasible this would rid Alice of the
diculty of keeping her key secret.
In modern public key cryptosystems the diculty of determining from tends
to be based on an unsolved deep mathematical problem, preference being given
to problems that have withstood the onslaught of the nest minds from Gauss
18The ring of polynomials with integer coecients is denoted Z[x].
14
ANDREW GRANVILLE
onwards, like the factoring problem. We now discuss the most famous of these
public key cryptosystems.
3a.1. The RSA cryptosystem. In 1982, Ron Rivest, Adi Shamir and Len Adleman proposed a public key cryptosystem which is at the heart of much computer
security today and yet is simple enough to teach to high school students. The idea
is that Bob takes two large primes p < q, and their product n = pq, and determines two integers d and e for which de 1 (mod (p 1)(q 1)) (which is easy).
The public key, which Alice uses, will consist of the numbers n and the encryption
key e, whereas Bob keeps the decryption key d secret (as well as p and q). We
will suppose that the message m is an integer19 in [1, n 1]. To encrypt m, Alice
computes r := (m) me (mod n), with (m) [1, n 1], which can be done
rapidly (see section 3b.2). To decrypt Bob computes (r) := rd (mod n), with
(r) [1, n 1]. Using Fermats Little Theorem (for p and q) the reader can easily
verify that mde m (mod n), and thus = 1 .
Oscar knows n, and if he could factor n, then he could easily determine ; thus
the RSA cryptosystems security depends on the diculty of factoring n. As noted
above, this is far beyond what is feasible today if we take p and q to be primes that
contain more than 200 digits. Finding such large primes, however, is easy using the
methods discussed in this article!
Thus the ability to factor n gives Oscar the ability to break the RSA cryptosystem, though it is unclear whether the RSA cryptosystem might be broken much
more easily. It makes sense then to try to come up with a public key cryptosystem whose security is essentially as strong as the diculty of the factoring problem.
3a.2. The ability to take square roots (mod n) is not as benign as it
sounds. In section 2.4 we saw that if we could nd a square root b of 1 mod n
which is neither 1 nor 1, then this proves that n is composite. In fact b yields a
partial factorization of odd n, for
(3.1)
by 02, etc., and then into several such integers by cutting the digits (in binary representation)
into blocks of length < log n.
15
3a.3. On the diculty of nding non-squares (mod p). For a given odd
prime p it is easy to nd a square mod p: take 1 or 4 or 9, or indeed any
a2 (mod p). Exactly (p 1)/2 of the non-zero values mod p are squares mod
p, and so exactly (p 1)/2 are not squares mod p. One might guess that they
would also be easy to nd, but we do not know a surere way to quickly nd such
a value for each prime p (though we do know a quick way to identify a non-square
once we have one).
Much as in the search for witnesses discussed in section 2.4, the most obvious
idea is to try a = 2, 3, 4, . . . consecutively until we nd a non-square. It is believed
that there is a non-square 2(log p)2 , but we cannot prove this (though we can
also deduce this from the Generalized Riemann Hypothesis).
Another way to proceed is to pick integers a1 , a2 , . . . , ak , . . . from {1, 2, 3, . . . ,
n1} at random until we nd a non-square. The probability that none of a1 , a2 , . . . ,
ak are non-squares mod p is 1/2k , so with a hundred or so such choices it is
inconceivable that we could fail!
3b. Computational issues: Running times of calculations
3b.1. Arithmetic on a computer. Suppose a and b are two positive integers,
each with no more than digits when written in binary. We are interested in
the number of bit operations a computer takes to perform various calculations.
Both addition and subtraction can obviously be performed in O() bit operations.20
The most ecient method for multiplication (using Fast Fourier Transforms) takes
time21 O( log log log ). The precise log and log log powers in these estimates
are more or less irrelevant to our analysis, so to simplify the writing we dene O(y)
to be O(y(log y)O(1) ). Then division of a by b and reducing a (mod b) also take
time O().
Now suppose a and b are two polynomials, with integer coecients, of degree less
than r whose coecients have no more than binary digits. Adding or subtracting
will take O(r) operations. To multiply a(x) and b(x) we use the method of single
point evaluation which is so well exploited in MAPLE. The idea comes from the
observation that there is a natural bijection
r1
i
ai x Z[x] : A < ai A for all i
Z/(2A)r ,
a(x) =
i=0
where (a) = a(2A). To recover a0 , a1 , . . . ar1 successively from this value, note
that a0 a(2A) mod 2A and A < a0 A so a0 is uniquely determined. Then
a1 (a(2A) a0 )/(2A) mod 2A and A < a1 A, so a1 is uniquely determined,
and we continue like this. In an algorithm to determine c(x) := a(x)b(x), we rst
note that the absolute values of the coecients of a(x)b(x) are all < A := r22 .
Then we evaluate a(2A) and b(2A) and multiply these integers together to get
a(2A)b(2A), and then recover c(2A) (by a single point evaluation). Unsurprisingly
the most expensive task is the multiplication (of two integers which are each <
+ log r)).
(2A)r ) and so this algorithm takes time O(r(
In our application we also need to reduce polynomials a(x) mod (n, xr 1), where
the coecients of a have O( + log r) digits and a has degree < 2r. Replacing each
20The notation O() can be read as bounded by a xed multiple of .
21
16
ANDREW GRANVILLE
xr+j by xj and then reducing the coecients of the resulting polynomial mod n will
g (x) (x + a)2
= (x + a)n
{0, 1, . . . , n1} and reducing mod (n, xr 1), so has running time O(r(+log
r)).
3/2 (log n)3 ) bit operations, as we
3b.3. The AKS algorithm runs in O(r
will now show. To transform the theorem of Agrawal, Kayal and Saxena into an
algorithm we proceed as follows:
Determine whether n is a perfect power.
We leave this challenge to our inventive reader, while noting that this can be done
takes O(r(log
n)2 ) bit operations.23
Determine whether gcd(a, n) > 1 for some a r, which will take O(r(log
n)2 )
bit operations using the Euclidean algorithm, provided r < n.
Finally we verify whether the Childs Binomial Theorem holds:
that it would take too many bit operations to determine whether [(log n)2 ] equals J 1 or J.
However, if we allow j to run up to J here, and in the AKS theorem if we allow a to run up
to the smallest integer that is obviously > r log n, then we avoid this diculty and do not
signicantly increase the running time.
17
the powers of n will generate all of the r 1 non-zero residues mod r), and this
is borne out in computation though we cannot prove that this will always be true.
Evidently, if there are such r then the AKS algorithm will run in O((log
n)6 ) bit
operations which, as we have explained, is as good as we can hope for.
We can show unconditionally that there are integers r for which the order of n
(mod r) is > (log n)2 and with r not too big. In section 4.3 we will use elementary
estimates about prime numbers to show that such r exist around (log n)5 , which
1
(4.2)
(x + a)n xn + a
mod (p, xr 1)
for
each integer a, 1 a A. We can factor xr 1 into irreducibles in Z[x],
as d|r d (x), where d (x) is the dth cyclotomic polynomial, whose roots are the
primitive dth roots of unity. Each r (x) is irreducible in Z[x], but may not be
irreducible in (Z/pZ)[x], so let h(x) be an irreducible factor of r (x) (mod p).
Then (4.2) implies that
(4.3)
(x + a)n xn + a
18
ANDREW GRANVILLE
by (4.2). Dene S to be the set of positive integers k for which g(xk ) g(x)k
mod (p, xr 1) for all g H. Then g(xk ) g(x)k in F for each k S, so that
the Childs Binomial Theorem holds for elements of G in this eld for the set of
exponents S! Note that p, n S.
Our plan is to give upper and lower bounds on the size of G to establish a
contradiction.
4.1. Upper bounds on |G|.
Lemma 4.1. If a, b S, then ab S.
Proof. If g(x) H, then g(xb ) g(x)b mod (p, xr 1); and so, replacing x by xa ,
we get g((xa )b ) g(xa )b mod (p, (xa )r 1), and therefore mod (p, xr 1) since
xr 1 divides xar 1. Therefore
g(x)ab = (g(x)a )b g(xa )b g((xa )b ) = g(xab ) mod (p, xr 1)
as desired.
Lemma 4.2. If a, b S and a b mod r, then a b mod |G|.
Proof. For any g(x) Z[x] we have that u v divides g(u) g(v). Therefore xr 1
divides xab 1, which divides xa xb , which divides g(xa ) g(xb ); and so we
deduce that if g(x) H, then g(x)a g(xa ) g(xb ) g(x)b mod (p, xr 1).
Thus if g(x) G, then g(x)ab 1 in F; but G is a cyclic group, so taking g to be
a generator of G we deduce that |G| divides a b.
Let R be the subgroup of (Z/rZ) generated by n and p. Since n is not a power
of p, the integers
ni pj with i, j 0 are distinct. There are > |R| such integers with
0 i, j |R| and so two must be congruent (mod r), say
ni p j nI p J
(mod r).
By Lemma 4.1 these integers are both in S. By Lemma 4.2 their dierence is
divisible by |G|, and therefore
(4.4)
|G| n |R| 1.
d
19
divides xb xa , which divides g(xb ) g(xa ) for any g(x) Z[x]. If g(x) H, then
d
d
d
g(x)n g(xn ) mod (p, xr 1) by Lemma 4.1 since n S, and g(xn ) g(x)
d
d
mod (p, xr 1) (as xr 1 divides xn x) so that g(x)n g(x) mod (p, xr 1).
But then g(x)b g(x)a mod (p, xr 1) since nd 1 divides b a. Therefore
g(xb ) g(xa ) g(x)a g(x)b
mod (p, xr 1)
since a S, which implies that b S. Now let b = n/p and a = np(n 1)1 , so
that a S by Lemma 4.1 since p, n S. Also b a (mod nd 1) so b = n/p S
by the above.
d
4.2. Lower bounds on |G|. We wish to show that there are many distinct
elements of G. If f (x), g(x) Z[x] with f (x) g(x) mod (p, h(x)), then we can
write f (x) g(x) h(x)k(x) mod p for some polynomial k(x) Z[x]. Thus if f
and g both have smaller degree than h, then
k(x) 0 (mod p) and so f (x) g(x)
(mod p). Hence all polynomials of the form 1aA (x + a)ea of degree < m (the
degree of h(x)) are distinct elements of G. Therefore if m, the order of p (mod r),
is large, then we can get good lower bounds on |G|.
This was what Agrawal, Kayal and Saxena did in their rst preprint, and to
prove such r exist they needed to use non-trivial tools of analytic number theory.
In their second preprint, inspired by remarks of Hendrik Lenstra, they were able to
replace m by |R| in this result, which allows them to give an entirely elementary
proof of their theorem and to get a stronger result when they do invoke the deeper
estimates.
Lemma 4.3. Suppose that f (x), g(x) Z[x] with f (x) g(x) mod (p, h(x)) and
that the reductions of f and g in F both belong to G. If f and g both have degree
< |R|, then f (x) g(x) (mod p).
Proof. Consider (y) := f (y) g(y) Z[y] as reduced in F. If k S, then
(xk ) = f (xk ) g(xk ) f (x)k g(x)k 0 mod (p, h(x)).
It can be shown that x has order r in F so that {xk : k R} are all distinct roots
of (y) mod (p, h(x)). Now, (y) has degree < |R|, but |R| distinct roots mod
(p, h(x)), and so (y) 0 mod (p, h(x)), which implies that (y) 0 (mod p)
since its coecients are independent of x.
By denition R contains all the elements generated by n (mod r), and so R is
2
at least as large as d, the order of n (mod
r), which is > (log n) by assumption.
Therefore
A, |R| > B, where B := [ |R| log n]. Lemma 4.3 implies that the
products aT (x + a) give distinct elements of G for every proper subset T of
{0, 1, 2, . . . , B}, and so
20
ANDREW GRANVILLE
Lemma 4.4. If n 6, then there is a prime r [(log n)5 , 2(log n)5 ] for which the
order of n mod r is > (log n)2 .
Proof. If not, then the order of n mod r is I := (log
n)2 for every prime r
5
[N, 2N ] with N := (log n) , so that their product divides iI (ni 1). But then
5
2N
r
(ni 1) < n iI i < 2(log n) ,
N r2N
r prime
iI
The bound on r here holds for all n 6, and thus using this bound our running
time analysis of AKS is eective; that is, one can explicitly bound the running time
of the algorithm for all n 6. In some of the better bounds on r discussed in
section 3.4, the proofs are not eective in that they do not imply how large n must
be for the given upper bound for r to hold.
4.4. Large prime factors of the order of n (mod r). The other estimates
mentioned in section 3b.4 all follow from using deeper results of analytic number
theory which show that there are many primes r for which r 1 has a large prime
factor q = qr > (log n)2 . By showing that this large prime q divides the order of
n (mod r) for all but a small set of exceptional r, we deduce that the order of n
(mod r) is q > (log n)2 . (In the rst version of the AKS paper they needed m, the
order of p (mod r), to be > (log n)2 and obtained this through the same argument,
since the order of n (mod r) divides the product of the orders of p (mod r), where
the product is taken over the prime divisors p of n, and thus q must divide the
order of p (mod r) for some prime p dividing n.) We now describe our argument a
little more explicitly in terms of a well-believed
Conjecture. For any given in the range 0 < < 1/2 there exists c = c() > 0
such that there are at least 2cR/ log R primes r in [R, 2R] for which r 1 has a
prime factor q > r1/2+ , provided R is suciently large.
Lemma 4.5. Assume the conjecture for some , 0 < < 1/2. Suppose n is a
suciently large integer and that c()R2 log n. There are at least c()R/ log R
primes r in [R, 2R] for which the order of n (mod r) is > r1/2+ .
Proof. We will show that the number N of primes r given in the conjecture for which
q does not divide the order of n (mod r) is < c()R/ log R. Now, if r is such a prime,
then the order of n (mod r) divides (r1)/q, and
r/q r1/2 (2R)1/2 .
so is <
m
Therefore the product of such r divides
(n 1), which implies that
RN
m(2R)1/2
12
nm < nR
m(2R)1/2
21
In section 5.3 we will prove the conjecture for some value of > .11 using basic
tools of analytic number theory, so that Corollary 4.6 shows us that we can take
r < (log n)50/11 . Fouvry proved a version of the conjecture for some > 1/6, and
so a modication of Corollary 4.6 shows us that we can take r < (log n)3 . These
estimates were used in section 3b.4.
5. Results about counting primes that everyone should know
5.1. Primes and sums over primes. The prime number theorem tells us that
(x), the number of primes up to x, is26 x/ ln x as x . This can be shown
to be equivalent to the statement
(5.1)
ln p x
px
p prime
2
,
thus
log p 2m;
primes in (m, 2m] always divide 2m
m<p2m
m
and summing over m = [x/2i ] + 1 for i = 1, 2, . . . we deduce that px log p
2(x + log x), a weak but explicit version of (5.1). Now
x
ln n =
ln p =
ln p
1=
ln p
pa
a
a
a
nx
nx
p |n
p x
nx
pa |n
p x
ln p
x
+ O(x),
+ O(1) ln p = x
=
a
p
p1
a
p x
since
px
px<pa
and
ln p = O(1)
px
ln p =
pa x
ln p = O(x),
a1 px1/a
using
our weak form of (5.1). One can use elementary calculus to show that
nx ln n = x ln x + O(x) and thus deduce from the above that
(5.2)
ln p
= ln x + O(1).
p1
px
22
ANDREW GRANVILLE
(a,q)=1
(q) (ln x)A
qQ
qa 2R
q prime, a1
R<p2R
p1 mod qa
ln q
R<p2R qa |p1
(5.3)
ln(p 1) R,
R<p2R
a
a
the prime powers q , with a 2 and q R, to the rst sum in (5.3) is
R
R
ln q
=
O
4
,
a1 (q 1) ln R
ln R
q
a
q R
a2
the AKS algorithm runs in O((log n)6+o(1) ) bit operations by Corollary 4.6.
29Though this rst appeared in a paper of Montgomery and Vaughan.
23
with a 2 and q a R, in the rst sum in (5.3) we bound the number of primes
by the number of terms in the arithmetic progression, namely 2R/q a . Now for
given q the number
of such a 2 is (ln R)/(ln q), and so their contribution to
the sum is 2 R(ln R), which totals to O(R/ ln R) when summing over all q
(2R)1/3
. For the remaining q we only have the a = 2 term which thus contributes
2R q>(2R)1/3 ln q/q 2 = O(R/ ln R). Thus the total contribution of all these
q a , a 2 terms in (5.3) is O(R/ ln R).
apply the Bombieri-Vinogradov Theorem with A = 2, Q =
Next we
R/(ln R)B(2) and x = R and 2R to obtain
ln q
R
R
+ O(1)
ln q((2R; q, 1) (R; q, 1))
ln R
q
1
2
qQ
qQ
q prime
q prime
.
ln q {(2R; q, 1) (R; q, 1)}
(q
1)
ln(R/q)
1/2+
1/2+
Q<q(2R)
q prime
Q<q(2R)
To estimate this last sum we break the sum over q into intervals (QLi , QLi+1 ] for
i = 0, 1, 2, . . . , I 1. Here L is chosen so that QLI = (2R)1/2+ , and we shall require
that L as R does, but with L = Ro(1) . In such an interval we nd that each
ln(R/q) ln(R1/2 /Li ), and so the sum over the interval is 2R ln L/ ln(R1/2 /Li )
1 ln R
1
. Approximating
by (5.2). Thus the quantity above is 2R I1
i=0 2 ln L i
this sum by an integral and then taking u = t ln L/ ln R we get
1
I1
1 ln R
t
2R
dt 2R
(1/2 u)1 du = 2R ln(1/(1 2)).
2 ln L
t=0
0
Combining the above estimates for the various q and using the fact that log q
log 2R give
R
,
(5.4)
{(2R; q, 1) (R; q, 1)} {2c() + o(1)}
ln
R
1/2+
(2R)
<q
q prime
1e1/4
2
.1105996084.
5.4. How can it be so hard to make some results explicit? In section 5.2 we
stated that one can unconditionally prove (x; q, a) (x)/(q) when (a, q) = 1
where (q) = #{a (mod q) : (a, q) = 1} when x > exp(q ) for any xed > 0 once
q is suciently large, but there is a catch. If 1/2, we do not know how large
we mean when we write q is suciently large (though we do for > 1/2). The
reader might suppose that this fault exposes a lack of calculation ability on the part
of analytic number theorists,30 but this is not the case. It is in the very nature of
the proof itself that, at least for now, an explicit version is impossible, for the proof
comes in two parts. The rst, under the supposition that the Generalized Riemann
30I know I did when I rst encountered this statement.
24
ANDREW GRANVILLE
Hypothesis is true, gives an explicit version of the result. The second, under the
supposition that the Generalized Riemann Hypothesis is false, gives an explicit
version of the result in terms of the rst counterexample. We strongly believe that
the Generalized Riemann Hypothesis is true, but it remains an unproved conjecture,
indeed one of the great open problems of mathematics. So, as long as it remains
unproved, we seem to be stuck with this situation, for how can we put numbers into
the second case in the proof when we believe that there are no counterexamples?
This result of Siegel underpins many of the key results of analytic number theory;
hence many of the results inherit this property of being inexplicit. Make Siegels
result explicit and you change the face of analytic number theory, but for now there
is no sign that this will happen and so we are lumbered with this considerable burden, particularly when trying to apply analytic number theory results to determine
complexity of algorithms.
6. A randomized algorithm with running time O((log n)4+o(1) )
Next we describe an algorithm that distinguishes primes from composites and
provides a proof within O((log n)4+o(1) ) steps. The only drawback is that this is
not guaranteed to work. Each time one runs the algorithm the probability that it
reports back is 1/2, but each run is independent, so after 100 runs the probability
that one has not yet distinguished whether the given integer is prime or composite
is < 1/2100 , which is negligible. This is arguably our best solution to at least part of
the problem that Gauss described as so elegant and celebrated. As we discussed
in section 1.1, this does not yet work in practice on quite such large numbers as
certain tests which are not yet proven but believed to run in polynomial time, but
I believe that it is only a matter of time before this situation is rectied.
The algorithm is a modication of AKS given by Bernstein, following up on
ideas of Berrizbeitia, as developed by Qi Cheng (and a similar modication was
also given by Mihailescu and Avanzi). This is an RP algorithm for primality testing which is faster, easier and more elegant than that of Adleman and Huang. In
practice this makes the original AKS algorithm irrelevant, for if we run the witness test, which is an RP algorithm for compositeness, half of the time and run
the AKS-Berrizbeitia-Cheng-Bernstein-Mihailescu-Avanzi RP algorithm for primality the other half, then a number n is, in practice, certain to yield its secrets faster
(in around O((log n)4+o(1) ) steps) than by the original AKS algorithm!
6.1. Yet another characterization of the primes. For a given monic polynomial f (x) with integer coecients of degree d 1 and positive integer n, we say
that Z[x]/(n, f (x)) is an almosteld with parameters (e, v(x)) if
(a) Positive integer e divides nd 1,
d
(b) v(x)n 1 1 mod (n, f (x)), and
d
(c) v(x)(n 1)/q 1 is a unit in Z[x]/(n, f (x)) for all primes q dividing e.
If n is prime and f (x) (mod n) is irreducible, then Z[x]/(n, f (x)) is a eld;
moreover any generator v(x) of the multiplicative group of elements of this eld
satises (b) and (c) for any e satisfying (a).
Bernstein. For given integer n 2, suppose that Z[x]/(n, f (x)) is an almosteld
with parameters (e, v(x)) where e > (2d log n)2 . Then n is prime if and only if
n is not a perfect power,
25
(t 1)n tn 1 mod (n, f (x), te v(x)) in Z[x, t] (that is, we work with
polynomials with integer coecients in independent variables t and x).
Proof. Write N = nd and v = v(x). If n is a perfect power, then n is composite. If n
is prime, then the second condition holds by the Childs Binomial Theorem. So we
may henceforth assume that n is not a perfect power and is not prime, and we wish
d
d
to show that (t 1)n tn 1 mod (n, f (x), te v(x)). Let p be a prime dividing
n and h(x) an irreducible factor of f (x) (mod p), so that F = Z[x]/(p, h(x)) is
isomorphic to a nite eld. Let P = |F| = pdeg h , and note that since p < n and
deg h deg f , hence P < N .
Let v (N 1)/e mod (p, h(x)) so that is an element of order e in F: To see
this, note that e v N 1 1 mod (p, h(x)) by (b); whereas if had order m, a
proper divisor of e, then let q be a prime divisor of e/m so that 1 e/q v (N 1)/q
mod (p, h(x)), contradicting (c).
i
ai
in F[t] are distinct, and so those of
The polynomials of the form e1
i=0 ( t 1)
e
degree e 1 are distinct in F[t]/(t v).
Now tN = tN 1 t v (N 1)/e t (mod te v), so that tN t mod (p, h(x), te v).
Thus our second criterion implies that (t 1)N t 1 mod (p, h(x), te v).
Moreover replacing t by i t gives ( i t 1)N i+1 t 1 mod (p, h(x), te v)
i
for any integer i 0 (since ( i t)e v = te v), and thus (t 1)N i t 1
e
mod (p, h(x), te v) by a suitable induction argument. Note that (t 1)N (t 1)
e
mod (p, h(x), t v).
i
iI N
Therefore
for
proper
subsets
I
of
{0,
1,
.
.
.
,
e
1}
the
powers
(t
1)
i
e
iI ( t 1) mod (p, h(x), t v) all have degree e 1 and so are distinct
polynomials, and thus there are at least 2e 1 distinct powers of (t 1) mod
(p, h(x), te v).
Now e is the order of an element of F , which is a cyclic group of order P 1,
and so P 1 is a multiple of e. Therefore v (P 1)/e is an eth root of 1 in F, so
must be a power of , say . Arguing as in two paragraphs above, but now with N
j
and replaced by P and , we see that (t 1)P j t 1 mod (p, h(x), te v).
i j
Combining these results we obtain that (t1)N P i+j t1 mod (p, h(x), te v)
for all integers i, j.
There are more than e pairs of integers (i, j) with 0 i, j [ e], and so there
exist two numbers of the form i + j (with i and j in this range) that are congruent
(mod e), say i + j I + J (mod e). Therefore if u := N i P j and U := N I P J ,
then (t 1)u i+j t 1 = I+J t 1 (t 1)U mod (p, h(x), te v). We will
v) so that we can deduce that there
show that t 1 is a unit mod (p,
h(x), te
e
are no more than |U u| < (N P ) 1 < N 2 e 1 distinct powers of (t 1) mod
(p, h(x), te v), and thus 2e < N 2 e , contradicting the hypothesis.
Now v(x) = 1 in F by (c), so that t 1 is not a factor of te v(x) in F[t]; in
other words t 1 is a unit in the ring F[t]/(te v(x), that is mod (p, h(x), te v).
6.2. Running this primality test in practice. We will show that if n is prime,
then an almosteld may be found rapidly in random polynomial time. The primality
test given by the statement of the theorem may evidently be implemented by similar
methods to those we discussed previously.
26
ANDREW GRANVILLE
Assume that n
is prime. By the inclusion-exclusion formula one can prove that
there are31 (1/d) |d (d/)n irreducible polynomials mod n of degree d. The
biggest term here is the one with = d; that is, roughly 1/d of the polynomials of
degree d are irreducible. Thus selecting degree d polynomials at random we should
expect to nd an irreducible one in O(d) selections. Verifying f is irreducible can
d
be done by checking, via the Euclidean algorithm, that xn x 0 mod (n, f (x))
d/q
and xn x is a unit in Z[x]/(n, f (x)) for all primes q dividing d. Once we have
found f we know that Z[x]/(n, f (x)) is a eld. The elements of Z[x]/(n, f (x)) can
be represented by the polynomials v(x)
mod n of degree < d. The proportion
of these that satisfy (b) and (c) is p|e (1 1/p) > 1/2 ln ln e, and so selecting
such v(x) at random we should expect to nd v(x) satisfying (b) and (c) in O(d)
selections.
d
d
The main part of the running time comes in verifying that (t 1)n tn 1
e
mod (n, f (x), t v(x)), which will take d log n steps, each of which will cost
O(de(log n)1+o(1) ) bit operations, giving a total time of O(d2 e(log n)2+o(1) ) bit operations. The conditions d 1, e > (2 log n)2 imply that the running time cannot
be better than O((log n)4+o(1) ), and we will indicate in the next section how to nd
d and e so that we obtain this running time.
6.3. More analytic number theory. To nd an almosteld when n is prime we
need to nd d and e for which e divides nd 1 and with d and e satisfying certain
conditions. Constructions typically give e as a product of primes p which do not
divide n and for which p 1 divides d, since then p divides nd 1 by Fermats Little
Theorem, and thus e divides nd 1.
However, to ensure that e is large, for instance e > (2d log n)2 as required in
the hypothesis of Bernsteins result, we need to use the ideas of analytic number
theory. Our general construction looks as follows: For given z < y, with z y for
xed > 0, let d be the least common multiple of the integers up to z and e be the
of p 1 are
product of all primes p y such that all prime power divisors q a
z.
Note that d = exp(z+o(z)) by the prime number theorem and e = py p
pP p,
where P is the set of primes p y for which p 1 has a prime power divisor q a
which is > z.
If p P write p 1 = kq a with q a > z, so that k < y/z 1/. Now the number
27
This time we take z = y with y = (2 + 3) log log n. We need the generalization
of Lemma 6.1 to prime pairs of the form q, kq + 1:
Lemma 6.2. There exists an absolute constant c > 0 such that there are
c(k/(k))(x/(log x)2 ) primes q x for which kq + 1 is also prime, for all even
integers k and all x 2.
In this case, corresponding to each prime p P with a = 1, we have a prime
pair q, kq + 1 with k 1/ and q y/k. For given k 1/ there are
cy/(log(y))2 such prime pairs, by Lemma 6.2 with x = y/k, since (k) 1. There
fore |P| = O(y/((log y)2 ) + y/) = o(y/ log y), so the product of the primes in P
is y |P| = exp(o(y)). Thus e = exp(y + o(y)) by the prime number theorem, and
so e > (2d log n)2 as required; but now the running time will be O((log n)4+O() ),
and letting 0 we get the desired result.
6.4. Bernsteins construction was originally analyzed using a beautiful result
of Prachar. If p 1 divides d for every prime p dividing e, as
above, then how
large can e be in terms of d? An obvious upper bound for e is m|d (m + 1) in the
case that one more than each divisor of d is a prime. So if (d) is the number of
divisors of d, then e < d (d) . Can we obtain e which is anywhere near this big?
Prachars idea is to look for primes of the form mk + 1 for some small integer k,
as m runs through the divisors of d. Under the assumption of the Generalized
Riemann Hypothesis, for each xed m|d, there are > d2 /2 log d primes of the form
mk + 1 with d2 < k < 3d2 , so their product is > exp(d2 ). Therefore there exists
some value of k < 3d2 such that the product e of the primes of the form km + 1
with m|d is > exp( (d)/2) (and we replace d above by D := kd < 3d3 ). If we took
the original d to be the product of the primes z, then, by the prime number
theorem, d = exp(z + o(z)) and log e > 2(z)1 so that D < (log e)c log log log e , for
some constant c > 0. This argument can be justied without assumption, since as
we discussed in section 5.2, we almost always get the expected number of primes
in an arithmetic progression in the relevant ranges (see [2] for the technical details).
6.5. Lenstras 1985 nite eld primality test uses many of the same ideas as
the AKS test and these variants. It is surprising how close researchers were twenty
years ago to obtaining a polynomial time primality test.
Lenstra. For a given almosteld Z[x]/(n, f (x)) with parameters (e, v(x)), if
ni
(d) g(T ) := d1
i=0 (T v(x) ) (Z[x]/(n, f (x)))[T ],
then p nj (mod e) for some j, 0 j d 1, for each prime p dividing n.
Proof. Let h(x) be an irreducible factor of f in (Z/pZ)[x], and dene F :=
Z[x]/(p, h(x)). Now g(v(x)) = 0 by (d), so that g(v(x)p ) = g(v(x))p = 0 in F,
i
by the Childs Binomial Theorem. Therefore v(x)p = v(x)n in F for some i. This
implies that p ni mod the order of v(x) in F. The result follows since the order
of v(x) in F is divisible by e, by (b) and by (c).
We use this as a primality test by selecting d and e as in section 6.4 so that n2 >
e > n and D < (log n)O(log log log n) and then nding an almosteld as described in
section 6.2. If (d) does not hold, then n is composite (since our choice of f (x) is
irreducible if n is prime). If d does hold, then the candidates to be prime factors
28
ANDREW GRANVILLE
of n by Lenstras theorem are the least residues of n, n2 , n3 , . . . , nd1 (mod e), and
these are easily checked by trial division. The running time of this random test
for the primality of n is thus O((log n)O(log log log n) ), just a smidgin slower than
polynomial time. (This is a simplication of Lenstras test, which was actually
a little more involved but compensated by being faster in terms of the constant
implicit in the O in the exponent.)
7. Stop the press: (log n)6 achieved
7.1. Lenstra and Pomerance obtain the desired running time. Lenstra
and Pomerance signicantly modied the AKS algorithm so that it will, in theory,
29
k r 1.
34For example, 5 = (1 + 2 )(1 2 ) in Q( ).
4
4
4
30
ANDREW GRANVILLE
31
primes r x. If we have such an r, then it is very likely that n(r1)/q (mod r) has
order q for some such q dividing r 1, for if not, then n has order < x (mod r),
but there are few such r as can be shown by an argument like that in the proof of
Lemma 4.5.
2
Let Q be the set of primes q (x , x1/2 ] such that there exists a prime r x
which is 1 (mod q) and for which q is the order of n(r1)/q (mod r). Using
the above observations, together
with a sharpened form of the Brun-Titchmarsh
theorem, one can show that qQ 1/q > 3/11 o(1).
Let = 2 /2 log log n. For each q Q let q := log q/2 log log n and dene
S0 = qQ (q , q ) and then S to be the closure of S0 under addition. By the
prime number theorem, x <q<x , q prime 1/q ln(/) = dt
t , from which we
easily deduce that 0t1/4+, tS dt
>
{1
+
o(1)}
1/q
>
3/11
o(1) by the
qQ
t
above.
Therefore 1 S by Lemma 7.2 so that there exists V S0 such that
v
= 1. Let U be the set of q Q for which v (q , q ) as we run through
vV
v V (this simple argument
may be modied to show that dierent v give rise to
dierent q). Thus 0 < qU log q 2 log log n < 2 as required for Proposition 7.1.
8. Minor improvements and tempting ideas
In this section we begin with four areas in which there are elegant ideas for improving the proof of section 4, selected for the beauty of the mathematics involved.
Next we discuss Agrawal and his students quest for a polynomial time primality
test, and then develop their ideas to rewrite the AKS test without polynomials.
8.1. Varying the elements of G. In section 4 we took G to be the subgroup of
F generated by V := {x + 1, . . . , x + [A]} and veried that n S = SG (by checking
(4.1) for a = 1, 2, . . . , A). If we can deduce from this knowledge that (4.1) holds
for other values of a, then we can expand the size of V without changing the proof
except for showing that G is larger than before. So for any a, 1 a A verify
that an a (mod n) (else n is composite), and then select integer b so that ba 1
(mod n). Now, (4.1) can be rewritten as (x + a)n (xn + a) = nu(x) + (xr 1)v(x)
for certain u(x), v(x) Z[x], and one can show that deg u < n, deg v < n r using
the Euclidean algorithm in Z[x]. Replacing x by 1/x and multiplying through
by (bx)n we obtain (b + abx)n (bn + (abn )xn ) = nU (x) (xr 1)V (x) where
U (x) := bn xn u(1/x), V (x) := bn xnr v(1/x) Z[x]. Now (x + b)n (abx + b)n
(mod n) and xn (abn )xn (mod n), so we have proved that (4.1) also holds when
we replace a by b. Thus we may replace V by V {a1 (mod n) : 1 a A}, a
set that will be almost twice as large (and thus our lower bound on the size of G
will be squared).
Just as we replaced x by x1 in (4.1) to nd another case of the Childs Binomial
Theorem, so we may also replace x by xk for any integer k since xr 1 divides
xkr 1, and thus we may generate G using all polynomials of the form
{xk + a : k N, 1 a A} {xk + b : k N, 1 a A, b a1
(mod n)}.
From Lemma 4.3 we deduced, in section 4.2, that each polynomial generated as
a product of the polynomials {x, x + 1, . . . , x + [A]} which has degree < |R| belongs
to a dierent element of G, and thus we obtained a lower bound on the size of
G by counting the number of such polynomials without repeated roots. Voloch
suggested a cleverer counting argument. Suppose that G contains k distinct linear
32
ANDREW GRANVILLE
1jk
{gu =
(x + ai )ui mi : u U }
1ik
(x + ai )max{ui vi ,0} =
(x + aj )max{vj uj ,0}
i
which are both of degree < |R|, and this contradicts Lemma 4.3. Thus |G| |U |.
Determining u(k, ), the size of the largest such set U , is an elegant and open36
combinatorial problem about
which we know the following:
s and
Dene Us,t := {u Zk :
1ik max{ui , 0}
1ik max{ui , 0}
k
t}. Evidently U,0 = {u Z : Each ui 0 and
1ik ui } satises (8.1).
satises
(8.1)
for
any
0
v
,
0}
(max{u
,
0}
+
max{v
i
i
i
i , 0}) s + ( s) = .
1ik
1ik
On the other hand if we x v U , then U v + U, by (8.1). Thus we deduce that
|U, | u(k, ) max0s |Us,s |. We can determine the number of elements of
Us,t by counting those n Us,t with exactly j values of i for which ui 0 to obtain
k
k
s+j
t
|Us,t | =
.
j
j
kj
j=0
8.2. Upper bounds on |G|, revisited. In section 4.1 the pigeonhole principle
was used to nd two elements of R which are congruent mod r. Poonen and Lenstra
replaced this by
Minkowskis convex body theorem. Let be a lattice which is a sublattice of
Z2 . Let U be a convex 37 body in R2 which is symmetric about the origin. If the area
of U is at least 4 times the determinant 38 of , then U contains a lattice point of
other than the origin.
We also note the following lemma which is left as an elementary exercise for the
reader:
Lemma 8.1. For positive a, b, c dene T = {(x, y) R2 : x, y 0, and ax+by c}
and U = {t t : t, t T }. Then U = T T V V , where V = {(x, y)
R2 : 0 ax, by c}, so U is a symmetric convex body in R2 , whose area is 6 times
the area of T .
33
points, call them (i, j) and (I, J), which are congruent
mod r. As in section 4.1, |G|
|R|/3
i
j
I
J
1, an improvement on (4.3).
divides p (n/p) p (n/p) , so that |G| n
8.3. Using polynomials of higher degree in G. Above we obtained lower
bounds on the size of G by determining a large set of polynomials in G of degree
< |R|, since Lemma 4.3 guarantees that they will be distinct. Voloch showed that
few pairs of polynomials in G of slightly larger degree can be equal, and so improved
the lower bounds on |G|. His method revolves around one of the most remarkable
results in the arithmetic of function elds:
abc-theorem for polynomials. If a, b, c C[t] and not all in C, with a + b = c,
where a and b have no common factors, then the degrees of a, b and c are each less
than the total number of distinct roots of abc.
For a proof, see [13]. By the example in Theorem 1, tp + 1 = (t + 1)p , this result
does not carry over to elds of characteristic p, for in this example the maximum
degree is p and yet there are just 2 distinct roots. However it is not dicult to
modify the proof in [13] to show that if a + b = c where a, b, c are genuine coprime
polynomials in a eld of characteristic p, then either a = Ap , b = B p and c = C p
where A + B = C or the above conclusion holds. From this we deduce
Lemma 8.2. Let K be a eld of characteristic p. Suppose a, b, c K[t] and have
no common factor, and that no two of u, v, w K[t] have a common factor, where
au + bv = cw and g = (au, bv, cw). Then g = (a, b)(a, c)(b, c) and either au, bv and
cw each equal g times the pth power of a polynomial in K, or
"
!
2 max {deg u, deg v, deg w} > max(deg a, deg b, deg c)# distinct roots of abc/g 2 .
Proof. Note that (a, b) divides au + bv = cw and has no common factor with
c, so divides w. Similarly (b, c) divides u, and (a, c) divides v. Therefore g =
(a, b)(a, c)(b, c) and g divides uvw.
Applying the above abc-theorem for polynomials in a eld of characteristic p
we nd that either au/g, bv/g and cw/g are each a pth power or the degrees of
au/g, bv/g and cw/g are all less than the ! number of distinct roots
" of
(au/g)(bv/g)(cw/g), which is deg(uvw/g) + # of distinct roots of abc/g 2 , and
the result follows.
Corollary 8.3. Let K be a eld of characteristic p. If a, b, c K[t] and have no
common factor, and m K[t] with a b c mod m, where a and m have no
common factor, then
max{deg a, deg b, deg c}
1
2
(deg m + p), 2 deg m #{distinct roots of abc/g }
min
2
where g = (a, b)(a, c)(b, c).
Proof. Let U = (b c)/m, V = (c a)/m and W = (b a)/m. Let h = (U, V, W )
and U = hu, V = hv, W = hw, so that au + bv = cw. Let G = (au, bv, cw). Note
that (a, b, c) = (u, v, w) = 1 by denition. Also (u, v, c) divides (b c, c a, c) =
(b, a, c) = 1, so (u, v, c) = 1 and similarly (u, w, b) = (v, w, a) = 1. Therefore the
hypothesis of Lemma 8.2 holds with g = G. Note that, by denition,
(8.2)
34
ANDREW GRANVILLE
If au/g, bv/g and cw/g are not all pth powers (or all belong to K), then by substituting (8.2) into Lemma 8.2, we get the second lower bound here. If au/g, bv/g
and cw/g are all pth powers but are not all in K, then
p max{deg au, deg bv, deg cw} 2 max{deg a, deg b, deg c} deg(m)
by (8.2).
Suppose that p is prime and h(x) is irreducible mod p where deg h(x) p/3.
Suppose that V is a set of linear polynomials. Consider the set V of polynomials
of degree < 2 deg h |V |, which are products of elements of V . By Corollary 8.3
(with K = Fp , m = h and a, b, c V ) we deduce that there are at least 12 |V |
distinct polynomials in V mod h(x). This allows us to improve the lower bound
on G that we achieved in section 4 and may be combined with the ideas in section
8.1.
8.4. Fermats Last Theorem, again. Fouvrys result, mentioned in section
3b.4, was inspired by its application to the rst case of Fermats Last Theorem,
that is, integer solutions x, y, z to xp + y p = z p where p does not divide xyz. In
1910 Wieferich proved that if this has a solution, then 2p1 1 (mod p2 ), which
seems to be true very rarely. It turns out that knowledge about such congruences
can be used to reduce the value of r above.
Lemma 8.4. Let be a given prime and let L = if is odd, L = 4 if = 2. Let k
be the order of n mod L. Suppose that e is the highest power of dividing nk 1.
Then kj is the order of n mod e+j .
Proof. If nK 1 = E s where does not divide integer s, and E > 2, then, by the
binomial theorem, nK = (1 + E s) 1 + E+1 s (mod E+2 ). The result follows
from a suitable induction hypothesis.
Thus if n 3 (mod 8), then n has order 2i2 mod 2i , for all i 3, so n has
order > log2 n for some r < 8 log2 n, taking r to be an appropriate power of two.
More generally if n 1 (mod 2s ) for any s 2, then n mod r has order > log2 n
for some r < 2s log2 n. This argument may be extended to odd primes :
Corollary 8.5. If n1
1 (mod 2 ) for odd prime , then there exists r
2
2
log n for which n mod r has order > log2 n.
Proof. We may assume is odd and so, in Lemma 8.4, we have e = 1. Select
j as small as possible so that kj > log2 n, and thus j kj log2 n. Then
r := j+1 2 log2 n.
8.5. Undergraduate theses. Manindra Agrawal had worked for several years
with students trying to nd a criteria for primality of this sort. In April 2001,
Pashant Pandey and Rajat Bhattacharjees bachelors thesis Primality Testing
studied what would happen if
(8.3)
holds for odd composite n and odd prime r. Their hope was that if this happens,
then n 1, 0 or 1 (mod r). However such a neat conclusion seems unlikely,
even for r = 5: To prove that there are innitely many Carmichael numbers, one
constructs integers n with prime factors that have certain extraordinary divisibility
properties. In a similar vein Lenstra and Pomerance conjecture that there are
35
36
ANDREW GRANVILLE
recurrence sequences (the best known example being the Fibonacci numbers); we
give, without proof, the properties we need in the following paragraph.
i
Suppose that j=1 (x j ) = x 1
i=0 ui x Z[x] has no repeated roots.
Let c(t) Q(t). Dene fk = i=1 ci ki for each k 0, where cj = c(j ). An
alternate way to dene fk is by the values for k = 0, 1, 2, . . . , 1 and thereafter by
1
fm+ = i=0 ui fm+i for all m 0. The Fibonacci numbers make up the example
where our polynomial is x2 x 1 and c(t) = t/(t2 + 1), with F0 = 0, F1 = 1.
We claim that if we know 1 , 2 , , and f0 , . . . , f1 , then we can recover
j
c1 , , c . For this information gives us the linear equations
i=1 i ci = fj
for j = 0, 1, , 1 in the unknowns ci , and the matrix of
coecients is the
j
Vandermonde {i }1i, 0j1 , which has determinant 1i<j (j i ),
and this is non-zero since the i are distinct. In fact this argument works mod n
provided the determinant is coprime to n.
Take = r and i = i + a for each i where = e2i/r is a primitive rth root
of unity. The Vandermonde determinant above is then a power of r (by the proof
of Proposition 8.6) and so coprime to n. Choose c(t) so that (c(i ), n) = 1 for
all i, and dene C(t) = c(t)((t + a)n (tn + a)). If ( i + a)n in + a mod n
for all i, then Fj 0 mod n for all j, evidently. Moreover if Fj 0 mod n
for 0 j r 1, then we can deduce, as above, that C( i ) 0 mod n for all
i, and
therefore ( i + a)n in + a mod n. Now Fj = fn+j afj gj where
gj := ri=1 ci in ( i + a)j . We haveenormous freedom in choosing c(t); we select
c(t) = (t a)/r. Given that (1/r) 0ir1 ij = 0 unless r divides j, in which
case it
equals 1, we deduce that fj = 0 for 0 j r 2 and fr1 = 1; whereas
gj = kj ajk where k is the least non-negative residue of (n + 1) mod r, and we
dene kj = 0 if j < k. Thus we have proved the following:
Proposition 8.8. Suppose that (n, r) = 1. Dene the sequence {fm }m0 of integers by f0 = f1 = = fr2 = 0, fr1 = 1 and
r1
r
fm+r =
(1)j arj fm+j + (ar + 1)fm for all m 0.
j
j=1
Then (x + a)n xn + a mod (n, xr 1) if and only if
j jk
a
+ afj mod n for 0 j r 1.
fn+j
k
In the special case a = 1 we can work with a linear recurrence of order =
(r 1)/2, because (1 i )n 1 in mod n is trivial for i = 0 and since it
gives (1 i )n 1 in mod n by multiplying through by in . So take
i = (1 2i )(1 2i ) for 1 i , with c(i ) = i + i (which can be attained
by an appropriate polynomial c(t)). We dene C(t) so that C(i ) = c(i )(i )
where
(n+1)/2
(i ) = i
+ (1)(n1)/2 ( i i )( in in )
(x (1 2i )(1 2i )) = x(r1)/2
gr,i xi .
gr (x) :=
1i(r1)/2
0i(r3)/2
37
(r3)/2
fm+(r1)/2 =
j=0
38
ANDREW GRANVILLE
16. H.W. Lenstra, Jr., Galois theory and primality testing, Lecture Notes in Mathematics 1142
(1985), 169-189. MR0812498 (87g:11171)
17. H.W. Lenstra, Jr., and Carl Pomerance, Primality testing with Gaussian periods (to appear).
18. Yu. V. Matijasevich, Hilberts Tenth Problem, MIT Press, Cambridge, MA, 1993. MR1244324
(94m:03002b)
19. Preda Mihailescu and Roberto Avanzi, Ecient quasi - deterministic primality test improving AKS (to appear).
20. G
erald Tenenbaum and Michel Mend`es France, The Prime Numbers and Their Distribution,
Student Mathematical Library 6 (2000), Amer. Math. Soc. MR1756233 (2001j:11086)
21. Paulo Ribenboim, The new book of prime number records, Springer-Verlag, New York, 1995.
MR1377060 (96k:11112)
22. Jose Felipe Voloch, On some subgroups of the multiplicative group of nite rings (to appear).
matiques et Statistique, Universit
D
epartement de Mathe
e de Montr
eal, CP 6128
succ. Centre-Ville, Montr
eal, QC H3C 3J7, Canada
E-mail address: andrew@dms.umontreal.ca