6 #ifndef BITCOIN_UINT256_H
7 #define BITCOIN_UINT256_H
25 template<
unsigned int BITS>
35 for (
int i = 0; i <
WIDTH; i++)
44 for (
int i = 0; i <
WIDTH; i++)
52 for (
int i = 0; i <
WIDTH; i++)
62 for (
int i = 0; i <
WIDTH; i++) {
71 pn[0] = (
unsigned int)b;
72 pn[1] = (
unsigned int)(b >> 32);
73 for (
int i = 2; i <
WIDTH; i++)
80 for (
int i = 0; i <
WIDTH; i++)
87 for (
int i = 0; i <
WIDTH; i++)
94 for (
int i = 0; i <
WIDTH; i++)
101 pn[0] ^= (
unsigned int)b;
102 pn[1] ^= (
unsigned int)(b >> 32);
108 pn[0] |= (
unsigned int)b;
109 pn[1] |= (
unsigned int)(b >> 32);
116 for (
int i = 0; i <
WIDTH; i++)
120 for (
int i = 0; i <
WIDTH; i++)
122 if (i+k+1 < WIDTH && shift != 0)
123 pn[i+k+1] |= (a.
pn[i] >> (32-shift));
125 pn[i+k] |= (a.
pn[i] << shift);
133 for (
int i = 0; i <
WIDTH; i++)
137 for (
int i = 0; i <
WIDTH; i++)
139 if (i-k-1 >= 0 && shift != 0)
140 pn[i-k-1] |= (a.
pn[i] << (32-shift));
142 pn[i-k] |= (a.
pn[i] >> shift);
150 for (
int i = 0; i <
WIDTH; i++)
152 uint64_t n = carry + pn[i] + b.
pn[i];
153 pn[i] = n & 0xffffffff;
186 while (++pn[i] == 0 && i <
WIDTH-1)
203 while (--pn[i] == (uint32_t)-1 && i <
WIDTH-1)
221 if (a.
pn[i] < b.
pn[i])
223 else if (a.
pn[i] > b.
pn[i])
233 if (a.
pn[i] < b.
pn[i])
235 else if (a.
pn[i] > b.
pn[i])
245 if (a.
pn[i] > b.
pn[i])
247 else if (a.
pn[i] < b.
pn[i])
257 if (a.
pn[i] > b.
pn[i])
259 else if (a.
pn[i] < b.
pn[i])
268 if (a.
pn[i] != b.
pn[i])
275 if (a.
pn[0] != (
unsigned int)b)
277 if (a.
pn[1] != (
unsigned int)(b >> 32))
299 char psz[
sizeof(
pn)*2 + 1];
300 for (
unsigned int i = 0; i <
sizeof(
pn); i++)
301 sprintf(psz + i*2,
"%02x", ((
unsigned char*)
pn)[
sizeof(pn) - i - 1]);
302 return std::string(psz, psz +
sizeof(pn)*2);
307 memset(pn,0,
sizeof(pn));
310 while (isspace(*psz))
314 if (psz[0] ==
'0' && tolower(psz[1]) ==
'x')
318 const char* pbegin = psz;
322 unsigned char* p1 = (
unsigned char*)pn;
323 unsigned char* pend = p1 +
WIDTH * 4;
324 while (psz >= pbegin && p1 < pend)
329 *p1 |= ((
unsigned char)::
HexDigit(*psz--) << 4);
347 return (
unsigned char*)&pn[0];
352 return (
unsigned char*)&pn[
WIDTH];
357 return (
unsigned char*)&pn[0];
360 const unsigned char*
end()
const
362 return (
unsigned char*)&pn[
WIDTH];
373 return pn[0] | (uint64_t)pn[1] << 32;
382 template<
typename Stream>
384 void Serialize(Stream& s,
int nType,
int nVersion)
const
386 s.write((
char*)pn,
sizeof(pn));
389 template<
typename Stream>
393 s.read((
char*)pn,
sizeof(pn));
426 for (
int i = 0; i <
WIDTH; i++)
432 for (
int i = 0; i <
WIDTH; i++)
438 for (
int i = 0; i <
WIDTH; i++)
445 pn[0] = (
unsigned int)b;
446 pn[1] = (
unsigned int)(b >> 32);
447 for (
int i = 2; i <
WIDTH; i++)
453 pn[0] = (
unsigned int)b;
454 pn[1] = (
unsigned int)(b >> 32);
455 for (
int i = 2; i <
WIDTH; i++)
465 explicit uint160(
const std::vector<unsigned char>& vch)
467 if (vch.size() ==
sizeof(
pn))
538 for (
int i = 0; i <
WIDTH; i++)
544 for (
int i = 0; i <
WIDTH; i++)
550 for (
int i = 0; i <
WIDTH; i++)
557 pn[0] = (
unsigned int)b;
558 pn[1] = (
unsigned int)(b >> 32);
559 for (
int i = 2; i <
WIDTH; i++)
565 pn[0] = (
unsigned int)b;
566 pn[1] = (
unsigned int)(b >> 32);
567 for (
int i = 2; i <
WIDTH; i++)
577 explicit uint256(
const std::vector<unsigned char>& vch)
579 if (vch.size() ==
sizeof(
pn))
const uint160 operator-(const base_uint160 &a, const base_uint160 &b)
void SetHex(const char *psz)
const unsigned char * begin() const
void SetHex(const std::string &str)
const uint160 operator>>(const base_uint160 &a, unsigned int shift)
uint256(const basetype &b)
bool operator<=(const base_uint160 &a, const uint160 &b)
base_uint & operator|=(uint64_t b)
base_uint & operator<<=(unsigned int shift)
const base_uint operator-() const
friend bool operator!=(const base_uint &a, uint64_t b)
base_uint & operator+=(const base_uint &b)
uint160(const std::string &str)
friend bool operator<=(const base_uint &a, const base_uint &b)
base_uint & operator|=(const base_uint &b)
base_uint & operator-=(uint64_t b64)
friend bool operator>(const base_uint &a, const base_uint &b)
uint160(const basetype &b)
Base class without constructors for uint256 and uint160.
base_uint & operator+=(uint64_t b64)
void Serialize(Stream &s, int nType, int nVersion) const
uint160 & operator=(uint64_t b)
const uint160 operator<<(const base_uint160 &a, unsigned int shift)
base_uint & operator&=(const base_uint &b)
signed char HexDigit(char c)
uint256(const std::vector< unsigned char > &vch)
base_uint & operator-=(const base_uint &b)
friend bool operator==(const base_uint &a, const base_uint &b)
const signed char p_util_hexdigit[256]
friend bool operator<(const base_uint &a, const base_uint &b)
bool operator!=(const uint160 &a, uint64_t b)
const uint160 operator^(const base_uint160 &a, const base_uint160 &b)
uint256 & operator=(const basetype &b)
uint160 & operator=(const basetype &b)
base_uint< 160 > base_uint160
bool operator<(const base_uint160 &a, const uint160 &b)
const uint160 operator|(const base_uint160 &a, const base_uint160 &b)
base_uint & operator^=(uint64_t b)
std::string GetHex() const
uint256 & operator=(uint64_t b)
const uint160 operator&(const base_uint160 &a, const base_uint160 &b)
friend bool operator!=(const base_uint &a, const base_uint &b)
void * memcpy(void *a, const void *b, size_t c)
unsigned int GetSerializeSize(int nType, int nVersion) const
std::string ToString() const
bool operator==(const uint160 &a, uint64_t b)
bool operator>(const base_uint160 &a, const uint160 &b)
base_uint< 256 > base_uint256
const uint160 operator+(const base_uint160 &a, const base_uint160 &b)
base_uint & operator=(uint64_t b)
uint160(const std::vector< unsigned char > &vch)
const base_uint operator--(int)
const base_uint operator~() const
const base_uint operator++(int)
friend bool operator>=(const base_uint &a, const base_uint &b)
const unsigned char * end() const
void Unserialize(Stream &s, int nType, int nVersion)
unsigned int size() const
uint64_t GetLow64() const
friend bool operator==(const base_uint &a, uint64_t b)
bool operator>=(const base_uint160 &a, const uint160 &b)
uint256(const std::string &str)
base_uint & operator>>=(unsigned int shift)
base_uint & operator^=(const base_uint &b)