MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
algebra.hpp
1 /*
2  Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; see the file COPYING. If not, write to the
15  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
16  MA 02110-1301 USA.
17 */
18 
19 /* based on Wei Dai's algebra.h from CryptoPP */
20 
21 #ifndef TAO_CRYPT_ALGEBRA_HPP
22 #define TAO_CRYPT_ALGEBRA_HPP
23 
24 #include "integer.hpp"
25 
26 namespace TaoCrypt {
27 
28 
29 // "const Element&" returned by member functions are references
30 // to internal data members. Since each object may have only
31 // one such data member for holding results, the following code
32 // will produce incorrect results:
33 // abcd = group.Add(group.Add(a,b), group.Add(c,d));
34 // But this should be fine:
35 // abcd = group.Add(a, group.Add(b, group.Add(c,d));
36 
37 // Abstract Group
38 class TAOCRYPT_NO_VTABLE AbstractGroup : public virtual_base
39 {
40 public:
41  typedef Integer Element;
42 
43  virtual ~AbstractGroup() {}
44 
45  virtual bool Equal(const Element &a, const Element &b) const =0;
46  virtual const Element& Identity() const =0;
47  virtual const Element& Add(const Element &a, const Element &b) const =0;
48  virtual const Element& Inverse(const Element &a) const =0;
49  virtual bool InversionIsFast() const {return false;}
50 
51  virtual const Element& Double(const Element &a) const;
52  virtual const Element& Subtract(const Element &a, const Element &b) const;
53  virtual Element& Accumulate(Element &a, const Element &b) const;
54  virtual Element& Reduce(Element &a, const Element &b) const;
55 
56  virtual Element ScalarMultiply(const Element &a, const Integer &e) const;
57  virtual Element CascadeScalarMultiply(const Element &x, const Integer &e1,
58  const Element &y, const Integer &e2) const;
59 
60  virtual void SimultaneousMultiply(Element *results, const Element &base,
61  const Integer *exponents, unsigned int exponentsCount) const;
62 };
63 
64 // Abstract Ring
65 class TAOCRYPT_NO_VTABLE AbstractRing : public AbstractGroup
66 {
67 public:
68  typedef Integer Element;
69 
70  AbstractRing() : AbstractGroup() {m_mg.m_pRing = this;}
71  AbstractRing(const AbstractRing &source) : AbstractGroup()
72  {m_mg.m_pRing = this;}
73  AbstractRing& operator=(const AbstractRing &source) {return *this;}
74 
75  virtual bool IsUnit(const Element &a) const =0;
76  virtual const Element& MultiplicativeIdentity() const =0;
77  virtual const Element& Multiply(const Element&, const Element&) const =0;
78  virtual const Element& MultiplicativeInverse(const Element &a) const =0;
79 
80  virtual const Element& Square(const Element &a) const;
81  virtual const Element& Divide(const Element &a, const Element &b) const;
82 
83  virtual Element Exponentiate(const Element &a, const Integer &e) const;
84  virtual Element CascadeExponentiate(const Element &x, const Integer &e1,
85  const Element &y, const Integer &e2) const;
86 
87  virtual void SimultaneousExponentiate(Element *results, const Element&,
88  const Integer *exponents, unsigned int exponentsCount) const;
89 
90  virtual const AbstractGroup& MultiplicativeGroup() const
91  {return m_mg;}
92 
93 private:
94  class MultiplicativeGroupT : public AbstractGroup
95  {
96  public:
97  const AbstractRing& GetRing() const
98  {return *m_pRing;}
99 
100  bool Equal(const Element &a, const Element &b) const
101  {return GetRing().Equal(a, b);}
102 
103  const Element& Identity() const
104  {return GetRing().MultiplicativeIdentity();}
105 
106  const Element& Add(const Element &a, const Element &b) const
107  {return GetRing().Multiply(a, b);}
108 
109  Element& Accumulate(Element &a, const Element &b) const
110  {return a = GetRing().Multiply(a, b);}
111 
112  const Element& Inverse(const Element &a) const
113  {return GetRing().MultiplicativeInverse(a);}
114 
115  const Element& Subtract(const Element &a, const Element &b) const
116  {return GetRing().Divide(a, b);}
117 
118  Element& Reduce(Element &a, const Element &b) const
119  {return a = GetRing().Divide(a, b);}
120 
121  const Element& Double(const Element &a) const
122  {return GetRing().Square(a);}
123 
124  Element ScalarMultiply(const Element &a, const Integer &e) const
125  {return GetRing().Exponentiate(a, e);}
126 
127  Element CascadeScalarMultiply(const Element &x, const Integer &e1,
128  const Element &y, const Integer &e2) const
129  {return GetRing().CascadeExponentiate(x, e1, y, e2);}
130 
131  void SimultaneousMultiply(Element *results, const Element &base,
132  const Integer *exponents, unsigned int exponentsCount) const
133  {GetRing().SimultaneousExponentiate(results, base, exponents,
134  exponentsCount);}
135 
136  const AbstractRing* m_pRing;
137  };
138 
139  MultiplicativeGroupT m_mg;
140 };
141 
142 
143 // Abstract Euclidean Domain
144 class TAOCRYPT_NO_VTABLE AbstractEuclideanDomain
145  : public AbstractRing
146 {
147 public:
148  typedef Integer Element;
149 
150  virtual void DivisionAlgorithm(Element &r, Element &q, const Element &a,
151  const Element &d) const =0;
152 
153  virtual const Element& Mod(const Element &a, const Element &b) const =0;
154  virtual const Element& Gcd(const Element &a, const Element &b) const;
155 
156 protected:
157  mutable Element result;
158 };
159 
160 
161 // EuclideanDomainOf
163 {
164 public:
165  typedef Integer Element;
166 
167  EuclideanDomainOf() {}
168 
169  bool Equal(const Element &a, const Element &b) const
170  {return a==b;}
171 
172  const Element& Identity() const
173  {return Element::Zero();}
174 
175  const Element& Add(const Element &a, const Element &b) const
176  {return result = a+b;}
177 
178  Element& Accumulate(Element &a, const Element &b) const
179  {return a+=b;}
180 
181  const Element& Inverse(const Element &a) const
182  {return result = -a;}
183 
184  const Element& Subtract(const Element &a, const Element &b) const
185  {return result = a-b;}
186 
187  Element& Reduce(Element &a, const Element &b) const
188  {return a-=b;}
189 
190  const Element& Double(const Element &a) const
191  {return result = a.Doubled();}
192 
193  const Element& MultiplicativeIdentity() const
194  {return Element::One();}
195 
196  const Element& Multiply(const Element &a, const Element &b) const
197  {return result = a*b;}
198 
199  const Element& Square(const Element &a) const
200  {return result = a.Squared();}
201 
202  bool IsUnit(const Element &a) const
203  {return a.IsUnit();}
204 
205  const Element& MultiplicativeInverse(const Element &a) const
206  {return result = a.MultiplicativeInverse();}
207 
208  const Element& Divide(const Element &a, const Element &b) const
209  {return result = a/b;}
210 
211  const Element& Mod(const Element &a, const Element &b) const
212  {return result = a%b;}
213 
214  void DivisionAlgorithm(Element &r, Element &q, const Element &a,
215  const Element &d) const
216  {Element::Divide(r, q, a, d);}
217 
218 private:
219  mutable Element result;
220 };
221 
222 
223 
224 } // namespace
225 
226 #endif // TAO_CRYPT_ALGEBRA_HPP