14template <
unsigned int BITS>
18 for (
int i = 0; i < WIDTH; i++)
22 for (
int i = 0; i < WIDTH; i++) {
23 if (i +
k + 1 < WIDTH && shift != 0)
24 pn[i +
k + 1] |= (a.
pn[i] >> (32 - shift));
26 pn[i +
k] |= (a.
pn[i] << shift);
31template <
unsigned int BITS>
35 for (
int i = 0; i < WIDTH; i++)
39 for (
int i = 0; i < WIDTH; i++) {
40 if (i -
k - 1 >= 0 && shift != 0)
41 pn[i -
k - 1] |= (a.
pn[i] << (32 - shift));
43 pn[i -
k] |= (a.
pn[i] >> shift);
48template <
unsigned int BITS>
52 for (
int i = 0; i < WIDTH; i++) {
53 uint64_t n = carry + (uint64_t)b32 * pn[i];
54 pn[i] = n & 0xffffffff;
60template <
unsigned int BITS>
64 for (
int j = 0; j < WIDTH; j++) {
66 for (
int i = 0; i + j < WIDTH; i++) {
67 uint64_t n = carry + a.
pn[i + j] + (uint64_t)pn[j] * b.
pn[i];
68 a.
pn[i + j] = n & 0xffffffff;
76template <
unsigned int BITS>
82 int num_bits = num.
bits();
83 int div_bits = div.
bits();
86 if (div_bits > num_bits)
88 int shift = num_bits - div_bits;
93 pn[shift / 32] |= (1U << (shift & 31));
102template <
unsigned int BITS>
105 for (
int i = WIDTH - 1; i >= 0; i--) {
114template <
unsigned int BITS>
117 for (
int i = WIDTH - 1; i >= 2; i--) {
121 if (pn[1] != (b >> 32))
123 if (pn[0] != (b & 0xfffffffful))
128template <
unsigned int BITS>
133 for (
int i = 0; i < WIDTH; i++) {
135 fact *= 4294967296.0;
140template <
unsigned int BITS>
144 for (
int x = 0; x < this->WIDTH; ++x) {
150template <
unsigned int BITS>
156template <
unsigned int BITS>
159 for (
int pos = WIDTH - 1; pos >= 0; pos--) {
161 for (
int nbits = 31; nbits > 0; nbits--) {
162 if (pn[pos] & 1U << nbits)
163 return 32 * pos + nbits + 1;
178 int nSize = nCompact >> 24;
179 uint32_t nWord = nCompact & 0x007fffff;
181 nWord >>= 8 * (3 - nSize);
185 *
this <<= 8 * (nSize - 3);
188 *pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0;
190 *pfOverflow = nWord != 0 && ((nSize > 34) ||
191 (nWord > 0xff && nSize > 33) ||
192 (nWord > 0xffff && nSize > 32));
199 uint32_t nCompact = 0;
201 nCompact =
GetLow64() << 8 * (3 - nSize);
208 if (nCompact & 0x00800000) {
212 assert((nCompact & ~0x007fffffU) == 0);
214 nCompact |= nSize << 24;
215 nCompact |= (fNegative && (nCompact & 0x007fffff) ? 0x00800000 : 0);
222 for(
int x=0; x<a.
WIDTH; ++x)
229 for(
int x=0; x<b.
WIDTH; ++x)
arith_uint256 UintToArith256(const uint256 &a)
uint256 ArithToUint256(const arith_uint256 &a)
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
Template base class for fixed-sized opaque blobs.
constexpr unsigned char * begin()
std::string GetHex() const
Template base class for unsigned big integers.
base_uint & operator/=(const base_uint &b)
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>>=(unsigned int shift)
static constexpr int WIDTH
base_uint & operator*=(uint32_t b32)
bool EqualTo(uint64_t b) const
base_uint & operator<<=(unsigned int shift)
std::string ToString() const
uint64_t GetLow64() const
std::string GetHex() const
Hex encoding of the number (with the most significant digits first).
unsigned int bits() const
Returns the position of the highest bit set plus one, or zero if the value is zero.
void WriteLE32(B *ptr, uint32_t x)
uint32_t ReadLE32(const B *ptr)
constexpr auto CeilDiv(const Dividend dividend, const Divisor divisor)
Integer ceiling division (for unsigned values).