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.

arith_uint256.h 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2015 The Bitcoin 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_ARITH_UINT256_H
  6. #define BITCOIN_ARITH_UINT256_H
  7. #include <assert.h>
  8. #include <cstring>
  9. #include <stdexcept>
  10. #include <stdint.h>
  11. #include <string>
  12. #include <vector>
  13. class uint256;
  14. class uint_error : public std::runtime_error {
  15. public:
  16. explicit uint_error(const std::string& str) : std::runtime_error(str) {}
  17. };
  18. /** Template base class for unsigned big integers. */
  19. template<unsigned int BITS>
  20. class base_uint
  21. {
  22. protected:
  23. enum { WIDTH=BITS/32 };
  24. uint32_t pn[WIDTH];
  25. public:
  26. base_uint()
  27. {
  28. for (int i = 0; i < WIDTH; i++)
  29. pn[i] = 0;
  30. }
  31. base_uint(const base_uint& b)
  32. {
  33. for (int i = 0; i < WIDTH; i++)
  34. pn[i] = b.pn[i];
  35. }
  36. base_uint& operator=(const base_uint& b)
  37. {
  38. for (int i = 0; i < WIDTH; i++)
  39. pn[i] = b.pn[i];
  40. return *this;
  41. }
  42. base_uint(uint64_t b)
  43. {
  44. pn[0] = (unsigned int)b;
  45. pn[1] = (unsigned int)(b >> 32);
  46. for (int i = 2; i < WIDTH; i++)
  47. pn[i] = 0;
  48. }
  49. explicit base_uint(const std::string& str);
  50. bool operator!() const
  51. {
  52. for (int i = 0; i < WIDTH; i++)
  53. if (pn[i] != 0)
  54. return false;
  55. return true;
  56. }
  57. const base_uint operator~() const
  58. {
  59. base_uint ret;
  60. for (int i = 0; i < WIDTH; i++)
  61. ret.pn[i] = ~pn[i];
  62. return ret;
  63. }
  64. const base_uint operator-() const
  65. {
  66. base_uint ret;
  67. for (int i = 0; i < WIDTH; i++)
  68. ret.pn[i] = ~pn[i];
  69. ret++;
  70. return ret;
  71. }
  72. double getdouble() const;
  73. base_uint& operator=(uint64_t b)
  74. {
  75. pn[0] = (unsigned int)b;
  76. pn[1] = (unsigned int)(b >> 32);
  77. for (int i = 2; i < WIDTH; i++)
  78. pn[i] = 0;
  79. return *this;
  80. }
  81. base_uint& operator^=(const base_uint& b)
  82. {
  83. for (int i = 0; i < WIDTH; i++)
  84. pn[i] ^= b.pn[i];
  85. return *this;
  86. }
  87. base_uint& operator&=(const base_uint& b)
  88. {
  89. for (int i = 0; i < WIDTH; i++)
  90. pn[i] &= b.pn[i];
  91. return *this;
  92. }
  93. base_uint& operator|=(const base_uint& b)
  94. {
  95. for (int i = 0; i < WIDTH; i++)
  96. pn[i] |= b.pn[i];
  97. return *this;
  98. }
  99. base_uint& operator^=(uint64_t b)
  100. {
  101. pn[0] ^= (unsigned int)b;
  102. pn[1] ^= (unsigned int)(b >> 32);
  103. return *this;
  104. }
  105. base_uint& operator|=(uint64_t b)
  106. {
  107. pn[0] |= (unsigned int)b;
  108. pn[1] |= (unsigned int)(b >> 32);
  109. return *this;
  110. }
  111. base_uint& operator<<=(unsigned int shift);
  112. base_uint& operator>>=(unsigned int shift);
  113. base_uint& operator+=(const base_uint& b)
  114. {
  115. uint64_t carry = 0;
  116. for (int i = 0; i < WIDTH; i++)
  117. {
  118. uint64_t n = carry + pn[i] + b.pn[i];
  119. pn[i] = n & 0xffffffff;
  120. carry = n >> 32;
  121. }
  122. return *this;
  123. }
  124. base_uint& operator-=(const base_uint& b)
  125. {
  126. *this += -b;
  127. return *this;
  128. }
  129. base_uint& operator+=(uint64_t b64)
  130. {
  131. base_uint b;
  132. b = b64;
  133. *this += b;
  134. return *this;
  135. }
  136. base_uint& operator-=(uint64_t b64)
  137. {
  138. base_uint b;
  139. b = b64;
  140. *this += -b;
  141. return *this;
  142. }
  143. base_uint& operator*=(uint32_t b32);
  144. base_uint& operator*=(const base_uint& b);
  145. base_uint& operator/=(const base_uint& b);
  146. base_uint& operator++()
  147. {
  148. // prefix operator
  149. int i = 0;
  150. while (++pn[i] == 0 && i < WIDTH-1)
  151. i++;
  152. return *this;
  153. }
  154. const base_uint operator++(int)
  155. {
  156. // postfix operator
  157. const base_uint ret = *this;
  158. ++(*this);
  159. return ret;
  160. }
  161. base_uint& operator--()
  162. {
  163. // prefix operator
  164. int i = 0;
  165. while (--pn[i] == (uint32_t)-1 && i < WIDTH-1)
  166. i++;
  167. return *this;
  168. }
  169. const base_uint operator--(int)
  170. {
  171. // postfix operator
  172. const base_uint ret = *this;
  173. --(*this);
  174. return ret;
  175. }
  176. int CompareTo(const base_uint& b) const;
  177. bool EqualTo(uint64_t b) const;
  178. friend inline const base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; }
  179. friend inline const base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; }
  180. friend inline const base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; }
  181. friend inline const base_uint operator/(const base_uint& a, const base_uint& b) { return base_uint(a) /= b; }
  182. friend inline const base_uint operator|(const base_uint& a, const base_uint& b) { return base_uint(a) |= b; }
  183. friend inline const base_uint operator&(const base_uint& a, const base_uint& b) { return base_uint(a) &= b; }
  184. friend inline const base_uint operator^(const base_uint& a, const base_uint& b) { return base_uint(a) ^= b; }
  185. friend inline const base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; }
  186. friend inline const base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
  187. friend inline const base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
  188. friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
  189. friend inline bool operator!=(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) != 0; }
  190. friend inline bool operator>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) > 0; }
  191. friend inline bool operator<(const base_uint& a, const base_uint& b) { return a.CompareTo(b) < 0; }
  192. friend inline bool operator>=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) >= 0; }
  193. friend inline bool operator<=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <= 0; }
  194. friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); }
  195. friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); }
  196. std::string GetHex() const;
  197. void SetHex(const char* psz);
  198. void SetHex(const std::string& str);
  199. std::string ToString() const;
  200. unsigned int size() const
  201. {
  202. return sizeof(pn);
  203. }
  204. /**
  205. * Returns the position of the highest bit set plus one, or zero if the
  206. * value is zero.
  207. */
  208. unsigned int bits() const;
  209. uint64_t GetLow64() const
  210. {
  211. assert(WIDTH >= 2);
  212. return pn[0] | (uint64_t)pn[1] << 32;
  213. }
  214. };
  215. /** 256-bit unsigned big integer. */
  216. class arith_uint256 : public base_uint<256> {
  217. public:
  218. arith_uint256() {}
  219. arith_uint256(const base_uint<256>& b) : base_uint<256>(b) {}
  220. arith_uint256(uint64_t b) : base_uint<256>(b) {}
  221. explicit arith_uint256(const std::string& str) : base_uint<256>(str) {}
  222. /**
  223. * The "compact" format is a representation of a whole
  224. * number N using an unsigned 32bit number similar to a
  225. * floating point format.
  226. * The most significant 8 bits are the unsigned exponent of base 256.
  227. * This exponent can be thought of as "number of bytes of N".
  228. * The lower 23 bits are the mantissa.
  229. * Bit number 24 (0x800000) represents the sign of N.
  230. * N = (-1^sign) * mantissa * 256^(exponent-3)
  231. *
  232. * Satoshi's original implementation used BN_bn2mpi() and BN_mpi2bn().
  233. * MPI uses the most significant bit of the first byte as sign.
  234. * Thus 0x1234560000 is compact (0x05123456)
  235. * and 0xc0de000000 is compact (0x0600c0de)
  236. *
  237. * Bitcoin only uses this "compact" format for encoding difficulty
  238. * targets, which are unsigned 256bit quantities. Thus, all the
  239. * complexities of the sign bit and using base 256 are probably an
  240. * implementation accident.
  241. */
  242. arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = NULL, bool *pfOverflow = NULL);
  243. uint32_t GetCompact(bool fNegative = false) const;
  244. friend uint256 ArithToUint256(const arith_uint256 &);
  245. friend arith_uint256 UintToArith256(const uint256 &);
  246. };
  247. uint256 ArithToUint256(const arith_uint256 &);
  248. arith_uint256 UintToArith256(const uint256 &);
  249. #endif // BITCOIN_ARITH_UINT256_H