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 8887374 : base_uint() 33 89461 : { 34 79181217 : for (int i = 0; i < WIDTH; i++) 35 70383304 : pn[i] = 0; 36 8887374 : } 37 : 38 3954115 : base_uint(const base_uint& b) 39 1852380 : { 40 18915615 : for (int i = 0; i < WIDTH; i++) 41 16813880 : pn[i] = b.pn[i]; 42 3954115 : } 43 : 44 805764 : base_uint& operator=(const base_uint& b) 45 : { 46 7251876 : for (int i = 0; i < WIDTH; i++) 47 6446112 : pn[i] = b.pn[i]; 48 805764 : return *this; 49 : } 50 : 51 1260787 : base_uint(uint64_t b) 52 122943 : { 53 1137844 : pn[0] = (unsigned int)b; 54 1137844 : pn[1] = (unsigned int)(b >> 32); 55 7964908 : for (int i = 2; i < WIDTH; i++) 56 6827064 : pn[i] = 0; 57 1260787 : } 58 : 59 36004 : base_uint operator~() const 60 : { 61 36004 : base_uint ret; 62 324036 : for (int i = 0; i < WIDTH; i++) 63 288032 : ret.pn[i] = ~pn[i]; 64 36004 : return ret; 65 : } 66 : 67 52187 : base_uint operator-() const 68 : { 69 52187 : base_uint ret; 70 469683 : for (int i = 0; i < WIDTH; i++) 71 417496 : ret.pn[i] = ~pn[i]; 72 52187 : ++ret; 73 52187 : return ret; 74 : } 75 : 76 : double getdouble() const; 77 : 78 37251 : base_uint& operator=(uint64_t b) 79 : { 80 37251 : pn[0] = (unsigned int)b; 81 37251 : pn[1] = (unsigned int)(b >> 32); 82 260757 : for (int i = 2; i < WIDTH; i++) 83 223506 : pn[i] = 0; 84 37251 : 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 209613 : base_uint& operator+=(const base_uint& b) 126 : { 127 209613 : uint64_t carry = 0; 128 1886517 : for (int i = 0; i < WIDTH; i++) 129 : { 130 1676904 : uint64_t n = carry + pn[i] + b.pn[i]; 131 1676904 : pn[i] = n & 0xffffffff; 132 1676904 : carry = n >> 32; 133 1676904 : } 134 209613 : return *this; 135 : } 136 : 137 51668 : base_uint& operator-=(const base_uint& b) 138 : { 139 51668 : *this += -b; 140 51668 : 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 52443 : base_uint& operator++() 165 : { 166 : // prefix operator 167 52443 : int i = 0; 168 68226 : while (i < WIDTH && ++pn[i] == 0) 169 15783 : i++; 170 52443 : 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 157430 : friend inline base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; } 202 1516 : friend inline base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; } 203 1012 : friend inline base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; } 204 36987 : 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 104935 : 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 91 : 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 8437 : 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 67883993 : friend inline bool operator>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) > 0; } 215 66222159 : friend inline bool operator<(const base_uint& a, const base_uint& b) { return a.CompareTo(b) < 0; } 216 33084705 : friend inline bool operator>=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) >= 0; } 217 1532 : friend inline bool operator<=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <= 0; } 218 762470 : 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 253101 : uint64_t GetLow64() const 236 : { 237 : static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter."); 238 253101 : 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 17416904 : arith_uint256() {} 246 445576 : arith_uint256(const base_uint<256>& b) : base_uint<256>(b) {} 247 2029802 : 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