Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto 2 : // Copyright (c) 2009-2019 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 : 6 : #ifndef BITCOIN_ARITH_UINT256_H 7 : #define BITCOIN_ARITH_UINT256_H 8 : 9 : #include <cstdint> 10 : #include <cstring> 11 : #include <limits> 12 : #include <stdexcept> 13 : #include <string> 14 : 15 : class uint256; 16 : 17 : class uint_error : public std::runtime_error { 18 : public: 19 6 : explicit uint_error(const std::string& str) : std::runtime_error(str) {} 20 : }; 21 : 22 : /** Template base class for unsigned big integers. */ 23 : template<unsigned int BITS> 24 : class base_uint 25 : { 26 : protected: 27 : static_assert(BITS / 32 > 0 && BITS % 32 == 0, "Template parameter BITS must be a positive multiple of 32."); 28 : static constexpr int WIDTH = BITS / 32; 29 : uint32_t pn[WIDTH]; 30 : public: 31 : 32 18856251 : base_uint() 33 1284765 : { 34 158143346 : for (int i = 0; i < WIDTH; i++) 35 140571860 : pn[i] = 0; 36 18856251 : } 37 : 38 29266502 : base_uint(const base_uint& b) 39 13183579 : { 40 144746261 : for (int i = 0; i < WIDTH; i++) 41 128663338 : pn[i] = b.pn[i]; 42 29266502 : } 43 : 44 3930801 : base_uint& operator=(const base_uint& b) 45 : { 46 35377206 : for (int i = 0; i < WIDTH; i++) 47 31446405 : pn[i] = b.pn[i]; 48 3930801 : return *this; 49 : } 50 : 51 6320654 : base_uint(uint64_t b) 52 1534287 : { 53 4786367 : pn[0] = (unsigned int)b; 54 4786367 : pn[1] = (unsigned int)(b >> 32); 55 33504552 : for (int i = 2; i < WIDTH; i++) 56 28718185 : pn[i] = 0; 57 6320654 : } 58 : 59 629362 : base_uint operator~() const 60 : { 61 629362 : base_uint ret; 62 5664258 : for (int i = 0; i < WIDTH; i++) 63 5034896 : ret.pn[i] = ~pn[i]; 64 629362 : return ret; 65 : } 66 : 67 653712 : base_uint operator-() const 68 : { 69 653712 : base_uint ret; 70 5883408 : for (int i = 0; i < WIDTH; i++) 71 5229696 : ret.pn[i] = ~pn[i]; 72 653712 : ++ret; 73 653712 : return ret; 74 : } 75 : 76 : double getdouble() const; 77 : 78 631030 : base_uint& operator=(uint64_t b) 79 : { 80 631030 : pn[0] = (unsigned int)b; 81 631030 : pn[1] = (unsigned int)(b >> 32); 82 4417210 : for (int i = 2; i < WIDTH; i++) 83 3786180 : pn[i] = 0; 84 631030 : return *this; 85 : } 86 : 87 534 : base_uint& operator^=(const base_uint& b) 88 : { 89 4806 : for (int i = 0; i < WIDTH; i++) 90 4272 : pn[i] ^= b.pn[i]; 91 534 : return *this; 92 : } 93 : 94 18 : base_uint& operator&=(const base_uint& b) 95 : { 96 162 : for (int i = 0; i < WIDTH; i++) 97 144 : pn[i] &= b.pn[i]; 98 18 : return *this; 99 : } 100 : 101 274 : base_uint& operator|=(const base_uint& b) 102 : { 103 2466 : for (int i = 0; i < WIDTH; i++) 104 2192 : pn[i] |= b.pn[i]; 105 274 : return *this; 106 : } 107 : 108 2 : base_uint& operator^=(uint64_t b) 109 : { 110 2 : pn[0] ^= (unsigned int)b; 111 2 : pn[1] ^= (unsigned int)(b >> 32); 112 2 : return *this; 113 : } 114 : 115 2 : base_uint& operator|=(uint64_t b) 116 : { 117 2 : pn[0] |= (unsigned int)b; 118 2 : pn[1] |= (unsigned int)(b >> 32); 119 2 : return *this; 120 : } 121 : 122 : base_uint& operator<<=(unsigned int shift); 123 : base_uint& operator>>=(unsigned int shift); 124 : 125 2590839 : base_uint& operator+=(const base_uint& b) 126 : { 127 2590839 : uint64_t carry = 0; 128 23317551 : for (int i = 0; i < WIDTH; i++) 129 : { 130 20726712 : uint64_t n = carry + pn[i] + b.pn[i]; 131 20726712 : pn[i] = n & 0xffffffff; 132 20726712 : carry = n >> 32; 133 20726712 : } 134 2590839 : return *this; 135 : } 136 : 137 653193 : base_uint& operator-=(const base_uint& b) 138 : { 139 653193 : *this += -b; 140 653193 : return *this; 141 : } 142 : 143 256 : base_uint& operator+=(uint64_t b64) 144 : { 145 256 : base_uint b; 146 256 : b = b64; 147 256 : *this += b; 148 256 : return *this; 149 : } 150 : 151 1 : base_uint& operator-=(uint64_t b64) 152 : { 153 1 : base_uint b; 154 1 : b = b64; 155 1 : *this += -b; 156 1 : return *this; 157 : } 158 : 159 : base_uint& operator*=(uint32_t b32); 160 : base_uint& operator*=(const base_uint& b); 161 : base_uint& operator/=(uint64_t b32); 162 : base_uint& operator/=(const base_uint& b); 163 : 164 653968 : base_uint& operator++() 165 : { 166 : // prefix operator 167 653968 : int i = 0; 168 669751 : while (i < WIDTH && ++pn[i] == 0) 169 15783 : i++; 170 653968 : return *this; 171 : } 172 : 173 255 : base_uint operator++(int) 174 : { 175 : // postfix operator 176 255 : const base_uint ret = *this; 177 255 : ++(*this); 178 255 : return ret; 179 : } 180 : 181 511 : base_uint& operator--() 182 : { 183 : // prefix operator 184 511 : int i = 0; 185 2303 : while (i < WIDTH && --pn[i] == std::numeric_limits<uint32_t>::max()) 186 1792 : i++; 187 511 : return *this; 188 : } 189 : 190 255 : base_uint operator--(int) 191 : { 192 : // postfix operator 193 255 : const base_uint ret = *this; 194 255 : --(*this); 195 255 : return ret; 196 : } 197 : 198 : int CompareTo(const base_uint& b) const; 199 : bool EqualTo(uint64_t b) const; 200 : 201 1937131 : friend inline base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; } 202 1965 : friend inline base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; } 203 1433 : friend inline base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; } 204 630766 : friend inline base_uint operator/(const base_uint& a, const base_uint& b) { return base_uint(a) /= b; } 205 13 : friend inline base_uint operator|(const base_uint& a, const base_uint& b) { return base_uint(a) |= b; } 206 13 : friend inline base_uint operator&(const base_uint& a, const base_uint& b) { return base_uint(a) &= b; } 207 529 : friend inline base_uint operator^(const base_uint& a, const base_uint& b) { return base_uint(a) ^= b; } 208 541170 : friend inline base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; } 209 54647 : friend inline base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; } 210 23937 : friend inline base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; } 211 68 : friend inline base_uint operator/(const base_uint& a, uint64_t b) { return base_uint(a) /= b; } 212 322997 : friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; } 213 774 : friend inline bool operator!=(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) != 0; } 214 770012262 : friend inline bool operator>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) > 0; } 215 632749935 : friend inline bool operator<(const base_uint& a, const base_uint& b) { return a.CompareTo(b) < 0; } 216 336532664 : friend inline bool operator>=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) >= 0; } 217 220959 : friend inline bool operator<=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <= 0; } 218 2989638 : friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); } 219 260 : friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); } 220 : 221 : std::string GetHex() const; 222 : std::string ToString() const; 223 : 224 4 : unsigned int size() const 225 : { 226 4 : return sizeof(pn); 227 : } 228 : 229 : /** 230 : * Returns the position of the highest bit set plus one, or zero if the 231 : * value is zero. 232 : */ 233 : unsigned int bits() const; 234 : 235 689757 : uint64_t GetLow64() const 236 : { 237 : static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter."); 238 689757 : return pn[0] | (uint64_t)pn[1] << 32; 239 : } 240 : }; 241 : 242 : /** 256-bit unsigned big integer. */ 243 : class arith_uint256 : public base_uint<256> { 244 : public: 245 32573455 : arith_uint256() {} 246 3644780 : arith_uint256(const base_uint<256>& b) : base_uint<256>(b) {} 247 6504139 : arith_uint256(uint64_t b) : base_uint<256>(b) {} 248 : 249 : /** 250 : * The "compact" format is a representation of a whole 251 : * number N using an unsigned 32bit number similar to a 252 : * floating point format. 253 : * The most significant 8 bits are the unsigned exponent of base 256. 254 : * This exponent can be thought of as "number of bytes of N". 255 : * The lower 23 bits are the mantissa. 256 : * Bit number 24 (0x800000) represents the sign of N. 257 : * N = (-1^sign) * mantissa * 256^(exponent-3) 258 : * 259 : * Satoshi's original implementation used BN_bn2mpi() and BN_mpi2bn(). 260 : * MPI uses the most significant bit of the first byte as sign. 261 : * Thus 0x1234560000 is compact (0x05123456) 262 : * and 0xc0de000000 is compact (0x0600c0de) 263 : * 264 : * Bitcoin only uses this "compact" format for encoding difficulty 265 : * targets, which are unsigned 256bit quantities. Thus, all the 266 : * complexities of the sign bit and using base 256 are probably an 267 : * implementation accident. 268 : */ 269 : arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = nullptr, bool *pfOverflow = nullptr); 270 : uint32_t GetCompact(bool fNegative = false) const; 271 : 272 : friend uint256 ArithToUint256(const arith_uint256 &); 273 : friend arith_uint256 UintToArith256(const uint256 &); 274 : }; 275 : 276 : uint256 ArithToUint256(const arith_uint256 &); 277 : arith_uint256 UintToArith256(const uint256 &); 278 : 279 : extern template class base_uint<256>; 280 : 281 : #endif // BITCOIN_ARITH_UINT256_H