Skip to content

fix: RSA random_prime always returns 1000003 and can return the prime it should skip - #262

Open
saviourmk wants to merge 1 commit into
pluto:mainfrom
saviourmk:fix/rsa-random-prime
Open

saviourmk wants to merge 1 commit into
pluto:mainfrom
saviourmk:fix/rsa-random-prime

Conversation

@saviourmk

Copy link
Copy Markdown

random_prime in the RSA module says it generates a random prime above 1 000 000, but it counts up from 1 000 000 and stops at the first prime, so it returns 1 000 003 every time. The first_prime argument looks meant to skip a prime you already have, but the loop condition !is_prime(n) && n != first_prime stops as soon as n == first_prime, so random_prime(1_000_003) hands back 1 000 003 again, and two calls give p == q.

It changes the following:

It now draws random numbers between 1 000 000 and 2 000 000 until it finds a prime that isn't first_prime. The doc comment says so and has a small example of getting the two primes of a key. I replaced the second of the two identical random_prime tests with one that checks the skipped prime never comes back, and one that checks 20 calls don't all return the same prime. Both fail on main and pass here.

Trial division is still fine at this size. If the Miller-Rabin module from #261 goes in, this could use it later for bigger primes.

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.

1 participant