forked from boostorg/integer
[ci skip] Modular exponentiation, modular multiplicative inverse, extended Euclidean algorithm, discrete logarithm.
This commit is contained in:
+5
-1
@@ -10,12 +10,16 @@ obj has_gmpxx : has_gmpxx.cpp ;
|
||||
explicit has_gmpxx ;
|
||||
|
||||
test-suite integer
|
||||
:
|
||||
:
|
||||
[ run integer_traits_test.cpp ]
|
||||
[ run integer_test.cpp : : : <toolset>gcc:<cxxflags>-Wno-long-long <toolset>darwin:<cxxflags>-Wno-long-long <toolset>sun:<cxxflags>"-Qoption ccfe -tmpldepth=128" ]
|
||||
[ run integer_mask_test.cpp ]
|
||||
[ run static_log2_test.cpp ]
|
||||
[ run static_min_max_test.cpp ]
|
||||
[ run discrete_log_test.cpp ]
|
||||
[ run extended_euclidean_test.cpp ]
|
||||
[ run modular_exponentiation_test.cpp ]
|
||||
[ run modular_multiplicative_inverse_test.cpp ]
|
||||
[ compile integer_traits_include_test.cpp ]
|
||||
[ compile integer_include_test.cpp ]
|
||||
[ compile integer_mask_include_test.cpp ]
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* (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)
|
||||
*/
|
||||
|
||||
#define BOOST_TEST_MODULE discrete_log_test
|
||||
#include <boost/test/included/unit_test.hpp>
|
||||
#include <boost/integer/discrete_log.hpp>
|
||||
|
||||
|
||||
using boost::integer::trial_multiplication_discrete_log;
|
||||
using boost::integer::baby_step_giant_step_discrete_log;
|
||||
|
||||
template<class Z>
|
||||
void test_trial_multiplication_discrete_log()
|
||||
{
|
||||
|
||||
boost::optional<Z> x = trial_multiplication_discrete_log<Z>(2, 1, 3);
|
||||
BOOST_CHECK_EQUAL(0, x.value());
|
||||
x = trial_multiplication_discrete_log<Z>(2, 2, 3);
|
||||
BOOST_CHECK_EQUAL(1, x.value());
|
||||
|
||||
x = trial_multiplication_discrete_log<Z>(2, 1, 4);
|
||||
BOOST_CHECK_EQUAL(0, x.value());
|
||||
|
||||
x = trial_multiplication_discrete_log<Z>(2, 2, 4);
|
||||
BOOST_CHECK_EQUAL(1, x.value());
|
||||
|
||||
// No solution to 2^^x mod 4 = 3:
|
||||
x = trial_multiplication_discrete_log<Z>(2, 3, 4);
|
||||
BOOST_TEST(!x);
|
||||
|
||||
x = trial_multiplication_discrete_log<Z>(7, 7, 41);
|
||||
BOOST_CHECK_EQUAL(1, x.value());
|
||||
x = trial_multiplication_discrete_log<Z>(7, 8, 41);
|
||||
BOOST_CHECK_EQUAL(2, x.value());
|
||||
x = trial_multiplication_discrete_log<Z>(7, 15, 41);
|
||||
BOOST_CHECK_EQUAL(3, x.value());
|
||||
x = trial_multiplication_discrete_log<Z>(7, 23, 41);
|
||||
BOOST_CHECK_EQUAL(4, x.value());
|
||||
x = trial_multiplication_discrete_log<Z>(7, 38, 41);
|
||||
BOOST_CHECK_EQUAL(5, x.value());
|
||||
x = trial_multiplication_discrete_log<Z>(7, 20, 41);
|
||||
BOOST_CHECK_EQUAL(6, x.value());
|
||||
|
||||
|
||||
Z k = 1;
|
||||
for (Z i = 0; i < 40; ++i)
|
||||
{
|
||||
x = trial_multiplication_discrete_log<Z>(7, k, 41);
|
||||
BOOST_CHECK_EQUAL(i, x.value());
|
||||
k = (7*k) % 41;
|
||||
}
|
||||
}
|
||||
|
||||
template<class Z>
|
||||
void test_bsgs_discrete_log()
|
||||
{
|
||||
baby_step_giant_step_discrete_log<Z> dl(7, 41);
|
||||
BOOST_CHECK_EQUAL(dl(7), 1);
|
||||
BOOST_CHECK_EQUAL(dl(8), 2);
|
||||
BOOST_CHECK_EQUAL(dl(15), 3);
|
||||
BOOST_CHECK_EQUAL(dl(23), 4);
|
||||
BOOST_CHECK_EQUAL(dl(38), 5);
|
||||
BOOST_CHECK_EQUAL(dl(20), 6);
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(discrete_log_test)
|
||||
{
|
||||
test_trial_multiplication_discrete_log<size_t>();
|
||||
test_bsgs_discrete_log<int>();
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* (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)
|
||||
*/
|
||||
#define BOOST_TEST_MODULE extended_euclidean_test
|
||||
#include <boost/test/included/unit_test.hpp>
|
||||
#include <boost/multiprecision/cpp_int.hpp>
|
||||
#include <boost/integer/common_factor.hpp>
|
||||
#include <boost/integer/extended_euclidean.hpp>
|
||||
|
||||
using boost::multiprecision::int128_t;
|
||||
using boost::integer::extended_euclidean;
|
||||
using boost::integer::gcd;
|
||||
|
||||
template<class Z>
|
||||
void test_extended_euclidean()
|
||||
{
|
||||
Z max_arg = 500;
|
||||
for (Z m = 1; m < max_arg; ++m)
|
||||
{
|
||||
for (Z n = 1; n < max_arg; ++n)
|
||||
{
|
||||
std::tuple<Z, Z, Z> u = extended_euclidean(m, n);
|
||||
Z gcdmn = gcd(m, n);
|
||||
Z x = std::get<1>(u);
|
||||
Z y = std::get<2>(u);
|
||||
BOOST_CHECK_EQUAL(std::get<0>(u), gcdmn);
|
||||
BOOST_CHECK_EQUAL(m*x + n*y, gcdmn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(extended_euclidean_test)
|
||||
{
|
||||
test_extended_euclidean<int>();
|
||||
test_extended_euclidean<long>();
|
||||
test_extended_euclidean<size_t>();
|
||||
test_extended_euclidean<int128_t>();
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* (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)
|
||||
*
|
||||
*/
|
||||
|
||||
#define BOOST_TEST_MODULE modular_exponentiation_test
|
||||
#include <boost/test/included/unit_test.hpp>
|
||||
#include <boost/multiprecision/cpp_int.hpp>
|
||||
#include <boost/integer/modular_exponentiation.hpp>
|
||||
|
||||
using boost::multiprecision::int128_t;
|
||||
using boost::integer::modular_exponentiation;
|
||||
|
||||
template<class Z>
|
||||
void test_modular_exponentiation()
|
||||
{
|
||||
Z base = 7;
|
||||
Z modulus = 51;
|
||||
Z expected = 1;
|
||||
for (Z exponent = 0; exponent < 10000; ++exponent)
|
||||
{
|
||||
Z x = modular_exponentiation<Z>(base, exponent, modulus);
|
||||
BOOST_CHECK_EQUAL(expected, x);
|
||||
expected = (expected*base) % modulus;
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(modular_exponentiation_test)
|
||||
{
|
||||
test_modular_exponentiation<int>();
|
||||
test_modular_exponentiation<unsigned>();
|
||||
test_modular_exponentiation<short>();
|
||||
test_modular_exponentiation<size_t>();
|
||||
test_modular_exponentiation<int128_t>();
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* (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)
|
||||
*/
|
||||
#define BOOST_TEST_MODULE modular_multiplicative_inverse_test
|
||||
#include <boost/test/included/unit_test.hpp>
|
||||
#include <boost/multiprecision/cpp_int.hpp>
|
||||
#include <boost/integer/common_factor.hpp>
|
||||
#include <boost/integer/modular_multiplicative_inverse.hpp>
|
||||
|
||||
using boost::multiprecision::int128_t;
|
||||
using boost::integer::modular_multiplicative_inverse;
|
||||
using boost::integer::gcd;
|
||||
|
||||
template<class Z>
|
||||
void test_modular_multiplicative_inverse()
|
||||
{
|
||||
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);
|
||||
boost::optional<Z> inv_a = modular_multiplicative_inverse(a, modulus);
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(extended_euclidean_test)
|
||||
{
|
||||
test_modular_multiplicative_inverse<int>();
|
||||
test_modular_multiplicative_inverse<long>();
|
||||
test_modular_multiplicative_inverse<long long>();
|
||||
test_modular_multiplicative_inverse<int128_t>();
|
||||
}
|
||||
Reference in New Issue
Block a user