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.

bignum.h 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2013 The Bitcoin Core developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #ifndef BITCOIN_TEST_BIGNUM_H
  6. #define BITCOIN_TEST_BIGNUM_H
  7. #include <algorithm>
  8. #include <limits>
  9. #include <stdexcept>
  10. #include <stdint.h>
  11. #include <string>
  12. #include <vector>
  13. #include <openssl/bn.h>
  14. class bignum_error : public std::runtime_error
  15. {
  16. public:
  17. explicit bignum_error(const std::string& str) : std::runtime_error(str) {}
  18. };
  19. /** C++ wrapper for BIGNUM (OpenSSL bignum) */
  20. class CBigNum : public BIGNUM
  21. {
  22. public:
  23. CBigNum()
  24. {
  25. BN_init(this);
  26. }
  27. CBigNum(const CBigNum& b)
  28. {
  29. BN_init(this);
  30. if (!BN_copy(this, &b))
  31. {
  32. BN_clear_free(this);
  33. throw bignum_error("CBigNum::CBigNum(const CBigNum&): BN_copy failed");
  34. }
  35. }
  36. CBigNum& operator=(const CBigNum& b)
  37. {
  38. if (!BN_copy(this, &b))
  39. throw bignum_error("CBigNum::operator=: BN_copy failed");
  40. return (*this);
  41. }
  42. ~CBigNum()
  43. {
  44. BN_clear_free(this);
  45. }
  46. CBigNum(long long n) { BN_init(this); setint64(n); }
  47. explicit CBigNum(const std::vector<unsigned char>& vch)
  48. {
  49. BN_init(this);
  50. setvch(vch);
  51. }
  52. int getint() const
  53. {
  54. BN_ULONG n = BN_get_word(this);
  55. if (!BN_is_negative(this))
  56. return (n > (BN_ULONG)std::numeric_limits<int>::max() ? std::numeric_limits<int>::max() : n);
  57. else
  58. return (n > (BN_ULONG)std::numeric_limits<int>::max() ? std::numeric_limits<int>::min() : -(int)n);
  59. }
  60. void setint64(int64_t sn)
  61. {
  62. unsigned char pch[sizeof(sn) + 6];
  63. unsigned char* p = pch + 4;
  64. bool fNegative;
  65. uint64_t n;
  66. if (sn < (int64_t)0)
  67. {
  68. // Since the minimum signed integer cannot be represented as positive so long as its type is signed,
  69. // and it's not well-defined what happens if you make it unsigned before negating it,
  70. // we instead increment the negative integer by 1, convert it, then increment the (now positive) unsigned integer by 1 to compensate
  71. n = -(sn + 1);
  72. ++n;
  73. fNegative = true;
  74. } else {
  75. n = sn;
  76. fNegative = false;
  77. }
  78. bool fLeadingZeroes = true;
  79. for (int i = 0; i < 8; i++)
  80. {
  81. unsigned char c = (n >> 56) & 0xff;
  82. n <<= 8;
  83. if (fLeadingZeroes)
  84. {
  85. if (c == 0)
  86. continue;
  87. if (c & 0x80)
  88. *p++ = (fNegative ? 0x80 : 0);
  89. else if (fNegative)
  90. c |= 0x80;
  91. fLeadingZeroes = false;
  92. }
  93. *p++ = c;
  94. }
  95. unsigned int nSize = p - (pch + 4);
  96. pch[0] = (nSize >> 24) & 0xff;
  97. pch[1] = (nSize >> 16) & 0xff;
  98. pch[2] = (nSize >> 8) & 0xff;
  99. pch[3] = (nSize) & 0xff;
  100. BN_mpi2bn(pch, p - pch, this);
  101. }
  102. void setvch(const std::vector<unsigned char>& vch)
  103. {
  104. std::vector<unsigned char> vch2(vch.size() + 4);
  105. unsigned int nSize = vch.size();
  106. // BIGNUM's byte stream format expects 4 bytes of
  107. // big endian size data info at the front
  108. vch2[0] = (nSize >> 24) & 0xff;
  109. vch2[1] = (nSize >> 16) & 0xff;
  110. vch2[2] = (nSize >> 8) & 0xff;
  111. vch2[3] = (nSize >> 0) & 0xff;
  112. // swap data to big endian
  113. reverse_copy(vch.begin(), vch.end(), vch2.begin() + 4);
  114. BN_mpi2bn(&vch2[0], vch2.size(), this);
  115. }
  116. std::vector<unsigned char> getvch() const
  117. {
  118. unsigned int nSize = BN_bn2mpi(this, NULL);
  119. if (nSize <= 4)
  120. return std::vector<unsigned char>();
  121. std::vector<unsigned char> vch(nSize);
  122. BN_bn2mpi(this, &vch[0]);
  123. vch.erase(vch.begin(), vch.begin() + 4);
  124. reverse(vch.begin(), vch.end());
  125. return vch;
  126. }
  127. friend inline const CBigNum operator-(const CBigNum& a, const CBigNum& b);
  128. };
  129. inline const CBigNum operator+(const CBigNum& a, const CBigNum& b)
  130. {
  131. CBigNum r;
  132. if (!BN_add(&r, &a, &b))
  133. throw bignum_error("CBigNum::operator+: BN_add failed");
  134. return r;
  135. }
  136. inline const CBigNum operator-(const CBigNum& a, const CBigNum& b)
  137. {
  138. CBigNum r;
  139. if (!BN_sub(&r, &a, &b))
  140. throw bignum_error("CBigNum::operator-: BN_sub failed");
  141. return r;
  142. }
  143. inline const CBigNum operator-(const CBigNum& a)
  144. {
  145. CBigNum r(a);
  146. BN_set_negative(&r, !BN_is_negative(&r));
  147. return r;
  148. }
  149. inline bool operator==(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) == 0); }
  150. inline bool operator!=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) != 0); }
  151. inline bool operator<=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) <= 0); }
  152. inline bool operator>=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) >= 0); }
  153. inline bool operator<(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) < 0); }
  154. inline bool operator>(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) > 0); }
  155. #endif // BITCOIN_TEST_BIGNUM_H