Apex › Cryptography

Is this number prime? (Miller–Rabin)

How crypto libraries find the big primes RSA needs — deciding primality in a few modular exponentiations, no factoring. Write n−1 = d·2^r; for a base a, compute a^d mod n and square up to r−1 times. Mod a prime the only square roots of 1 are ±1, so a prime must start at 1 or hit n−1; reach 1 through any other root and n is composite, a is a witness. At least 3/4 of bases witness any composite, so k rounds err with probability ≤ 4⁻ᵏ, and unlike Fermat there are no Carmichael numbers that fool every base. Verified in node: matches trial division for n<5000, ≥3/4 of bases witness every composite, and it exposes 561/1105/1729/2465. Test a number and watch the squaring sequence decide.

More in Cryptography