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 , starting at ; what survives is prime. primesBetween runs the same sieve on a window using the primes up to , so a window high up costs no more than its width. The prime number theorem says : for that guess is 72 382, against the true 78 498.
Is it prime? Proven below 264
A strong probable prime test to base writes with odd and checks that or for some . A prime always passes (the only square roots of 1 modulo a prime are ); 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 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 , provePrime looks for a certificate. Pocklington's theorem: write with completely factored. If for each prime there is a base with
then every prime factor of satisfies . (The order of modulo divides but not , so divides it, and the order divides .) If , a prime factor would exceed , so is prime. The Brillhart–Lehmer–Selfridge refinement needs only : writing , is prime exactly when is not a perfect square.
Each prime in needs its own certificate, so the result is a tree, ending in primes below 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 , checks that , that each listed divides and that they multiply to , 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 cannot be factored far enough (a third of its bits for BLS), provePrime turns to an elliptic curve: a curve modulo with a point whose order is a large prime , which is certified in turn. is such a case. The same verifier checks those certificates; see Elliptic curve certificates. With { elliptic: false } only is used, and a number it cannot certify comes back with proven: false and the reason.
@ratmath/numtheory and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.