2018-01-28 14:47:14 -06:00
|
|
|
/*
|
|
|
|
* (C) Copyright Nick Thompson 2018.
|
|
|
|
* Use, modification and distribution are subject to the
|
|
|
|
* Boost Software License, Version 1.0. (See accompanying file
|
2021-01-20 11:34:42 +03:00
|
|
|
* LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
|
2018-01-28 14:47:14 -06:00
|
|
|
*/
|
2018-11-05 22:56:03 +03:00
|
|
|
|
|
|
|
#include "multiprecision_config.hpp"
|
2018-10-29 08:52:20 -06:00
|
|
|
|
|
|
|
#ifndef DISABLE_MP_TESTS
|
2018-11-04 01:00:57 +03:00
|
|
|
#include <boost/integer/mod_inverse.hpp>
|
|
|
|
#include <boost/cstdint.hpp>
|
|
|
|
#include <boost/optional/optional.hpp>
|
2018-10-29 13:10:02 -06:00
|
|
|
#include <boost/core/lightweight_test.hpp>
|
2018-01-28 14:47:14 -06:00
|
|
|
#include <boost/multiprecision/cpp_int.hpp>
|
|
|
|
#include <boost/integer/common_factor.hpp>
|
|
|
|
|
|
|
|
using boost::multiprecision::int128_t;
|
2018-02-10 13:56:11 -06:00
|
|
|
using boost::multiprecision::int256_t;
|
2018-02-09 17:19:26 -06:00
|
|
|
using boost::integer::mod_inverse;
|
2018-01-28 14:47:14 -06:00
|
|
|
using boost::integer::gcd;
|
|
|
|
|
|
|
|
template<class Z>
|
2018-02-09 17:19:26 -06:00
|
|
|
void test_mod_inverse()
|
2018-01-28 14:47:14 -06:00
|
|
|
{
|
2018-10-26 11:19:43 -06:00
|
|
|
//Z max_arg = std::numeric_limits<Z>::max();
|
2018-10-25 09:38:16 -06:00
|
|
|
Z max_arg = 500;
|
2018-01-28 14:47:14 -06:00
|
|
|
for (Z modulus = 2; modulus < max_arg; ++modulus)
|
|
|
|
{
|
2018-10-26 11:19:43 -06:00
|
|
|
if (modulus % 1000 == 0)
|
|
|
|
{
|
|
|
|
std::cout << "Testing all inverses modulo " << modulus << std::endl;
|
|
|
|
}
|
|
|
|
for (Z a = 1; a < modulus; ++a)
|
2018-01-28 14:47:14 -06:00
|
|
|
{
|
|
|
|
Z gcdam = gcd(a, modulus);
|
2018-12-04 10:55:03 -07:00
|
|
|
Z inv_a = mod_inverse(a, modulus);
|
2018-01-28 14:47:14 -06:00
|
|
|
// Should fail if gcd(a, mod) != 1:
|
|
|
|
if (gcdam > 1)
|
|
|
|
{
|
2018-12-04 10:55:03 -07:00
|
|
|
BOOST_TEST(inv_a == 0);
|
2018-01-28 14:47:14 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-04 10:55:03 -07:00
|
|
|
BOOST_TEST(inv_a > 0);
|
2018-10-26 11:19:43 -06:00
|
|
|
// Cast to a bigger type so the multiplication won't overflow.
|
2018-12-04 10:55:03 -07:00
|
|
|
int256_t a_inv = inv_a;
|
2018-10-26 11:19:43 -06:00
|
|
|
int256_t big_a = a;
|
|
|
|
int256_t m = modulus;
|
|
|
|
int256_t outta_be_one = (a_inv*big_a) % m;
|
2018-10-29 13:10:02 -06:00
|
|
|
BOOST_TEST_EQ(outta_be_one, 1);
|
2018-01-28 14:47:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-26 18:42:39 -06:00
|
|
|
int main()
|
2018-01-28 14:47:14 -06:00
|
|
|
{
|
2018-11-04 01:00:57 +03:00
|
|
|
test_mod_inverse<boost::int16_t>();
|
|
|
|
test_mod_inverse<boost::int32_t>();
|
|
|
|
test_mod_inverse<boost::int64_t>();
|
2018-02-09 17:19:26 -06:00
|
|
|
test_mod_inverse<int128_t>();
|
2018-10-26 18:42:39 -06:00
|
|
|
|
2018-10-29 13:10:02 -06:00
|
|
|
return boost::report_errors();
|
2018-01-28 14:47:14 -06:00
|
|
|
}
|
2018-10-28 23:28:28 -06:00
|
|
|
#else
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2018-10-28 22:40:36 -06:00
|
|
|
#endif
|