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++)
45 pn[0] = (
unsigned int)b;
46 pn[1] = (
unsigned int)(b >> 32);
47 for (
int i = 2; i <
WIDTH; i++)
54 for (
int i = 0; i <
WIDTH; i++)
62 for (
int i = 0; i <
WIDTH; i++)
72 pn[0] = (
unsigned int)b;
73 pn[1] = (
unsigned int)(b >> 32);
74 for (
int i = 2; i <
WIDTH; i++)
81 for (
int i = 0; i <
WIDTH; i++)
88 for (
int i = 0; i <
WIDTH; i++)
95 for (
int i = 0; i <
WIDTH; i++)
102 pn[0] ^= (
unsigned int)b;
103 pn[1] ^= (
unsigned int)(b >> 32);
109 pn[0] |= (
unsigned int)b;
110 pn[1] |= (
unsigned int)(b >> 32);
120 for (
int i = 0; i <
WIDTH; i++)
122 uint64_t n = carry +
pn[i] + b.
pn[i];
123 pn[i] = n & 0xffffffff;
159 while (i <
WIDTH && ++
pn[i] == 0)
176 while (i <
WIDTH && --
pn[i] == std::numeric_limits<uint32_t>::max())
191 bool EqualTo(uint64_t b)
const;
208 std::string
GetHex()
const;
220 unsigned int bits()
const;
224 static_assert(
WIDTH >= 2,
"Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter.");
225 return pn[0] | (uint64_t)
pn[1] << 32;
257 uint32_t
GetCompact(
bool fNegative =
false)
const;
264static_assert(std::is_trivially_copyable_v<arith_uint256>);
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
base_uint & operator=(const base_uint &b)=default
uint32_t pn[WIDTH]
Big integer represented with 32-bit digits, least-significant first.
base_uint(const base_uint &b)=default
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)
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
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)