6 #ifndef CRYPTOPP_MODARITH_H 7 #define CRYPTOPP_MODARITH_H 38 typedef int RandomizationParameter;
41 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 48 :
AbstractRing<Integer>(), m_modulus(modulus), m_result((word)0, modulus.reg.size()) {}
53 :
AbstractRing<Integer>(), m_modulus(ma.m_modulus), m_result((word)0, ma.m_modulus.reg.size()) {}
86 {m_modulus = newModulus; m_result.reg.
resize(m_modulus.reg.size());}
110 const Integer& Half(
const Integer &a)
const;
117 bool Equal(
const Integer &a,
const Integer &b)
const 129 const Integer& Add(
const Integer &a,
const Integer &b)
const;
135 Integer& Accumulate(Integer &a,
const Integer &b)
const;
140 const Integer& Inverse(
const Integer &a)
const;
146 const Integer& Subtract(
const Integer &a,
const Integer &b)
const;
152 Integer& Reduce(Integer &a,
const Integer &b)
const;
158 const Integer&
Double(
const Integer &a)
const 172 const Integer&
Multiply(
const Integer &a,
const Integer &b)
const 173 {
return m_result1 = a*b%m_modulus;}
179 const Integer&
Square(
const Integer &a)
const 180 {
return m_result1 = a.
Squared()%m_modulus;}
200 const Integer&
Divide(
const Integer &a,
const Integer &b)
const 201 {
return Multiply(a, MultiplicativeInverse(b));}
209 Integer CascadeExponentiate(
const Integer &x,
const Integer &e1,
const Integer &y,
const Integer &e2)
const;
221 void SimultaneousExponentiate(Element *results,
const Element &base,
const Integer *exponents,
unsigned int exponentsCount)
const;
226 {
return (m_modulus-1).BitCount();}
231 {
return (m_modulus-1).ByteCount();}
243 CRYPTOPP_UNUSED(ignore_for_now);
252 {
return m_modulus == rhs.m_modulus;}
254 static const RandomizationParameter DefaultRandomizationParameter ;
258 mutable Integer m_result, m_result1;
277 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 295 {
return (a<<(WORD_BITS*m_modulus.reg.size()))%m_modulus;}
300 {
return m_result1 =
Integer::Power2(WORD_BITS*m_modulus.reg.size())%m_modulus;}
302 const Integer&
Multiply(
const Integer &a,
const Integer &b)
const;
304 const Integer&
Square(
const Integer &a)
const;
308 Integer
CascadeExponentiate(
const Integer &x,
const Integer &e1,
const Integer &y,
const Integer &e2)
const const Integer & Double(const Integer &a) const
Doubles an element in the ring.
bool Equal(const Integer &a, const Integer &b) const
Compare two elements for equality.
const Integer & Square(const Integer &a) const
Square an element in the ring.
bool IsMontgomeryRepresentation() const
Retrieves the representation.
const Integer & Divide(const Integer &a, const Integer &b) const
Divides elements in the ring.
Utility functions for the Crypto++ library.
static Integer Gcd(const Integer &a, const Integer &n)
greatest common divisor
void resize(size_type newSize)
Change size and preserve contents.
virtual Integer ConvertOut(const Integer &a) const
Reduces an element in the congruence class.
virtual Element CascadeExponentiate(const Element &x, const Integer &e1, const Element &y, const Integer &e2) const
TODO.
Abstract base classes that provide a uniform interface to this library.
const Integer & MultiplicativeInverse(const Integer &a) const
Calculate the multiplicative inverse of an element in the ring.
Abstract Euclidean domain.
unsigned int MaxElementByteLength() const
Provides the maximum byte size of an element in the ring.
Ring of congruence classes modulo n.
Interface for random number generators.
unsigned int MaxElementBitLength() const
Provides the maximum bit size of an element in the ring.
Integer InverseMod(const Integer &n) const
calculate multiplicative inverse of *this mod n
Classes for performing mathematics over different fields.
static const Integer & One()
Integer representing 1.
Integer ConvertIn(const Integer &a) const
Reduces an element in the congruence class.
const Integer & Identity() const
Provides the Identity element.
bool IsUnit() const
is 1 or -1
Classes and functions for secure memory allocations.
virtual ModularArithmetic * Clone() const
Clone a MontgomeryRepresentation.
bool operator==(const ModularArithmetic &rhs) const
Compares two ModularArithmetic for equality.
const Integer & Multiply(const Integer &a, const Integer &b) const
Multiplies elements in the ring.
static Integer Power2(size_t e)
Exponentiates to a power of 2.
Multiple precision integer with arithmetic operations.
const Integer & GetModulus() const
Retrieves the modulus.
const Integer & MultiplicativeIdentity() const
Retrieves the multiplicative identity.
ModularArithmetic(const Integer &modulus=Integer::One())
Construct a ModularArithmetic.
virtual Integer ConvertIn(const Integer &a) const
Reduces an element in the congruence class.
virtual ModularArithmetic * Clone() const
Clone a ModularArithmetic.
void SetModulus(const Integer &newModulus)
Sets the modulus.
Performs modular arithmetic in Montgomery representation for increased speed.
Element RandomElement(RandomNumberGenerator &rng, const RandomizationParameter &ignore_for_now=0) const
Provides a random element in the ring.
Integer CascadeExponentiate(const Integer &x, const Integer &e1, const Integer &y, const Integer &e2) const
TODO.
Multiple precision integer with arithmetic operations.
static const Integer & Zero()
Integer representing 0.
Crypto++ library namespace.
ModularArithmetic(const ModularArithmetic &ma)
Copy construct a ModularArithmetic.
const Integer & MultiplicativeIdentity() const
Retrieves the multiplicative identity.
void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const
Exponentiates a base to multiple exponents in the ring.
virtual void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const
Exponentiates a base to multiple exponents in the Ring.
virtual bool IsMontgomeryRepresentation() const
Retrieves the representation.
bool IsUnit(const Integer &a) const
Determines whether an element is a unit in the ring.