Skip to content

feat: Miller-Rabin primality testing - #261

Open
saviourmk wants to merge 1 commit into
pluto:mainfrom
saviourmk:feat/primality-testing
Open

saviourmk wants to merge 1 commit into
pluto:mainfrom
saviourmk:feat/primality-testing

Conversation

@saviourmk

Copy link
Copy Markdown

This PR closes #92

It changes the following:

Adds a primality module with Miller-Rabin. is_prime is deterministic for every u64, by testing the first twelve primes as bases, which is known to be enough below 2^64. miller_rabin is the probabilistic version with a chosen number of random bases, and random_prime(bits, rng) returns a random prime with exactly that many bits. mod_pow works in u128 so the squarings can't overflow. The README walks from the Fermat test and Carmichael numbers to why Miller-Rabin's square root check catches them, and both are in the book summary and the main README list.

Tests compare both versions against trial division for every n below 20 000, check Carmichael numbers and strong pseudoprimes for the smaller base sets (2047, 3 215 031 751, 3 825 123 056 546 413 051 and so on), some large primes and composites near 2^64, and that random_prime returns the right bit length from 2 to 64 bits.

I didn't hook it into RSA here so this stays one thing. AKS from the issue could come later as a slower but deterministic comparison.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: primality testing

1 participant