| 
									
										
										
										
											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 | 
					
						
							|  |  |  |  *  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-02-10 13:56:11 -06:00
										 |  |  | #define BOOST_TEST_MODULE mod_inverse_test
 | 
					
						
							| 
									
										
										
										
											2018-01-28 14:47:14 -06:00
										 |  |  | #include <boost/test/included/unit_test.hpp>
 | 
					
						
							|  |  |  | #include <boost/multiprecision/cpp_int.hpp>
 | 
					
						
							|  |  |  | #include <boost/integer/common_factor.hpp>
 | 
					
						
							| 
									
										
										
										
											2018-02-09 17:19:26 -06:00
										 |  |  | #include <boost/integer/mod_inverse.hpp>
 | 
					
						
							| 
									
										
										
										
											2018-01-28 14:47:14 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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-02-10 13:56:11 -06:00
										 |  |  |     std::cout << "Testing the modular multiplicative inverse on type " << boost::typeindex::type_id<Z>().pretty_name() << "\n"; | 
					
						
							| 
									
										
										
										
											2018-01-28 14:47:14 -06:00
										 |  |  |     Z max_arg = 1000; | 
					
						
							|  |  |  |     for (Z modulus = 2; modulus < max_arg; ++modulus) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         for (Z a = 1; a < max_arg; ++a) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             Z gcdam = gcd(a, modulus); | 
					
						
							| 
									
										
										
										
											2018-02-09 17:19:26 -06:00
										 |  |  |             boost::optional<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) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 BOOST_CHECK(!inv_a); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 BOOST_CHECK(inv_a.value() > 0); | 
					
						
							|  |  |  |                 Z outta_be_one = (inv_a.value()*a) % modulus; | 
					
						
							|  |  |  |                 BOOST_CHECK_EQUAL(outta_be_one, 1); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-10 13:56:11 -06:00
										 |  |  | BOOST_AUTO_TEST_CASE(mod_inverse_test) | 
					
						
							| 
									
										
										
										
											2018-01-28 14:47:14 -06:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-02-10 13:56:11 -06:00
										 |  |  |     test_mod_inverse<short int>(); | 
					
						
							| 
									
										
										
										
											2018-02-09 17:19:26 -06:00
										 |  |  |     test_mod_inverse<int>(); | 
					
						
							|  |  |  |     test_mod_inverse<long>(); | 
					
						
							|  |  |  |     test_mod_inverse<long long>(); | 
					
						
							|  |  |  |     test_mod_inverse<int128_t>(); | 
					
						
							| 
									
										
										
										
											2018-02-10 13:56:11 -06:00
										 |  |  |     test_mod_inverse<int256_t>(); | 
					
						
							| 
									
										
										
										
											2018-01-28 14:47:14 -06:00
										 |  |  | } |