6#ifndef BITCOIN_ARITH_UINT256_H
7#define BITCOIN_ARITH_UINT256_H
20 explicit uint_error(
const std::string& str) :
std::runtime_error(str) {}
24template<
unsigned int BITS>
28 static_assert(BITS / 32 > 0 && BITS % 32 == 0,
"Template parameter BITS must be a positive multiple of 32.");
29 static constexpr int WIDTH = BITS / 32;
36 for (
int i = 0; i <
WIDTH; i++)
42 for (
int i = 0; i <
WIDTH; i++)
49 for (
int i = 0; i <
WIDTH; i++)
57 pn[0] = (
unsigned int)b;
58 pn[1] = (
unsigned int)(b >> 32);
59 for (
int i = 2; i <
WIDTH; i++)
66 for (
int i = 0; i <
WIDTH; i++)
74 for (
int i = 0; i <
WIDTH; i++)
84 pn[0] = (
unsigned int)b;
85 pn[1] = (
unsigned int)(b >> 32);
86 for (
int i = 2; i <
WIDTH; i++)
93 for (
int i = 0; i <
WIDTH; i++)
100 for (
int i = 0; i <
WIDTH; i++)
107 for (
int i = 0; i <
WIDTH; i++)
114 pn[0] ^= (
unsigned int)b;
115 pn[1] ^= (
unsigned int)(b >> 32);
121 pn[0] |= (
unsigned int)b;
122 pn[1] |= (
unsigned int)(b >> 32);
132 for (
int i = 0; i <
WIDTH; i++)
134 uint64_t n = carry +
pn[i] + b.
pn[i];
135 pn[i] = n & 0xffffffff;
171 while (i <
WIDTH && ++
pn[i] == 0)
188 while (i <
WIDTH && --
pn[i] == std::numeric_limits<uint32_t>::max())
203 bool EqualTo(uint64_t b)
const;
220 std::string
GetHex()
const;
232 unsigned int bits()
const;
236 static_assert(
WIDTH >= 2,
"Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter.");
237 return pn[0] | (uint64_t)
pn[1] << 32;
269 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 base_uint &b)
base_uint operator~() const
uint32_t pn[WIDTH]
Big integer represented with 32-bit digits, least-significant first.
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)
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)
friend std::strong_ordering operator<=>(const base_uint &a, const base_uint &b)
base_uint & operator|=(const base_uint &b)
friend base_uint 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)
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 base_uint operator/(const base_uint &a, const base_uint &b)
friend base_uint 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)
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)