Add Tonelli-Shanks algorithm for modular square roots - #14995
Open
felipeofdev-ai wants to merge 3 commits into
Open
Add Tonelli-Shanks algorithm for modular square roots#14995felipeofdev-ai wants to merge 3 commits into
felipeofdev-ai wants to merge 3 commits into
Conversation
Implements Legendre symbol checks and both the p≡3 (mod 4) fast path and the full Tonelli-Shanks procedure, with doctests for success and error cases.
for more information, see https://pre-commit.ci
Author
|
Thanks for reviewing — CI is green on my side. Happy to adjust anything you need. — Felipe Fernandes · Systems & Agentic AI Engineer |
felipeofdev-ai
force-pushed
the
feat/tonelli-shanks
branch
from
August 4, 2026 02:56
93bf6ca to
26a16e1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your change:
Checklist:
Summary
Adds
maths/tonelli_shanks.pyimplementing the Tonelli-Shanks algorithm to compute modular square roots modulo an odd prime.legendre_symbolfor quadratic-residue checksprime ≡ 3 (mod 4)prime ≡ 1 (mod 4)ValueErrorfor non-residues / invalid primespython -m doctest)Reference: https://en.wikipedia.org/wiki/Tonelli%E2%80%93Shanks_algorithm
— Felipe Fernandes · Systems & Agentic AI Engineer
https://github.com/felipeofdev-ai · https://felipeofdev-ai.github.io/