7 #ifndef ANONCOIN_UINT256_H
8 #define ANONCOIN_UINT256_H
26 template<
unsigned int BITS>
36 for (
int i = 0; i <
WIDTH; i++)
45 for (
int i = 0; i <
WIDTH; i++)
53 for (
int i = 0; i <
WIDTH; i++)
63 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);
117 for (
int i = 0; i <
WIDTH; i++)
121 for (
int i = 0; i <
WIDTH; i++)
123 if (i+k+1 < WIDTH && shift != 0)
124 pn[i+k+1] |= (a.
pn[i] >> (32-shift));
126 pn[i+k] |= (a.
pn[i] << shift);
134 for (
int i = 0; i <
WIDTH; i++)
138 for (
int i = 0; i <
WIDTH; i++)
140 if (i-k-1 >= 0 && shift != 0)
141 pn[i-k-1] |= (a.
pn[i] << (32-shift));
143 pn[i-k] |= (a.
pn[i] >> shift);
151 for (
int i = 0; i <
WIDTH; i++)
153 uint64_t n = carry + pn[i] + b.
pn[i];
154 pn[i] = n & 0xffffffff;
187 while (++pn[i] == 0 && i <
WIDTH-1)
204 while (--pn[i] == (uint32_t)-1 && i <
WIDTH-1)
222 if (a.
pn[i] < b.
pn[i])
224 else if (a.
pn[i] > b.
pn[i])
234 if (a.
pn[i] < b.
pn[i])
236 else if (a.
pn[i] > b.
pn[i])
246 if (a.
pn[i] > b.
pn[i])
248 else if (a.
pn[i] < b.
pn[i])
258 if (a.
pn[i] > b.
pn[i])
260 else if (a.
pn[i] < b.
pn[i])
269 if (a.
pn[i] != b.
pn[i])
276 if (a.
pn[0] != (
unsigned int)b)
278 if (a.
pn[1] != (
unsigned int)(b >> 32))
300 char psz[
sizeof(
pn)*2 + 1];
301 for (
unsigned int i = 0; i <
sizeof(
pn); i++)
302 sprintf(psz + i*2,
"%02x", ((
unsigned char*)
pn)[
sizeof(pn) - i - 1]);
303 return std::string(psz, psz +
sizeof(pn)*2);
308 memset(pn,0,
sizeof(pn));
311 while (isspace(*psz))
315 if (psz[0] ==
'0' && tolower(psz[1]) ==
'x')
319 const char* pbegin = psz;
323 unsigned char* p1 = (
unsigned char*)pn;
324 unsigned char* pend = p1 +
WIDTH * 4;
325 while (psz >= pbegin && p1 < pend)
330 *p1 |= ((
unsigned char)::
HexDigit(*psz--) << 4);
348 return (
unsigned char*)&pn[0];
353 return (
unsigned char*)&pn[
WIDTH];
358 return (
unsigned char*)&pn[0];
361 const unsigned char*
end()
const
363 return (
unsigned char*)&pn[
WIDTH];
374 return pn[0] | (uint64_t)pn[1] << 32;
383 template<
typename Stream>
385 void Serialize(Stream& s,
int nType,
int nVersion)
const
387 s.write((
char*)pn,
sizeof(pn));
390 template<
typename Stream>
394 s.read((
char*)pn,
sizeof(pn));
427 for (
int i = 0; i <
WIDTH; i++)
433 for (
int i = 0; i <
WIDTH; i++)
439 for (
int i = 0; i <
WIDTH; i++)
446 pn[0] = (
unsigned int)b;
447 pn[1] = (
unsigned int)(b >> 32);
448 for (
int i = 2; i <
WIDTH; i++)
454 pn[0] = (
unsigned int)b;
455 pn[1] = (
unsigned int)(b >> 32);
456 for (
int i = 2; i <
WIDTH; i++)
466 explicit uint160(
const std::vector<unsigned char>& vch)
468 if (vch.size() ==
sizeof(
pn))
539 for (
int i = 0; i <
WIDTH; i++)
545 for (
int i = 0; i <
WIDTH; i++)
551 for (
int i = 0; i <
WIDTH; i++)
558 pn[0] = (
unsigned int)b;
559 pn[1] = (
unsigned int)(b >> 32);
560 for (
int i = 2; i <
WIDTH; i++)
566 pn[0] = (
unsigned int)b;
567 pn[1] = (
unsigned int)(b >> 32);
568 for (
int i = 2; i <
WIDTH; i++)
578 explicit uint256(
const std::vector<unsigned char>& vch)
580 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)