Primearium Lab · encryption

The Prime Forge

Every encryption key needs fresh, secret primes with hundreds of digits. There is no list to look them up in and no formula to produce them. Your browser simply tries random numbers: most shatter against a small divisor, the few survivors face the Miller-Rabin test, and the one that passes every round is tempered into a prime.

Odd numbers tried
0
Shattered by the sieve
0
Miller-Rabin tests
0
Real search time
-
measured in your browser
Size of the prime
Miller-Rabin rounds
Replay

The prime number theorem predicts the forge

Near a number N, roughly one number in ln N is prime. Even numbers are never tried, so we need about ln N / 2 odd tries. For 2048 bits, ln N ≈ 2048 · 0.693 ≈ 1420, so about 710 tries. The sieve then throws away most of them for the price of a few divisions, and only about 115 need the expensive Miller-Rabin test.

your forgeprediction (prime number theorem)

The number of tries is random: it follows a geometric distribution with mean ln N / 2, so single runs scatter widely (from a handful to several times the mean). The average of many runs settles on the prediction.

Why it takes only a blink

Proven

1 Primes thin out slowly

The prime number theorem (1896) says the chance that a random number near N is prime is about 1 / ln N. Doubling the number of digits only doubles the number of tries. A 600-digit prime is found after a few hundred tries, not after billions.

Proven

2 The sieve is cheap

Proven

3 Miller-Rabin is fast and sharp

One round is a single modular power: ad mod n, done by repeated squaring in about 2 · (number of bits) multiplications. For every odd composite n > 9, at least 3/4 of the possible bases a expose it.

Liars and witnesses

Which bases tell the truth?

Fermat's little theorem: if n is prime, then an−1 ≡ 1 (mod n) for every base a not divisible by n. So when an−1 ≢ 1, the base a is a witness: n is certainly composite. When a composite n still gives 1, a is a liar. Each square below is one base a = 1, 2, …, n − 1. Switch between the two tests and watch Miller-Rabin unmask almost all of Fermat's liars.

    Proven

    Carmichael numbers: perfect impostors

    561 = 3 · 11 · 17 fools Fermat's test for every base coprime to it. Korselt (1899): n is such a number exactly when it is squarefree and p − 1 divides n − 1 for each prime p | n. Alford, Granville and Pomerance proved in 1994 that there are infinitely many. Below 106 there are 43 (counted by our test); those below 105:

    Proven

    Miller-Rabin: at most 1 in 4 bases lie

    Write n − 1 = d · 2s. Instead of only an−1, Miller-Rabin looks at the whole chain ad, a2d, a4d, … For a prime, the only square roots of 1 are ±1; a composite usually reveals another one. Monier and Rabin (1980): for odd composite n > 9 at most φ(n)/4 bases are strong liars. 91 = 7 · 13 and 8911 hit that bound exactly; 561 has only 10 strong liars out of 560 bases.

    2047: the first number that fools base 2

    2047 = 23 · 89, and 211 = 2048 ≡ 1 (mod 2047), so 21023 ≡ 1: base 2 lies even in the strong test. It is the smallest such number. Below 106 our test counts 245 composites that fool Fermat's test to base 2 (the first is 341 = 11 · 31) and only 46 that fool Miller-Rabin to base 2.

    How sure is “probably prime”?

    The forge picks its bases at random. Each round catches a composite with probability at least 3/4, independently, so after k rounds the chance that a composite survives is at most 4−k (Proven). For random candidates, like the ones in the forge, the real error is far smaller still (Damgård, Landrock and Pomerance, 1993).

    Proven

    Certainty for “small” numbers

    Fixed bases are enough below known limits. ψk is the smallest odd composite that fools the first k primes as Miller-Rabin bases; every number below ψk that passes those k bases is prime. With the 13 primes 2 … 41 the test is a proof for every n < ψ13 = 3 317 044 064 679 887 385 961 981 ≈ 3.3 · 1024 (Sorenson and Webster); the first 12 primes already decide every 64-bit number.

    Proven

    AKS (2002): a proof in polynomial time

    Agrawal, Kayal and Saxena found the first test that is deterministic, unconditional and polynomial in the number of digits. It settled a famous theoretical question, but even improved versions are far too slow for keys: nobody generates RSA primes with AKS.

    Proven Conjecture

    ECPP: a certificate anyone can check

    Elliptic curve primality proving (Goldwasser-Kilian, Atkin-Morain) builds a chain of elliptic curves that is a proof of primality and can be re-checked quickly (Proven). Its running time is only heuristic (Conjecture), but in practice it proves primes with many thousands of digits.

    Baillie-PSW: no counterexample known

    One strong test to base 2 plus a strong Lucas test. No composite number is known that passes both; it has been checked for every n < 264. Heuristics suggest that counterexamples exist, but none has ever been found. Many libraries use it as their standard test.

    How it is used

    From two primes to a key

    An RSA key needs two secret primes p and q of equal size; the public key contains only their product n = p · q. A 2048-bit key is made from two 1024-bit primes with 309 digits each. Encrypting is easy for everyone, decrypting needs p and q. How RSA works, and why multiplying is easy but factoring is hard, is shown in RSA secrets.

    This is a demonstration, not a key generator. The numbers are real primes, but never use them as a key: real keys are made by audited crypto libraries, with extra checks, and are never shown on a web page.

    Forge the two primes of a key

    Real generators also make sure the top bits are set (so n has exactly the right size) and that p and q are not too close to each other.

    Why randomness matters

    If two keys share one prime, anybody can break both in a millisecond: gcd(n1, n2) = p, without any factoring. With good randomness this practically never happens. Here 24 devices make keys; in the weak case each picks its first prime from a generator that had only 8 possible starting states.

    It really happened

    • Documented incident 2008, Debian OpenSSL. A code change (2006) left the random generator seeded essentially only by the process ID, so only about 32,768 different keys per type and size could be produced (CVE-2008-0166).
    • Documented incident 2012, “Ron was wrong, Whit is right”. Lenstra and colleagues collected millions of public keys and found that about two in every thousand RSA moduli offered no security, mostly because they shared a prime with another key.
    • Documented incident 2012, “Mining your Ps and Qs”. Heninger and colleagues scanned the internet and computed private keys of 0.50% of TLS hosts and 0.03% of SSH hosts, mostly embedded devices that generated keys right after boot with little entropy.
    • Documented incident 2017, ROCA. A widely used chip library (Infineon) made primes with a hidden structure instead of truly random ones. Nemec and colleagues showed how to factor such keys; smart cards, security tokens and national ID cards (for example in Estonia) were affected (CVE-2017-15361).

    For mathematicians

    exact statements, the numbers we verified, references

    Expected number of tries

    Proven Prime number theorem (Hadamard, de la Vallée Poussin 1896): π(x) ~ x / ln x. A uniformly random odd b-bit number is prime with probability ≈ 2 / ln N, so the number of independent draws until a prime is geometric with mean E ≈ ln N / 2 = (b ln 2)/2 (the integral below averages over [2b−1, 2b)). After sieving by the odd primes up to 997, a fraction S of the odd numbers survives (Mertens: S ≈ 2e−γ/ln 997 ≈ 16.3%), so ≈ S · E candidates reach Miller-Rabin; the last one is the prime.

    E[X] = 1 / ( 2 ∫12 du / ((b−1) ln 2 + ln u) ) ≈ ln N / 2   ·   S = ∏3≤p≤997 (1 − 1/p) =

    A separate verification program checks these results: the formula agrees with exact prime counts for 16, 20 and 24 bits within 0.4%; 2000 forged 64-bit primes and 400 forged 256-bit primes have mean try counts within the expected statistical spread of the prediction; every forged prime is confirmed by the site's shared primality test.

    Fermat, Carmichael, Miller-Rabin

    Proven Fermat's little theorem; Korselt's criterion (1899); Alford-Granville-Pomerance (1994): C(x) > x2/7 for all large x, so there are infinitely many Carmichael numbers. Every Carmichael number has at least three prime factors.

    n − 1 = d · 2s, d odd:   ad ≡ 1   or   ad·2r ≡ −1 (mod n) for some 0 ≤ r < s

    Proven Monier (1980), Rabin (1980): an odd composite n > 9 has at most φ(n)/4 strong liars among 1 ≤ a ≤ n − 1. Hence k independent random bases err with probability ≤ 4−k. Deterministic bases: ψ1 = 2047, ψ2 = 1 373 653, ψ3 = 25 326 001, ψ4 = 3 215 031 751 (Pomerance-Selfridge-Wagstaff 1980), up to ψ8 by Jaeschke (1993), ψ9-ψ11 by Jiang-Deng (2014), ψ12, ψ13 by Sorenson-Webster (2017).

    Conjecture Miller (1976): if the generalized Riemann hypothesis is true, testing all bases a ≤ 2 (ln n)2 (Bach's constant, 1990) is a deterministic polynomial-time test. GRH for Dirichlet L-functions was proven formally in Nov 2026 (Lean), so this test is now proven deterministic; a human-readable account and peer review are in progress.

    What we checked by computer

    Deterministic Miller-Rabin (13 bases) agrees with a sieve for every n ≤ 106. 43 Carmichael numbers below 106, 245 Fermat and 46 strong pseudoprimes to base 2. Strong liars ≤ φ(n)/4 for every odd composite 9 < n ≤ 104 (n = 9 has 2 of 6). The table values ψ1 … ψ13: factorizations and the fact that they fool the first k prime bases are verified; minimality is verified by search for ψ1-ψ3 and taken from the papers above for the rest.

    A separate verification program checks the results. The forge draws candidates and Miller–Rabin bases from a cryptographically secure random source, sieves by the 167 odd primes below 1000, and runs in a Web Worker with JavaScript BigInt.

    References

    1. J. Hadamard; C. de la Vallée Poussin (1896), the prime number theorem; see H. Davenport, Multiplicative Number Theory, Springer.
    2. A. Korselt, Problème chinois, L'Intermédiaire des Mathématiciens 6 (1899), 142-143.
    3. W. R. Alford, A. Granville, C. Pomerance, There are infinitely many Carmichael numbers, Annals of Math. 139 (1994), 703-722.
    4. G. L. Miller, Riemann's hypothesis and tests for primality, J. Comput. System Sci. 13 (1976), 300-317; E. Bach, Explicit bounds for primality testing and related problems, Math. Comp. 55 (1990), 355-380.
    5. M. O. Rabin, Probabilistic algorithm for testing primality, J. Number Theory 12 (1980), 128-138.
    6. L. Monier, Evaluation and comparison of two efficient probabilistic primality testing algorithms, Theoret. Comput. Sci. 12 (1980), 97-108.
    7. I. Damgård, P. Landrock, C. Pomerance, Average case error estimates for the strong probable prime test, Math. Comp. 61 (1993), 177-194.
    8. C. Pomerance, J. L. Selfridge, S. S. Wagstaff Jr., The pseudoprimes to 25 · 109, Math. Comp. 35 (1980), 1003-1026; R. Baillie, S. S. Wagstaff Jr., Lucas pseudoprimes, Math. Comp. 35 (1980), 1391-1417.
    9. G. Jaeschke, On strong pseudoprimes to several bases, Math. Comp. 61 (1993); Y. Jiang, Y. Deng, Strong pseudoprimes to the first eight prime bases, Math. Comp. 83 (2014); J. Sorenson, J. Webster, Strong pseudoprimes to twelve prime bases, Math. Comp. 86 (2017), 985-1003.
    10. M. Agrawal, N. Kayal, N. Saxena, PRIMES is in P, Annals of Math. 160 (2004), 781-793 (preprint 2002).
    11. S. Goldwasser, J. Kilian, Almost all primes can be quickly certified, STOC 1986; A. O. L. Atkin, F. Morain, Elliptic curves and primality proving, Math. Comp. 61 (1993), 29-68.
    12. NIST, FIPS 186-5, Digital Signature Standard (2023): how RSA primes are generated and tested in practice.
    13. A. K. Lenstra, J. P. Hughes, M. Augier, J. W. Bos, T. Kleinjung, C. Wachter, Ron was wrong, Whit is right, IACR ePrint 2012/064 (CRYPTO 2012).
    14. N. Heninger, Z. Durumeric, E. Wustrow, J. A. Halderman, Mining your Ps and Qs: detection of widespread weak keys in network devices, USENIX Security 2012.
    15. M. Nemec, M. Sys, P. Svenda, D. Klinec, V. Matyas, The return of Coppersmith's attack: practical factorization of widely used RSA moduli, ACM CCS 2017.
    16. OEIS A002997 (Carmichael numbers), A001567 (Fermat pseudoprimes to base 2), A001262 (strong pseudoprimes to base 2), A014233 (ψk).