Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

570 lines
22 KiB

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