Remove dependency on boost.format, remove unfettered use of auto in order to move towards C++03 compatibility, use BOOST_THROW_EXCEPTION.

This commit is contained in:
Nick Thompson
2018-10-24 14:29:22 -06:00
parent 9167594533
commit ada03a59d7
4 changed files with 46 additions and 32 deletions

View File

@ -9,10 +9,12 @@
#ifndef BOOST_INTEGER_DISCRETE_LOG_HPP #ifndef BOOST_INTEGER_DISCRETE_LOG_HPP
#define BOOST_INTEGER_DISCRETE_LOG_HPP #define BOOST_INTEGER_DISCRETE_LOG_HPP
#include <stdexcept>
#include <limits> #include <limits>
#include <sstream>
#include <unordered_map> #include <unordered_map>
#include <boost/throw_exception.hpp>
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <boost/format.hpp>
#include <boost/multiprecision/integer.hpp> #include <boost/multiprecision/integer.hpp>
#include <boost/integer/common_factor_rt.hpp> #include <boost/integer/common_factor_rt.hpp>
#include <boost/integer/mod_inverse.hpp> #include <boost/integer/mod_inverse.hpp>
@ -23,36 +25,41 @@ namespace boost { namespace integer {
template<class Z> template<class Z>
boost::optional<Z> trial_multiplication_discrete_log(Z base, Z arg, Z modulus) boost::optional<Z> trial_multiplication_discrete_log(Z base, Z arg, Z modulus)
{ {
using std::numeric_limits;
static_assert(numeric_limits<Z>::is_integer,
"The discrete log works on integral types.\n");
if (base <= 1) if (base <= 1)
{ {
auto e = boost::format("The base b is %1%, but must be > 1.\n") % base; std::ostringstream oss;
throw std::domain_error(e.str()); oss << "The base b is " << base << ", but must be > 1.\n";
BOOST_THROW_EXCEPTION(std::domain_error(oss.str()));
} }
if (modulus < 3) if (modulus < 3)
{ {
auto e = boost::format("The modulus must be > 2, but is %1%") % modulus; std::ostringstream oss;
throw std::domain_error(e.str()); oss << "The modulus must be > 2, but is " << modulus << ".\n";
BOOST_THROW_EXCEPTION(std::domain_error(oss.str()));
} }
if (arg < 1) if (arg < 1)
{ {
auto e = boost::format("The argument must be > 0, but is %1%") % arg; std::ostringstream oss;
throw std::domain_error(e.str()); 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 || arg >= modulus)
{ {
if (base >= 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; std::ostringstream oss;
throw std::domain_error(e.str()); 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) 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; std::ostringstream oss;
throw std::domain_error(e.str()); 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) 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) 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) 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); m_root_p = boost::multiprecision::sqrt(modulus);
if (m_root_p*m_root_p != modulus) if (m_root_p*m_root_p != modulus)
@ -102,12 +109,16 @@ public:
m_root_p += 1; m_root_p += 1;
} }
auto x = mod_inverse(base, modulus); boost::optional<Z> x = mod_inverse(base, modulus);
if (!x) if (!x)
{ {
auto d = boost::integer::gcd(base, modulus); Z 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; std::ostringstream oss;
throw std::logic_error(e.str()); 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); m_inv_base_pow_m = boost::multiprecision::powm(x.value(), m_root_p, modulus);

View File

@ -8,11 +8,12 @@
#define BOOST_INTEGER_EXTENDED_EUCLIDEAN_HPP #define BOOST_INTEGER_EXTENDED_EUCLIDEAN_HPP
#include <tuple> #include <tuple>
#include <limits> #include <limits>
#include <stdexcept>
#include <boost/throw_exception.hpp>
namespace boost { namespace integer { namespace boost { namespace integer {
// From "The Joy of Factoring", Algorithm 2.7. // 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). // 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? // Is this the natural ordering?, or must people simply have to read the docs?
template<class Z> template<class Z>
@ -27,7 +28,7 @@ std::tuple<Z, Z, Z> extended_euclidean(Z m, Z n)
if (m < 1 || n < 1) 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; bool swapped = false;
if (m < n) if (m < n)

View File

@ -6,7 +6,8 @@
*/ */
#ifndef BOOST_INTEGER_MOD_INVERSE_HPP #ifndef BOOST_INTEGER_MOD_INVERSE_HPP
#define BOOST_INTEGER_MOD_INVERSE_HPP #define BOOST_INTEGER_MOD_INVERSE_HPP
#include <limits> #include <stdexcept>
#include <boost/throw_exception.hpp>
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <boost/integer/extended_euclidean.hpp> #include <boost/integer/extended_euclidean.hpp>
@ -18,15 +19,13 @@ namespace boost { namespace integer {
// mpz_invert (gmplib) // mpz_invert (gmplib)
// modinv (some dude on stackoverflow) // modinv (some dude on stackoverflow)
// Would mod_inverse be sometimes mistaken as the modular *additive* inverse? // 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<class Z> template<class Z>
boost::optional<Z> mod_inverse(Z a, Z modulus) boost::optional<Z> mod_inverse(Z a, Z modulus)
{ {
using std::numeric_limits;
static_assert(numeric_limits<Z>::is_integer,
"The modular multiplicative inverse works on integral types.\n");
if (modulus < 2) 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: // make sure a < modulus:
a = a % modulus; a = a % modulus;

View File

@ -73,7 +73,10 @@ template<class Z>
void test_trial_multiplication_with_prime_modulus() void test_trial_multiplication_with_prime_modulus()
{ {
std::cout << "Testing trial multiplication with prime modulus on type " << boost::typeindex::type_id<Z>().pretty_name() << "\n"; std::cout << "Testing trial multiplication with prime modulus on type " << boost::typeindex::type_id<Z>().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); Z modulus = boost::math::prime(i);
for (Z base = 2; base < modulus; ++base) for (Z base = 2; base < modulus; ++base)
@ -95,7 +98,7 @@ template<class Z>
void test_bsgs_with_prime_modulus() void test_bsgs_with_prime_modulus()
{ {
std::cout << "Testing baby-step, giant-step with prime modulus on type " << boost::typeindex::type_id<Z>().pretty_name() << "\n"; std::cout << "Testing baby-step, giant-step with prime modulus on type " << boost::typeindex::type_id<Z>().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); Z p = boost::math::prime(i);
for (Z j = 2; j < p; ++j) for (Z j = 2; j < p; ++j)
@ -116,7 +119,7 @@ void test_bsgs_with_prime_modulus()
BOOST_AUTO_TEST_CASE(discrete_log_test) BOOST_AUTO_TEST_CASE(discrete_log_test)
{ {
test_trial_multiplication_discrete_log<size_t>(); test_trial_multiplication_discrete_log<size_t>();
test_bsgs_discrete_log<int>(); test_bsgs_discrete_log<long long>();
test_trial_multiplication_with_prime_modulus<long long>(); test_trial_multiplication_with_prime_modulus<long long>();
test_bsgs_with_prime_modulus<long long>(); test_bsgs_with_prime_modulus<long long>();
} }