Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the Strong Lucas Probable Prime test #640

Merged
merged 4 commits into from
Nov 15, 2024

Conversation

chiphogg
Copy link
Collaborator

This is more involved than the Miller-Rabin test, but we can tame the complexity by breaking it down into helper functions, performing tasks such as:

  • Find an appropriate D parameter.
  • Increment the index of the (U_k, V_k) sequence elements by one.
  • Double the index of the (U_k, V_k) sequence elements.

etc.

With these helpers, the algorithm becomes straightforward (see, for instance,
https://en.wikipedia.org/wiki/Lucas_pseudoprime#Strong_Lucas_pseudoprimes). We start by ruling out perfect squares (because if we don't, then the search for D will never terminate). Then we find our D, and decompose n + 1 into s and d parameters (exactly as we did for Miller-Rabin, except there we used n - 1). At this point, the strong test is easy: check whether U_d is 0, then check V_d for 0, as well as (s - 1) copies of V_next, where the index of V_next doubles the previous checked index.

A similar testing strategy as for the Miller Rabin gives us sufficient confidence.

  1. Test that we get small primes right.
  2. Test that we get known pseudoprimes "correctly wrong".
  3. Test some really big primes.

(Remember, a probable prime test must mark every actual prime as "probably prime".)

Helps #509.

This is more involved than the Miller-Rabin test, but we can tame the
complexity by breaking it down into helper functions, performing tasks
such as:

- Increment the index of the (U_k, V_k) sequence elements by one.
- Double the index of the (U_k, V_k) sequence elements.
- Find an appropriate D parameter.

etc.

With these helpers, the algorithm becomes straightforward (see, for
instance,
https://en.wikipedia.org/wiki/Lucas_pseudoprime#Strong_Lucas_pseudoprimes).
We start by ruling out perfect squares (because if we don't, then the
search for `D` will never terminate).  Then we find our `D`, and
decompose `n + 1` into `s` and `d` parameters (exactly as we did for
Miller-Rabin, except there we used `n - 1`).  At this point, the strong
test is easy: check whether `U_d` is 0, then check `V_d`, as well as `V`
for all successive doublings of the index less than `s`.

A similar testing strategy as for the Miller Rabin gives us sufficient
confidence.

1. Test that we get small primes right.
2. Test that we get known pseudoprimes "correctly wrong".
3. Test some really big primes.

(Remember, a probable prime test must mark every actual prime as
"probably prime".)

Helps mpusz#509.
mpusz
mpusz previously approved these changes Nov 15, 2024
Copy link
Owner

@mpusz mpusz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The implementation looks good. I have no idea how it works, though! 😉 To much maths for me... I would need to spend some time reading about those algorithms, but I do not have time for it now.

Thanks for doing this!!!

Looks like we were violating a precondition.
@mpusz
Copy link
Owner

mpusz commented Nov 15, 2024

Subtracting from `n` doesn't work in literally every case: it fails for
`0` input.
@chiphogg
Copy link
Collaborator Author

Not anymore!

@mpusz mpusz merged commit ec91498 into mpusz:master Nov 15, 2024
74 of 76 checks passed
@chiphogg chiphogg deleted the chiphogg/strong-lucas#509 branch November 15, 2024 18:29
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.

2 participants