Engineering/Mathematics/Algorithm engineering
Deterministic Primality Testing: the AKS Algorithm
In 2002 three researchers settled a question open for centuries: primality can be decided in deterministic polynomial time, unconditionally. The algorithm is short enough to state on one page, its proof is elementary, and it is almost never the right tool for the job.
- Landmark result
- Computing
- Theory over practice
- ≈13 min read
- Resolved PRIMES ∈ P
01
Executive summary
Before 2002, primality was known to be decidable in polynomial time only under unproved hypotheses, or with randomness, or with certificates that were hard to find. The AKS algorithm removed all three qualifications at once.
Its starting point is a polynomial generalisation of Fermat's little theorem that characterises primality exactly — no analogue of Carmichael numbers exists to spoil it. The difficulty is that testing the identity directly costs exponential time, so the algorithm works modulo a small auxiliary polynomial Xr−1, and the technical heart of the proof is showing that a suitable small r exists and that the truncated test is still conclusive.
Why include it in a practitioner's library
Not because it will be deployed, but because it settles what is possible. Security arguments must distinguish between 'no efficient algorithm is known' and 'no efficient algorithm exists'. AKS is a case where the first became the second, and it is a reminder that factoring and discrete logarithms enjoy no proof of hardness either.
02
The polynomial identity
Characterisation of primality
If n is prime, every binomial coefficient C(n,i) for 0 < i < n is divisible by n, so all middle terms vanish — this is the Frobenius endomorphism. If n is composite with a prime power pk ∥ n, then C(n,p) is not divisible by pk, so the coefficient of Xp survives and the identity fails.
Unlike the integer Fermat condition, this test has no false positives at all. The obstacle is purely computational: the left side has n+1 terms, so evaluating it takes time proportional to n, which is exponential in the input length.
The key idea in one sentence
Test the identity modulo Xr−1 for a small r, so that only r coefficients need to be tracked; the proof then shows that if the truncated identity holds for enough values of a, no composite can survive.
03
The algorithm
AKS primality test (original formulation)
- input: integer n > 1
- 1. if n is a perfect power b^e (e ≥ 2): return COMPOSITE
- 2. find the smallest r with ord_r(n) > (log₂ n)²
- 3. for a = 1 .. r: if 1 < gcd(a,n) < n: return COMPOSITE
- 4. if n ≤ r: return PRIME
- 5. for a = 1 .. ⌊√(φ(r))·log₂ n⌋:
- if (X + a)^n ≢ X^n + a (mod X^r − 1, n): return COMPOSITE
- 6. return PRIME
Step 5 dominates: each check is a modular exponentiation in the ring ℤ_n[X]/(X^r − 1), costing Õ(r·ℓ²) bit operations, repeated Õ(√r·ℓ) times.
Rule out perfect powers
Cheap and necessary: the later argument assumes n is not a prime power.
Find a suitable r
A counting argument shows some r = O(ℓ5) works, so the search terminates quickly. This bound is what drives the original complexity estimate.
Screen for small factors
The gcd loop catches any composite with a factor below r — a side benefit that also simplifies the analysis.
Test the truncated identity
The expensive stage. Each value of a requires one exponentiation of a polynomial with r coefficients modulo n.
Conclude
If every check passes, n is prime — with certainty, and with no hypothesis assumed.
Structure of the correctness proof
Suppose a composite n passed every check, and let p be a prime factor. The polynomials X + a that satisfy the identity generate a multiplicative group in a suitable quotient ring. Counting that group two ways — from below using the many a values, and from above using the number of distinct residues achievable by the exponents nipj — produces a contradiction unless n is a power of p, which step 1 already excluded.
04
Complexity and improvements
| Version | Complexity | Note |
|---|---|---|
| Original AKS, 2002 | Õ(ℓ12) | As first announced |
| Revised analysis | Õ(ℓ10.5) | Sharper bound on r in the published version |
| Lenstra–Pomerance variant | Õ(ℓ6) | Uses Gaussian periods in place of Xr−1 |
| Conditional on Artin-type conjectures | Õ(ℓ3) | If suitable small r always exist, which is expected but unproved |
Polynomial time is not the same as practical
The exponent and the hidden constants matter enormously. At cryptographic sizes AKS is slower than Miller–Rabin by roughly ten orders of magnitude, and slower than ECPP — which produces an actual certificate — by several. Its value is that it exists, is unconditional, and requires no randomness.
05
AKS in the landscape of primality proving
| Method | Output | Needs randomness? | Needs a hypothesis? | Practical at 1024 bits? |
|---|---|---|---|---|
| Miller–Rabin | Probable prime | Yes | No | Yes — milliseconds |
| Miller–Rabin, fixed bases | Proof below a threshold | No | No | Yes, for small inputs only |
| Miller–Rabin, ERH base bound | Proof | No | Yes (ERH) | Yes, but conditional |
| Pocklington | Certificate | Only to find the prime | No | Yes, when p−1 is partly factored |
| ECPP | Certificate | Yes | Heuristic running time | Yes — seconds to minutes |
| AKS | Yes/no answer | No | No | No |
ECPP is the practical choice for certified primes: its certificates are short and verify in milliseconds, even though its own running-time analysis is heuristic.
- Certificates are often what is actually wanted. A short proof that a third party can check quickly is more useful than an unrepeatable yes/no answer, which is why ECPP and Pocklington dominate in practice.
- The result changed the theoretical landscape, not the engineering one. No deployed system switched tests; but the question of whether randomness is essential for primality was closed.
- The analogous question for factoring remains wide open. No polynomial-time classical algorithm is known, and none is proved impossible — which is precisely the assumption underlying RSA.
06
Quick reference and FAQ
| Fact | Statement |
|---|---|
| Result | PRIMES is in P (Agrawal, Kayal and Saxena, 2002) |
| Core identity | (X+a)n ≡ Xn+a in ℤn[X] iff n is prime |
| Truncation | Work modulo Xr−1 with ordr(n) > (log2 n)2 |
| Bound on r | O(ℓ5) suffices |
| Original complexity | Õ(ℓ10.5) |
| Best variant | Õ(ℓ6) (Lenstra–Pomerance) |
| Error | None — deterministic and unconditional |
| Practical use | None at cryptographic sizes |
Why does the polynomial identity avoid the Carmichael problem?
Does AKS produce a factor when it says composite?
Would a faster variant ever displace Miller–Rabin?
Does AKS say anything about factoring?
08
References and further reading
- M. Agrawal, N. Kayal and N. Saxena, 'PRIMES is in P', Annals of Mathematics 160 (2004) 781–793.
- V. Shoup, A Computational Introduction to Number Theory and Algebra, Cambridge University Press, 2005 — Chapter 22.
- H. W. Lenstra Jr. and C. Pomerance, 'Primality testing with Gaussian periods', J. Eur. Math. Soc. 21 (2019) 1229–1269.
- A. O. L. Atkin and F. Morain, 'Elliptic curves and primality proving', Math. Comp. 61 (1993) 29–68.
KEVOS® Knowledge LibraryEngineering → MathematicsTaxonomy ID: ENG-MATHPage ID: aks-deterministic-primality-testingReview cycle: annual
