Closed friedrichsenm closed 3 years ago
My use case only involves numbers 1 mod 4, so this may happen other times too
Unlikely, p \cong 1 mod 4
with p
a perfect square of a prime A001248 is a special case for the Jacobi symbol. A001248. LibTomMath's primetest checks for perfect squares, so checking for primality would catch that.
I'm a bit torn here. On one side: the Tonelli-Shanks algorithm used here assumes the modulus to be prime and the user is expected to check for that condition. On the other side: LibTomMath should fail safely in all cases of error (if mathematically possible and economically feasible, of course!) especially in the case of wrong input.
The solution for composites p \cong 1 mod 4
for the first loop works but it can still run a long time if the factor is large.
The solution for composites p \cong 1 mod 4)
for the first loop works but it can still run a long time if M
is large (can be up to 2^INT_MAX
large) but I have not yet checked if that is possible at all and if such a very large M
is itself a red flag.
Still: both patches are cheap and allow the algorithm to halt in case of bad input.
(Can be made even cheaper because S
and therefore M
cannot get bigger than 2^INT_MAX
)
What do my dear colleagues say? Is it a proper bug or not?
Sorry for the delay but the current situation is an unknown kind of stress; we were used to sales promising the moon to the customers, pointy-haired bosses insisting on implementing aforementioned sattelite and we won't even twitch anymore when we hear the supersonic boom of a passing deadline but this is a new one. A lot of us are also self-employed/have a little company and with some of our clients in financial troubles, way too often fatal nowadays, while others take advantage of the situation (to put it mildly!) we might not be as prompt in our reactions as the users of LibTomMath became accustomed to.
No problem with the delay!
FYI, the use case is for an algorithm that takes in a positive number and outputs its representation as the sum of four squares. High level, it makes a couple of random guesses for the first two square and then hopes that what's left is is a prime number 1 mod 4 so you can find its representation as the sum of two squares. You could do a primality test here before calling the function to make sure it will work, but adding that check might slow down the algorithm more than just trying to find the other two squares.
If
*prime
isn't prime but is composite that is 1 mod 4, you can hit infinite loops in two places. (My use case only involves numbers 1 mod 4, so this may happen other times too)The first is in lines 62-69
I was able to fix this in a local copy adding the following check after line 66
The second infinite loop was in lines 84-111
I was able to fix this by adding the following after line 88