Crypto++  8.0
Free C++ class library of cryptographic schemes
modarith.h
Go to the documentation of this file.
1 // modarith.h - originally written and placed in the public domain by Wei Dai
2 
3 /// \file modarith.h
4 /// \brief Class file for performing modular arithmetic.
5 
6 #ifndef CRYPTOPP_MODARITH_H
7 #define CRYPTOPP_MODARITH_H
8 
9 // implementations are in integer.cpp
10 
11 #include "cryptlib.h"
12 #include "integer.h"
13 #include "algebra.h"
14 #include "secblock.h"
15 #include "misc.h"
16 
17 #if CRYPTOPP_MSC_VERSION
18 # pragma warning(push)
19 # pragma warning(disable: 4231 4275)
20 #endif
21 
22 NAMESPACE_BEGIN(CryptoPP)
23 
24 CRYPTOPP_DLL_TEMPLATE_CLASS AbstractGroup<Integer>;
25 CRYPTOPP_DLL_TEMPLATE_CLASS AbstractRing<Integer>;
26 CRYPTOPP_DLL_TEMPLATE_CLASS AbstractEuclideanDomain<Integer>;
27 
28 /// \brief Ring of congruence classes modulo n
29 /// \details This implementation represents each congruence class as the smallest
30 /// non-negative integer in that class.
31 /// \details <tt>const Element&</tt> returned by member functions are references
32 /// to internal data members. Since each object may have only
33 /// one such data member for holding results, the following code
34 /// will produce incorrect results:
35 /// <pre> abcd = group.Add(group.Add(a,b), group.Add(c,d));</pre>
36 /// But this should be fine:
37 /// <pre> abcd = group.Add(a, group.Add(b, group.Add(c,d));</pre>
38 class CRYPTOPP_DLL ModularArithmetic : public AbstractRing<Integer>
39 {
40 public:
41 
42  typedef int RandomizationParameter;
43  typedef Integer Element;
44 
45  virtual ~ModularArithmetic() {}
46 
47  /// \brief Construct a ModularArithmetic
48  /// \param modulus congruence class modulus
50  : AbstractRing<Integer>(), m_modulus(modulus), m_result(static_cast<word>(0), modulus.reg.size()) {}
51 
52  /// \brief Copy construct a ModularArithmetic
53  /// \param ma other ModularArithmetic
55  : AbstractRing<Integer>(), m_modulus(ma.m_modulus), m_result(static_cast<word>(0), ma.m_modulus.reg.size()) {}
56 
57  /// \brief Construct a ModularArithmetic
58  /// \param bt BER encoded ModularArithmetic
59  ModularArithmetic(BufferedTransformation &bt); // construct from BER encoded parameters
60 
61  /// \brief Clone a ModularArithmetic
62  /// \returns pointer to a new ModularArithmetic
63  /// \details Clone effectively copy constructs a new ModularArithmetic. The caller is
64  /// responsible for deleting the pointer returned from this method.
65  virtual ModularArithmetic * Clone() const {return new ModularArithmetic(*this);}
66 
67  /// \brief Encodes in DER format
68  /// \param bt BufferedTransformation object
69  void DEREncode(BufferedTransformation &bt) const;
70 
71  /// \brief Encodes element in DER format
72  /// \param out BufferedTransformation object
73  /// \param a Element to encode
74  void DEREncodeElement(BufferedTransformation &out, const Element &a) const;
75 
76  /// \brief Decodes element in DER format
77  /// \param in BufferedTransformation object
78  /// \param a Element to decode
79  void BERDecodeElement(BufferedTransformation &in, Element &a) const;
80 
81  /// \brief Retrieves the modulus
82  /// \returns the modulus
83  const Integer& GetModulus() const {return m_modulus;}
84 
85  /// \brief Sets the modulus
86  /// \param newModulus the new modulus
87  void SetModulus(const Integer &newModulus)
88  {m_modulus = newModulus; m_result.reg.resize(m_modulus.reg.size());}
89 
90  /// \brief Retrieves the representation
91  /// \returns true if the if the modulus is in Montgomery form for multiplication, false otherwise
92  virtual bool IsMontgomeryRepresentation() const {return false;}
93 
94  /// \brief Reduces an element in the congruence class
95  /// \param a element to convert
96  /// \returns the reduced element
97  /// \details ConvertIn is useful for derived classes, like MontgomeryRepresentation, which
98  /// must convert between representations.
99  virtual Integer ConvertIn(const Integer &a) const
100  {return a%m_modulus;}
101 
102  /// \brief Reduces an element in the congruence class
103  /// \param a element to convert
104  /// \returns the reduced element
105  /// \details ConvertOut is useful for derived classes, like MontgomeryRepresentation, which
106  /// must convert between representations.
107  virtual Integer ConvertOut(const Integer &a) const
108  {return a;}
109 
110  /// \brief Divides an element by 2
111  /// \param a element to convert
112  const Integer& Half(const Integer &a) const;
113 
114  /// \brief Compare two elements for equality
115  /// \param a first element
116  /// \param b second element
117  /// \returns true if the elements are equal, false otherwise
118  /// \details Equal() tests the elements for equality using <tt>a==b</tt>
119  bool Equal(const Integer &a, const Integer &b) const
120  {return a==b;}
121 
122  /// \brief Provides the Identity element
123  /// \returns the Identity element
124  const Integer& Identity() const
125  {return Integer::Zero();}
126 
127  /// \brief Adds elements in the ring
128  /// \param a first element
129  /// \param b second element
130  /// \returns the sum of <tt>a</tt> and <tt>b</tt>
131  const Integer& Add(const Integer &a, const Integer &b) const;
132 
133  /// \brief TODO
134  /// \param a first element
135  /// \param b second element
136  /// \returns TODO
137  Integer& Accumulate(Integer &a, const Integer &b) const;
138 
139  /// \brief Inverts the element in the ring
140  /// \param a first element
141  /// \returns the inverse of the element
142  const Integer& Inverse(const Integer &a) const;
143 
144  /// \brief Subtracts elements in the ring
145  /// \param a first element
146  /// \param b second element
147  /// \returns the difference of <tt>a</tt> and <tt>b</tt>. The element <tt>a</tt> must provide a Subtract member function.
148  const Integer& Subtract(const Integer &a, const Integer &b) const;
149 
150  /// \brief TODO
151  /// \param a first element
152  /// \param b second element
153  /// \returns TODO
154  Integer& Reduce(Integer &a, const Integer &b) const;
155 
156  /// \brief Doubles an element in the ring
157  /// \param a the element
158  /// \returns the element doubled
159  /// \details Double returns <tt>Add(a, a)</tt>. The element <tt>a</tt> must provide an Add member function.
160  const Integer& Double(const Integer &a) const
161  {return Add(a, a);}
162 
163  /// \brief Retrieves the multiplicative identity
164  /// \returns the multiplicative identity
165  /// \details the base class implementations returns 1.
167  {return Integer::One();}
168 
169  /// \brief Multiplies elements in the ring
170  /// \param a the multiplicand
171  /// \param b the multiplier
172  /// \returns the product of a and b
173  /// \details Multiply returns <tt>a*b\%n</tt>.
174  const Integer& Multiply(const Integer &a, const Integer &b) const
175  {return m_result1 = a*b%m_modulus;}
176 
177  /// \brief Square an element in the ring
178  /// \param a the element
179  /// \returns the element squared
180  /// \details Square returns <tt>a*a\%n</tt>. The element <tt>a</tt> must provide a Square member function.
181  const Integer& Square(const Integer &a) const
182  {return m_result1 = a.Squared()%m_modulus;}
183 
184  /// \brief Determines whether an element is a unit in the ring
185  /// \param a the element
186  /// \returns true if the element is a unit after reduction, false otherwise.
187  bool IsUnit(const Integer &a) const
188  {return Integer::Gcd(a, m_modulus).IsUnit();}
189 
190  /// \brief Calculate the multiplicative inverse of an element in the ring
191  /// \param a the element
192  /// \details MultiplicativeInverse returns <tt>a<sup>-1</sup>\%n</tt>. The element <tt>a</tt> must
193  /// provide a InverseMod member function.
194  const Integer& MultiplicativeInverse(const Integer &a) const
195  {return m_result1 = a.InverseMod(m_modulus);}
196 
197  /// \brief Divides elements in the ring
198  /// \param a the dividend
199  /// \param b the divisor
200  /// \returns the quotient
201  /// \details Divide returns <tt>a*b<sup>-1</sup>\%n</tt>.
202  const Integer& Divide(const Integer &a, const Integer &b) const
203  {return Multiply(a, MultiplicativeInverse(b));}
204 
205  /// \brief TODO
206  /// \param x first element
207  /// \param e1 first exponent
208  /// \param y second element
209  /// \param e2 second exponent
210  /// \returns TODO
211  Integer CascadeExponentiate(const Integer &x, const Integer &e1, const Integer &y, const Integer &e2) const;
212 
213  /// \brief Exponentiates a base to multiple exponents in the ring
214  /// \param results an array of Elements
215  /// \param base the base to raise to the exponents
216  /// \param exponents an array of exponents
217  /// \param exponentsCount the number of exponents in the array
218  /// \details SimultaneousExponentiate() raises the base to each exponent in the exponents array and stores the
219  /// result at the respective position in the results array.
220  /// \details SimultaneousExponentiate() must be implemented in a derived class.
221  /// \pre <tt>COUNTOF(results) == exponentsCount</tt>
222  /// \pre <tt>COUNTOF(exponents) == exponentsCount</tt>
223  void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const;
224 
225  /// \brief Provides the maximum bit size of an element in the ring
226  /// \returns maximum bit size of an element
227  unsigned int MaxElementBitLength() const
228  {return (m_modulus-1).BitCount();}
229 
230  /// \brief Provides the maximum byte size of an element in the ring
231  /// \returns maximum byte size of an element
232  unsigned int MaxElementByteLength() const
233  {return (m_modulus-1).ByteCount();}
234 
235  /// \brief Provides a random element in the ring
236  /// \param rng RandomNumberGenerator used to generate material
237  /// \param ignore_for_now unused
238  /// \returns a random element that is uniformly distributed
239  /// \details RandomElement constructs a new element in the range <tt>[0,n-1]</tt>, inclusive.
240  /// The element's class must provide a constructor with the signature <tt>Element(RandomNumberGenerator rng,
241  /// Element min, Element max)</tt>.
242  Element RandomElement(RandomNumberGenerator &rng , const RandomizationParameter &ignore_for_now = 0) const
243  // left RandomizationParameter arg as ref in case RandomizationParameter becomes a more complicated struct
244  {
245  CRYPTOPP_UNUSED(ignore_for_now);
246  return Element(rng, Integer::Zero(), m_modulus - Integer::One()) ;
247  }
248 
249  /// \brief Compares two ModularArithmetic for equality
250  /// \param rhs other ModularArithmetic
251  /// \returns true if this is equal to the other, false otherwise
252  /// \details The operator tests for equality using <tt>this.m_modulus == rhs.m_modulus</tt>.
253  bool operator==(const ModularArithmetic &rhs) const
254  {return m_modulus == rhs.m_modulus;}
255 
256  static const RandomizationParameter DefaultRandomizationParameter ;
257 
258 protected:
259  Integer m_modulus;
260  mutable Integer m_result, m_result1;
261 };
262 
263 // const ModularArithmetic::RandomizationParameter ModularArithmetic::DefaultRandomizationParameter = 0 ;
264 
265 /// \brief Performs modular arithmetic in Montgomery representation for increased speed
266 /// \details The Montgomery representation represents each congruence class <tt>[a]</tt> as
267 /// <tt>a*r\%n</tt>, where <tt>r</tt> is a convenient power of 2.
268 /// \details <tt>const Element&</tt> returned by member functions are references to
269 /// internal data members. Since each object may have only one such data member for holding
270 /// results, the following code will produce incorrect results:
271 /// <pre> abcd = group.Add(group.Add(a,b), group.Add(c,d));</pre>
272 /// But this should be fine:
273 /// <pre> abcd = group.Add(a, group.Add(b, group.Add(c,d));</pre>
274 class CRYPTOPP_DLL MontgomeryRepresentation : public ModularArithmetic
275 {
276 public:
277  virtual ~MontgomeryRepresentation() {}
278 
279  /// \brief Construct a MontgomeryRepresentation
280  /// \param modulus congruence class modulus
281  /// \note The modulus must be odd.
282  MontgomeryRepresentation(const Integer &modulus);
283 
284  /// \brief Clone a MontgomeryRepresentation
285  /// \returns pointer to a new MontgomeryRepresentation
286  /// \details Clone effectively copy constructs a new MontgomeryRepresentation. The caller is
287  /// responsible for deleting the pointer returned from this method.
288  virtual ModularArithmetic * Clone() const {return new MontgomeryRepresentation(*this);}
289 
290  bool IsMontgomeryRepresentation() const {return true;}
291 
292  Integer ConvertIn(const Integer &a) const
293  {return (a<<(WORD_BITS*m_modulus.reg.size()))%m_modulus;}
294 
295  Integer ConvertOut(const Integer &a) const;
296 
298  {return m_result1 = Integer::Power2(WORD_BITS*m_modulus.reg.size())%m_modulus;}
299 
300  const Integer& Multiply(const Integer &a, const Integer &b) const;
301 
302  const Integer& Square(const Integer &a) const;
303 
304  const Integer& MultiplicativeInverse(const Integer &a) const;
305 
306  Integer CascadeExponentiate(const Integer &x, const Integer &e1, const Integer &y, const Integer &e2) const
307  {return AbstractRing<Integer>::CascadeExponentiate(x, e1, y, e2);}
308 
309  void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const
310  {AbstractRing<Integer>::SimultaneousExponentiate(results, base, exponents, exponentsCount);}
311 
312 private:
313  Integer m_u;
314  mutable IntegerSecBlock m_workspace;
315 };
316 
317 NAMESPACE_END
318 
319 #if CRYPTOPP_MSC_VERSION
320 # pragma warning(pop)
321 #endif
322 
323 #endif
const Integer & Double(const Integer &a) const
Doubles an element in the ring.
Definition: modarith.h:160
bool Equal(const Integer &a, const Integer &b) const
Compare two elements for equality.
Definition: modarith.h:119
const Integer & Square(const Integer &a) const
Square an element in the ring.
Definition: modarith.h:181
bool IsMontgomeryRepresentation() const
Retrieves the representation.
Definition: modarith.h:290
const Integer & Divide(const Integer &a, const Integer &b) const
Divides elements in the ring.
Definition: modarith.h:202
Utility functions for the Crypto++ library.
virtual const Element & Subtract(const Element &a, const Element &b) const
Subtracts elements in the group.
Definition: algebra.cpp:20
virtual const Element & Multiply(const Element &a, const Element &b) const =0
Multiplies elements in the group.
static Integer Gcd(const Integer &a, const Integer &n)
Calculate greatest common divisor.
Definition: integer.cpp:4416
void resize(size_type newSize)
Change size and preserve contents.
Definition: secblock.h:1031
virtual Integer ConvertOut(const Integer &a) const
Reduces an element in the congruence class.
Definition: modarith.h:107
virtual Element CascadeExponentiate(const Element &x, const Integer &e1, const Element &y, const Integer &e2) const
TODO.
Definition: algebra.cpp:323
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.
Definition: modarith.h:194
Abstract Euclidean domain.
Definition: algebra.h:276
unsigned int MaxElementByteLength() const
Provides the maximum byte size of an element in the ring.
Definition: modarith.h:232
Ring of congruence classes modulo n.
Definition: modarith.h:38
Interface for random number generators.
Definition: cryptlib.h:1383
unsigned int MaxElementBitLength() const
Provides the maximum bit size of an element in the ring.
Definition: modarith.h:227
Classes for performing mathematics over different fields.
Interface for buffered transformations.
Definition: cryptlib.h:1598
static const Integer & One()
Integer representing 1.
Definition: integer.cpp:4868
Integer ConvertIn(const Integer &a) const
Reduces an element in the congruence class.
Definition: modarith.h:292
const Integer & Identity() const
Provides the Identity element.
Definition: modarith.h:124
bool IsUnit() const
Determine if 1 or -1.
Definition: integer.cpp:4387
Classes and functions for secure memory allocations.
virtual ModularArithmetic * Clone() const
Clone a MontgomeryRepresentation.
Definition: modarith.h:288
bool operator==(const ModularArithmetic &rhs) const
Compares two ModularArithmetic for equality.
Definition: modarith.h:253
const Integer & Multiply(const Integer &a, const Integer &b) const
Multiplies elements in the ring.
Definition: modarith.h:174
virtual const Element & MultiplicativeInverse(const Element &a) const =0
Calculate the multiplicative inverse of an element in the group.
static Integer Power2(size_t e)
Exponentiates to a power of 2.
Definition: integer.cpp:3079
Multiple precision integer with arithmetic operations.
Definition: integer.h:49
const Integer & GetModulus() const
Retrieves the modulus.
Definition: modarith.h:83
const Integer & MultiplicativeIdentity() const
Retrieves the multiplicative identity.
Definition: modarith.h:297
virtual const Element & Inverse(const Element &a) const =0
Inverts the element in the group.
ModularArithmetic(const Integer &modulus=Integer::One())
Construct a ModularArithmetic.
Definition: modarith.h:49
virtual Integer ConvertIn(const Integer &a) const
Reduces an element in the congruence class.
Definition: modarith.h:99
virtual ModularArithmetic * Clone() const
Clone a ModularArithmetic.
Definition: modarith.h:65
virtual Element & Accumulate(Element &a, const Element &b) const
TODO.
Definition: algebra.cpp:27
void SetModulus(const Integer &newModulus)
Sets the modulus.
Definition: modarith.h:87
Performs modular arithmetic in Montgomery representation for increased speed.
Definition: modarith.h:274
Element RandomElement(RandomNumberGenerator &rng, const RandomizationParameter &ignore_for_now=0) const
Provides a random element in the ring.
Definition: modarith.h:242
Integer CascadeExponentiate(const Integer &x, const Integer &e1, const Integer &y, const Integer &e2) const
TODO.
Definition: modarith.h:306
virtual const Element & Add(const Element &a, const Element &b) const =0
Adds elements in the group.
Multiple precision integer with arithmetic operations.
static const Integer & Zero()
Integer representing 0.
Definition: integer.cpp:4856
virtual Element & Reduce(Element &a, const Element &b) const
Reduces an element in the congruence class.
Definition: algebra.cpp:32
Crypto++ library namespace.
ModularArithmetic(const ModularArithmetic &ma)
Copy construct a ModularArithmetic.
Definition: modarith.h:54
const Integer & MultiplicativeIdentity() const
Retrieves the multiplicative identity.
Definition: modarith.h:166
void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const
Exponentiates a base to multiple exponents in the ring.
Definition: modarith.h:309
virtual void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const
Exponentiates a base to multiple exponents in the Ring.
Definition: algebra.cpp:334
virtual bool IsMontgomeryRepresentation() const
Retrieves the representation.
Definition: modarith.h:92
bool IsUnit(const Integer &a) const
Determines whether an element is a unit in the ring.
Definition: modarith.h:187