forked from boostorg/integer
[ci skip] Use less verbose naming. Add asserts as verfication of algorithms is a negligible fraction of total runtime. Use boost::multiprecision::powm and boost::multiprecision::sqrt rather than one-offs.
This commit is contained in:
+1
-2
@@ -18,8 +18,7 @@ test-suite integer
|
||||
[ 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 ]
|
||||
[ run mod_inverse_test.cpp ]
|
||||
[ compile integer_traits_include_test.cpp ]
|
||||
[ compile integer_include_test.cpp ]
|
||||
[ compile integer_mask_include_test.cpp ]
|
||||
|
||||
@@ -8,10 +8,11 @@
|
||||
#define BOOST_TEST_MODULE discrete_log_test
|
||||
#include <boost/test/included/unit_test.hpp>
|
||||
#include <boost/integer/discrete_log.hpp>
|
||||
#include <boost/math/special_functions/prime.hpp>
|
||||
|
||||
|
||||
using boost::integer::trial_multiplication_discrete_log;
|
||||
using boost::integer::baby_step_giant_step_discrete_log;
|
||||
using boost::integer::bsgs_discrete_log;
|
||||
|
||||
template<class Z>
|
||||
void test_trial_multiplication_discrete_log()
|
||||
@@ -58,13 +59,52 @@ void test_trial_multiplication_discrete_log()
|
||||
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);
|
||||
bsgs_discrete_log<Z> dl_7(7, 41);
|
||||
BOOST_CHECK_EQUAL(dl_7(7), 1);
|
||||
BOOST_CHECK_EQUAL(dl_7(8), 2);
|
||||
BOOST_CHECK_EQUAL(dl_7(15), 3);
|
||||
BOOST_CHECK_EQUAL(dl_7(23), 4);
|
||||
BOOST_CHECK_EQUAL(dl_7(38), 5);
|
||||
BOOST_CHECK_EQUAL(dl_7(20), 6);
|
||||
}
|
||||
|
||||
template<class Z>
|
||||
void test_trial_multiplication_with_prime_base()
|
||||
{
|
||||
for (Z i = 0; i < boost::math::max_prime; ++i)
|
||||
{
|
||||
Z p = boost::math::prime(i);
|
||||
for (Z j = 2; j < p; ++j)
|
||||
{
|
||||
bsgs_discrete_log<Z> dl_j(j, p);
|
||||
for (Z k = 1; k < p; ++k)
|
||||
{
|
||||
boost::optional<Z> dl = trial_multiplication_discrete_log(j, k, p);
|
||||
// It is guaranteed to exist with the modulus is prime:
|
||||
BOOST_ASSERT(dl);
|
||||
BOOST_CHECK_EQUAL(k, boost::multiprecision::powm(j, dl.value(), p));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<class Z>
|
||||
void test_bsgs_with_prime_base()
|
||||
{
|
||||
for (Z i = 0; i < boost::math::max_prime; ++i)
|
||||
{
|
||||
Z p = boost::math::prime(i);
|
||||
for (Z j = 2; j < p; ++j)
|
||||
{
|
||||
bsgs_discrete_log<Z> dl_j(j, p);
|
||||
for (Z k = 1; k < p; ++k)
|
||||
{
|
||||
Z dl = dl_j(k);
|
||||
BOOST_CHECK_EQUAL(k, boost::multiprecision::powm(j, dl, p));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -72,4 +112,6 @@ BOOST_AUTO_TEST_CASE(discrete_log_test)
|
||||
{
|
||||
test_trial_multiplication_discrete_log<size_t>();
|
||||
test_bsgs_discrete_log<int>();
|
||||
test_trial_multiplication_with_prime_base<long long>();
|
||||
test_bsgs_with_prime_base<long long>();
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ using boost::integer::gcd;
|
||||
template<class Z>
|
||||
void test_extended_euclidean()
|
||||
{
|
||||
Z max_arg = 500;
|
||||
Z max_arg = 1000;
|
||||
for (Z m = 1; m < max_arg; ++m)
|
||||
{
|
||||
for (Z n = 1; n < max_arg; ++n)
|
||||
@@ -36,6 +36,6 @@ BOOST_AUTO_TEST_CASE(extended_euclidean_test)
|
||||
{
|
||||
test_extended_euclidean<int>();
|
||||
test_extended_euclidean<long>();
|
||||
test_extended_euclidean<size_t>();
|
||||
test_extended_euclidean<long long>();
|
||||
test_extended_euclidean<int128_t>();
|
||||
}
|
||||
|
||||
@@ -8,14 +8,14 @@
|
||||
#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>
|
||||
#include <boost/integer/mod_inverse.hpp>
|
||||
|
||||
using boost::multiprecision::int128_t;
|
||||
using boost::integer::modular_multiplicative_inverse;
|
||||
using boost::integer::mod_inverse;
|
||||
using boost::integer::gcd;
|
||||
|
||||
template<class Z>
|
||||
void test_modular_multiplicative_inverse()
|
||||
void test_mod_inverse()
|
||||
{
|
||||
Z max_arg = 1000;
|
||||
for (Z modulus = 2; modulus < max_arg; ++modulus)
|
||||
@@ -23,7 +23,7 @@ void test_modular_multiplicative_inverse()
|
||||
for (Z a = 1; a < max_arg; ++a)
|
||||
{
|
||||
Z gcdam = gcd(a, modulus);
|
||||
boost::optional<Z> inv_a = modular_multiplicative_inverse(a, modulus);
|
||||
boost::optional<Z> inv_a = mod_inverse(a, modulus);
|
||||
// Should fail if gcd(a, mod) != 1:
|
||||
if (gcdam > 1)
|
||||
{
|
||||
@@ -41,8 +41,8 @@ void test_modular_multiplicative_inverse()
|
||||
|
||||
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>();
|
||||
test_mod_inverse<int>();
|
||||
test_mod_inverse<long>();
|
||||
test_mod_inverse<long long>();
|
||||
test_mod_inverse<int128_t>();
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
* (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>();
|
||||
}
|
||||
Reference in New Issue
Block a user