-
Notifications
You must be signed in to change notification settings - Fork 199
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
s_mp_invmod_odd returns wrong result for negative numbers #534
Comments
That output is correct. LibTomMath computes two different remainders. If we take The comment about the absolute value
I think Pari/GP can be trusted to be correct so let it check
That is correct, Pari/GP gives
That is correct, The (slower but general) function So what happened and why did it not get caught? I cannot see a reason for that sign to get copied over and would concur with @friedrichsenm and remove that part completely, including the mentioned comment. Additionally: add some tests to Might take an hour or two. (Sorry, @ssinai1 bugfixes first, but you are not forgotten! ) |
While writing the tests and making a small table with Pari/GP I have been reminded of the fact that The generated table:
I faintly remember there was an argument against it but forgot and couldn't find it. |
Fix: removed sign operation in s_mp_invmod_odd (issue #534)
Using
mp_invod
can return incorrect results for negative numbers. Using it to find the inverse of-1 mod 7
yields-6
instead of6
. The problem looks like it could be because of a couple of places:libtommath/s_mp_invmod_odd.c
Lines 31 to 32 in 4b47368
The comment is assuming that
mp_mod
gives the absolute value, but it doesn't. It will make the value positive though.Then, in
libtommath/s_mp_invmod_odd.c
Lines 98 to 109 in 4b47368
the sign of
a
is used to flip the sign of the inverse, but the sign should already be correct sincemp_mod
is using a positive member of the equivalence class fora
. Removing this sign change should fix it.The text was updated successfully, but these errors were encountered: