7 #ifndef CRYPTOPP_ALGEBRA_H 8 #define CRYPTOPP_ALGEBRA_H 32 virtual ~AbstractGroup() {}
34 virtual bool Equal(
const Element &a,
const Element &b)
const =0;
35 virtual const Element& Identity()
const =0;
36 virtual const Element& Add(
const Element &a,
const Element &b)
const =0;
37 virtual const Element& Inverse(
const Element &a)
const =0;
38 virtual bool InversionIsFast()
const {
return false;}
40 virtual const Element& Double(
const Element &a)
const;
41 virtual const Element& Subtract(
const Element &a,
const Element &b)
const;
42 virtual Element& Accumulate(Element &a,
const Element &b)
const;
43 virtual Element& Reduce(Element &a,
const Element &b)
const;
45 virtual Element ScalarMultiply(
const Element &a,
const Integer &e)
const;
46 virtual Element CascadeScalarMultiply(
const Element &x,
const Integer &e1,
const Element &y,
const Integer &e2)
const;
48 virtual void SimultaneousMultiply(Element *results,
const Element &base,
const Integer *exponents,
unsigned int exponentsCount)
const;
59 {CRYPTOPP_UNUSED(source); m_mg.m_pRing =
this;}
61 {CRYPTOPP_UNUSED(source);
return *
this;}
63 virtual bool IsUnit(
const Element &a)
const =0;
64 virtual const Element& MultiplicativeIdentity()
const =0;
65 virtual const Element& Multiply(
const Element &a,
const Element &b)
const =0;
66 virtual const Element& MultiplicativeInverse(
const Element &a)
const =0;
68 virtual const Element&
Square(
const Element &a)
const;
69 virtual const Element& Divide(
const Element &a,
const Element &b)
const;
71 virtual Element Exponentiate(
const Element &a,
const Integer &e)
const;
72 virtual Element CascadeExponentiate(
const Element &x,
const Integer &e1,
const Element &y,
const Integer &e2)
const;
74 virtual void SimultaneousExponentiate(Element *results,
const Element &base,
const Integer *exponents,
unsigned int exponentsCount)
const;
86 bool Equal(
const Element &a,
const Element &b)
const 87 {
return GetRing().Equal(a, b);}
89 const Element& Identity()
const 90 {
return GetRing().MultiplicativeIdentity();}
92 const Element& Add(
const Element &a,
const Element &b)
const 93 {
return GetRing().Multiply(a, b);}
95 Element& Accumulate(Element &a,
const Element &b)
const 96 {
return a = GetRing().Multiply(a, b);}
98 const Element& Inverse(
const Element &a)
const 99 {
return GetRing().MultiplicativeInverse(a);}
101 const Element& Subtract(
const Element &a,
const Element &b)
const 102 {
return GetRing().Divide(a, b);}
104 Element& Reduce(Element &a,
const Element &b)
const 105 {
return a = GetRing().Divide(a, b);}
107 const Element& Double(
const Element &a)
const 108 {
return GetRing().Square(a);}
110 Element ScalarMultiply(
const Element &a,
const Integer &e)
const 111 {
return GetRing().Exponentiate(a, e);}
113 Element CascadeScalarMultiply(
const Element &x,
const Integer &e1,
const Element &y,
const Integer &e2)
const 114 {
return GetRing().CascadeExponentiate(x, e1, y, e2);}
116 void SimultaneousMultiply(Element *results,
const Element &base,
const Integer *exponents,
unsigned int exponentsCount)
const 117 {GetRing().SimultaneousExponentiate(results, base, exponents, exponentsCount);}
122 MultiplicativeGroupT m_mg;
128 template <
class T,
class E = Integer>
133 BaseAndExponent(
const T &base,
const E &exponent) : base(base), exponent(exponent) {}
134 bool operator<(const BaseAndExponent<T, E> &rhs)
const {
return exponent < rhs.exponent;}
140 template <
class Element,
class Iterator>
142 template <
class Element,
class Iterator>
143 Element GeneralCascadeExponentiation(
const AbstractRing<Element> &ring, Iterator begin, Iterator end);
153 virtual void DivisionAlgorithm(Element &r, Element &q,
const Element &a,
const Element &d)
const =0;
155 virtual const Element& Mod(
const Element &a,
const Element &b)
const =0;
156 virtual const Element& Gcd(
const Element &a,
const Element &b)
const;
159 mutable Element result;
172 bool Equal(
const Element &a,
const Element &b)
const 175 const Element& Identity()
const 176 {
return Element::Zero();}
178 const Element& Add(
const Element &a,
const Element &b)
const 179 {
return result = a+b;}
181 Element& Accumulate(Element &a,
const Element &b)
const 184 const Element& Inverse(
const Element &a)
const 185 {
return result = -a;}
187 const Element& Subtract(
const Element &a,
const Element &b)
const 188 {
return result = a-b;}
190 Element& Reduce(Element &a,
const Element &b)
const 193 const Element& Double(
const Element &a)
const 194 {
return result = a.Doubled();}
196 const Element& MultiplicativeIdentity()
const 197 {
return Element::One();}
199 const Element& Multiply(
const Element &a,
const Element &b)
const 200 {
return result = a*b;}
202 const Element&
Square(
const Element &a)
const 203 {
return result = a.Squared();}
205 bool IsUnit(
const Element &a)
const 208 const Element& MultiplicativeInverse(
const Element &a)
const 209 {
return result = a.MultiplicativeInverse();}
211 const Element& Divide(
const Element &a,
const Element &b)
const 212 {
return result = a/b;}
214 const Element& Mod(
const Element &a,
const Element &b)
const 215 {
return result = a%b;}
217 void DivisionAlgorithm(Element &r, Element &q,
const Element &a,
const Element &d)
const 218 {Element::Divide(r, q, a, d);}
221 {CRYPTOPP_UNUSED(rhs);
return true;}
224 mutable Element result;
231 typedef T EuclideanDomain;
232 typedef typename T::Element Element;
234 QuotientRing(
const EuclideanDomain &domain,
const Element &modulus)
235 : m_domain(domain), m_modulus(modulus) {}
237 const EuclideanDomain & GetDomain()
const 240 const Element& GetModulus()
const 243 bool Equal(
const Element &a,
const Element &b)
const 244 {
return m_domain.Equal(m_domain.Mod(m_domain.Subtract(a, b), m_modulus), m_domain.Identity());}
246 const Element& Identity()
const 247 {
return m_domain.Identity();}
249 const Element& Add(
const Element &a,
const Element &b)
const 250 {
return m_domain.Add(a, b);}
252 Element& Accumulate(Element &a,
const Element &b)
const 253 {
return m_domain.Accumulate(a, b);}
255 const Element& Inverse(
const Element &a)
const 256 {
return m_domain.Inverse(a);}
258 const Element& Subtract(
const Element &a,
const Element &b)
const 259 {
return m_domain.Subtract(a, b);}
261 Element& Reduce(Element &a,
const Element &b)
const 262 {
return m_domain.Reduce(a, b);}
264 const Element& Double(
const Element &a)
const 265 {
return m_domain.Double(a);}
267 bool IsUnit(
const Element &a)
const 268 {
return m_domain.IsUnit(m_domain.Gcd(a, m_modulus));}
270 const Element& MultiplicativeIdentity()
const 271 {
return m_domain.MultiplicativeIdentity();}
273 const Element& Multiply(
const Element &a,
const Element &b)
const 274 {
return m_domain.Mod(m_domain.Multiply(a, b), m_modulus);}
276 const Element&
Square(
const Element &a)
const 277 {
return m_domain.Mod(m_domain.Square(a), m_modulus);}
279 const Element& MultiplicativeInverse(
const Element &a)
const;
282 {
return m_domain == rhs.m_domain && m_modulus == rhs.m_modulus;}
285 EuclideanDomain m_domain;
291 #ifdef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES 292 #include "algebra.cpp" Utility functions for the Crypto++ library.
Abstract Euclidean Domain.
Library configuration file.
Multiple precision integer with arithmetic operations.
Crypto++ library namespace.