Return integer with zero signaling common factor rather than boost::optional<Z>.

This commit is contained in:
Nick Thompson
2018-12-04 10:55:03 -07:00
parent cad4623876
commit 51b259da19
3 changed files with 16 additions and 18 deletions

View File

@ -14,7 +14,7 @@ A fast algorithm for computing modular multiplicative inverses based on the exte
namespace boost { namespace integer { namespace boost { namespace integer {
template<class Z> template<class Z>
boost::optional<Z> mod_inverse(Z a, Z m); Z mod_inverse(Z a, Z m);
}} }}
@ -22,20 +22,19 @@ A fast algorithm for computing modular multiplicative inverses based on the exte
[section Usage] [section Usage]
Multiplicative modular inverses exist if and only if /a/ and /m/ are coprime. int x = mod_inverse(2, 5);
So for example // prints x = 3:
std::cout << "x = " << x << "\n";
auto x = mod_inverse(2, 5); int y = mod_inverse(2, 4);
if (x) if (y == 0)
{
int should_be_three = x.value();
}
auto y = mod_inverse(2, 4);
if (!y)
{ {
std::cout << "There is no inverse of 2 mod 4\n"; std::cout << "There is no inverse of 2 mod 4\n";
} }
Multiplicative modular inverses exist if and only if /a/ and /m/ are coprime.
If /a/ and /m/ share a common factor, then `mod_inverse(a, m)` returns zero.
[endsect] [endsect]
[section References] [section References]

View File

@ -8,7 +8,6 @@
#define BOOST_INTEGER_MOD_INVERSE_HPP #define BOOST_INTEGER_MOD_INVERSE_HPP
#include <stdexcept> #include <stdexcept>
#include <boost/throw_exception.hpp> #include <boost/throw_exception.hpp>
#include <boost/optional.hpp>
#include <boost/integer/extended_euclidean.hpp> #include <boost/integer/extended_euclidean.hpp>
namespace boost { namespace integer { namespace boost { namespace integer {
@ -21,7 +20,7 @@ namespace boost { namespace integer {
// Would mod_inverse be sometimes mistaken as the modular *additive* inverse? // Would mod_inverse be sometimes mistaken as the modular *additive* inverse?
// In any case, I think this is the best name we can get for this function without agonizing. // In any case, I think this is the best name we can get for this function without agonizing.
template<class Z> template<class Z>
boost::optional<Z> mod_inverse(Z a, Z modulus) Z mod_inverse(Z a, Z modulus)
{ {
if (modulus < 2) if (modulus < 2)
{ {
@ -32,12 +31,12 @@ boost::optional<Z> mod_inverse(Z a, Z modulus)
if (a == 0) if (a == 0)
{ {
// a doesn't have a modular multiplicative inverse: // a doesn't have a modular multiplicative inverse:
return boost::none; return 0;
} }
euclidean_result_t<Z> u = extended_euclidean(a, modulus); euclidean_result_t<Z> u = extended_euclidean(a, modulus);
if (u.gcd > 1) if (u.gcd > 1)
{ {
return boost::none; return 0;
} }
// x might not be in the range 0 < x < m, let's fix that: // x might not be in the range 0 < x < m, let's fix that:
while (u.x <= 0) while (u.x <= 0)

View File

@ -36,17 +36,17 @@ void test_mod_inverse()
for (Z a = 1; a < modulus; ++a) for (Z a = 1; a < modulus; ++a)
{ {
Z gcdam = gcd(a, modulus); Z gcdam = gcd(a, modulus);
boost::optional<Z> inv_a = mod_inverse(a, modulus); Z inv_a = mod_inverse(a, modulus);
// Should fail if gcd(a, mod) != 1: // Should fail if gcd(a, mod) != 1:
if (gcdam > 1) if (gcdam > 1)
{ {
BOOST_TEST(!inv_a); BOOST_TEST(inv_a == 0);
} }
else else
{ {
BOOST_TEST(inv_a.value() > 0); BOOST_TEST(inv_a > 0);
// Cast to a bigger type so the multiplication won't overflow. // Cast to a bigger type so the multiplication won't overflow.
int256_t a_inv = inv_a.value(); int256_t a_inv = inv_a;
int256_t big_a = a; int256_t big_a = a;
int256_t m = modulus; int256_t m = modulus;
int256_t outta_be_one = (a_inv*big_a) % m; int256_t outta_be_one = (a_inv*big_a) % m;