Crypto++  5.6.4
Free C++ class library of cryptographic schemes
integer.h
Go to the documentation of this file.
1 // integer.h - written and placed in the public domain by Wei Dai
2 
3 //! \file integer.h
4 //! \brief Multiple precision integer with arithmetic operations
5 //! \details The Integer class can represent positive and negative integers
6 //! with absolute value less than (256**sizeof(word))<sup>(256**sizeof(int))</sup>.
7 //! \details Internally, the library uses a sign magnitude representation, and the class
8 //! has two data members. The first is a IntegerSecBlock (a SecBlock<word>) and it is
9 //! used to hold the representation. The second is a Sign, and its is used to track
10 //! the sign of the Integer.
11 
12 #ifndef CRYPTOPP_INTEGER_H
13 #define CRYPTOPP_INTEGER_H
14 
15 #include "cryptlib.h"
16 #include "secblock.h"
17 #include "stdcpp.h"
18 
19 #include <iosfwd>
20 
21 NAMESPACE_BEGIN(CryptoPP)
22 
23 //! \struct InitializeInteger
24 //! Performs static intialization of the Integer class
26 {
28 };
29 
30 // http://github.com/weidai11/cryptopp/issues/256
31 #if defined(CRYPTOPP_WORD128_AVAILABLE)
33 #else
35 #endif
36 
37 //! \brief Multiple precision integer with arithmetic operations
38 //! \details The Integer class can represent positive and negative integers
39 //! with absolute value less than (256**sizeof(word))<sup>(256**sizeof(int))</sup>.
40 //! \details Internally, the library uses a sign magnitude representation, and the class
41 //! has two data members. The first is a IntegerSecBlock (a SecBlock<word>) and it is
42 //! used to hold the representation. The second is a Sign, and its is used to track
43 //! the sign of the Integer.
44 //! \nosubgrouping
45 class CRYPTOPP_DLL Integer : private InitializeInteger, public ASN1Object
46 {
47 public:
48  //! \name ENUMS, EXCEPTIONS, and TYPEDEFS
49  //@{
50  //! \brief Exception thrown when division by 0 is encountered
51  class DivideByZero : public Exception
52  {
53  public:
54  DivideByZero() : Exception(OTHER_ERROR, "Integer: division by zero") {}
55  };
56 
57  //! \brief Exception thrown when a random number cannot be found that
58  //! satisfies the condition
60  {
61  public:
62  RandomNumberNotFound() : Exception(OTHER_ERROR, "Integer: no integer satisfies the given parameters") {}
63  };
64 
65  //! \enum Sign
66  //! \brief Used internally to represent the integer
67  //! \details Sign is used internally to represent the integer. It is also used in a few API functions.
68  //! \sa Signedness
69  enum Sign {
70  //! \brief the value is positive or 0
71  POSITIVE=0,
72  //! \brief the value is negative
73  NEGATIVE=1};
74 
75  //! \enum Signedness
76  //! \brief Used when importing and exporting integers
77  //! \details Signedness is usually used in API functions.
78  //! \sa Sign
79  enum Signedness {
80  //! \brief an unsigned value
82  //! \brief a signed value
83  SIGNED};
84 
85  //! \enum RandomNumberType
86  //! \brief Properties of a random integer
88  //! \brief a number with no special properties
89  ANY,
90  //! \brief a number which is probabilistically prime
91  PRIME};
92  //@}
93 
94  //! \name CREATORS
95  //@{
96  //! \brief Creates the zero integer
97  Integer();
98 
99  //! copy constructor
100  Integer(const Integer& t);
101 
102  //! \brief Convert from signed long
103  Integer(signed long value);
104 
105  //! \brief Convert from lword
106  //! \param sign enumeration indicating Sign
107  //! \param value the long word
108  Integer(Sign sign, lword value);
109 
110  //! \brief Convert from two words
111  //! \param sign enumeration indicating Sign
112  //! \param highWord the high word
113  //! \param lowWord the low word
114  Integer(Sign sign, word highWord, word lowWord);
115 
116  //! \brief Convert from a C-string
117  //! \param str C-string value
118  //! \param order byte order
119  //! \details \p str can be in base 2, 8, 10, or 16. Base is determined by a case
120  //! insensitive suffix of 'h', 'o', or 'b'. No suffix means base 10.
121  //! \details Byte order was added at Crypto++ 5.7 to allow use of little-endian
122  //! integers with curve25519, Poly1305 and Microsoft CAPI.
123  explicit Integer(const char *str);
124  explicit Integer(const char *str, ByteOrder order);
125 
126  //! \brief Convert from a wide C-string
127  //! \param str wide C-string value
128  //! \param order byte order
129  //! \details \p str can be in base 2, 8, 10, or 16. Base is determined by a case
130  //! insensitive suffix of 'h', 'o', or 'b'. No suffix means base 10.
131  //! \details Byte order was added at Crypto++ 5.7 to allow use of little-endian
132  //! integers with curve25519, Poly1305 and Microsoft CAPI.
133  explicit Integer(const wchar_t *str);
134  explicit Integer(const wchar_t *str, ByteOrder order);
135 
136  //! \brief Convert from a big-endian byte array
137  //! \param encodedInteger big-endian byte array
138  //! \param byteCount length of the byte array
139  //! \param sign enumeration indicating Signedness
140  //! \param order byte order
141  //! \details Byte order was added at Crypto++ 5.7 to allow use of little-endian
142  //! integers with curve25519, Poly1305 and Microsoft CAPI.
143  Integer(const byte *encodedInteger, size_t byteCount, Signedness sign=UNSIGNED);
144  Integer(const byte *encodedInteger, size_t byteCount, Signedness sign, ByteOrder order);
145 
146  //! \brief Convert from a big-endian array
147  //! \param bt BufferedTransformation object with big-endian byte array
148  //! \param byteCount length of the byte array
149  //! \param sign enumeration indicating Signedness
150  //! \param order byte order
151  //! \details Byte order was added at Crypto++ 5.7 to allow use of little-endian
152  //! integers with curve25519, Poly1305 and Microsoft CAPI.
153  Integer(BufferedTransformation &bt, size_t byteCount, Signedness sign=UNSIGNED);
154  Integer(BufferedTransformation &bt, size_t byteCount, Signedness sign, ByteOrder order);
155 
156  //! \brief Convert from a BER encoded byte array
157  //! \param bt BufferedTransformation object with BER encoded byte array
158  explicit Integer(BufferedTransformation &bt);
159 
160  //! \brief Create a random integer
161  //! \param rng RandomNumberGenerator used to generate material
162  //! \param bitCount the number of bits in the resulting integer
163  //! \details The random integer created is uniformly distributed over <tt>[0, 2<sup>bitCount</sup>]</tt>.
164  Integer(RandomNumberGenerator &rng, size_t bitCount);
165 
166  //! \brief Integer representing 0
167  //! \returns an Integer representing 0
168  //! \details Zero() avoids calling constructors for frequently used integers
169  static const Integer & CRYPTOPP_API Zero();
170  //! \brief Integer representing 1
171  //! \returns an Integer representing 1
172  //! \details One() avoids calling constructors for frequently used integers
173  static const Integer & CRYPTOPP_API One();
174  //! \brief Integer representing 2
175  //! \returns an Integer representing 2
176  //! \details Two() avoids calling constructors for frequently used integers
177  static const Integer & CRYPTOPP_API Two();
178 
179  //! \brief Create a random integer of special form
180  //! \param rng RandomNumberGenerator used to generate material
181  //! \param min the minimum value
182  //! \param max the maximum value
183  //! \param rnType RandomNumberType to specify the type
184  //! \param equiv the equivalence class based on the parameter \p mod
185  //! \param mod the modulus used to reduce the equivalence class
186  //! \throw RandomNumberNotFound if the set is empty.
187  //! \details Ideally, the random integer created should be uniformly distributed
188  //! over <tt>{x | min <= x <= max</tt> and \p x is of rnType and <tt>x \% mod == equiv}</tt>.
189  //! However the actual distribution may not be uniform because sequential
190  //! search is used to find an appropriate number from a random starting
191  //! point.
192  //! \details May return (with very small probability) a pseudoprime when a prime
193  //! is requested and <tt>max > lastSmallPrime*lastSmallPrime</tt>. \p lastSmallPrime
194  //! is declared in nbtheory.h.
195  Integer(RandomNumberGenerator &rng, const Integer &min, const Integer &max, RandomNumberType rnType=ANY, const Integer &equiv=Zero(), const Integer &mod=One());
196 
197  //! \brief Exponentiates to a power of 2
198  //! \returns the Integer 2<sup>e</sup>
199  //! \sa a_times_b_mod_c() and a_exp_b_mod_c()
200  static Integer CRYPTOPP_API Power2(size_t e);
201  //@}
202 
203  //! \name ENCODE/DECODE
204  //@{
205  //! \brief The minimum number of bytes to encode this integer
206  //! \param sign enumeration indicating Signedness
207  //! \note The MinEncodedSize() of 0 is 1.
208  size_t MinEncodedSize(Signedness sign=UNSIGNED) const;
209 
210  //! \brief Encode in big-endian format
211  //! \param output big-endian byte array
212  //! \param outputLen length of the byte array
213  //! \param sign enumeration indicating Signedness
214  //! \details Unsigned means encode absolute value, signed means encode two's complement if negative.
215  //! \details outputLen can be used to ensure an Integer is encoded to an exact size (rather than a
216  //! minimum size). An exact size is useful, for example, when encoding to a field element size.
217  void Encode(byte *output, size_t outputLen, Signedness sign=UNSIGNED) const;
218 
219  //! \brief Encode in big-endian format
220  //! \param bt BufferedTransformation object
221  //! \param outputLen length of the encoding
222  //! \param sign enumeration indicating Signedness
223  //! \details Unsigned means encode absolute value, signed means encode two's complement if negative.
224  //! \details outputLen can be used to ensure an Integer is encoded to an exact size (rather than a
225  //! minimum size). An exact size is useful, for example, when encoding to a field element size.
226  void Encode(BufferedTransformation &bt, size_t outputLen, Signedness sign=UNSIGNED) const;
227 
228  //! \brief Encode in DER format
229  //! \param bt BufferedTransformation object
230  //! \details Encodes the Integer using Distinguished Encoding Rules
231  //! The result is placed into a BufferedTransformation object
232  void DEREncode(BufferedTransformation &bt) const;
233 
234  //! encode absolute value as big-endian octet string
235  //! \param bt BufferedTransformation object
236  //! \param length the number of mytes to decode
237  void DEREncodeAsOctetString(BufferedTransformation &bt, size_t length) const;
238 
239  //! \brief Encode absolute value in OpenPGP format
240  //! \param output big-endian byte array
241  //! \param bufferSize length of the byte array
242  //! \returns length of the output
243  //! \details OpenPGPEncode places result into a BufferedTransformation object and returns the
244  //! number of bytes used for the encoding
245  size_t OpenPGPEncode(byte *output, size_t bufferSize) const;
246 
247  //! \brief Encode absolute value in OpenPGP format
248  //! \param bt BufferedTransformation object
249  //! \returns length of the output
250  //! \details OpenPGPEncode places result into a BufferedTransformation object and returns the
251  //! number of bytes used for the encoding
252  size_t OpenPGPEncode(BufferedTransformation &bt) const;
253 
254  //! \brief Decode from big-endian byte array
255  //! \param input big-endian byte array
256  //! \param inputLen length of the byte array
257  //! \param sign enumeration indicating Signedness
258  void Decode(const byte *input, size_t inputLen, Signedness sign=UNSIGNED);
259 
260  //! \brief Decode nonnegative value from big-endian byte array
261  //! \param bt BufferedTransformation object
262  //! \param inputLen length of the byte array
263  //! \param sign enumeration indicating Signedness
264  //! \note <tt>bt.MaxRetrievable() >= inputLen</tt>.
265  void Decode(BufferedTransformation &bt, size_t inputLen, Signedness sign=UNSIGNED);
266 
267  //! \brief Decode from BER format
268  //! \param input big-endian byte array
269  //! \param inputLen length of the byte array
270  void BERDecode(const byte *input, size_t inputLen);
271 
272  //! \brief Decode from BER format
273  //! \param bt BufferedTransformation object
275 
276  //! \brief Decode nonnegative value from big-endian octet string
277  //! \param bt BufferedTransformation object
278  //! \param length length of the byte array
279  void BERDecodeAsOctetString(BufferedTransformation &bt, size_t length);
280 
281  //! \brief Exception thrown when an error is encountered decoding an OpenPGP integer
283  {
284  public:
285  OpenPGPDecodeErr() : Exception(INVALID_DATA_FORMAT, "OpenPGP decode error") {}
286  };
287 
288  //! \brief Decode from OpenPGP format
289  //! \param input big-endian byte array
290  //! \param inputLen length of the byte array
291  void OpenPGPDecode(const byte *input, size_t inputLen);
292  //! \brief Decode from OpenPGP format
293  //! \param bt BufferedTransformation object
294  void OpenPGPDecode(BufferedTransformation &bt);
295  //@}
296 
297  //! \name ACCESSORS
298  //@{
299  //! \brief Determines if the Integer is convertable to Long
300  //! \returns true if *this can be represented as a signed long
301  //! \sa ConvertToLong()
302  bool IsConvertableToLong() const;
303  //! \brief Convert the Integer to Long
304  //! \return equivalent signed long if possible, otherwise undefined
305  //! \sa IsConvertableToLong()
306  signed long ConvertToLong() const;
307 
308  //! \brief Determines the number of bits required to represent the Integer
309  //! \returns number of significant bits = floor(log2(abs(*this))) + 1
310  unsigned int BitCount() const;
311  //! \brief Determines the number of bytes required to represent the Integer
312  //! \returns number of significant bytes = ceiling(BitCount()/8)
313  unsigned int ByteCount() const;
314  //! \brief Determines the number of words required to represent the Integer
315  //! \returns number of significant words = ceiling(ByteCount()/sizeof(word))
316  unsigned int WordCount() const;
317 
318  //! \brief Provides the i-th bit of the Integer
319  //! \returns the i-th bit, i=0 being the least significant bit
320  bool GetBit(size_t i) const;
321  //! \brief Provides the i-th byte of the Integer
322  //! \returns the i-th byte
323  byte GetByte(size_t i) const;
324  //! \brief Provides the low order bits of the Integer
325  //! \returns n lowest bits of *this >> i
326  lword GetBits(size_t i, size_t n) const;
327 
328  //! \brief Determines if the Integer is 0
329  //! \returns true if the Integer is 0, false otherwise
330  bool IsZero() const {return !*this;}
331  //! \brief Determines if the Integer is non-0
332  //! \returns true if the Integer is non-0, false otherwise
333  bool NotZero() const {return !IsZero();}
334  //! \brief Determines if the Integer is negative
335  //! \returns true if the Integer is negative, false otherwise
336  bool IsNegative() const {return sign == NEGATIVE;}
337  //! \brief Determines if the Integer is non-negative
338  //! \returns true if the Integer is non-negative, false otherwise
339  bool NotNegative() const {return !IsNegative();}
340  //! \brief Determines if the Integer is positive
341  //! \returns true if the Integer is positive, false otherwise
342  bool IsPositive() const {return NotNegative() && NotZero();}
343  //! \brief Determines if the Integer is non-positive
344  //! \returns true if the Integer is non-positive, false otherwise
345  bool NotPositive() const {return !IsPositive();}
346  //! \brief Determines if the Integer is even parity
347  //! \returns true if the Integer is even, false otherwise
348  bool IsEven() const {return GetBit(0) == 0;}
349  //! \brief Determines if the Integer is odd parity
350  //! \returns true if the Integer is odd, false otherwise
351  bool IsOdd() const {return GetBit(0) == 1;}
352  //@}
353 
354  //! \name MANIPULATORS
355  //@{
356  //!
357  Integer& operator=(const Integer& t);
358 
359  //!
360  Integer& operator+=(const Integer& t);
361  //!
362  Integer& operator-=(const Integer& t);
363  //!
364  //! \sa a_times_b_mod_c() and a_exp_b_mod_c()
365  Integer& operator*=(const Integer& t) {return *this = Times(t);}
366  //!
367  Integer& operator/=(const Integer& t) {return *this = DividedBy(t);}
368  //!
369  //! \sa a_times_b_mod_c() and a_exp_b_mod_c()
370  Integer& operator%=(const Integer& t) {return *this = Modulo(t);}
371  //!
372  Integer& operator/=(word t) {return *this = DividedBy(t);}
373  //!
374  //! \sa a_times_b_mod_c() and a_exp_b_mod_c()
375  Integer& operator%=(word t) {return *this = Integer(POSITIVE, 0, Modulo(t));}
376 
377  //!
378  Integer& operator<<=(size_t);
379  //!
380  Integer& operator>>=(size_t);
381 
382  //! \brief Set this Integer to random integer
383  //! \param rng RandomNumberGenerator used to generate material
384  //! \param bitCount the number of bits in the resulting integer
385  //! \details The random integer created is uniformly distributed over <tt>[0, 2<sup>bitCount</sup>]</tt>.
386  void Randomize(RandomNumberGenerator &rng, size_t bitCount);
387 
388  //! \brief Set this Integer to random integer
389  //! \param rng RandomNumberGenerator used to generate material
390  //! \param min the minimum value
391  //! \param max the maximum value
392  //! \details The random integer created is uniformly distributed over <tt>[min, max]</tt>.
393  void Randomize(RandomNumberGenerator &rng, const Integer &min, const Integer &max);
394 
395  //! \brief Set this Integer to random integer of special form
396  //! \param rng RandomNumberGenerator used to generate material
397  //! \param min the minimum value
398  //! \param max the maximum value
399  //! \param rnType RandomNumberType to specify the type
400  //! \param equiv the equivalence class based on the parameter \p mod
401  //! \param mod the modulus used to reduce the equivalence class
402  //! \throw RandomNumberNotFound if the set is empty.
403  //! \details Ideally, the random integer created should be uniformly distributed
404  //! over <tt>{x | min <= x <= max</tt> and \p x is of rnType and <tt>x \% mod == equiv}</tt>.
405  //! However the actual distribution may not be uniform because sequential
406  //! search is used to find an appropriate number from a random starting
407  //! point.
408  //! \details May return (with very small probability) a pseudoprime when a prime
409  //! is requested and <tt>max > lastSmallPrime*lastSmallPrime</tt>. \p lastSmallPrime
410  //! is declared in nbtheory.h.
411  bool Randomize(RandomNumberGenerator &rng, const Integer &min, const Integer &max, RandomNumberType rnType, const Integer &equiv=Zero(), const Integer &mod=One());
412 
413  bool GenerateRandomNoThrow(RandomNumberGenerator &rng, const NameValuePairs &params = g_nullNameValuePairs);
414  void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &params = g_nullNameValuePairs)
415  {
416  if (!GenerateRandomNoThrow(rng, params))
417  throw RandomNumberNotFound();
418  }
419 
420  //! \brief Set the n-th bit to value
421  //! \details 0-based numbering.
422  void SetBit(size_t n, bool value=1);
423 
424  //! \brief Set the n-th byte to value
425  //! \details 0-based numbering.
426  void SetByte(size_t n, byte value);
427 
428  //! \brief Reverse the Sign of the Integer
429  void Negate();
430 
431  //! \brief Sets the Integer to positive
432  void SetPositive() {sign = POSITIVE;}
433 
434  //! \brief Sets the Integer to negative
435  void SetNegative() {if (!!(*this)) sign = NEGATIVE;}
436 
437  //! \brief Swaps this Integer with another Integer
438  void swap(Integer &a);
439  //@}
440 
441  //! \name UNARY OPERATORS
442  //@{
443  //!
444  bool operator!() const;
445  //!
446  Integer operator+() const {return *this;}
447  //!
448  Integer operator-() const;
449  //!
450  Integer& operator++();
451  //!
452  Integer& operator--();
453  //!
454  Integer operator++(int) {Integer temp = *this; ++*this; return temp;}
455  //!
456  Integer operator--(int) {Integer temp = *this; --*this; return temp;}
457  //@}
458 
459  //! \name BINARY OPERATORS
460  //@{
461  //! \brief Perform signed comparison
462  //! \param a the Integer to comapre
463  //! \retval -1 if <tt>*this < a</tt>
464  //! \retval 0 if <tt>*this = a</tt>
465  //! \retval 1 if <tt>*this > a</tt>
466  int Compare(const Integer& a) const;
467 
468  //!
469  Integer Plus(const Integer &b) const;
470  //!
471  Integer Minus(const Integer &b) const;
472  //!
473  //! \sa a_times_b_mod_c() and a_exp_b_mod_c()
474  Integer Times(const Integer &b) const;
475  //!
476  Integer DividedBy(const Integer &b) const;
477  //!
478  //! \sa a_times_b_mod_c() and a_exp_b_mod_c()
479  Integer Modulo(const Integer &b) const;
480  //!
481  Integer DividedBy(word b) const;
482  //!
483  //! \sa a_times_b_mod_c() and a_exp_b_mod_c()
484  word Modulo(word b) const;
485 
486  //!
487  Integer operator>>(size_t n) const {return Integer(*this)>>=n;}
488  //!
489  Integer operator<<(size_t n) const {return Integer(*this)<<=n;}
490  //@}
491 
492  //! \name OTHER ARITHMETIC FUNCTIONS
493  //@{
494  //!
495  Integer AbsoluteValue() const;
496  //!
497  Integer Doubled() const {return Plus(*this);}
498  //!
499  //! \sa a_times_b_mod_c() and a_exp_b_mod_c()
500  Integer Squared() const {return Times(*this);}
501  //! extract square root, if negative return 0, else return floor of square root
502  Integer SquareRoot() const;
503  //! return whether this integer is a perfect square
504  bool IsSquare() const;
505 
506  //! is 1 or -1
507  bool IsUnit() const;
508  //! return inverse if 1 or -1, otherwise return 0
509  Integer MultiplicativeInverse() const;
510 
511  //! calculate r and q such that (a == d*q + r) && (0 <= r < abs(d))
512  static void CRYPTOPP_API Divide(Integer &r, Integer &q, const Integer &a, const Integer &d);
513  //! use a faster division algorithm when divisor is short
514  static void CRYPTOPP_API Divide(word &r, Integer &q, const Integer &a, word d);
515 
516  //! returns same result as Divide(r, q, a, Power2(n)), but faster
517  static void CRYPTOPP_API DivideByPowerOf2(Integer &r, Integer &q, const Integer &a, unsigned int n);
518 
519  //! greatest common divisor
520  static Integer CRYPTOPP_API Gcd(const Integer &a, const Integer &n);
521  //! calculate multiplicative inverse of *this mod n
522  //! \sa a_times_b_mod_c() and a_exp_b_mod_c()
523  Integer InverseMod(const Integer &n) const;
524  //!
525  //! \sa a_times_b_mod_c() and a_exp_b_mod_c()
526  word InverseMod(word n) const;
527  //@}
528 
529  //! \name INPUT/OUTPUT
530  //@{
531  //! \brief Extraction operator
532  //! \param in a reference to a std::istream
533  //! \param a a reference to an Integer
534  //! \returns a reference to a std::istream reference
535  friend CRYPTOPP_DLL std::istream& CRYPTOPP_API operator>>(std::istream& in, Integer &a);
536  //!
537  //! \brief Insertion operator
538  //! \param out a reference to a std::ostream
539  //! \param a a constant reference to an Integer
540  //! \returns a reference to a std::ostream reference
541  //! \details The output integer responds to std::hex, std::oct, std::hex, std::upper and
542  //! std::lower. The output includes the suffix \a \b h (for hex), \a \b . (\a \b dot, for dec)
543  //! and \a \b o (for octal). There is currently no way to supress the suffix.
544  //! \details If you want to print an Integer without the suffix or using an arbitrary base, then
545  //! use IntToString<Integer>().
546  //! \sa IntToString<Integer>
547  friend CRYPTOPP_DLL std::ostream& CRYPTOPP_API operator<<(std::ostream& out, const Integer &a);
548  //@}
549 
550 #ifndef CRYPTOPP_DOXYGEN_PROCESSING
551  //! modular multiplication
552  CRYPTOPP_DLL friend Integer CRYPTOPP_API a_times_b_mod_c(const Integer &x, const Integer& y, const Integer& m);
553  //! modular exponentiation
554  CRYPTOPP_DLL friend Integer CRYPTOPP_API a_exp_b_mod_c(const Integer &x, const Integer& e, const Integer& m);
555 #endif
556 
557 private:
558 
559  Integer(word value, size_t length);
560  int PositiveCompare(const Integer &t) const;
561 
562  IntegerSecBlock reg;
563  Sign sign;
564 
565 #ifndef CRYPTOPP_DOXYGEN_PROCESSING
566  friend class ModularArithmetic;
567  friend class MontgomeryRepresentation;
568  friend class HalfMontgomeryRepresentation;
569 
570  friend void PositiveAdd(Integer &sum, const Integer &a, const Integer &b);
571  friend void PositiveSubtract(Integer &diff, const Integer &a, const Integer &b);
572  friend void PositiveMultiply(Integer &product, const Integer &a, const Integer &b);
573  friend void PositiveDivide(Integer &remainder, Integer &quotient, const Integer &dividend, const Integer &divisor);
574 #endif
575 };
576 
577 //!
578 inline bool operator==(const CryptoPP::Integer& a, const CryptoPP::Integer& b) {return a.Compare(b)==0;}
579 //!
580 inline bool operator!=(const CryptoPP::Integer& a, const CryptoPP::Integer& b) {return a.Compare(b)!=0;}
581 //!
582 inline bool operator> (const CryptoPP::Integer& a, const CryptoPP::Integer& b) {return a.Compare(b)> 0;}
583 //!
584 inline bool operator>=(const CryptoPP::Integer& a, const CryptoPP::Integer& b) {return a.Compare(b)>=0;}
585 //!
586 inline bool operator< (const CryptoPP::Integer& a, const CryptoPP::Integer& b) {return a.Compare(b)< 0;}
587 //!
588 inline bool operator<=(const CryptoPP::Integer& a, const CryptoPP::Integer& b) {return a.Compare(b)<=0;}
589 //!
590 inline CryptoPP::Integer operator+(const CryptoPP::Integer &a, const CryptoPP::Integer &b) {return a.Plus(b);}
591 //!
592 inline CryptoPP::Integer operator-(const CryptoPP::Integer &a, const CryptoPP::Integer &b) {return a.Minus(b);}
593 //!
594 //! \sa a_times_b_mod_c() and a_exp_b_mod_c()
595 inline CryptoPP::Integer operator*(const CryptoPP::Integer &a, const CryptoPP::Integer &b) {return a.Times(b);}
596 //!
597 inline CryptoPP::Integer operator/(const CryptoPP::Integer &a, const CryptoPP::Integer &b) {return a.DividedBy(b);}
598 //!
599 //! \sa a_times_b_mod_c() and a_exp_b_mod_c()
600 inline CryptoPP::Integer operator%(const CryptoPP::Integer &a, const CryptoPP::Integer &b) {return a.Modulo(b);}
601 //!
602 inline CryptoPP::Integer operator/(const CryptoPP::Integer &a, CryptoPP::word b) {return a.DividedBy(b);}
603 //!
604 //! \sa a_times_b_mod_c() and a_exp_b_mod_c()
605 inline CryptoPP::word operator%(const CryptoPP::Integer &a, CryptoPP::word b) {return a.Modulo(b);}
606 
607 NAMESPACE_END
608 
609 #ifndef __BORLANDC__
610 NAMESPACE_BEGIN(std)
611 inline void swap(CryptoPP::Integer &a, CryptoPP::Integer &b)
612 {
613  a.swap(b);
614 }
615 NAMESPACE_END
616 #endif
617 
618 #endif
Integer::operator*=
Integer & operator*=(const Integer &t)
Definition: integer.h:365
Integer::NotZero
bool NotZero() const
Determines if the Integer is non-0.
Definition: integer.h:333
Integer::SetPositive
void SetPositive()
Sets the Integer to positive.
Definition: integer.h:432
Integer::Squared
Integer Squared() const
Definition: integer.h:500
ASN1Object
Interface for encoding and decoding ASN1 objects.
Definition: cryptlib.h:2970
secblock.h
Classes and functions for secure memory allocations.
BufferedTransformation
Interface for buffered transformations.
Definition: cryptlib.h:1353
ModularArithmetic
Ring of congruence classes modulo n.
Definition: modarith.h:35
operator%
inline ::Integer operator%(const ::Integer &a, const ::Integer &b)
Definition: integer.h:600
Integer::ANY
@ ANY
a number with no special properties
Definition: integer.h:89
Integer::UNSIGNED
@ UNSIGNED
an unsigned value
Definition: integer.h:81
Integer::IsEven
bool IsEven() const
Determines if the Integer is even parity.
Definition: integer.h:348
GetByte
unsigned int GetByte(ByteOrder order, T value, unsigned int index)
Gets a byte from a value.
Definition: misc.h:1694
Integer::IsZero
bool IsZero() const
Determines if the Integer is 0.
Definition: integer.h:330
Integer::RandomNumberType
RandomNumberType
Properties of a random integer.
Definition: integer.h:87
RandomNumberGenerator
Interface for random number generators.
Definition: cryptlib.h:1187
operator>
bool operator>(const ::PolynomialMod2 &a, const ::PolynomialMod2 &b)
compares degree
Definition: gf2n.h:247
Exception
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:140
operator==
bool operator==(const OID &lhs, const OID &rhs)
Compare two OIDs for equality.
ASN1Object::BERDecode
virtual void BERDecode(BufferedTransformation &bt)=0
Decode this object from a BufferedTransformation.
operator<=
bool operator<=(const ::PolynomialMod2 &a, const ::PolynomialMod2 &b)
compares degree
Definition: gf2n.h:256
Integer::RandomNumberNotFound
Exception thrown when a random number cannot be found that satisfies the condition.
Definition: integer.h:60
Integer::operator%=
Integer & operator%=(word t)
Definition: integer.h:375
Integer::IsOdd
bool IsOdd() const
Determines if the Integer is odd parity.
Definition: integer.h:351
Integer::Signedness
Signedness
Used when importing and exporting integers.
Definition: integer.h:79
Integer::NotPositive
bool NotPositive() const
Determines if the Integer is non-positive.
Definition: integer.h:345
ByteOrder
ByteOrder
Provides the byte ordering.
Definition: cryptlib.h:123
Integer::NotNegative
bool NotNegative() const
Determines if the Integer is non-negative.
Definition: integer.h:339
Integer::SetNegative
void SetNegative()
Sets the Integer to negative.
Definition: integer.h:435
Integer::Sign
Sign
Used internally to represent the integer.
Definition: integer.h:69
Integer::IsPositive
bool IsPositive() const
Determines if the Integer is positive.
Definition: integer.h:342
Integer::DivideByZero
Exception thrown when division by 0 is encountered.
Definition: integer.h:52
InitializeInteger
Performs static intialization of the Integer class.
Definition: integer.h:26
g_nullNameValuePairs
const NameValuePairs & g_nullNameValuePairs
An empty set of name-value pairs.
Definition: cryptlib.cpp:80
Integer::OpenPGPDecodeErr
Exception thrown when an error is encountered decoding an OpenPGP integer.
Definition: integer.h:283
CryptoPP
Crypto++ library namespace.
Integer::operator%=
Integer & operator%=(const Integer &t)
Definition: integer.h:370
operator>=
bool operator>=(const ::PolynomialMod2 &a, const ::PolynomialMod2 &b)
compares degree
Definition: gf2n.h:250
Integer::IsNegative
bool IsNegative() const
Determines if the Integer is negative.
Definition: integer.h:336
operator+
OID operator+(const OID &lhs, unsigned long rhs)
Append a value to an OID.
operator!=
bool operator!=(const OID &lhs, const OID &rhs)
Compare two OIDs for inequality.
SecBlock
Secure memory block with allocator and cleanup.
Definition: secblock.h:438
operator*
inline ::Integer operator*(const ::Integer &a, const ::Integer &b)
Definition: integer.h:595
MontgomeryRepresentation
Performs modular arithmetic in Montgomery representation for increased speed.
Definition: modarith.h:275
ASN1Object::DEREncode
virtual void DEREncode(BufferedTransformation &bt) const =0
Encode this object into a BufferedTransformation.
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:278
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
Integer
Multiple precision integer with arithmetic operations.
Definition: integer.h:46