1. Home
  2. Explore
  3. Mathematics
  4. Large Prime Number Generation

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Large Prime Number Generation

Primality Testing and Prime Number Generation

The Challenge of Large Primes

Finding a prime number with thousands of digits is impossible by trial division — checking divisibility by all smaller numbers — because the number of operations required exceeds the age of the universe. Yet computers routinely generate such primes for use in public-key cryptography, where they protect sensitive information on the internet. The key is that these algorithms do not guarantee 100% certainty; instead they produce probable primes with extremely high confidence (e.g., 99.9999%).

Fermat's Little Theorem

In 1640, the French mathematician Pierre de Fermat proved a fundamental property of all prime numbers. For any prime ppp and any positive integer aaa less than ppp:

ap−1≡1(modp)a^{p-1} \equiv 1 \pmod{p}ap−1≡1(modp)

This congruence means that ap−1a^{p-1}ap−1 is exactly one more than a multiple of ppp. The notation (modp)\pmod{p}(modp) refers to modular arithmetic, where numbers are considered only by their remainder when divided by the modulus ppp.

Computing ap−1(modp)a^{p-1} \pmod{p}ap−1(modp) for large numbers is done efficiently using modular exponentiation — repeatedly squaring and reducing modulo ppp — rather than computing the enormous intermediate value.

Fermat Primality Test

The Fermat test uses the contrapositive of Fermat's Little Theorem: if an−1≢1(modn)a^{n-1} \not\equiv 1 \pmod{n}an−1≡1(modn) for some base aaa, then nnn is composite (not prime). The base aaa is called a witness to compositeness.

  • Example with n=51n=51n=51, a=2a=2a=2: 250≡4(mod51)2^{50} \equiv 4 \pmod{51}250≡4(mod51). Since 4≢14 \not\equiv 14≡1, 515151 is composite (3×173 \times 173×17).

  • Example with n=17n=17n=17, a=5a=5a=5: 516≡1(mod17)5^{16} \equiv 1 \pmod{17}516≡1(mod17). The test declares prime, which is correct.

🚨 Fatal Flaw: Carmichael Numbers

The test can fail for composite numbers because some bases satisfy the congruence even though nnn is composite. A base that incorrectly passes the test is called a liar.

Carmichael numbers are composite numbers that satisfy an−1≡1(modn)a^{n-1} \equiv 1 \pmod{n}an−1≡1(modn) for all bases aaa that are coprime to nnn.

The first Carmichael number is 561=3×11×17561 = 3 \times 11 \times 17561=3×11×17. For most bases (e.g., a=100a=100a=100), 100560≡1(mod561)100^{560} \equiv 1 \pmod{561}100560≡1(mod561), so the Fermat test falsely declares 561561561 prime. Only bases that share a factor with 561561561 (like a=42a=42a=42, which gives 42560≡375(mod561)42^{560} \equiv 375 \pmod{561}42560≡375(mod561)) reveal compositeness.

Václav Šimerka discovered the first seven Carmichael numbers in the 19th century. Paul Erdős conjectured infinitely many exist, and in 1994 Red Alford, Andrew Granville, and Carl Pomerance proved that there are indeed infinitely many Carmichael numbers. This means the Fermat test is fundamentally unreliable for large numbers, because randomly finding a base that shares a factor with a huge Carmichael number is as hard as factoring the number itself.

Miller's Test

In the early 1970s, graduate student Gary Miller developed a stronger primality test that fixes the Carmichael problem by examining a sequence of exponentiations instead of just one.

How Miller's Sequence Works

Given nnn (candidate) and base aaa, first compute an−1(modn)a^{n-1} \pmod{n}an−1(modn). Then repeatedly divide the exponent by 2, computing a(n−1)/2a^{(n-1)/2}a(n−1)/2, a(n−1)/4a^{(n-1)/4}a(n−1)/4, etc., until the exponent becomes odd. This produces a sequence of values x0,x1,…,xkx_0, x_1, \ldots, x_kx0​,x1​,…,xk​ where xk=an−1(modn)x_k = a^{n-1} \pmod{n}xk​=an−1(modn) and each xix_{i}xi​ is the square of xi−1x_{i-1}xi−1​ modulo nnn.

Example with n=561n=561n=561, a=100a=100a=100:

  • Compute 100560≡1(mod561)100^{560} \equiv 1 \pmod{561}100560≡1(mod561) (Fermat passes here).

  • Halve exponent: 100280≡1(mod561)100^{280} \equiv 1 \pmod{561}100280≡1(mod561).

  • Halve again: 100140≡67(mod561)100^{140} \equiv 67 \pmod{561}100140≡67(mod561).

  • 10070≡166(mod561)100^{70} \equiv 166 \pmod{561}10070≡166(mod561).

  • 10035≡298(mod561)100^{35} \equiv 298 \pmod{561}10035≡298(mod561) (35 is odd; stop).

Sequence: 298,166,67,1,1298, 166, 67, 1, 1298,166,67,1,1.

Conditions for Primality

Miller’s test declares nnn prime if:

  1. The final term is 1 (same as the Fermat condition).

  2. Whenever a 1 appears in the sequence, its immediate predecessor is either 1 or n−1n-1n−1.

In the example above, the final term is 1 (condition 1 met), but the predecessor of the first 1 is 67 — not 1 or 560 — so condition 2 fails. Thus 561561561 is correctly identified as composite. Base 100100100 is a witness for Miller’s test but a liar for Fermat’s test.

Example with n=41n=41n=41, a=8a=8a=8:

Sequence: 840≡1(mod41)8^{40} \equiv 1 \pmod{41}840≡1(mod41), 820≡40(mod41)8^{20} \equiv 40 \pmod{41}820≡40(mod41), 810≡9(mod41)8^{10} \equiv 9 \pmod{41}810≡9(mod41) (odd exponent reached). Sequence: 9,40,19, 40, 19,40,1. Condition 1: final is 1. Condition 2: before the 1 is 40 (which is n−1n-1n−1), and before that is 9 (but the 9 is not preceded by 1? Actually the sequence goes 9→40→1, so the number before the 1 is 40 (good), and the number before 40 is 9 (no 1 to check). The condition checks every 1's predecessor; here there is only one 1, and its predecessor is 40 (=41-1), so condition 2 holds. The test declares prime, which is correct.

Why Condition 2 Works

The intuition comes from a property of prime moduli: if ppp is prime, the only square roots of 1 modulo ppp are 111 and p−1p-1p−1 (which is −1mod  p-1 \mod p−1modp). In the sequence, each term is the square of the previous term. If a 1 appears, its predecessor squared must be 1, so that predecessor must be either 1 or p−1p-1p−1. If it is anything else, nnn cannot be prime because that would be an "illegal" square root of 1.

Miller's Test Still Has Liars

Example with n=221n=221n=221 (13×1713 \times 1713×17), base a=174a=174a=174:

Sequence: 174220≡1174^{220} \equiv 1174220≡1, 174110≡220174^{110} \equiv 220174110≡220, 17455≡47174^{55} \equiv 4717455≡47 (odd). Sequence: 47,220,147, 220, 147,220,1. Final term is 1, and predecessor of 1 is 220 = 221−1221-1221−1. Both conditions are satisfied, so Miller’s test falsely declares 221221221 prime. Base 174174174 is a liar for Miller’s test.

So Miller’s test, while better, is not perfect for all bases.

The Extended Riemann Hypothesis and a Deterministic Miller Test

Miller proved a remarkable result: if the Extended Riemann Hypothesis (ERH) is true, then for every composite nnn, there exists a witness base aaa such that a<2(ln⁡n)2a < 2(\ln n)^2a<2(lnn)2. Consequently, to test nnn deterministically, one can run Miller’s test with every base aaa from 2 up to 2(ln⁡n)22(\ln n)^22(lnn)2. If none reveal compositeness, nnn must be prime. This is efficient because (ln⁡n)2(\ln n)^2(lnn)2 grows slowly; even for thousand-digit numbers, the bound is manageable.

The ERH is an extension of the famous Riemann Hypothesis about the distribution of prime numbers. Although most mathematicians believe it is true, it remains unproven. Using a deterministic test that depends on an unproven conjecture is risky, especially for applications like cryptography where lives depend on security.

The Miller–Rabin Probabilistic Test

Miller also showed that for any odd composite nnn, at least 50% of the possible bases (from 1 to n−1n-1n−1) are witnesses to compositeness under Miller’s test. This means that if one randomly selects a base and runs the test, a false declaration of primality has probability at most 1/21/21/2. Repeating the test with independent random bases drives the error probability down exponentially.

Michael Rabin later improved the bound from 50% to 75% (at least 3/43/43/4 of bases are witnesses). After kkk random tests all declaring "prime," the probability that nnn is actually composite is at most (1/4)k(1/4)^k(1/4)k.

  • With k=10k=10k=10: false positive probability <1/106< 1/10^6<1/106.

  • With k=20k=20k=20: <1/1012< 1/10^{12}<1/1012 (about 1 in a trillion).

This Miller–Rabin test became the gold standard for practical primality testing. It is used in virtually every secure internet connection (TLS/SSL), where large primes are needed for key generation. The test is fast, reliable enough for practical purposes, and does not depend on any unproven hypothesis.

Generating Large Primes with the Prime Number Theorem

Testing for primality and generating a random prime are two different tasks. The strategy for generating a large prime with nnn digits is deceptively simple:

  1. Pick a random nnn-digit number.

  2. Test if it is prime (e.g., with Miller–Rabin).

  3. If yes, output it; if no, repeat.

The success of this algorithm relies on the Prime Number Theorem, which estimates the density of primes. For an nnn-digit number, the probability that a randomly chosen number is prime is approximately:

1ln⁡(10n)=1nln⁡10≈12.3026 n\frac{1}{\ln(10^n)} = \frac{1}{n \ln 10} \approx \frac{1}{2.3026\, n}ln(10n)1​=nln101​≈2.3026n1​
  • For 10-digit numbers: ~1 in 23.

  • For 100-digit numbers: ~1 in 230.

  • For 1000-digit numbers: ~1 in 2300.

Even though 1 in 2300 seems low, a computer can test thousands of candidates per second, so a 1000-digit prime is typically found within a few seconds. In practice, the Miller–Rabin test is applied to each candidate, and the process terminates quickly.

(An actual implementation in Python can generate a 2281-digit prime — corresponding to the number of subscribers at the time — in a few minutes with 99.9999% confidence using 10 random bases per iteration.)

The AKS Primality Test

In 2002, Manindra Agrawal, Neeraj Kayal, and Nitin Saxena (two undergraduates at IIT Kanpur) discovered a deterministic polynomial-time primality test that always gives a correct answer — no probabilistic uncertainty and no reliance on unproven conjectures. The test, known as the AKS primality test, is a landmark theoretical achievement.

However, the AKS algorithm runs much slower than Miller–Rabin in practice, so it has not replaced the probabilistic test for everyday use. The search continues for a test that is both fast and deterministic, or for a proof of the Extended Riemann Hypothesis that would make Miller’s original deterministic test unconditionally correct. The intersection of number theory and computer science remains an active and fascinating field.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent