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