mirror of
https://github.com/boostorg/integer.git
synced 2025-11-02 01:01:38 +01:00
[ci skip] Modular exponentiation, modular multiplicative inverse, extended Euclidean algorithm, discrete logarithm.
This commit is contained in:
@@ -15,7 +15,7 @@ programming problems.
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace math
|
||||
namespace integer
|
||||
{
|
||||
|
||||
template < typename IntegerType >
|
||||
@@ -53,10 +53,10 @@ programming problems.
|
||||
|
||||
[section GCD Function Object]
|
||||
|
||||
[*Header: ] [@../../../../boost/math/common_factor_rt.hpp <boost/math/common_factor_rt.hpp>]
|
||||
[*Header: ] [@../../../../boost/integer/common_factor_rt.hpp <boost/integer/common_factor_rt.hpp>]
|
||||
|
||||
template < typename IntegerType >
|
||||
class boost::math::gcd_evaluator
|
||||
class boost::integer::gcd_evaluator
|
||||
{
|
||||
public:
|
||||
// Types
|
||||
@@ -65,12 +65,12 @@ programming problems.
|
||||
typedef IntegerType second_argument_type;
|
||||
|
||||
// Function object interface
|
||||
constexpr result_type operator ()(
|
||||
constexpr result_type operator ()(
|
||||
first_argument_type const &a,
|
||||
second_argument_type const &b ) const;
|
||||
};
|
||||
|
||||
The boost::math::gcd_evaluator class template defines a function object
|
||||
The boost::integer::gcd_evaluator class template defines a function object
|
||||
class to return the greatest common divisor of two integers.
|
||||
The template is parameterized by a single type, called IntegerType here.
|
||||
This type should be a numeric type that represents integers.
|
||||
@@ -82,17 +82,17 @@ the GCD function template. If a numeric type wants to customize evaluations
|
||||
of its greatest common divisors, then the type should specialize on the
|
||||
gcd_evaluator class template.
|
||||
|
||||
Note that these function objects are `constexpr` in C++14 and later only.
|
||||
Note that these function objects are `constexpr` in C++14 and later only.
|
||||
They are also declared `noexcept` when appropriate.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section LCM Function Object]
|
||||
|
||||
[*Header: ] [@../../../../boost/math/common_factor_rt.hpp <boost/math/common_factor_rt.hpp>]
|
||||
[*Header: ] [@../../../../boost/integer/common_factor_rt.hpp <boost/integer/common_factor_rt.hpp>]
|
||||
|
||||
template < typename IntegerType >
|
||||
class boost::math::lcm_evaluator
|
||||
class boost::integer::lcm_evaluator
|
||||
{
|
||||
public:
|
||||
// Types
|
||||
@@ -101,12 +101,12 @@ They are also declared `noexcept` when appropriate.
|
||||
typedef IntegerType second_argument_type;
|
||||
|
||||
// Function object interface
|
||||
constexpr result_type operator ()(
|
||||
constexpr result_type operator ()(
|
||||
first_argument_type const &a,
|
||||
second_argument_type const &b ) const;
|
||||
};
|
||||
|
||||
The boost::math::lcm_evaluator class template defines a function object
|
||||
The boost::integer::lcm_evaluator class template defines a function object
|
||||
class to return the least common multiple of two integers. The template
|
||||
is parameterized by a single type, called IntegerType here. This type
|
||||
should be a numeric type that represents integers. The result of the
|
||||
@@ -126,13 +126,13 @@ They are also declared `noexcept` when appropriate.
|
||||
|
||||
[section:run_time Run-time GCD & LCM Determination]
|
||||
|
||||
[*Header: ] [@../../../../boost/math/common_factor_rt.hpp <boost/math/common_factor_rt.hpp>]
|
||||
[*Header: ] [@../../../../boost/integer/common_factor_rt.hpp <boost/integer/common_factor_rt.hpp>]
|
||||
|
||||
template < typename IntegerType >
|
||||
constexpr IntegerType boost::math::gcd( IntegerType const &a, IntegerType const &b );
|
||||
constexpr IntegerType boost::integer::gcd( IntegerType const &a, IntegerType const &b );
|
||||
|
||||
template < typename IntegerType >
|
||||
constexpr IntegerType boost::math::lcm( IntegerType const &a, IntegerType const &b );
|
||||
constexpr IntegerType boost::integer::lcm( IntegerType const &a, IntegerType const &b );
|
||||
|
||||
template < typename IntegerType, typename... Args >
|
||||
constexpr IntegerType gcd( IntegerType const &a, IntegerType const &b, Args const&... );
|
||||
@@ -148,18 +148,18 @@ They are also declared `noexcept` when appropriate.
|
||||
std::pair<typename std::iterator_traits<I>::value_type, I>
|
||||
lcm_range(I first, I last);
|
||||
|
||||
The boost::math::gcd function template returns the greatest common
|
||||
The boost::integer::gcd function template returns the greatest common
|
||||
(nonnegative) divisor of the two integers passed to it.
|
||||
`boost::math::gcd_range` is the iteration of the above gcd algorithm over a
|
||||
range, returning the greatest common divisor of all the elements. The algorithm
|
||||
terminates when the gcd reaches unity or the end of the range. Thus it also
|
||||
returns the iterator after the last element inspected because this may not be
|
||||
`boost::integer::gcd_range` is the iteration of the above gcd algorithm over a
|
||||
range, returning the greatest common divisor of all the elements. The algorithm
|
||||
terminates when the gcd reaches unity or the end of the range. Thus it also
|
||||
returns the iterator after the last element inspected because this may not be
|
||||
equal to the end of the range. The variadic version of `gcd` behaves similarly
|
||||
but does not indicate which input value caused the gcd to reach unity.
|
||||
|
||||
The boost::math::lcm function template returns the least common
|
||||
The boost::integer::lcm function template returns the least common
|
||||
(nonnegative) multiple of the two integers passed to it.
|
||||
As with gcd, there are range and variadic versions of the function for
|
||||
As with gcd, there are range and variadic versions of the function for
|
||||
more than 2 arguments.
|
||||
|
||||
Note that these functions are constexpr in C++14 and later only.
|
||||
@@ -171,17 +171,17 @@ They are also declared `noexcept` when appropriate.
|
||||
|
||||
[note These functions are deprecated in favor of constexpr `gcd` and `lcm` on C++14 capable compilers.]
|
||||
|
||||
[*Header: ] [@../../../../boost/math/common_factor_ct.hpp <boost/math/common_factor_ct.hpp>]
|
||||
[*Header: ] [@../../../../boost/integer/common_factor_ct.hpp <boost/integer/common_factor_ct.hpp>]
|
||||
|
||||
typedef ``['unspecified]`` static_gcd_type;
|
||||
|
||||
template < static_gcd_type Value1, static_gcd_type Value2 >
|
||||
struct boost::math::static_gcd : public mpl::integral_c<static_gcd_type, implementation_defined>
|
||||
struct boost::integer::static_gcd : public mpl::integral_c<static_gcd_type, implementation_defined>
|
||||
{
|
||||
};
|
||||
|
||||
template < static_gcd_type Value1, static_gcd_type Value2 >
|
||||
struct boost::math::static_lcm : public mpl::integral_c<static_gcd_type, implementation_defined>
|
||||
struct boost::integer::static_lcm : public mpl::integral_c<static_gcd_type, implementation_defined>
|
||||
{
|
||||
};
|
||||
|
||||
@@ -190,7 +190,7 @@ for use in integral-constant-expressions by the compiler. Usually this
|
||||
the same type as `boost::uintmax_t`, but may fall back to being `unsigned long`
|
||||
for some older compilers.
|
||||
|
||||
The boost::math::static_gcd and boost::math::static_lcm class templates
|
||||
The boost::integer::static_gcd and boost::integer::static_lcm class templates
|
||||
take two value-based template parameters of the ['static_gcd_type] type
|
||||
and inherit from the type `boost::mpl::integral_c`.
|
||||
Inherited from the base class, they have a member /value/
|
||||
@@ -201,7 +201,7 @@ is beyond the range of `static_gcd_type`.
|
||||
|
||||
[h3 Example]
|
||||
|
||||
#include <boost/math/common_factor.hpp>
|
||||
#include <boost/integer/common_factor.hpp>
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <iostream>
|
||||
@@ -212,28 +212,28 @@ is beyond the range of `static_gcd_type`.
|
||||
using std::endl;
|
||||
|
||||
cout << "The GCD and LCM of 6 and 15 are "
|
||||
<< boost::math::gcd(6, 15) << " and "
|
||||
<< boost::math::lcm(6, 15) << ", respectively."
|
||||
<< boost::integer::gcd(6, 15) << " and "
|
||||
<< boost::integer::lcm(6, 15) << ", respectively."
|
||||
<< endl;
|
||||
|
||||
cout << "The GCD and LCM of 8 and 9 are "
|
||||
<< boost::math::static_gcd<8, 9>::value
|
||||
<< boost::integer::static_gcd<8, 9>::value
|
||||
<< " and "
|
||||
<< boost::math::static_lcm<8, 9>::value
|
||||
<< boost::integer::static_lcm<8, 9>::value
|
||||
<< ", respectively." << endl;
|
||||
|
||||
int a[] = { 4, 5, 6 }, b[] = { 7, 8, 9 }, c[3];
|
||||
std::transform( a, a + 3, b, c, boost::math::gcd_evaluator<int>() );
|
||||
std::transform( a, a + 3, b, c, boost::integer::gcd_evaluator<int>() );
|
||||
std::copy( c, c + 3, std::ostream_iterator<int>(cout, " ") );
|
||||
}
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:gcd_header Header <boost/math/common_factor.hpp>]
|
||||
[section:gcd_header Header <boost/integer/common_factor.hpp>]
|
||||
|
||||
This header simply includes the headers
|
||||
[@../../../../boost/math/common_factor_ct.hpp <boost/math/common_factor_ct.hpp>]
|
||||
and [@../../../../boost/math/common_factor_rt.hpp <boost/math/common_factor_rt.hpp>].
|
||||
[@../../../../boost/integer/common_factor_ct.hpp <boost/integer/common_factor_ct.hpp>]
|
||||
and [@../../../../boost/integer/common_factor_rt.hpp <boost/integer/common_factor_rt.hpp>].
|
||||
|
||||
Note this is a legacy header: it used to contain the actual implementation,
|
||||
but the compile-time and run-time facilities
|
||||
@@ -243,7 +243,7 @@ were moved to separate headers (since they were independent of each other).
|
||||
|
||||
[section:demo Demonstration Program]
|
||||
|
||||
The program [@../../../../libs/math/test/common_factor_test.cpp common_factor_test.cpp] is a demonstration of the results from
|
||||
The program [@../../../../libs/integer/test/common_factor_test.cpp common_factor_test.cpp] is a demonstration of the results from
|
||||
instantiating various examples of the run-time GCD and LCM function
|
||||
templates and the compile-time GCD and LCM class templates.
|
||||
(The run-time GCD and LCM class templates are tested indirectly through
|
||||
@@ -256,13 +256,13 @@ the run-time function templates.)
|
||||
The greatest common divisor and least common multiple functions are
|
||||
greatly used in some numeric contexts, including some of the other
|
||||
Boost libraries. Centralizing these functions to one header improves
|
||||
code factoring and eases maintainence.
|
||||
code factoring and eases maintenance.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:gcd_history History]
|
||||
|
||||
* 24th April 2017 Moved to Jeremy Murphy's improved algorithms, added constexpr and noexcept support,
|
||||
* 24th April 2017 Moved to Jeremy Murphy's improved algorithms, added constexpr and noexcept support,
|
||||
added compiler intrinsic support, added variadic and range based versions of the algorithms.
|
||||
* 13 May 2013 Moved into main Boost.Math Quickbook documentation.
|
||||
* 17 Dec 2005: Converted documentation to Quickbook Format.
|
||||
@@ -288,5 +288,3 @@ Distributed under 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).
|
||||
]
|
||||
|
||||
|
||||
|
||||
97
doc/modular_arithmetic/discrete_log.qbk
Normal file
97
doc/modular_arithmetic/discrete_log.qbk
Normal file
@@ -0,0 +1,97 @@
|
||||
[section:discrete_log Discrete Log]
|
||||
|
||||
[section Introduction]
|
||||
|
||||
The discrete log is the inverse of modular exponentiation.
|
||||
To wit, if /a/[sup /x/] = /b/ mod /p/, then we write /x/ = log[sub a](/b/).
|
||||
Fast algorithms for modular exponentiation exists, but currently there are no polynomial time algorithms known for the discrete logarithm,
|
||||
a fact which is the basis for the security of Diffie-Hellman key exchange.
|
||||
|
||||
Despite having exponential complexity in the number of bits, the algorithms for discrete logarithm provided by Boost are still useful,
|
||||
for there are many uses of the discrete logarithm outside of cryptography which do not require massive inputs.
|
||||
The algorithms provided by Boost should be acceptable up to roughly 64 bits.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section Synopsis]
|
||||
|
||||
#include <boost/integer/discrete_log.hpp>
|
||||
|
||||
namespace boost { namespace integer {
|
||||
|
||||
template<class Z>
|
||||
boost::optional<Z> trial_multiplication_discrete_log(Z base, Z arg, Z p);
|
||||
|
||||
|
||||
template<class Z>
|
||||
class baby_step_giant_step_discrete_log
|
||||
{
|
||||
public:
|
||||
baby_step_giant_step_discrete_log(Z base, Z p);
|
||||
|
||||
Z operator()(Z arg) const;
|
||||
|
||||
};
|
||||
}}
|
||||
|
||||
[endsect]
|
||||
|
||||
[section Usage]
|
||||
|
||||
|
||||
Boost provides two algorithms for the discrete log: Trial multiplication and the "baby-step giant step" algorithm.
|
||||
Basic usage is shown below:
|
||||
|
||||
auto logarithm = trial_multiplication_discrete_log(2, 3, 5);
|
||||
if (logarithm)
|
||||
{
|
||||
std::cout << "log_2(3) mod 5 = " << l.value() << std::endl;
|
||||
}
|
||||
|
||||
auto bsgs = baby_step_giant_step_discrete_log(2, 5);
|
||||
int log = bsgs(3);
|
||||
std::cout << "log_2(3) mod 5 = " << log << std::endl;
|
||||
|
||||
|
||||
Of these, trial multiplication is more general, requires O(/p/) time and O(1) storage.
|
||||
The baby-step giant step algorithm requires O([radic] p) time and O([radic] p) storage, and is slightly less general as the generator must be coprime to the the modulus.
|
||||
Let's illustrate this with a few examples: Suppose we wish to compute log[sub 2](3) mod 4.
|
||||
Since 2[sup x] = 3 mod 4 has no solution, the result is undefined.
|
||||
|
||||
boost::optional<int> l = trial_multiplication_discrete_log(2, 3, 4);
|
||||
if (!l)
|
||||
{
|
||||
std::cout << "log_2(3) mod 4 is undefined!\n";
|
||||
}
|
||||
|
||||
The baby-step giant-step algorithm is less polite when the base and the modulus are not coprime:
|
||||
|
||||
try
|
||||
{
|
||||
auto bsgs = baby_step_giant_step_discrete_log(2, 4);
|
||||
}
|
||||
catch(std::exception const & e)
|
||||
{
|
||||
// e.what() is gonna tell you 2 and 4 are not coprime:
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
|
||||
|
||||
The baby-step giant-step discrete log will *never* compute a logarithm when the generator and modulus are not coprime, because it relies on the existence of modular multiplicative inverses.
|
||||
However, discrete logarithms can exist even when the generator and modulus share a common divisor greater than 1.
|
||||
For example, since 2[sup 1] = 2 mod 4, log[sub 2](2) = 1.
|
||||
Trial multiplication successfully recovers this value, and `baby_step_giant_step_discrete_log` blows up.
|
||||
|
||||
|
||||
[endsect]
|
||||
|
||||
[section References]
|
||||
Wagstaff, Samuel S., ['The Joy of Factoring], Vol. 68. American Mathematical Soc., 2013.
|
||||
[endsect]
|
||||
[endsect]
|
||||
[/
|
||||
Copyright 2018 Nick Thompson.
|
||||
Distributed under 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).
|
||||
]
|
||||
47
doc/modular_arithmetic/extended_euclidean.qbk
Normal file
47
doc/modular_arithmetic/extended_euclidean.qbk
Normal file
@@ -0,0 +1,47 @@
|
||||
[section:extended_euclidean Extended Euclidean Algorithm]
|
||||
|
||||
[section Introduction]
|
||||
|
||||
The extended Euclidean algorithm solves the integer relation /mx + ny/ = gcd(/m/, /n/) for /x/ and /y/.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section Synopsis]
|
||||
|
||||
#include <boost/integer/extended_euclidean.hpp>
|
||||
|
||||
namespace boost { namespace integer {
|
||||
|
||||
template<class Z>
|
||||
std::tuple<Z, Z, Z> extended_euclidean(Z m, Z n);
|
||||
|
||||
}}
|
||||
|
||||
[endsect]
|
||||
|
||||
[section Usage]
|
||||
|
||||
The tuple returned by the extended Euclidean algorithm contains, the greatest common divisor, /x/, and /y/, in that order:
|
||||
|
||||
int m = 12;
|
||||
int n = 15;
|
||||
auto tup = extended_euclidean(m, n);
|
||||
|
||||
int gcd = std::get<0>(tup);
|
||||
int x = std::get<1>(tup);
|
||||
int y = std::get<2>(tup);
|
||||
// mx + ny = gcd(m,n)
|
||||
|
||||
[endsect]
|
||||
|
||||
[section References]
|
||||
Wagstaff, Samuel S., ['The Joy of Factoring], Vol. 68. American Mathematical Soc., 2013.
|
||||
|
||||
[endsect]
|
||||
[endsect]
|
||||
[/
|
||||
Copyright 2018 Nick Thompson.
|
||||
Distributed under 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).
|
||||
]
|
||||
51
doc/modular_arithmetic/modular_multiplicative_inverse.qbk
Normal file
51
doc/modular_arithmetic/modular_multiplicative_inverse.qbk
Normal file
@@ -0,0 +1,51 @@
|
||||
[section:modular_multiplicative_inverse Modular Multiplicative Inverse]
|
||||
|
||||
[section Introduction]
|
||||
|
||||
The modular multiplicative inverse of a number /a/ is that number /x/ which satisfied /ax/ = 1 mod /p/.
|
||||
A fast algorithm for computing modular multiplicative inverses based on the extended Euclidean algorithm exists and is provided by Boost.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section Synopsis]
|
||||
|
||||
#include <boost/integer/modular_multiplicative_inverse.hpp>
|
||||
|
||||
namespace boost { namespace integer {
|
||||
|
||||
template<class Z>
|
||||
boost::optional<Z> modular_multiplicative_inverse(Z a, Z p);
|
||||
|
||||
}}
|
||||
|
||||
[endsect]
|
||||
|
||||
[section Usage]
|
||||
|
||||
Multiplicative modular inverses exist if and only if /a/ and /p/ are coprime.
|
||||
So for example
|
||||
|
||||
auto x = modular_multiplicative_inverse(2, 5);
|
||||
if (x)
|
||||
{
|
||||
int should_be_three = x.value();
|
||||
}
|
||||
auto y = modular_multiplicative_inverse(2, 4);
|
||||
if (!y)
|
||||
{
|
||||
std::cout << "There is no inverse of 2 mod 4\n";
|
||||
}
|
||||
|
||||
[endsect]
|
||||
|
||||
[section References]
|
||||
Wagstaff, Samuel S., ['The Joy of Factoring], Vol. 68. American Mathematical Soc., 2013.
|
||||
|
||||
[endsect]
|
||||
[endsect]
|
||||
[/
|
||||
Copyright 2018 Nick Thompson.
|
||||
Distributed under 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).
|
||||
]
|
||||
Reference in New Issue
Block a user