a*p % m may overflow, do not perform naive multiplication in unit tests or undefined behavior may result. [CI SKIP]

This commit is contained in:
Nick Thompson
2018-10-26 11:19:43 -06:00
parent 3f1603938c
commit 2d463f3ee7
4 changed files with 35 additions and 34 deletions

View File

@ -35,20 +35,19 @@ boost::optional<Z> mod_inverse(Z a, Z modulus)
return {};
}
euclidean_result_t<Z> u = extended_euclidean(a, modulus);
Z gcd = u.gcd;
if (gcd > 1)
if (u.gcd > 1)
{
return {};
}
Z x = u.x;
x = x % modulus;
// x might not be in the range 0 < x < m, let's fix that:
while (x <= 0)
while (u.x <= 0)
{
x += modulus;
u.x += modulus;
}
BOOST_ASSERT(x*a % modulus == 1);
return x;
// While indeed this is an inexpensive and comforting check,
// the multiplication overflows and hence makes the check itself buggy.
//BOOST_ASSERT(u.x*a % modulus == 1);
return u.x;
}
}}