diff --git a/include/boost/integer/discrete_log.hpp b/include/boost/integer/discrete_log.hpp index b4a7657..d5f94ce 100644 --- a/include/boost/integer/discrete_log.hpp +++ b/include/boost/integer/discrete_log.hpp @@ -9,10 +9,12 @@ #ifndef BOOST_INTEGER_DISCRETE_LOG_HPP #define BOOST_INTEGER_DISCRETE_LOG_HPP +#include #include +#include #include +#include #include -#include #include #include #include @@ -23,36 +25,41 @@ namespace boost { namespace integer { template boost::optional trial_multiplication_discrete_log(Z base, Z arg, Z modulus) { - using std::numeric_limits; - static_assert(numeric_limits::is_integer, - "The discrete log works on integral types.\n"); - if (base <= 1) { - auto e = boost::format("The base b is %1%, but must be > 1.\n") % base; - throw std::domain_error(e.str()); + std::ostringstream oss; + oss << "The base b is " << base << ", but must be > 1.\n"; + BOOST_THROW_EXCEPTION(std::domain_error(oss.str())); } if (modulus < 3) { - auto e = boost::format("The modulus must be > 2, but is %1%") % modulus; - throw std::domain_error(e.str()); + std::ostringstream oss; + oss << "The modulus must be > 2, but is " << modulus << ".\n"; + BOOST_THROW_EXCEPTION(std::domain_error(oss.str())); } if (arg < 1) { - auto e = boost::format("The argument must be > 0, but is %1%") % arg; - throw std::domain_error(e.str()); + std::ostringstream oss; + oss << "The argument must be > 0, but is " << arg << ".\n"; + BOOST_THROW_EXCEPTION(std::domain_error(oss.str())); } if (base >= modulus || arg >= modulus) { if (base >= modulus) { - auto e = boost::format("Error computing the discrete log: The base %1% is greater than the modulus %2%. Are the arguments in the wrong order?") % base % modulus; - throw std::domain_error(e.str()); + std::ostringstream oss; + oss << "Error computing the discrete log: The base " << base + << " is greater than the modulus " << modulus + << ". Are the arguments in the wrong order?"; + BOOST_THROW_EXCEPTION(std::domain_error(oss.str())); } if (arg >= modulus) { - auto e = boost::format("Error computing the discrete log: The argument %1% is greater than the modulus %2%. Are the arguments in the wrong order?") % arg % modulus; - throw std::domain_error(e.str()); + std::ostringstream oss; + oss << "Error computing the discrete log: The argument " << arg + << " is greater than the modulus " << modulus + << ". Are the arguments in the wrong order?"; + BOOST_THROW_EXCEPTION(std::domain_error(oss.str())); } } @@ -86,15 +93,15 @@ public: if (base <= 1) { - throw std::logic_error("The base must be > 1.\n"); + BOOST_THROW_EXCEPTION(std::logic_error("The base must be > 1.\n")); } if (modulus < 3) { - throw std::logic_error("The modulus must be > 2.\n"); + BOOST_THROW_EXCEPTION(std::logic_error("The modulus must be > 2.\n")); } if (base >= modulus) { - throw std::logic_error("Error computing the discrete log: Are your arguments in the wrong order?\n"); + BOOST_THROW_EXCEPTION(std::logic_error("Error computing the discrete log: Are your arguments in the wrong order?\n")); } m_root_p = boost::multiprecision::sqrt(modulus); if (m_root_p*m_root_p != modulus) @@ -102,12 +109,16 @@ public: m_root_p += 1; } - auto x = mod_inverse(base, modulus); + boost::optional x = mod_inverse(base, modulus); if (!x) { - auto d = boost::integer::gcd(base, modulus); - auto e = boost::format("The gcd of the base %1% and the modulus %2% is %3% != 1, hence the discrete log is not guaranteed to exist, which breaks the baby-step giant step algorithm. If you don't require existence proof for all inputs, use trial multiplication.\n") % base % modulus % d; - throw std::logic_error(e.str()); + Z d = boost::integer::gcd(base, modulus); + std::ostringstream oss; + oss << "The gcd of the base " << base << " and the modulus " << modulus << " is " << d + << ", which is not equal 1; hence the discrete log is not guaranteed to exist.\n" + << "This breaks the baby-step giant step algorithm.\n" + << "If you don't require existence for all inputs, use trial multiplication.\n"; + BOOST_THROW_EXCEPTION(std::logic_error(oss.str())); } m_inv_base_pow_m = boost::multiprecision::powm(x.value(), m_root_p, modulus); diff --git a/include/boost/integer/extended_euclidean.hpp b/include/boost/integer/extended_euclidean.hpp index 4b0e028..87f4131 100644 --- a/include/boost/integer/extended_euclidean.hpp +++ b/include/boost/integer/extended_euclidean.hpp @@ -8,11 +8,12 @@ #define BOOST_INTEGER_EXTENDED_EUCLIDEAN_HPP #include #include +#include +#include namespace boost { namespace integer { // From "The Joy of Factoring", Algorithm 2.7. -// Should the tuple be a named tuple? Is that possible? // Solves mx + ny = gcd(m,n). Returns tuple with (gcd(m,n), x, y). // Is this the natural ordering?, or must people simply have to read the docs? template @@ -27,7 +28,7 @@ std::tuple extended_euclidean(Z m, Z n) if (m < 1 || n < 1) { - throw std::domain_error("Arguments must be strictly positive.\n"); + BOOST_THROW_EXCEPTION(std::domain_error("Arguments must be strictly positive.\n")); } bool swapped = false; if (m < n) diff --git a/include/boost/integer/mod_inverse.hpp b/include/boost/integer/mod_inverse.hpp index 53b8a92..6c5eda7 100644 --- a/include/boost/integer/mod_inverse.hpp +++ b/include/boost/integer/mod_inverse.hpp @@ -6,7 +6,8 @@ */ #ifndef BOOST_INTEGER_MOD_INVERSE_HPP #define BOOST_INTEGER_MOD_INVERSE_HPP -#include +#include +#include #include #include @@ -18,15 +19,13 @@ namespace boost { namespace integer { // mpz_invert (gmplib) // modinv (some dude on stackoverflow) // Would mod_inverse be sometimes mistaken as the modular *additive* inverse? +// In any case, I think this is the best name we can get for this function without agonizing. template boost::optional mod_inverse(Z a, Z modulus) { - using std::numeric_limits; - static_assert(numeric_limits::is_integer, - "The modular multiplicative inverse works on integral types.\n"); if (modulus < 2) { - throw std::domain_error("Modulus must be > 1.\n"); + BOOST_THROW_EXCEPTION(std::domain_error("Modulus must be > 1.\n")); } // make sure a < modulus: a = a % modulus; diff --git a/test/discrete_log_test.cpp b/test/discrete_log_test.cpp index f4a1881..40c1f2b 100644 --- a/test/discrete_log_test.cpp +++ b/test/discrete_log_test.cpp @@ -73,7 +73,10 @@ template void test_trial_multiplication_with_prime_modulus() { std::cout << "Testing trial multiplication with prime modulus on type " << boost::typeindex::type_id().pretty_name() << "\n"; - for (Z i = 0; i < boost::math::max_prime; ++i) + // There are roughly 10,000 primes in the boost.math prime table, + // so this test could run indefinitely. I leave the syntax to get the test to run + // for years as commented-out code, since ideally we would test every input. + for (Z i = 0; i < 10 /*boost::math::max_prime*/; ++i) { Z modulus = boost::math::prime(i); for (Z base = 2; base < modulus; ++base) @@ -95,7 +98,7 @@ template void test_bsgs_with_prime_modulus() { std::cout << "Testing baby-step, giant-step with prime modulus on type " << boost::typeindex::type_id().pretty_name() << "\n"; - for (Z i = 0; i < boost::math::max_prime; ++i) + for (Z i = 0; i < 10 /*boost::math::max_prime*/; ++i) { Z p = boost::math::prime(i); for (Z j = 2; j < p; ++j) @@ -116,7 +119,7 @@ void test_bsgs_with_prime_modulus() BOOST_AUTO_TEST_CASE(discrete_log_test) { test_trial_multiplication_discrete_log(); - test_bsgs_discrete_log(); + test_bsgs_discrete_log(); test_trial_multiplication_with_prime_modulus(); test_bsgs_with_prime_modulus(); }