Primes and certificates

Primes and certificates

Saying a big number is prime is easy; proving it is harder. Here every yes and every no comes with its reason: below 264 a test with no exceptions, above it a certificate that a separate checker verifies, and when no certificate can be found, an honest refusal.

Listing and counting

import { primes, primePi, nthPrime, nextPrime, prevPrime, primesBetween } from "@ratmath/numtheory";

primes(50);                 // [2n, 3n, 5n, …, 47n]: the sieve of Eratosthenes
primePi(1000000);           // 78498n primes up to a million
nthPrime(10000);            // 104729n
nextPrime(10n ** 12n);      // 1000000000039n
prevPrime(100);             // 97n
primesBetween(10n ** 15n, 10n ** 15n + 100n);   // [1000000000000037n, 1000000000000091n]

The sieve crosses out the multiples of each prime p≤Np \le \sqrt{N}, starting at p2p^2; what survives is prime. primesBetween runs the same sieve on a window [a,b][a, b] using the primes up to b\sqrt b, so a window high up costs no more than its width. The prime number theorem says π(x)≈x/ln⁡x\pi(x) \approx x/\ln x: for x=106x = 10^6 that guess is 72 382, against the true 78 498.

Is it prime? Proven below 264

A strong probable prime test to base aa writes n−1=2sdn - 1 = 2^s d with dd odd and checks that ad≡1a^d \equiv 1 or a2rd≡−1(modn)a^{2^r d} \equiv -1 \pmod n for some r<sr < s. A prime always passes (the only square roots of 1 modulo a prime are ±1\pm 1); a composite fails for at least three quarters of the bases. The Baillie–PSW test combines one strong test to base 2 with a strong Lucas test. No composite passing both is known at all, and every number below 2642^{64} has been checked, so there it is a proof.

isPrime(2n ** 61n - 1n);          // true: proven, it is below 2^64
isProbablePrime(2n ** 255n - 19n); // true, but not a proof
isPrime(2n ** 255n - 19n);        // throws "unproven": n − 1 has too little known factorisation

Certificates

Above 2642^{64}, provePrime looks for a certificate. Pocklington's theorem: write n−1=F⋅Rn - 1 = F\cdot R with FF completely factored. If for each prime q∣Fq \mid F there is a base aa with

an−1≡1(modn)andgcd⁡ ⁣(a(n−1)/q−1, n)=1, a^{n-1} \equiv 1 \pmod n \quad\text{and}\quad \gcd\!\left(a^{(n-1)/q} - 1,\ n\right) = 1,

then every prime factor pp of nn satisfies p≡1(modF)p \equiv 1 \pmod F. (The order of aa modulo pp divides n−1n - 1 but not (n−1)/q(n-1)/q, so qeq^{e} divides it, and the order divides p−1p - 1.) If F>n−1F > \sqrt n - 1, a prime factor would exceed n\sqrt n, so nn is prime. The Brillhart–Lehmer–Selfridge refinement needs only F>n1/3F > n^{1/3}: writing n−1=F(c2F+c1)n - 1 = F(c_2 F + c_1), nn is prime exactly when c12−4c2c_1^2 - 4c_2 is not a perfect square.

Each prime qq in FF needs its own certificate, so the result is a tree, ending in primes below 2322^{32} that trial division settles.

const r = provePrime(2n ** 127n - 1n);
r.proven;                     // true
r.certificate.kind;           // "pocklington"
r.certificate.factors.map((f) => f.q);   // [2n, 3n, 7n, 19n, 43n, 73n, 127n, 337n, 5419n, 92737n]
verifyCertificate(r.certificate);         // { valid: true }

provePrime(561);              // { prime: false, proven: true, factor: 3n }
provePrime(2n ** 67n - 1n);   // { prime: false, proven: true, witness: 3n }: Cole's number

Why a separate verifier

verifyCertificate lives in its own file and imports nothing from the rest of the package: its own modular power, its own gcd, its own square root. It trusts nothing in the certificate; it recomputes FF, checks that F⋅R=n−1F\cdot R = n - 1, that each listed qeq^e divides FF and that they multiply to FF, each base condition, the size condition, and each sub-certificate in turn. So a bug in the search can produce no certificate, but it cannot produce a false one that passes. A composite answer is checked the same way: verifyComposite confirms a factor divides, or that a base is a Miller–Rabin witness.

When n−1n - 1 cannot be factored far enough (a third of its bits for BLS), provePrime turns to an elliptic curve: a curve modulo nn with a point whose order is a large prime qq, which is certified in turn. 1060+710^{60} + 7 is such a case. The same verifier checks those certificates; see Elliptic curve certificates. With { elliptic: false } only n−1n - 1 is used, and a number it cannot certify comes back with proven: false and the reason.

Try itEdit the code and run it with @ratmath/numtheory and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs