arith_uint256.h raw

   1  // Copyright (c) 2009-2010 Satoshi Nakamoto
   2  // Copyright (c) 2009-2022 The Limenka 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 LIMENKA_ARITH_UINT256_H
   7  #define LIMENKA_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      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      /** Big integer represented with 32-bit digits, least-significant first. */
  30      uint32_t pn[WIDTH];
  31  public:
  32  
  33      base_uint()
  34      {
  35          for (int i = 0; i < WIDTH; i++)
  36              pn[i] = 0;
  37      }
  38  
  39      base_uint(const base_uint& b) = default;
  40      base_uint& operator=(const base_uint& b) = default;
  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  
  50      base_uint operator~() const
  51      {
  52          base_uint ret;
  53          for (int i = 0; i < WIDTH; i++)
  54              ret.pn[i] = ~pn[i];
  55          return ret;
  56      }
  57  
  58      base_uint operator-() const
  59      {
  60          base_uint ret;
  61          for (int i = 0; i < WIDTH; i++)
  62              ret.pn[i] = ~pn[i];
  63          ++ret;
  64          return ret;
  65      }
  66  
  67      double getdouble() const;
  68  
  69      base_uint& operator=(uint64_t b)
  70      {
  71          pn[0] = (unsigned int)b;
  72          pn[1] = (unsigned int)(b >> 32);
  73          for (int i = 2; i < WIDTH; i++)
  74              pn[i] = 0;
  75          return *this;
  76      }
  77  
  78      base_uint& operator^=(const base_uint& b)
  79      {
  80          for (int i = 0; i < WIDTH; i++)
  81              pn[i] ^= b.pn[i];
  82          return *this;
  83      }
  84  
  85      base_uint& operator&=(const base_uint& b)
  86      {
  87          for (int i = 0; i < WIDTH; i++)
  88              pn[i] &= b.pn[i];
  89          return *this;
  90      }
  91  
  92      base_uint& operator|=(const base_uint& b)
  93      {
  94          for (int i = 0; i < WIDTH; i++)
  95              pn[i] |= b.pn[i];
  96          return *this;
  97      }
  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  
 106      base_uint& operator|=(uint64_t b)
 107      {
 108          pn[0] |= (unsigned int)b;
 109          pn[1] |= (unsigned int)(b >> 32);
 110          return *this;
 111      }
 112  
 113      base_uint& operator<<=(unsigned int shift);
 114      base_uint& operator>>=(unsigned int shift);
 115  
 116      base_uint& operator+=(const base_uint& b)
 117      {
 118          uint64_t carry = 0;
 119          for (int i = 0; i < WIDTH; i++)
 120          {
 121              uint64_t n = carry + pn[i] + b.pn[i];
 122              pn[i] = n & 0xffffffff;
 123              carry = n >> 32;
 124          }
 125          return *this;
 126      }
 127  
 128      base_uint& operator-=(const base_uint& b)
 129      {
 130          *this += -b;
 131          return *this;
 132      }
 133  
 134      base_uint& operator+=(uint64_t b64)
 135      {
 136          base_uint b;
 137          b = b64;
 138          *this += b;
 139          return *this;
 140      }
 141  
 142      base_uint& operator-=(uint64_t b64)
 143      {
 144          base_uint b;
 145          b = b64;
 146          *this += -b;
 147          return *this;
 148      }
 149  
 150      base_uint& operator*=(uint32_t b32);
 151      base_uint& operator*=(const base_uint& b);
 152      base_uint& operator/=(const base_uint& b);
 153  
 154      base_uint& operator++()
 155      {
 156          // prefix operator
 157          int i = 0;
 158          while (i < WIDTH && ++pn[i] == 0)
 159              i++;
 160          return *this;
 161      }
 162  
 163      base_uint operator++(int)
 164      {
 165          // postfix operator
 166          const base_uint ret = *this;
 167          ++(*this);
 168          return ret;
 169      }
 170  
 171      base_uint& operator--()
 172      {
 173          // prefix operator
 174          int i = 0;
 175          while (i < WIDTH && --pn[i] == std::numeric_limits<uint32_t>::max())
 176              i++;
 177          return *this;
 178      }
 179  
 180      base_uint operator--(int)
 181      {
 182          // postfix operator
 183          const base_uint ret = *this;
 184          --(*this);
 185          return ret;
 186      }
 187  
 188      /** Numeric ordering (unlike \ref base_blob::Compare) */
 189      int CompareTo(const base_uint& b) const;
 190      bool EqualTo(uint64_t b) const;
 191  
 192      friend inline base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; }
 193      friend inline base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; }
 194      friend inline base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; }
 195      friend inline base_uint operator/(const base_uint& a, const base_uint& b) { return base_uint(a) /= b; }
 196      friend inline base_uint operator|(const base_uint& a, const base_uint& b) { return base_uint(a) |= b; }
 197      friend inline base_uint operator&(const base_uint& a, const base_uint& b) { return base_uint(a) &= b; }
 198      friend inline base_uint operator^(const base_uint& a, const base_uint& b) { return base_uint(a) ^= b; }
 199      friend inline base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; }
 200      friend inline base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
 201      friend inline base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
 202      friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
 203      friend inline bool operator!=(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) != 0; }
 204      friend inline bool operator>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) > 0; }
 205      friend inline bool operator<(const base_uint& a, const base_uint& b) { return a.CompareTo(b) < 0; }
 206      friend inline bool operator>=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) >= 0; }
 207      friend inline bool operator<=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <= 0; }
 208      friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); }
 209      friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); }
 210  
 211      /** Hex encoding of the number (with the most significant digits first). */
 212      std::string GetHex() const;
 213      std::string ToString() const;
 214  
 215      unsigned int size() const
 216      {
 217          return sizeof(pn);
 218      }
 219  
 220      /**
 221       * Returns the position of the highest bit set plus one, or zero if the
 222       * value is zero.
 223       */
 224      unsigned int bits() const;
 225  
 226      uint64_t GetLow64() const
 227      {
 228          static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter.");
 229          return pn[0] | (uint64_t)pn[1] << 32;
 230      }
 231  };
 232  
 233  /** 256-bit unsigned big integer. */
 234  class arith_uint256 : public base_uint<256> {
 235  public:
 236      arith_uint256() = default;
 237      arith_uint256(const base_uint<256>& b) : base_uint<256>(b) {}
 238      arith_uint256(uint64_t b) : base_uint<256>(b) {}
 239  
 240      /**
 241       * The "compact" format is a representation of a whole
 242       * number N using an unsigned 32bit number similar to a
 243       * floating point format.
 244       * The most significant 8 bits are the unsigned exponent of base 256.
 245       * This exponent can be thought of as "number of bytes of N".
 246       * The lower 23 bits are the mantissa.
 247       * Bit number 24 (0x800000) represents the sign of N.
 248       * N = (-1^sign) * mantissa * 256^(exponent-3)
 249       *
 250       * Satoshi's original implementation used BN_bn2mpi() and BN_mpi2bn().
 251       * MPI uses the most significant bit of the first byte as sign.
 252       * Thus 0x1234560000 is compact (0x05123456)
 253       * and  0xc0de000000 is compact (0x0600c0de)
 254       *
 255       * Limenka only uses this "compact" format for encoding difficulty
 256       * targets, which are unsigned 256bit quantities.  Thus, all the
 257       * complexities of the sign bit and using base 256 are probably an
 258       * implementation accident.
 259       */
 260      arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = nullptr, bool *pfOverflow = nullptr);
 261      uint32_t GetCompact(bool fNegative = false) const;
 262  
 263      friend uint256 ArithToUint256(const arith_uint256 &);
 264      friend arith_uint256 UintToArith256(const uint256 &);
 265  };
 266  
 267  // Keeping the trivially copyable property is beneficial for performance
 268  static_assert(std::is_trivially_copyable_v<arith_uint256>);
 269  
 270  uint256 ArithToUint256(const arith_uint256 &);
 271  arith_uint256 UintToArith256(const uint256 &);
 272  
 273  extern template class base_uint<256>;
 274  
 275  #endif // LIMENKA_ARITH_UINT256_H
 276