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

math/big: infinite loop in Int.ModSqrt for p = 1 #51747

Open
sylvainpelissier opened this issue Mar 17, 2022 · 9 comments
Open

math/big: infinite loop in Int.ModSqrt for p = 1 #51747

sylvainpelissier opened this issue Mar 17, 2022 · 9 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@sylvainpelissier
Copy link

When passing p=1 in ModSqrt function, the code goes in infinite loop:

for Jacobi(&n, p) != -1 {

The Jacobi symbol will gives one for ever.

For example: https://go.dev/play/p/37ExZ6Y-Hdp

@ALTree ALTree changed the title Possible infinite loop in Tonelli–Shanks algorithm math/big: infinite loop in Int.ModSqrt for p = 1 Mar 17, 2022
@ALTree ALTree added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 17, 2022
@ALTree ALTree added this to the Go1.19 milestone Mar 17, 2022
@ALTree
Copy link
Member

ALTree commented Mar 17, 2022

It does indeed go into an infinite loop. Thanks for reporting.

The method's documentation says

The modulus p must be an odd prime.

and 1 is not a prime, but going into an infinite loop is still not the best way to handle an invalid parameter.

@ALTree
Copy link
Member

ALTree commented Mar 17, 2022

cc @griesemer @FiloSottile as per owners.

@gopherbot
Copy link

Change https://go.dev/cl/394077 mentions this issue: math/big: infinite loop in Int.ModSqrt for p = 1

@hopehook
Copy link
Member

hopehook commented Apr 5, 2022

Other examples of causing infinite loop in Int.ModSqrt:

  • p = -1
  • p = -9
  • p = 9

@gopherbot
Copy link

Change https://go.dev/cl/402457 mentions this issue: math/big: limit search for non-square in ModSqrt

@ianlancetaylor
Copy link
Contributor

Rolling forward to 1.20.

@griesemer
Copy link
Contributor

@rolandshoemaker There's a pending CL. Should this move to 1.23?

@rolandshoemaker
Copy link
Member

I think we can probably move this to backlog for now, it is unclear if we want to fix this with the particular method in the CL, and it's a relatively low priority problem.

@griesemer griesemer modified the milestones: Go1.22, Backlog Dec 13, 2023
@gonzalochief
Copy link

gonzalochief commented Apr 11, 2024

Hi,
I was going through this issue, and the only way to avoid getting an error on the implementation of the Tonelli-Shanks algorithm is to verify and validate that p is in fact a prime number.
The Jacobi function works when p is an odd integer, but the TS algorithm requires that p belongs to a subset of of integers which are primes.
The issue with that validation is that there are only two ways to achieve it:

  1. using a deterministic method, such as the AKS primality test. This method is computationally intensive, as it needs to compute polynomial modulus as part of the algorithm.
  2. Using a probabilistic method, such as the one proposed on https://go.dev/cl/394077

I am working on an alternative using AKS, by implementing an IsPrime() function, but the proposed change above might be an acceptable solution. Please let me know if a deterministic solution is of interest. This would eliminate any edge case that could come from using a probabilistic method to test if p is prime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
Status: No status
Development

No branches or pull requests

8 participants