Skip to content

Commit

Permalink
Select smaller root in some composite sqrtmod cases
Browse files Browse the repository at this point in the history
  • Loading branch information
danaj committed May 27, 2024
1 parent 7dea449 commit 8343430
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions rootmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ static UV _sqrtmod_prime_power(UV a, UV p, UV e) {
if (a >= p) a %= p;
if (p == 2 || a == 0) return a;
r = _sqrtmod_prime(a,p);
if (p-r < r) r = p-r;
return (sqrmod(r,p) == a) ? r : UV_MAX;
}

Expand All @@ -215,6 +216,7 @@ static UV _sqrtmod_prime_power(UV a, UV p, UV e) {

np = (p != 2 || (n > (UV_MAX/p))) ? n : n * p;
r = addmod(s, gcddivmod(submod(a,sqrmod(s,np),np), addmod(s,s,np), n), n);
if (n-r < r) r = n-r;
if (sqrmod(r,n) != (a % n)) return UV_MAX;
return r;
}
Expand Down

0 comments on commit 8343430

Please sign in to comment.