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