]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/integer/test/mod_inverse_test.cpp
2 * (C) Copyright Nick Thompson 2018.
3 * Use, modification and distribution are subject to the
4 * Boost Software License, Version 1.0. (See accompanying file
5 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 #include "multiprecision_config.hpp"
10 #ifndef DISABLE_MP_TESTS
11 #include <boost/integer/mod_inverse.hpp>
12 #include <boost/cstdint.hpp>
13 #include <boost/optional/optional.hpp>
14 #include <boost/core/lightweight_test.hpp>
15 #include <boost/multiprecision/cpp_int.hpp>
16 #include <boost/integer/common_factor.hpp>
18 using boost::multiprecision::int128_t
;
19 using boost::multiprecision::int256_t
;
20 using boost::integer::mod_inverse
;
21 using boost::integer::gcd
;
24 void test_mod_inverse()
26 //Z max_arg = std::numeric_limits<Z>::max();
28 for (Z modulus
= 2; modulus
< max_arg
; ++modulus
)
30 if (modulus
% 1000 == 0)
32 std::cout
<< "Testing all inverses modulo " << modulus
<< std::endl
;
34 for (Z a
= 1; a
< modulus
; ++a
)
36 Z gcdam
= gcd(a
, modulus
);
37 Z inv_a
= mod_inverse(a
, modulus
);
38 // Should fail if gcd(a, mod) != 1:
41 BOOST_TEST(inv_a
== 0);
45 BOOST_TEST(inv_a
> 0);
46 // Cast to a bigger type so the multiplication won't overflow.
47 int256_t a_inv
= inv_a
;
50 int256_t outta_be_one
= (a_inv
*big_a
) % m
;
51 BOOST_TEST_EQ(outta_be_one
, 1);
59 test_mod_inverse
<boost::int16_t>();
60 test_mod_inverse
<boost::int32_t>();
61 test_mod_inverse
<boost::int64_t>();
62 test_mod_inverse
<int128_t
>();
64 return boost::report_errors();