Branch data 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 <cstring>
# 10 : : #include <limits>
# 11 : : #include <stdexcept>
# 12 : : #include <stdint.h>
# 13 : : #include <string>
# 14 : :
# 15 : : class uint256;
# 16 : :
# 17 : : class uint_error : public std::runtime_error {
# 18 : : public:
# 19 : 4 : 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 constexpr int WIDTH = BITS / 32;
# 28 : : uint32_t pn[WIDTH];
# 29 : : public:
# 30 : :
# 31 : : base_uint()
# 32 : 22831077 : {
# 33 : 22831077 : static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
# 34 : :
# 35 [ + + ]: 205532820 : for (int i = 0; i < WIDTH; i++)
# 36 : 182701743 : pn[i] = 0;
# 37 : 22831077 : }
# 38 : :
# 39 : : base_uint(const base_uint& b)
# 40 : 5442194 : {
# 41 : 5442194 : static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
# 42 : :
# 43 [ + + ]: 48979746 : for (int i = 0; i < WIDTH; i++)
# 44 : 43537552 : pn[i] = b.pn[i];
# 45 : 5442194 : }
# 46 : :
# 47 : : base_uint& operator=(const base_uint& b)
# 48 : 1206454 : {
# 49 [ + + ]: 10858086 : for (int i = 0; i < WIDTH; i++)
# 50 : 9651632 : pn[i] = b.pn[i];
# 51 : 1206454 : return *this;
# 52 : 1206454 : }
# 53 : :
# 54 : : base_uint(uint64_t b)
# 55 : 2028825 : {
# 56 : 2028825 : static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
# 57 : :
# 58 : 2028825 : pn[0] = (unsigned int)b;
# 59 : 2028825 : pn[1] = (unsigned int)(b >> 32);
# 60 [ + + ]: 14201773 : for (int i = 2; i < WIDTH; i++)
# 61 : 12172948 : pn[i] = 0;
# 62 : 2028825 : }
# 63 : :
# 64 : : explicit base_uint(const std::string& str);
# 65 : :
# 66 : : const base_uint operator~() const
# 67 : 185987 : {
# 68 : 185987 : base_uint ret;
# 69 [ + + ]: 1673883 : for (int i = 0; i < WIDTH; i++)
# 70 : 1487896 : ret.pn[i] = ~pn[i];
# 71 : 185987 : return ret;
# 72 : 185987 : }
# 73 : :
# 74 : : const base_uint operator-() const
# 75 : 214823 : {
# 76 : 214823 : base_uint ret;
# 77 [ + + ]: 1933407 : for (int i = 0; i < WIDTH; i++)
# 78 : 1718584 : ret.pn[i] = ~pn[i];
# 79 : 214823 : ++ret;
# 80 : 214823 : return ret;
# 81 : 214823 : }
# 82 : :
# 83 : : double getdouble() const;
# 84 : :
# 85 : : base_uint& operator=(uint64_t b)
# 86 : 188652 : {
# 87 : 188652 : pn[0] = (unsigned int)b;
# 88 : 188652 : pn[1] = (unsigned int)(b >> 32);
# 89 [ + + ]: 1320564 : for (int i = 2; i < WIDTH; i++)
# 90 : 1131912 : pn[i] = 0;
# 91 : 188652 : return *this;
# 92 : 188652 : }
# 93 : :
# 94 : : base_uint& operator^=(const base_uint& b)
# 95 : 1068 : {
# 96 [ + + ]: 9612 : for (int i = 0; i < WIDTH; i++)
# 97 : 8544 : pn[i] ^= b.pn[i];
# 98 : 1068 : return *this;
# 99 : 1068 : }
# 100 : :
# 101 : : base_uint& operator&=(const base_uint& b)
# 102 : 36 : {
# 103 [ + + ]: 324 : for (int i = 0; i < WIDTH; i++)
# 104 : 288 : pn[i] &= b.pn[i];
# 105 : 36 : return *this;
# 106 : 36 : }
# 107 : :
# 108 : : base_uint& operator|=(const base_uint& b)
# 109 : 548 : {
# 110 [ + + ]: 4932 : for (int i = 0; i < WIDTH; i++)
# 111 : 4384 : pn[i] |= b.pn[i];
# 112 : 548 : return *this;
# 113 : 548 : }
# 114 : :
# 115 : : base_uint& operator^=(uint64_t b)
# 116 : 4 : {
# 117 : 4 : pn[0] ^= (unsigned int)b;
# 118 : 4 : pn[1] ^= (unsigned int)(b >> 32);
# 119 : 4 : return *this;
# 120 : 4 : }
# 121 : :
# 122 : : base_uint& operator|=(uint64_t b)
# 123 : 4 : {
# 124 : 4 : pn[0] |= (unsigned int)b;
# 125 : 4 : pn[1] |= (unsigned int)(b >> 32);
# 126 : 4 : return *this;
# 127 : 4 : }
# 128 : :
# 129 : : base_uint& operator<<=(unsigned int shift);
# 130 : : base_uint& operator>>=(unsigned int shift);
# 131 : :
# 132 : : base_uint& operator+=(const base_uint& b)
# 133 : 871309 : {
# 134 : 871309 : uint64_t carry = 0;
# 135 [ + + ]: 7841781 : for (int i = 0; i < WIDTH; i++)
# 136 : 6970472 : {
# 137 : 6970472 : uint64_t n = carry + pn[i] + b.pn[i];
# 138 : 6970472 : pn[i] = n & 0xffffffff;
# 139 : 6970472 : carry = n >> 32;
# 140 : 6970472 : }
# 141 : 871309 : return *this;
# 142 : 871309 : }
# 143 : :
# 144 : : base_uint& operator-=(const base_uint& b)
# 145 : 213785 : {
# 146 : 213785 : *this += -b;
# 147 : 213785 : return *this;
# 148 : 213785 : }
# 149 : :
# 150 : : base_uint& operator+=(uint64_t b64)
# 151 : 512 : {
# 152 : 512 : base_uint b;
# 153 : 512 : b = b64;
# 154 : 512 : *this += b;
# 155 : 512 : return *this;
# 156 : 512 : }
# 157 : :
# 158 : : base_uint& operator-=(uint64_t b64)
# 159 : 2 : {
# 160 : 2 : base_uint b;
# 161 : 2 : b = b64;
# 162 : 2 : *this += -b;
# 163 : 2 : return *this;
# 164 : 2 : }
# 165 : :
# 166 : : base_uint& operator*=(uint32_t b32);
# 167 : : base_uint& operator*=(const base_uint& b);
# 168 : : base_uint& operator/=(const base_uint& b);
# 169 : :
# 170 : : base_uint& operator++()
# 171 : 215335 : {
# 172 : : // prefix operator
# 173 : 215335 : int i = 0;
# 174 [ + + ][ + + ]: 228838 : while (i < WIDTH && ++pn[i] == 0)
# 175 : 13503 : i++;
# 176 : 215335 : return *this;
# 177 : 215335 : }
# 178 : :
# 179 : : const base_uint operator++(int)
# 180 : 510 : {
# 181 : : // postfix operator
# 182 : 510 : const base_uint ret = *this;
# 183 : 510 : ++(*this);
# 184 : 510 : return ret;
# 185 : 510 : }
# 186 : :
# 187 : : base_uint& operator--()
# 188 : 1022 : {
# 189 : : // prefix operator
# 190 : 1022 : int i = 0;
# 191 [ + - ][ + + ]: 4606 : while (i < WIDTH && --pn[i] == std::numeric_limits<uint32_t>::max())
# 192 : 3584 : i++;
# 193 : 1022 : return *this;
# 194 : 1022 : }
# 195 : :
# 196 : : const base_uint operator--(int)
# 197 : 510 : {
# 198 : : // postfix operator
# 199 : 510 : const base_uint ret = *this;
# 200 : 510 : --(*this);
# 201 : 510 : return ret;
# 202 : 510 : }
# 203 : :
# 204 : : int CompareTo(const base_uint& b) const;
# 205 : : bool EqualTo(uint64_t b) const;
# 206 : :
# 207 : 656494 : friend inline const base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; }
# 208 : 3248 : friend inline const base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; }
# 209 : 2235 : friend inline const base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; }
# 210 : 188122 : friend inline const base_uint operator/(const base_uint& a, const base_uint& b) { return base_uint(a) /= b; }
# 211 : 26 : friend inline const base_uint operator|(const base_uint& a, const base_uint& b) { return base_uint(a) |= b; }
# 212 : 26 : friend inline const base_uint operator&(const base_uint& a, const base_uint& b) { return base_uint(a) &= b; }
# 213 : 1058 : friend inline const base_uint operator^(const base_uint& a, const base_uint& b) { return base_uint(a) ^= b; }
# 214 : 165372 : friend inline const base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; }
# 215 : 108968 : friend inline const base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
# 216 : 17280 : friend inline const base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
# 217 : 15040 : friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
# 218 : 1548 : friend inline bool operator!=(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) != 0; }
# 219 : 282148103 : friend inline bool operator>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) > 0; }
# 220 : 234292478 : friend inline bool operator<(const base_uint& a, const base_uint& b) { return a.CompareTo(b) < 0; }
# 221 : 120616242 : friend inline bool operator>=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) >= 0; }
# 222 : 11264109 : friend inline bool operator<=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <= 0; }
# 223 : 1025461 : friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); }
# 224 : 520 : friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); }
# 225 : :
# 226 : : std::string GetHex() const;
# 227 : : void SetHex(const char* psz);
# 228 : : void SetHex(const std::string& str);
# 229 : : std::string ToString() const;
# 230 : :
# 231 : : unsigned int size() const
# 232 : 8 : {
# 233 : 8 : return sizeof(pn);
# 234 : 8 : }
# 235 : :
# 236 : : /**
# 237 : : * Returns the position of the highest bit set plus one, or zero if the
# 238 : : * value is zero.
# 239 : : */
# 240 : : unsigned int bits() const;
# 241 : :
# 242 : : uint64_t GetLow64() const
# 243 : 461931 : {
# 244 : 461931 : static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter.");
# 245 : 461931 : return pn[0] | (uint64_t)pn[1] << 32;
# 246 : 461931 : }
# 247 : : };
# 248 : :
# 249 : : /** 256-bit unsigned big integer. */
# 250 : : class arith_uint256 : public base_uint<256> {
# 251 : : public:
# 252 : 22430731 : arith_uint256() {}
# 253 : 611636 : arith_uint256(const base_uint<256>& b) : base_uint<256>(b) {}
# 254 : 1530241 : arith_uint256(uint64_t b) : base_uint<256>(b) {}
# 255 : 38 : explicit arith_uint256(const std::string& str) : base_uint<256>(str) {}
# 256 : :
# 257 : : /**
# 258 : : * The "compact" format is a representation of a whole
# 259 : : * number N using an unsigned 32bit number similar to a
# 260 : : * floating point format.
# 261 : : * The most significant 8 bits are the unsigned exponent of base 256.
# 262 : : * This exponent can be thought of as "number of bytes of N".
# 263 : : * The lower 23 bits are the mantissa.
# 264 : : * Bit number 24 (0x800000) represents the sign of N.
# 265 : : * N = (-1^sign) * mantissa * 256^(exponent-3)
# 266 : : *
# 267 : : * Satoshi's original implementation used BN_bn2mpi() and BN_mpi2bn().
# 268 : : * MPI uses the most significant bit of the first byte as sign.
# 269 : : * Thus 0x1234560000 is compact (0x05123456)
# 270 : : * and 0xc0de000000 is compact (0x0600c0de)
# 271 : : *
# 272 : : * Bitcoin only uses this "compact" format for encoding difficulty
# 273 : : * targets, which are unsigned 256bit quantities. Thus, all the
# 274 : : * complexities of the sign bit and using base 256 are probably an
# 275 : : * implementation accident.
# 276 : : */
# 277 : : arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = nullptr, bool *pfOverflow = nullptr);
# 278 : : uint32_t GetCompact(bool fNegative = false) const;
# 279 : :
# 280 : : friend uint256 ArithToUint256(const arith_uint256 &);
# 281 : : friend arith_uint256 UintToArith256(const uint256 &);
# 282 : : };
# 283 : :
# 284 : : uint256 ArithToUint256(const arith_uint256 &);
# 285 : : arith_uint256 UintToArith256(const uint256 &);
# 286 : :
# 287 : : extern template class base_uint<256>;
# 288 : :
# 289 : : #endif // BITCOIN_ARITH_UINT256_H
|