1 #ifndef CRYPTOPP_MODARITH_H
2 #define CRYPTOPP_MODARITH_H
11 NAMESPACE_BEGIN(CryptoPP)
23 typedef int RandomizationParameter;
27 : m_modulus(modulus), m_result((word)0, modulus.reg.size()) {}
30 : m_modulus(ma.m_modulus), m_result((word)0, m_modulus.reg.size()) {}
41 const Integer& GetModulus()
const {
return m_modulus;}
42 void SetModulus(
const Integer &newModulus) {m_modulus = newModulus; m_result.reg.
resize(m_modulus.reg.size());}
44 virtual bool IsMontgomeryRepresentation()
const {
return false;}
46 virtual Integer ConvertIn(
const Integer &a)
const
49 virtual Integer ConvertOut(
const Integer &a)
const
52 const Integer& Half(
const Integer &a)
const;
54 bool Equal(
const Integer &a,
const Integer &b)
const
57 const Integer& Identity()
const
60 const Integer& Add(
const Integer &a,
const Integer &b)
const;
62 Integer& Accumulate(Integer &a,
const Integer &b)
const;
64 const Integer& Inverse(
const Integer &a)
const;
66 const Integer& Subtract(
const Integer &a,
const Integer &b)
const;
68 Integer& Reduce(Integer &a,
const Integer &b)
const;
70 const Integer& Double(
const Integer &a)
const
73 const Integer& MultiplicativeIdentity()
const
76 const Integer& Multiply(
const Integer &a,
const Integer &b)
const
77 {
return m_result1 = a*b%m_modulus;}
79 const Integer&
Square(
const Integer &a)
const
80 {
return m_result1 = a.Squared()%m_modulus;}
82 bool IsUnit(
const Integer &a)
const
85 const Integer& MultiplicativeInverse(
const Integer &a)
const
88 const Integer& Divide(
const Integer &a,
const Integer &b)
const
89 {
return Multiply(a, MultiplicativeInverse(b));}
91 Integer CascadeExponentiate(
const Integer &x,
const Integer &e1,
const Integer &y,
const Integer &e2)
const;
93 void SimultaneousExponentiate(Element *results,
const Element &base,
const Integer *exponents,
unsigned int exponentsCount)
const;
95 unsigned int MaxElementBitLength()
const
96 {
return (m_modulus-1).BitCount();}
98 unsigned int MaxElementByteLength()
const
99 {
return (m_modulus-1).ByteCount();}
101 Element RandomElement(
RandomNumberGenerator &rng ,
const RandomizationParameter &ignore_for_now = 0 )
const
104 return Element( rng , Integer( (
long) 0) , m_modulus - Integer( (
long) 1 ) ) ;
108 {
return m_modulus == rhs.m_modulus;}
110 static const RandomizationParameter DefaultRandomizationParameter ;
114 mutable Integer m_result, m_result1;
129 bool IsMontgomeryRepresentation()
const {
return true;}
132 {
return (a<<(WORD_BITS*m_modulus.reg.size()))%m_modulus;}
136 const Integer& MultiplicativeIdentity()
const
137 {
return m_result1 =
Integer::Power2(WORD_BITS*m_modulus.reg.size())%m_modulus;}
148 void SimultaneousExponentiate(
Element *results,
const Element &base,
const Integer *exponents,
unsigned int exponentsCount)
const
static const Integer & One()
avoid calling constructors for these frequently used integers
void resize(size_type newSize)
change size and preserve contents
static Integer Gcd(const Integer &a, const Integer &n)
greatest common divisor
Abstract Euclidean Domain.
ring of congruence classes modulo n
interface for random number generators
bool IsUnit() const
is 1 or -1
multiple precision integer and basic arithmetics
static Integer Power2(size_t e)
return the integer 2**e
do modular arithmetics in Montgomery representation for increased speed
Integer InverseMod(const Integer &n) const
calculate multiplicative inverse of *this mod n
static const Integer & Zero()
avoid calling constructors for these frequently used integers