monotone

monotone Mtn Source Tree

Root/cryptopp/algebra.h

1#ifndef CRYPTOPP_ALGEBRA_H
2#define CRYPTOPP_ALGEBRA_H
3
4#include "config.h"
5
6NAMESPACE_BEGIN(CryptoPP)
7
8class Integer;
9
10// "const Element&" returned by member functions are references
11// to internal data members. Since each object may have only
12// one such data member for holding results, the following code
13// will produce incorrect results:
14// abcd = group.Add(group.Add(a,b), group.Add(c,d));
15// But this should be fine:
16// abcd = group.Add(a, group.Add(b, group.Add(c,d));
17
18//! Abstract Group
19template <class T> class AbstractGroup
20{
21public:
22typedef T Element;
23
24virtual ~AbstractGroup() {}
25
26virtual bool Equal(const Element &a, const Element &b) const =0;
27virtual const Element& Identity() const =0;
28virtual const Element& Add(const Element &a, const Element &b) const =0;
29virtual const Element& Inverse(const Element &a) const =0;
30virtual bool InversionIsFast() const {return false;}
31
32virtual const Element& Double(const Element &a) const;
33virtual const Element& Subtract(const Element &a, const Element &b) const;
34virtual Element& Accumulate(Element &a, const Element &b) const;
35virtual Element& Reduce(Element &a, const Element &b) const;
36
37virtual Element ScalarMultiply(const Element &a, const Integer &e) const;
38virtual Element CascadeScalarMultiply(const Element &x, const Integer &e1, const Element &y, const Integer &e2) const;
39
40virtual void SimultaneousMultiply(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const;
41};
42
43//! Abstract Ring
44template <class T> class AbstractRing : public AbstractGroup<T>
45{
46public:
47typedef T Element;
48
49AbstractRing() {m_mg.m_pRing = this;}
50AbstractRing(const AbstractRing &source) {m_mg.m_pRing = this;}
51AbstractRing& operator=(const AbstractRing &source) {return *this;}
52
53virtual bool IsUnit(const Element &a) const =0;
54virtual const Element& MultiplicativeIdentity() const =0;
55virtual const Element& Multiply(const Element &a, const Element &b) const =0;
56virtual const Element& MultiplicativeInverse(const Element &a) const =0;
57
58virtual const Element& Square(const Element &a) const;
59virtual const Element& Divide(const Element &a, const Element &b) const;
60
61virtual Element Exponentiate(const Element &a, const Integer &e) const;
62virtual Element CascadeExponentiate(const Element &x, const Integer &e1, const Element &y, const Integer &e2) const;
63
64virtual void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const;
65
66virtual const AbstractGroup<T>& MultiplicativeGroup() const
67{return m_mg;}
68
69private:
70class MultiplicativeGroupT : public AbstractGroup<T>
71{
72public:
73const AbstractRing<T>& GetRing() const
74{return *m_pRing;}
75
76bool Equal(const Element &a, const Element &b) const
77{return GetRing().Equal(a, b);}
78
79const Element& Identity() const
80{return GetRing().MultiplicativeIdentity();}
81
82const Element& Add(const Element &a, const Element &b) const
83{return GetRing().Multiply(a, b);}
84
85Element& Accumulate(Element &a, const Element &b) const
86{return a = GetRing().Multiply(a, b);}
87
88const Element& Inverse(const Element &a) const
89{return GetRing().MultiplicativeInverse(a);}
90
91const Element& Subtract(const Element &a, const Element &b) const
92{return GetRing().Divide(a, b);}
93
94Element& Reduce(Element &a, const Element &b) const
95{return a = GetRing().Divide(a, b);}
96
97const Element& Double(const Element &a) const
98{return GetRing().Square(a);}
99
100Element ScalarMultiply(const Element &a, const Integer &e) const
101{return GetRing().Exponentiate(a, e);}
102
103Element CascadeScalarMultiply(const Element &x, const Integer &e1, const Element &y, const Integer &e2) const
104{return GetRing().CascadeExponentiate(x, e1, y, e2);}
105
106void SimultaneousMultiply(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const
107{GetRing().SimultaneousExponentiate(results, base, exponents, exponentsCount);}
108
109const AbstractRing<T> *m_pRing;
110};
111
112MultiplicativeGroupT m_mg;
113};
114
115// ********************************************************
116
117//! Base and Exponent
118template <class T, class E = Integer>
119struct BaseAndExponent
120{
121public:
122BaseAndExponent() {}
123BaseAndExponent(const T &base, const E &exponent) : base(base), exponent(exponent) {}
124bool operator<(const BaseAndExponent<T, E> &rhs) const {return exponent < rhs.exponent;}
125T base;
126E exponent;
127};
128
129// VC60 workaround: incomplete member template support
130template <class Element, class Iterator>
131Element GeneralCascadeMultiplication(const AbstractGroup<Element> &group, Iterator begin, Iterator end);
132template <class Element, class Iterator>
133Element GeneralCascadeExponentiation(const AbstractRing<Element> &ring, Iterator begin, Iterator end);
134
135// ********************************************************
136
137//! Abstract Euclidean Domain
138template <class T> class AbstractEuclideanDomain : public AbstractRing<T>
139{
140public:
141typedef T Element;
142
143virtual void DivisionAlgorithm(Element &r, Element &q, const Element &a, const Element &d) const =0;
144
145virtual const Element& Mod(const Element &a, const Element &b) const =0;
146virtual const Element& Gcd(const Element &a, const Element &b) const;
147
148protected:
149mutable Element result;
150};
151
152// ********************************************************
153
154//! EuclideanDomainOf
155template <class T> class EuclideanDomainOf : public AbstractEuclideanDomain<T>
156{
157public:
158typedef T Element;
159
160EuclideanDomainOf() {}
161
162bool Equal(const Element &a, const Element &b) const
163{return a==b;}
164
165const Element& Identity() const
166{return Element::Zero();}
167
168const Element& Add(const Element &a, const Element &b) const
169{return result = a+b;}
170
171Element& Accumulate(Element &a, const Element &b) const
172{return a+=b;}
173
174const Element& Inverse(const Element &a) const
175{return result = -a;}
176
177const Element& Subtract(const Element &a, const Element &b) const
178{return result = a-b;}
179
180Element& Reduce(Element &a, const Element &b) const
181{return a-=b;}
182
183const Element& Double(const Element &a) const
184{return result = a.Doubled();}
185
186const Element& MultiplicativeIdentity() const
187{return Element::One();}
188
189const Element& Multiply(const Element &a, const Element &b) const
190{return result = a*b;}
191
192const Element& Square(const Element &a) const
193{return result = a.Squared();}
194
195bool IsUnit(const Element &a) const
196{return a.IsUnit();}
197
198const Element& MultiplicativeInverse(const Element &a) const
199{return result = a.MultiplicativeInverse();}
200
201const Element& Divide(const Element &a, const Element &b) const
202{return result = a/b;}
203
204const Element& Mod(const Element &a, const Element &b) const
205{return result = a%b;}
206
207void DivisionAlgorithm(Element &r, Element &q, const Element &a, const Element &d) const
208{Element::Divide(r, q, a, d);}
209
210private:
211mutable Element result;
212};
213
214//! Quotient Ring
215template <class T> class QuotientRing : public AbstractRing<typename T::Element>
216{
217public:
218typedef T EuclideanDomain;
219typedef typename T::Element Element;
220
221QuotientRing(const EuclideanDomain &domain, const Element &modulus)
222: m_domain(domain), m_modulus(modulus) {}
223
224const EuclideanDomain & GetDomain() const
225{return m_domain;}
226
227const Element& GetModulus() const
228{return m_modulus;}
229
230bool Equal(const Element &a, const Element &b) const
231{return m_domain.Equal(m_domain.Mod(m_domain.Subtract(a, b), m_modulus), m_domain.Identity());}
232
233const Element& Identity() const
234{return m_domain.Identity();}
235
236const Element& Add(const Element &a, const Element &b) const
237{return m_domain.Add(a, b);}
238
239Element& Accumulate(Element &a, const Element &b) const
240{return m_domain.Accumulate(a, b);}
241
242const Element& Inverse(const Element &a) const
243{return m_domain.Inverse(a);}
244
245const Element& Subtract(const Element &a, const Element &b) const
246{return m_domain.Subtract(a, b);}
247
248Element& Reduce(Element &a, const Element &b) const
249{return m_domain.Reduce(a, b);}
250
251const Element& Double(const Element &a) const
252{return m_domain.Double(a);}
253
254bool IsUnit(const Element &a) const
255{return m_domain.IsUnit(m_domain.Gcd(a, m_modulus));}
256
257const Element& MultiplicativeIdentity() const
258{return m_domain.MultiplicativeIdentity();}
259
260const Element& Multiply(const Element &a, const Element &b) const
261{return m_domain.Mod(m_domain.Multiply(a, b), m_modulus);}
262
263const Element& Square(const Element &a) const
264{return m_domain.Mod(m_domain.Square(a), m_modulus);}
265
266const Element& MultiplicativeInverse(const Element &a) const;
267
268protected:
269EuclideanDomain m_domain;
270Element m_modulus;
271};
272
273NAMESPACE_END
274
275#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status