ext-cryptopp/modarith.h

317 lines
13 KiB
C
Raw Normal View History

// modarith.h - originally written and placed in the public domain by Wei Dai
//! \file modarith.h
//! \brief Class file for performing modular arithmetic.
2015-11-05 06:59:46 +00:00
#ifndef CRYPTOPP_MODARITH_H
#define CRYPTOPP_MODARITH_H
// implementations are in integer.cpp
#include "cryptlib.h"
#include "integer.h"
#include "algebra.h"
#include "secblock.h"
#include "misc.h"
NAMESPACE_BEGIN(CryptoPP)
CRYPTOPP_DLL_TEMPLATE_CLASS AbstractGroup<Integer>;
CRYPTOPP_DLL_TEMPLATE_CLASS AbstractRing<Integer>;
CRYPTOPP_DLL_TEMPLATE_CLASS AbstractEuclideanDomain<Integer>;
//! \class ModularArithmetic
//! \brief Ring of congruence classes modulo n
2016-02-29 14:33:33 +00:00
//! \details This implementation represents each congruence class as the smallest
//! non-negative integer in that class.
2016-04-05 18:22:45 +00:00
//! \details <tt>const Element&</tt> returned by member functions are references
//! to internal data members. Since each object may have only
//! one such data member for holding results, the following code
//! will produce incorrect results:
//! <pre> abcd = group.Add(group.Add(a,b), group.Add(c,d));</pre>
//! But this should be fine:
//! <pre> abcd = group.Add(a, group.Add(b, group.Add(c,d));</pre>
2015-11-05 06:59:46 +00:00
class CRYPTOPP_DLL ModularArithmetic : public AbstractRing<Integer>
{
public:
typedef int RandomizationParameter;
typedef Integer Element;
2016-02-29 12:40:04 +00:00
virtual ~ModularArithmetic() {}
//! \brief Construct a ModularArithmetic
//! \param modulus congruence class modulus
2015-11-05 06:59:46 +00:00
ModularArithmetic(const Integer &modulus = Integer::One())
: AbstractRing<Integer>(), m_modulus(modulus), m_result((word)0, modulus.reg.size()) {}
2016-02-29 12:40:04 +00:00
//! \brief Copy construct a ModularArithmetic
//! \param ma other ModularArithmetic
2015-11-05 06:59:46 +00:00
ModularArithmetic(const ModularArithmetic &ma)
: AbstractRing<Integer>(), m_modulus(ma.m_modulus), m_result((word)0, ma.m_modulus.reg.size()) {}
2015-11-05 06:59:46 +00:00
2016-02-29 12:40:04 +00:00
//! \brief Construct a ModularArithmetic
//! \param bt BER encoded ModularArithmetic
2015-11-05 06:59:46 +00:00
ModularArithmetic(BufferedTransformation &bt); // construct from BER encoded parameters
2016-02-29 12:40:04 +00:00
//! \brief Clone a ModularArithmetic
2016-02-29 14:33:33 +00:00
//! \returns pointer to a new ModularArithmetic
//! \details Clone effectively copy constructs a new ModularArithmetic. The caller is
//! responsible for deleting the pointer returned from this method.
2015-11-05 06:59:46 +00:00
virtual ModularArithmetic * Clone() const {return new ModularArithmetic(*this);}
2016-02-29 12:40:04 +00:00
//! \brief Encodes in DER format
//! \param bt BufferedTransformation object
2015-11-05 06:59:46 +00:00
void DEREncode(BufferedTransformation &bt) const;
2016-02-29 12:40:04 +00:00
//! \brief Encodes element in DER format
//! \param out BufferedTransformation object
//! \param a Element to encode
2015-11-05 06:59:46 +00:00
void DEREncodeElement(BufferedTransformation &out, const Element &a) const;
2016-02-29 12:40:04 +00:00
//! \brief Decodes element in DER format
//! \param in BufferedTransformation object
//! \param a Element to decode
2015-11-05 06:59:46 +00:00
void BERDecodeElement(BufferedTransformation &in, Element &a) const;
2016-02-29 12:40:04 +00:00
//! \brief Retrieves the modulus
//! \returns the modulus
2015-11-05 06:59:46 +00:00
const Integer& GetModulus() const {return m_modulus;}
2016-02-29 12:40:04 +00:00
//! \brief Sets the modulus
//! \param newModulus the new modulus
void SetModulus(const Integer &newModulus)
{m_modulus = newModulus; m_result.reg.resize(m_modulus.reg.size());}
2015-11-05 06:59:46 +00:00
2016-02-29 12:40:04 +00:00
//! \brief Retrieves the representation
2016-10-17 03:03:44 +00:00
//! \returns true if the if the modulus is in Montgomery form for multiplication, false otherwise
2015-11-05 06:59:46 +00:00
virtual bool IsMontgomeryRepresentation() const {return false;}
2016-02-29 12:40:04 +00:00
//! \brief Reduces an element in the congruence class
//! \param a element to convert
//! \returns the reduced element
//! \details ConvertIn is useful for derived classes, like MontgomeryRepresentation, which
//! must convert between representations.
2015-11-05 06:59:46 +00:00
virtual Integer ConvertIn(const Integer &a) const
{return a%m_modulus;}
2016-02-29 12:40:04 +00:00
//! \brief Reduces an element in the congruence class
//! \param a element to convert
//! \returns the reduced element
//! \details ConvertOut is useful for derived classes, like MontgomeryRepresentation, which
//! must convert between representations.
2015-11-05 06:59:46 +00:00
virtual Integer ConvertOut(const Integer &a) const
{return a;}
2016-10-17 03:03:44 +00:00
//! \brief Divides an element by 2
2016-02-29 12:40:04 +00:00
//! \param a element to convert
2015-11-05 06:59:46 +00:00
const Integer& Half(const Integer &a) const;
2016-02-29 12:40:04 +00:00
//! \brief Compare two elements for equality
//! \param a first element
//! \param b second element
//! \returns true if the elements are equal, false otherwise
//! \details Equal() tests the elements for equality using <tt>a==b</tt>
2015-11-05 06:59:46 +00:00
bool Equal(const Integer &a, const Integer &b) const
{return a==b;}
2016-02-29 12:40:04 +00:00
//! \brief Provides the Identity element
//! \returns the Identity element
2015-11-05 06:59:46 +00:00
const Integer& Identity() const
{return Integer::Zero();}
2016-04-05 18:22:45 +00:00
//! \brief Adds elements in the ring
2016-02-29 12:40:04 +00:00
//! \param a first element
//! \param b second element
//! \returns the sum of <tt>a</tt> and <tt>b</tt>
2015-11-05 06:59:46 +00:00
const Integer& Add(const Integer &a, const Integer &b) const;
2016-02-29 12:40:04 +00:00
//! \brief TODO
//! \param a first element
//! \param b second element
//! \returns TODO
2015-11-05 06:59:46 +00:00
Integer& Accumulate(Integer &a, const Integer &b) const;
2016-04-05 18:22:45 +00:00
//! \brief Inverts the element in the ring
2016-02-29 12:40:04 +00:00
//! \param a first element
//! \returns the inverse of the element
2015-11-05 06:59:46 +00:00
const Integer& Inverse(const Integer &a) const;
2016-04-05 18:22:45 +00:00
//! \brief Subtracts elements in the ring
2016-02-29 12:40:04 +00:00
//! \param a first element
//! \param b second element
//! \returns the difference of <tt>a</tt> and <tt>b</tt>. The element <tt>a</tt> must provide a Subtract member function.
2015-11-05 06:59:46 +00:00
const Integer& Subtract(const Integer &a, const Integer &b) const;
2016-02-29 12:40:04 +00:00
//! \brief TODO
//! \param a first element
//! \param b second element
//! \returns TODO
2015-11-05 06:59:46 +00:00
Integer& Reduce(Integer &a, const Integer &b) const;
2016-04-05 18:22:45 +00:00
//! \brief Doubles an element in the ring
2016-02-29 12:40:04 +00:00
//! \param a the element
//! \returns the element doubled
//! \details Double returns <tt>Add(a, a)</tt>. The element <tt>a</tt> must provide an Add member function.
2015-11-05 06:59:46 +00:00
const Integer& Double(const Integer &a) const
{return Add(a, a);}
2016-02-29 12:40:04 +00:00
//! \brief Retrieves the multiplicative identity
//! \returns the multiplicative identity
//! \details the base class implementations returns 1.
2015-11-05 06:59:46 +00:00
const Integer& MultiplicativeIdentity() const
{return Integer::One();}
2016-04-05 18:22:45 +00:00
//! \brief Multiplies elements in the ring
//! \param a the multiplicand
//! \param b the multiplier
2016-02-29 12:40:04 +00:00
//! \returns the product of a and b
//! \details Multiply returns <tt>a*b\%n</tt>.
2015-11-05 06:59:46 +00:00
const Integer& Multiply(const Integer &a, const Integer &b) const
{return m_result1 = a*b%m_modulus;}
2016-04-05 18:22:45 +00:00
//! \brief Square an element in the ring
2016-02-29 12:40:04 +00:00
//! \param a the element
//! \returns the element squared
//! \details Square returns <tt>a*a\%n</tt>. The element <tt>a</tt> must provide a Square member function.
2015-11-05 06:59:46 +00:00
const Integer& Square(const Integer &a) const
{return m_result1 = a.Squared()%m_modulus;}
2016-04-05 18:22:45 +00:00
//! \brief Determines whether an element is a unit in the ring
2016-02-29 12:40:04 +00:00
//! \param a the element
//! \returns true if the element is a unit after reduction, false otherwise.
2015-11-05 06:59:46 +00:00
bool IsUnit(const Integer &a) const
{return Integer::Gcd(a, m_modulus).IsUnit();}
2016-04-05 18:22:45 +00:00
//! \brief Calculate the multiplicative inverse of an element in the ring
2016-02-29 12:40:04 +00:00
//! \param a the element
//! \details MultiplicativeInverse returns <tt>a<sup>-1</sup>\%n</tt>. The element <tt>a</tt> must
//! provide a InverseMod member function.
2015-11-05 06:59:46 +00:00
const Integer& MultiplicativeInverse(const Integer &a) const
{return m_result1 = a.InverseMod(m_modulus);}
2016-04-05 18:22:45 +00:00
//! \brief Divides elements in the ring
//! \param a the dividend
//! \param b the divisor
//! \returns the quotient
2016-02-29 12:40:04 +00:00
//! \details Divide returns <tt>a*b<sup>-1</sup>\%n</tt>.
2015-11-05 06:59:46 +00:00
const Integer& Divide(const Integer &a, const Integer &b) const
{return Multiply(a, MultiplicativeInverse(b));}
2016-02-29 12:40:04 +00:00
//! \brief TODO
//! \param x first element
//! \param e1 first exponent
//! \param y second element
//! \param e2 second exponent
//! \returns TODO
2015-11-05 06:59:46 +00:00
Integer CascadeExponentiate(const Integer &x, const Integer &e1, const Integer &y, const Integer &e2) const;
2016-04-05 18:22:45 +00:00
//! \brief Exponentiates a base to multiple exponents in the ring
2016-02-29 12:40:04 +00:00
//! \param results an array of Elements
//! \param base the base to raise to the exponents
//! \param exponents an array of exponents
//! \param exponentsCount the number of exponents in the array
//! \details SimultaneousExponentiate() raises the base to each exponent in the exponents array and stores the
//! result at the respective position in the results array.
//! \details SimultaneousExponentiate() must be implemented in a derived class.
//! \pre <tt>COUNTOF(results) == exponentsCount</tt>
//! \pre <tt>COUNTOF(exponents) == exponentsCount</tt>
2015-11-05 06:59:46 +00:00
void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const;
2016-04-05 18:22:45 +00:00
//! \brief Provides the maximum bit size of an element in the ring
2016-02-29 12:40:04 +00:00
//! \returns maximum bit size of an element
2015-11-05 06:59:46 +00:00
unsigned int MaxElementBitLength() const
{return (m_modulus-1).BitCount();}
2016-04-05 18:22:45 +00:00
//! \brief Provides the maximum byte size of an element in the ring
2016-02-29 12:40:04 +00:00
//! \returns maximum byte size of an element
2015-11-05 06:59:46 +00:00
unsigned int MaxElementByteLength() const
{return (m_modulus-1).ByteCount();}
2016-04-05 18:22:45 +00:00
//! \brief Provides a random element in the ring
2016-02-29 12:40:04 +00:00
//! \param rng RandomNumberGenerator used to generate material
//! \param ignore_for_now unused
//! \returns a random element that is uniformly distributed
//! \details RandomElement constructs a new element in the range <tt>[0,n-1]</tt>, inclusive.
//! The element's class must provide a constructor with the signature <tt>Element(RandomNumberGenerator rng,
//! Element min, Element max)</tt>.
2016-10-17 03:03:44 +00:00
Element RandomElement(RandomNumberGenerator &rng , const RandomizationParameter &ignore_for_now = 0) const
2015-11-05 06:59:46 +00:00
// left RandomizationParameter arg as ref in case RandomizationParameter becomes a more complicated struct
{
CRYPTOPP_UNUSED(ignore_for_now);
2016-09-10 08:57:48 +00:00
return Element(rng, Integer::Zero(), m_modulus - Integer::One()) ;
}
2015-11-05 06:59:46 +00:00
2016-02-29 12:40:04 +00:00
//! \brief Compares two ModularArithmetic for equality
//! \param rhs other ModularArithmetic
//! \returns true if this is equal to the other, false otherwise
//! \details The operator tests for equality using <tt>this.m_modulus == rhs.m_modulus</tt>.
2015-11-05 06:59:46 +00:00
bool operator==(const ModularArithmetic &rhs) const
{return m_modulus == rhs.m_modulus;}
static const RandomizationParameter DefaultRandomizationParameter ;
protected:
Integer m_modulus;
mutable Integer m_result, m_result1;
};
// const ModularArithmetic::RandomizationParameter ModularArithmetic::DefaultRandomizationParameter = 0 ;
//! \class MontgomeryRepresentation
//! \brief Performs modular arithmetic in Montgomery representation for increased speed
//! \details The Montgomery representation represents each congruence class <tt>[a]</tt> as
2016-02-29 12:40:04 +00:00
//! <tt>a*r\%n</tt>, where <tt>r</tt> is a convenient power of 2.
2016-10-17 03:03:44 +00:00
//! \details <tt>const Element&</tt> returned by member functions are references to
//! internal data members. Since each object may have only one such data member for holding
//! results, the following code will produce incorrect results:
2016-04-05 18:22:45 +00:00
//! <pre> abcd = group.Add(group.Add(a,b), group.Add(c,d));</pre>
//! But this should be fine:
//! <pre> abcd = group.Add(a, group.Add(b, group.Add(c,d));</pre>
2015-11-05 06:59:46 +00:00
class CRYPTOPP_DLL MontgomeryRepresentation : public ModularArithmetic
{
public:
2016-02-29 14:33:33 +00:00
virtual ~MontgomeryRepresentation() {}
2016-10-17 03:03:44 +00:00
//! \brief Construct a MontgomeryRepresentation
2016-02-29 14:33:33 +00:00
//! \param modulus congruence class modulus
//! \note The modulus must be odd.
MontgomeryRepresentation(const Integer &modulus);
2015-11-05 06:59:46 +00:00
2016-02-29 14:33:33 +00:00
//! \brief Clone a MontgomeryRepresentation
//! \returns pointer to a new MontgomeryRepresentation
//! \details Clone effectively copy constructs a new MontgomeryRepresentation. The caller is
//! responsible for deleting the pointer returned from this method.
2015-11-05 06:59:46 +00:00
virtual ModularArithmetic * Clone() const {return new MontgomeryRepresentation(*this);}
bool IsMontgomeryRepresentation() const {return true;}
Integer ConvertIn(const Integer &a) const
{return (a<<(WORD_BITS*m_modulus.reg.size()))%m_modulus;}
Integer ConvertOut(const Integer &a) const;
const Integer& MultiplicativeIdentity() const
{return m_result1 = Integer::Power2(WORD_BITS*m_modulus.reg.size())%m_modulus;}
const Integer& Multiply(const Integer &a, const Integer &b) const;
const Integer& Square(const Integer &a) const;
const Integer& MultiplicativeInverse(const Integer &a) const;
Integer CascadeExponentiate(const Integer &x, const Integer &e1, const Integer &y, const Integer &e2) const
{return AbstractRing<Integer>::CascadeExponentiate(x, e1, y, e2);}
void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const
{AbstractRing<Integer>::SimultaneousExponentiate(results, base, exponents, exponentsCount);}
private:
Integer m_u;
mutable IntegerSecBlock m_workspace;
};
NAMESPACE_END
#endif