forked from boostorg/integer
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:
@ -9,10 +9,12 @@
|
||||
|
||||
#ifndef BOOST_INTEGER_DISCRETE_LOG_HPP
|
||||
#define BOOST_INTEGER_DISCRETE_LOG_HPP
|
||||
#include <stdexcept>
|
||||
#include <limits>
|
||||
#include <sstream>
|
||||
#include <unordered_map>
|
||||
#include <boost/throw_exception.hpp>
|
||||
#include <boost/optional.hpp>
|
||||
#include <boost/format.hpp>
|
||||
#include <boost/multiprecision/integer.hpp>
|
||||
#include <boost/integer/common_factor_rt.hpp>
|
||||
#include <boost/integer/mod_inverse.hpp>
|
||||
@ -23,36 +25,41 @@ namespace boost { namespace integer {
|
||||
template<class Z>
|
||||
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)
|
||||
{
|
||||
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<Z> 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);
|
||||
|
||||
|
@ -8,11 +8,12 @@
|
||||
#define BOOST_INTEGER_EXTENDED_EUCLIDEAN_HPP
|
||||
#include <tuple>
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
#include <boost/throw_exception.hpp>
|
||||
|
||||
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<class Z>
|
||||
@ -27,7 +28,7 @@ std::tuple<Z, Z, Z> 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)
|
||||
|
@ -6,7 +6,8 @@
|
||||
*/
|
||||
#ifndef 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/integer/extended_euclidean.hpp>
|
||||
|
||||
@ -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<class Z>
|
||||
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)
|
||||
{
|
||||
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;
|
||||
|
Reference in New Issue
Block a user