6#ifndef BITCOIN_ARITH_UINT256_H
7#define BITCOIN_ARITH_UINT256_H
19 explicit uint_error(
const std::string &str) :
std::runtime_error(str) {}
25 static constexpr int WIDTH = BITS / 32;
31 BITS / 32 > 0 && BITS % 32 == 0,
32 "Template parameter BITS must be a positive multiple of 32.");
34 for (
int i = 0; i <
WIDTH; i++) {
41 BITS / 32 > 0 && BITS % 32 == 0,
42 "Template parameter BITS must be a positive multiple of 32.");
44 for (
int i = 0; i <
WIDTH; i++) {
50 for (
int i = 0; i <
WIDTH; i++) {
58 BITS / 32 > 0 && BITS % 32 == 0,
59 "Template parameter BITS must be a positive multiple of 32.");
62 pn[1] = (
unsigned int)(
b >> 32);
63 for (
int i = 2; i <
WIDTH; i++) {
68 explicit base_uint(
const std::string &str);
72 for (
int i = 0; i <
WIDTH; i++) {
80 for (
int i = 0; i <
WIDTH; i++) {
91 pn[1] = (
unsigned int)(
b >> 32);
92 for (
int i = 2; i <
WIDTH; i++) {
99 for (
int i = 0; i <
WIDTH; i++) {
106 for (
int i = 0; i <
WIDTH; i++) {
113 for (
int i = 0; i <
WIDTH; i++) {
121 pn[1] ^= (
unsigned int)(
b >> 32);
127 pn[1] |= (
unsigned int)(
b >> 32);
136 for (
int i = 0; i <
WIDTH; i++) {
138 pn[i] = n & 0xffffffff;
170 while (i <
WIDTH && ++
pn[i] == 0) {
186 while (i <
WIDTH && --
pn[i] == std::numeric_limits<uint32_t>::max()) {
240 return memcmp(
a.pn,
b.pn,
sizeof(
a.pn)) == 0;
243 return memcmp(
a.pn,
b.pn,
sizeof(
a.pn)) != 0;
246 return a.CompareTo(
b) > 0;
249 return a.CompareTo(
b) < 0;
252 return a.CompareTo(
b) >= 0;
255 return a.CompareTo(
b) <= 0;
261 return !
a.EqualTo(
b);
264 std::string
GetHex()
const;
266 void SetHex(
const std::string &str);
269 unsigned int size()
const {
return sizeof(
pn); }
275 unsigned int bits()
const;
278 static_assert(
WIDTH >= 2,
"Assertion WIDTH >= 2 failed (WIDTH = BITS / "
279 "32). BITS is a template parameter.");
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...
arith_uint256(const std::string &str)
uint32_t GetCompact(bool fNegative=false) const
static arith_uint256 fromDouble(double d)
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.
int CompareTo(const base_uint &b) const
unsigned int size() const
const base_uint operator~() const
const base_uint operator++(int)
base_uint & operator^=(const base_uint &b)
friend const base_uint operator/(const base_uint &a, const base_uint &b)
friend const base_uint operator*(const base_uint &a, uint32_t b)
const base_uint operator-() const
friend bool operator!=(const base_uint &a, const base_uint &b)
friend const base_uint 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)
base_uint(const base_uint &b)
static constexpr int WIDTH
const base_uint operator--(int)
friend const base_uint operator*(const base_uint &a, const base_uint &b)
friend const base_uint operator&(const base_uint &a, const base_uint &b)
base_uint & operator|=(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)
friend const 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 const base_uint operator^(const base_uint &a, const base_uint &b)
bool EqualTo(uint64_t b) const
base_uint & operator&=(const base_uint &b)
friend bool operator==(const base_uint &a, const base_uint &b)
friend const base_uint operator+(const base_uint &a, const base_uint &b)
friend bool operator!=(const base_uint &a, uint64_t 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
friend const base_uint operator<<(const base_uint &a, int shift)
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)
void SetHex(const char *psz)
std::string GetHex() const
base_uint & operator-=(const base_uint &b)
friend const 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)
Implement std::hash so RCUPtr can be used as a key for maps or sets.
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...