6#ifndef BITCOIN_ARITH_UINT256_H
7#define BITCOIN_ARITH_UINT256_H
19 explicit uint_error(
const std::string& str) :
std::runtime_error(str) {}
23template<
unsigned int BITS>
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;
34 for (
int i = 0; i <
WIDTH; i++)
40 for (
int i = 0; i <
WIDTH; i++)
47 for (
int i = 0; i <
WIDTH; i++)
55 pn[0] = (
unsigned int)b;
56 pn[1] = (
unsigned int)(b >> 32);
57 for (
int i = 2; i <
WIDTH; i++)
64 for (
int i = 0; i <
WIDTH; i++)
72 for (
int i = 0; i <
WIDTH; i++)
82 pn[0] = (
unsigned int)b;
83 pn[1] = (
unsigned int)(b >> 32);
84 for (
int i = 2; i <
WIDTH; i++)
91 for (
int i = 0; i <
WIDTH; i++)
98 for (
int i = 0; i <
WIDTH; i++)
105 for (
int i = 0; i <
WIDTH; i++)
112 pn[0] ^= (
unsigned int)b;
113 pn[1] ^= (
unsigned int)(b >> 32);
119 pn[0] |= (
unsigned int)b;
120 pn[1] |= (
unsigned int)(b >> 32);
130 for (
int i = 0; i <
WIDTH; i++)
132 uint64_t n = carry +
pn[i] + b.
pn[i];
133 pn[i] = n & 0xffffffff;
169 while (i <
WIDTH && ++
pn[i] == 0)
186 while (i <
WIDTH && --
pn[i] == std::numeric_limits<uint32_t>::max())
201 bool EqualTo(uint64_t b)
const;
223 std::string
GetHex()
const;
235 unsigned int bits()
const;
239 static_assert(
WIDTH >= 2,
"Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter.");
240 return pn[0] | (uint64_t)
pn[1] << 32;
272 uint32_t
GetCompact(
bool fNegative =
false)
const;
arith_uint256 UintToArith256(const uint256 &)
uint256 ArithToUint256(const arith_uint256 &)
256-bit unsigned big integer.
arith_uint256 & SetCompact(uint32_t nCompact, bool *pfNegative=nullptr, bool *pfOverflow=nullptr)
The "compact" format is a representation of a whole number N using an unsigned 32bit number similar t...
uint32_t GetCompact(bool fNegative=false) const
arith_uint256(uint64_t b)
arith_uint256(const base_uint< 256 > &b)
friend arith_uint256 UintToArith256(const uint256 &)
friend uint256 ArithToUint256(const arith_uint256 &)
Template base class for unsigned big integers.
base_uint operator~() const
int CompareTo(const base_uint &b) const
Numeric ordering (unlike base_blob::Compare)
base_uint operator--(int)
unsigned int size() const
base_uint & operator^=(const base_uint &b)
friend bool operator!=(const base_uint &a, const base_uint &b)
base_uint & operator|=(uint64_t b)
base_uint & operator+=(uint64_t b64)
base_uint & operator>>=(unsigned int shift)
base_uint & operator=(uint64_t b)
friend base_uint operator-(const base_uint &a, const base_uint &b)
base_uint(const base_uint &b)
friend base_uint operator*(const base_uint &a, const base_uint &b)
static constexpr int WIDTH
friend base_uint operator<<(const base_uint &a, int shift)
base_uint & operator|=(const base_uint &b)
friend base_uint operator&(const base_uint &a, const base_uint &b)
friend bool operator<(const base_uint &a, const base_uint &b)
base_uint & operator-=(uint64_t b64)
friend bool operator==(const base_uint &a, uint64_t b)
base_uint & operator^=(uint64_t b)
base_uint operator-() const
friend base_uint operator>>(const base_uint &a, int shift)
friend bool operator>=(const base_uint &a, const base_uint &b)
base_uint & operator*=(uint32_t b32)
friend base_uint operator|(const base_uint &a, const base_uint &b)
bool EqualTo(uint64_t b) const
friend base_uint operator*(const base_uint &a, uint32_t b)
base_uint & operator&=(const base_uint &b)
friend bool operator==(const base_uint &a, const base_uint &b)
friend bool operator!=(const base_uint &a, uint64_t b)
friend base_uint operator/(const base_uint &a, const base_uint &b)
friend base_uint operator^(const base_uint &a, const base_uint &b)
friend bool operator>(const base_uint &a, const base_uint &b)
friend bool operator<=(const base_uint &a, const base_uint &b)
base_uint & operator<<=(unsigned int shift)
std::string ToString() const
base_uint operator++(int)
base_uint & operator/=(const base_uint &b)
base_uint & operator+=(const base_uint &b)
uint64_t GetLow64() const
base_uint & operator=(const base_uint &b)
std::string GetHex() const
Hex encoding of the number (with the most significant digits first).
base_uint & operator-=(const base_uint &b)
friend base_uint operator+(const base_uint &a, const base_uint &b)
unsigned int bits() const
Returns the position of the highest bit set plus one, or zero if the value is zero.
uint_error(const std::string &str)