15 template <
unsigned int BITS>
21 template <
unsigned int BITS>
24 if (vch.size() !=
sizeof(pn))
25 throw uint_error(
"Converting vector of wrong size to base_uint");
26 memcpy(pn, vch.data(),
sizeof(pn));
29 template <
unsigned int BITS>
33 for (
int i = 0; i < WIDTH; i++)
37 for (
int i = 0; i < WIDTH; i++) {
38 if (i + k + 1 < WIDTH && shift != 0)
39 pn[i + k + 1] |= (a.pn[i] >> (32 - shift));
41 pn[i + k] |= (a.pn[i] << shift);
46 template <
unsigned int BITS>
50 for (
int i = 0; i < WIDTH; i++)
54 for (
int i = 0; i < WIDTH; i++) {
55 if (i - k - 1 >= 0 && shift != 0)
56 pn[i - k - 1] |= (a.
pn[i] << (32 - shift));
58 pn[i - k] |= (a.
pn[i] >> shift);
63 template <
unsigned int BITS>
67 for (
int i = 0; i < WIDTH; i++) {
68 uint64_t n = carry + (uint64_t)b32 * pn[i];
69 pn[i] = n & 0xffffffff;
75 template <
unsigned int BITS>
79 for (
int j = 0; j < WIDTH; j++) {
81 for (
int i = 0; i + j < WIDTH; i++) {
82 uint64_t n = carry + a.
pn[i + j] + (uint64_t)pn[j] * b.
pn[i];
83 a.
pn[i + j] = n & 0xffffffff;
91 template <
unsigned int BITS>
97 int num_bits = num.
bits();
98 int div_bits = div.
bits();
101 if (div_bits > num_bits)
103 int shift = num_bits - div_bits;
108 pn[shift / 32] |= (1 << (shift & 31));
117 template <
unsigned int BITS>
120 for (
int i = WIDTH - 1; i >= 0; i--) {
129 template <
unsigned int BITS>
132 for (
int i = WIDTH - 1; i >= 2; i--) {
136 if (pn[1] != (b >> 32))
138 if (pn[0] != (b & 0xfffffffful))
143 template <
unsigned int BITS>
148 for (
int i = 0; i < WIDTH; i++) {
150 fact *= 4294967296.0;
155 template <
unsigned int BITS>
158 char psz[
sizeof(pn) * 2 + 1];
159 for (
unsigned int i = 0; i <
sizeof(pn); i++)
160 sprintf(psz + i * 2,
"%02x", ((
unsigned char*)pn)[
sizeof(pn) - i - 1]);
161 return std::string(psz, psz +
sizeof(pn) * 2);
164 template <
unsigned int BITS>
167 memset(pn, 0,
sizeof(pn));
170 while (isspace(*psz))
174 if (psz[0] ==
'0' && tolower(psz[1]) ==
'x')
178 const char* pbegin = psz;
182 unsigned char* p1 = (
unsigned char*)pn;
183 unsigned char* pend = p1 + WIDTH * 4;
184 while (psz >= pbegin && p1 < pend) {
187 *p1 |= ((
unsigned char)::
HexDigit(*psz--) << 4);
193 template <
unsigned int BITS>
199 template <
unsigned int BITS>
205 template <
unsigned int BITS>
208 char psz[
sizeof(pn) * 2 + 1];
209 for (
unsigned int i = 0; i <
sizeof(pn); i++)
210 sprintf(psz + i * 2,
"%02x", ((
unsigned char*)pn)[i]);
211 return std::string(psz, psz +
sizeof(pn) * 2);
214 template <
unsigned int BITS>
217 for (
int pos = WIDTH - 1; pos >= 0; pos--) {
219 for (
int bits = 31; bits > 0; bits--) {
220 if (pn[pos] & 1 << bits)
221 return 32 * pos + bits + 1;
276 int nSize = nCompact >> 24;
277 uint32_t nWord = nCompact & 0x007fffff;
279 nWord >>= 8 * (3 - nSize);
283 *
this <<= 8 * (nSize - 3);
286 *pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0;
288 *pfOverflow = nWord != 0 && ((nSize > 34) ||
289 (nWord > 0xff && nSize > 33) ||
290 (nWord > 0xffff && nSize > 32));
296 int nSize = (
bits() + 7) / 8;
297 uint32_t nCompact = 0;
299 nCompact =
GetLow64() << 8 * (3 - nSize);
306 if (nCompact & 0x00800000) {
310 assert((nCompact & ~0x007fffff) == 0);
312 nCompact |= nSize << 24;
313 nCompact |= (fNegative && (nCompact & 0x007fffff) ? 0x00800000 : 0);
319 std::vector<unsigned char> vch;
320 const unsigned char* p = this->
begin();
321 for (
unsigned int i = 0; i < 32; i++) {
330 for(
int x=0; x<a.
WIDTH; ++x)
331 WriteLE32(b.
begin() + x*4, a.
pn[x]);
337 for(
int x=0; x<b.
WIDTH; ++x)
338 b.
pn[x] = ReadLE32(a.
begin() + x*4);
345 for(
int x=0; x<a.
WIDTH; ++x)
346 WriteLE32(b.
begin() + x*4, a.
pn[x]);
352 for(
int x=0; x<b.
WIDTH; ++x)
353 b.
pn[x] = ReadLE32(a.
begin() + x*4);
arith_uint512 UintToArith512(const uint512 &a)
arith_uint256 UintToArith256(const uint256 &a)
uint512 ArithToUint512(const arith_uint512 &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
512-bit unsigned big integer.
Template base class for unsigned big integers.
int CompareTo(const base_uint &b) const
base_uint & operator>>=(unsigned int shift)
static constexpr int WIDTH
std::string ToStringReverseEndian() const
base_uint & operator*=(uint32_t b32)
bool EqualTo(uint64_t b) const
base_uint & operator<<=(unsigned int shift)
std::string ToString() const
base_uint & operator/=(const base_uint &b)
uint64_t GetLow64() const
void SetHex(const char *psz)
std::string GetHex() const
unsigned int bits() const
Returns the position of the highest bit set plus one, or zero if the value is zero.
void * memcpy(void *a, const void *b, size_t c)
signed char HexDigit(char c)