6#ifndef CRYPTOPP_ALGEBRA_H
7#define CRYPTOPP_ALGEBRA_H
13NAMESPACE_BEGIN(CryptoPP)
38 virtual bool Equal(
const Element &a,
const Element &b)
const =0;
48 virtual const Element&
Add(
const Element &a,
const Element &b)
const =0;
53 virtual const Element&
Inverse(
const Element &a)
const =0;
62 virtual const Element& Double(
const Element &a)
const;
68 virtual const Element& Subtract(
const Element &a,
const Element &b)
const;
74 virtual Element& Accumulate(Element &a,
const Element &b)
const;
80 virtual Element& Reduce(Element &a,
const Element &b)
const;
86 virtual Element ScalarMultiply(
const Element &a,
const Integer &e)
const;
94 virtual Element CascadeScalarMultiply(
const Element &x,
const Integer &e1,
const Element &y,
const Integer &e2)
const;
106 virtual void SimultaneousMultiply(Element *results,
const Element &base,
const Integer *exponents,
unsigned int exponentsCount)
const;
129 {CRYPTOPP_UNUSED(source); m_mg.m_pRing =
this;}
134 {CRYPTOPP_UNUSED(source);
return *
this;}
139 virtual bool IsUnit(
const Element &a)
const =0;
149 virtual const Element&
Multiply(
const Element &a,
const Element &b)
const =0;
158 virtual const Element&
Square(
const Element &a)
const;
164 virtual const Element& Divide(
const Element &a,
const Element &b)
const;
170 virtual Element Exponentiate(
const Element &a,
const Integer &e)
const;
178 virtual Element CascadeExponentiate(
const Element &x,
const Integer &e1,
const Element &y,
const Integer &e2)
const;
190 virtual void SimultaneousExponentiate(Element *results,
const Element &base,
const Integer *exponents,
unsigned int exponentsCount)
const;
204 bool Equal(
const Element &a,
const Element &b)
const
205 {
return GetRing().
Equal(a, b);}
207 const Element& Identity()
const
208 {
return GetRing().MultiplicativeIdentity();}
210 const Element& Add(
const Element &a,
const Element &b)
const
211 {
return GetRing().Multiply(a, b);}
213 Element& Accumulate(Element &a,
const Element &b)
const
214 {
return a = GetRing().Multiply(a, b);}
216 const Element& Inverse(
const Element &a)
const
217 {
return GetRing().MultiplicativeInverse(a);}
219 const Element& Subtract(
const Element &a,
const Element &b)
const
220 {
return GetRing().Divide(a, b);}
222 Element& Reduce(Element &a,
const Element &b)
const
223 {
return a = GetRing().Divide(a, b);}
225 const Element& Double(
const Element &a)
const
226 {
return GetRing().Square(a);}
228 Element ScalarMultiply(
const Element &a,
const Integer &e)
const
229 {
return GetRing().Exponentiate(a, e);}
231 Element CascadeScalarMultiply(
const Element &x,
const Integer &e1,
const Element &y,
const Integer &e2)
const
232 {
return GetRing().CascadeExponentiate(x, e1, y, e2);}
234 void SimultaneousMultiply(Element *results,
const Element &base,
const Integer *exponents,
unsigned int exponentsCount)
const
235 {GetRing().SimultaneousExponentiate(results, base, exponents, exponentsCount);}
240 MultiplicativeGroupT m_mg;
248template <
class T,
class E = Integer>
253 BaseAndExponent(
const T &base,
const E &exponent) : base(base), exponent(exponent) {}
260template <
class Element,
class Iterator>
262template <
class Element,
class Iterator>
263 Element GeneralCascadeExponentiation(
const AbstractRing<Element> &ring, Iterator begin, Iterator end);
286 virtual void DivisionAlgorithm(Element &r, Element &q,
const Element &a,
const Element &d)
const =0;
292 virtual const Element& Mod(
const Element &a,
const Element &b)
const =0;
298 virtual const Element& Gcd(
const Element &a,
const Element &b)
const;
301 mutable Element result;
322 bool Equal(
const Element &a,
const Element &b)
const
326 {
return Element::Zero();}
328 const Element&
Add(
const Element &a,
const Element &b)
const
329 {
return result = a+b;}
334 const Element&
Inverse(
const Element &a)
const
335 {
return result = -a;}
337 const Element&
Subtract(
const Element &a,
const Element &b)
const
338 {
return result = a-b;}
340 Element&
Reduce(Element &a,
const Element &b)
const
343 const Element&
Double(
const Element &a)
const
344 {
return result = a.Doubled();}
347 {
return Element::One();}
349 const Element&
Multiply(
const Element &a,
const Element &b)
const
350 {
return result = a*b;}
352 const Element&
Square(
const Element &a)
const
353 {
return result = a.Squared();}
359 {
return result = a.MultiplicativeInverse();}
361 const Element&
Divide(
const Element &a,
const Element &b)
const
362 {
return result = a/b;}
364 const Element&
Mod(
const Element &a,
const Element &b)
const
365 {
return result = a%b;}
368 {Element::Divide(r, q, a, d);}
371 {CRYPTOPP_UNUSED(rhs);
return true;}
374 mutable Element result;
389 typedef T EuclideanDomain;
390 typedef typename T::Element Element;
392 QuotientRing(
const EuclideanDomain &domain,
const Element &modulus)
393 : m_domain(domain), m_modulus(modulus) {}
395 const EuclideanDomain & GetDomain()
const
398 const Element& GetModulus()
const
401 bool Equal(
const Element &a,
const Element &b)
const
402 {
return m_domain.Equal(m_domain.Mod(m_domain.Subtract(a, b), m_modulus), m_domain.Identity());}
405 {
return m_domain.Identity();}
407 const Element& Add(
const Element &a,
const Element &b)
const
408 {
return m_domain.Add(a, b);}
410 Element& Accumulate(Element &a,
const Element &b)
const
411 {
return m_domain.Accumulate(a, b);}
413 const Element& Inverse(
const Element &a)
const
414 {
return m_domain.Inverse(a);}
416 const Element& Subtract(
const Element &a,
const Element &b)
const
417 {
return m_domain.Subtract(a, b);}
419 Element& Reduce(Element &a,
const Element &b)
const
420 {
return m_domain.Reduce(a, b);}
422 const Element& Double(
const Element &a)
const
423 {
return m_domain.Double(a);}
425 bool IsUnit(
const Element &a)
const
426 {
return m_domain.IsUnit(m_domain.Gcd(a, m_modulus));}
429 {
return m_domain.MultiplicativeIdentity();}
431 const Element& Multiply(
const Element &a,
const Element &b)
const
432 {
return m_domain.Mod(m_domain.Multiply(a, b), m_modulus);}
434 const Element&
Square(
const Element &a)
const
435 {
return m_domain.Mod(m_domain.Square(a), m_modulus);}
437 const Element& MultiplicativeInverse(
const Element &a)
const;
440 {
return m_domain == rhs.m_domain && m_modulus == rhs.m_modulus;}
443 EuclideanDomain m_domain;
449#ifdef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
450#include "algebra.cpp"
Abstract Euclidean domain.
virtual void DivisionAlgorithm(Element &r, Element &q, const Element &a, const Element &d) const =0
Performs the division algorithm on two elements in the ring.
virtual bool Equal(const Element &a, const Element &b) const =0
Compare two elements for equality.
virtual bool InversionIsFast() const
Determine if inversion is fast.
virtual const Element & Add(const Element &a, const Element &b) const =0
Adds elements in the group.
virtual const Element & Identity() const =0
Provides the Identity element.
virtual const Element & Inverse(const Element &a) const =0
Inverts the element in the group.
virtual const Element & Multiply(const Element &a, const Element &b) const =0
Multiplies elements in the group.
virtual const AbstractGroup< T > & MultiplicativeGroup() const
Retrieves the multiplicative group.
AbstractRing & operator=(const AbstractRing &source)
Assign an AbstractRing.
virtual const Element & MultiplicativeInverse(const Element &a) const =0
Calculate the multiplicative inverse of an element in the group.
virtual bool IsUnit(const Element &a) const =0
Determines whether an element is a unit in the group.
AbstractRing(const AbstractRing &source)
Copy construct an AbstractRing.
virtual const Element & MultiplicativeIdentity() const =0
Retrieves the multiplicative identity.
AbstractRing()
Construct an AbstractRing.
const Element & MultiplicativeInverse(const Element &a) const
Calculate the multiplicative inverse of an element in the group.
const Element & Double(const Element &a) const
Doubles an element in the group.
const Element & Square(const Element &a) const
Square an element in the group.
const Element & Divide(const Element &a, const Element &b) const
Divides elements in the group.
const Element & MultiplicativeIdentity() const
Retrieves the multiplicative identity.
bool Equal(const Element &a, const Element &b) const
Compare two elements for equality.
Element & Accumulate(Element &a, const Element &b) const
TODO.
const Element & Inverse(const Element &a) const
Inverts the element in the group.
const Element & Subtract(const Element &a, const Element &b) const
Subtracts elements in the group.
const Element & Mod(const Element &a, const Element &b) const
Performs a modular reduction in the ring.
const Element & Add(const Element &a, const Element &b) const
Adds elements in the group.
const Element & Identity() const
Provides the Identity element.
bool IsUnit(const Element &a) const
Determines whether an element is a unit in the group.
const Element & Multiply(const Element &a, const Element &b) const
Multiplies elements in the group.
Element & Reduce(Element &a, const Element &b) const
Reduces an element in the congruence class.
void DivisionAlgorithm(Element &r, Element &q, const Element &a, const Element &d) const
Performs the division algorithm on two elements in the ring.
Multiple precision integer with arithmetic operations.
const Element & Identity() const
Provides the Identity element.
const Element & MultiplicativeIdentity() const
Retrieves the multiplicative identity.
Library configuration file.
Multiple precision integer with arithmetic operations.
Utility functions for the Crypto++ library.