1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef BITCOIN_UINT256_H
7 #define BITCOIN_UINT256_H
15 extern const signed char p_util_hexdigit[256]; // defined in util.cpp
17 inline signed char HexDigit(char c)
19 return p_util_hexdigit[(unsigned char)c];
22 /** Base class without constructors for uint256 and uint160.
23 * This makes the compiler let you use it in a union.
25 template<unsigned int BITS>
29 enum { WIDTH=BITS/32 };
33 bool operator!() const
35 for (int i = 0; i < WIDTH; i++)
41 const base_uint operator~() const
44 for (int i = 0; i < WIDTH; i++)
49 const base_uint operator-() const
52 for (int i = 0; i < WIDTH; i++)
58 double getdouble() const
62 for (int i = 0; i < WIDTH; i++) {
69 base_uint& operator=(uint64_t b)
71 pn[0] = (unsigned int)b;
72 pn[1] = (unsigned int)(b >> 32);
73 for (int i = 2; i < WIDTH; i++)
78 base_uint& operator^=(const base_uint& b)
80 for (int i = 0; i < WIDTH; i++)
85 base_uint& operator&=(const base_uint& b)
87 for (int i = 0; i < WIDTH; i++)
92 base_uint& operator|=(const base_uint& b)
94 for (int i = 0; i < WIDTH; i++)
99 base_uint& operator^=(uint64_t b)
101 pn[0] ^= (unsigned int)b;
102 pn[1] ^= (unsigned int)(b >> 32);
106 base_uint& operator|=(uint64_t b)
108 pn[0] |= (unsigned int)b;
109 pn[1] |= (unsigned int)(b >> 32);
113 base_uint& operator<<=(unsigned int shift)
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);
130 base_uint& operator>>=(unsigned int 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);
147 base_uint& operator+=(const base_uint& b)
150 for (int i = 0; i < WIDTH; i++)
152 uint64_t n = carry + pn[i] + b.pn[i];
153 pn[i] = n & 0xffffffff;
159 base_uint& operator-=(const base_uint& b)
165 base_uint& operator+=(uint64_t b64)
173 base_uint& operator-=(uint64_t b64)
182 base_uint& operator++()
186 while (++pn[i] == 0 && i < WIDTH-1)
191 const base_uint operator++(int)
194 const base_uint ret = *this;
199 base_uint& operator--()
203 while (--pn[i] == (uint32_t)-1 && i < WIDTH-1)
208 const base_uint operator--(int)
211 const base_uint ret = *this;
217 friend inline bool operator<(const base_uint& a, const base_uint& b)
219 for (int i = base_uint::WIDTH-1; i >= 0; i--)
221 if (a.pn[i] < b.pn[i])
223 else if (a.pn[i] > b.pn[i])
229 friend inline bool operator<=(const base_uint& a, const base_uint& b)
231 for (int i = base_uint::WIDTH-1; i >= 0; i--)
233 if (a.pn[i] < b.pn[i])
235 else if (a.pn[i] > b.pn[i])
241 friend inline bool operator>(const base_uint& a, const base_uint& b)
243 for (int i = base_uint::WIDTH-1; i >= 0; i--)
245 if (a.pn[i] > b.pn[i])
247 else if (a.pn[i] < b.pn[i])
253 friend inline bool operator>=(const base_uint& a, const base_uint& b)
255 for (int i = base_uint::WIDTH-1; i >= 0; i--)
257 if (a.pn[i] > b.pn[i])
259 else if (a.pn[i] < b.pn[i])
265 friend inline bool operator==(const base_uint& a, const base_uint& b)
267 for (int i = 0; i < base_uint::WIDTH; i++)
268 if (a.pn[i] != b.pn[i])
273 friend inline bool operator==(const base_uint& a, uint64_t b)
275 if (a.pn[0] != (unsigned int)b)
277 if (a.pn[1] != (unsigned int)(b >> 32))
279 for (int i = 2; i < base_uint::WIDTH; i++)
285 friend inline bool operator!=(const base_uint& a, const base_uint& b)
290 friend inline bool operator!=(const base_uint& a, uint64_t b)
297 std::string GetHex() const
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);
305 void SetHex(const char* psz)
307 memset(pn,0,sizeof(pn));
309 // skip leading spaces
310 while (isspace(*psz))
314 if (psz[0] == '0' && tolower(psz[1]) == 'x')
317 // hex string to uint
318 const char* pbegin = psz;
319 while (::HexDigit(*psz) != -1)
322 unsigned char* p1 = (unsigned char*)pn;
323 unsigned char* pend = p1 + WIDTH * 4;
324 while (psz >= pbegin && p1 < pend)
326 *p1 = ::HexDigit(*psz--);
329 *p1 |= ((unsigned char)::HexDigit(*psz--) << 4);
335 void SetHex(const std::string& str)
340 std::string ToString() const
345 unsigned char* begin()
347 return (unsigned char*)&pn[0];
352 return (unsigned char*)&pn[WIDTH];
355 const unsigned char* begin() const
357 return (unsigned char*)&pn[0];
360 const unsigned char* end() const
362 return (unsigned char*)&pn[WIDTH];
365 unsigned int size() const
370 uint64_t GetLow64() const
373 return pn[0] | (uint64_t)pn[1] << 32;
376 // unsigned int GetSerializeSize(int nType=0, int nVersion=PROTOCOL_VERSION) const
377 unsigned int GetSerializeSize(int nType, int nVersion) const
382 template<typename Stream>
383 // void Serialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION) const
384 void Serialize(Stream& s, int nType, int nVersion) const
386 s.write((char*)pn, sizeof(pn));
389 template<typename Stream>
390 // void Unserialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION)
391 void Unserialize(Stream& s, int nType, int nVersion)
393 s.read((char*)pn, sizeof(pn));
397 friend class uint160;
398 friend class uint256;
401 typedef base_uint<160> base_uint160;
402 typedef base_uint<256> base_uint256;
407 // uint160 and uint256 could be implemented as templates, but to keep
408 // compile errors and debugging cleaner, they're copy and pasted.
413 //////////////////////////////////////////////////////////////////////////////
418 /** 160-bit unsigned integer */
419 class uint160 : public base_uint160
422 typedef base_uint160 basetype;
426 for (int i = 0; i < WIDTH; i++)
430 uint160(const basetype& b)
432 for (int i = 0; i < WIDTH; i++)
436 uint160& operator=(const basetype& b)
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++)
451 uint160& operator=(uint64_t b)
453 pn[0] = (unsigned int)b;
454 pn[1] = (unsigned int)(b >> 32);
455 for (int i = 2; i < WIDTH; i++)
460 explicit uint160(const std::string& str)
465 explicit uint160(const std::vector<unsigned char>& vch)
467 if (vch.size() == sizeof(pn))
468 memcpy(pn, &vch[0], sizeof(pn));
474 inline bool operator==(const uint160& a, uint64_t b) { return (base_uint160)a == b; }
475 inline bool operator!=(const uint160& a, uint64_t b) { return (base_uint160)a != b; }
476 inline const uint160 operator<<(const base_uint160& a, unsigned int shift) { return uint160(a) <<= shift; }
477 inline const uint160 operator>>(const base_uint160& a, unsigned int shift) { return uint160(a) >>= shift; }
478 inline const uint160 operator<<(const uint160& a, unsigned int shift) { return uint160(a) <<= shift; }
479 inline const uint160 operator>>(const uint160& a, unsigned int shift) { return uint160(a) >>= shift; }
481 inline const uint160 operator^(const base_uint160& a, const base_uint160& b) { return uint160(a) ^= b; }
482 inline const uint160 operator&(const base_uint160& a, const base_uint160& b) { return uint160(a) &= b; }
483 inline const uint160 operator|(const base_uint160& a, const base_uint160& b) { return uint160(a) |= b; }
484 inline const uint160 operator+(const base_uint160& a, const base_uint160& b) { return uint160(a) += b; }
485 inline const uint160 operator-(const base_uint160& a, const base_uint160& b) { return uint160(a) -= b; }
487 inline bool operator<(const base_uint160& a, const uint160& b) { return (base_uint160)a < (base_uint160)b; }
488 inline bool operator<=(const base_uint160& a, const uint160& b) { return (base_uint160)a <= (base_uint160)b; }
489 inline bool operator>(const base_uint160& a, const uint160& b) { return (base_uint160)a > (base_uint160)b; }
490 inline bool operator>=(const base_uint160& a, const uint160& b) { return (base_uint160)a >= (base_uint160)b; }
491 inline bool operator==(const base_uint160& a, const uint160& b) { return (base_uint160)a == (base_uint160)b; }
492 inline bool operator!=(const base_uint160& a, const uint160& b) { return (base_uint160)a != (base_uint160)b; }
493 inline const uint160 operator^(const base_uint160& a, const uint160& b) { return (base_uint160)a ^ (base_uint160)b; }
494 inline const uint160 operator&(const base_uint160& a, const uint160& b) { return (base_uint160)a & (base_uint160)b; }
495 inline const uint160 operator|(const base_uint160& a, const uint160& b) { return (base_uint160)a | (base_uint160)b; }
496 inline const uint160 operator+(const base_uint160& a, const uint160& b) { return (base_uint160)a + (base_uint160)b; }
497 inline const uint160 operator-(const base_uint160& a, const uint160& b) { return (base_uint160)a - (base_uint160)b; }
499 inline bool operator<(const uint160& a, const base_uint160& b) { return (base_uint160)a < (base_uint160)b; }
500 inline bool operator<=(const uint160& a, const base_uint160& b) { return (base_uint160)a <= (base_uint160)b; }
501 inline bool operator>(const uint160& a, const base_uint160& b) { return (base_uint160)a > (base_uint160)b; }
502 inline bool operator>=(const uint160& a, const base_uint160& b) { return (base_uint160)a >= (base_uint160)b; }
503 inline bool operator==(const uint160& a, const base_uint160& b) { return (base_uint160)a == (base_uint160)b; }
504 inline bool operator!=(const uint160& a, const base_uint160& b) { return (base_uint160)a != (base_uint160)b; }
505 inline const uint160 operator^(const uint160& a, const base_uint160& b) { return (base_uint160)a ^ (base_uint160)b; }
506 inline const uint160 operator&(const uint160& a, const base_uint160& b) { return (base_uint160)a & (base_uint160)b; }
507 inline const uint160 operator|(const uint160& a, const base_uint160& b) { return (base_uint160)a | (base_uint160)b; }
508 inline const uint160 operator+(const uint160& a, const base_uint160& b) { return (base_uint160)a + (base_uint160)b; }
509 inline const uint160 operator-(const uint160& a, const base_uint160& b) { return (base_uint160)a - (base_uint160)b; }
511 inline bool operator<(const uint160& a, const uint160& b) { return (base_uint160)a < (base_uint160)b; }
512 inline bool operator<=(const uint160& a, const uint160& b) { return (base_uint160)a <= (base_uint160)b; }
513 inline bool operator>(const uint160& a, const uint160& b) { return (base_uint160)a > (base_uint160)b; }
514 inline bool operator>=(const uint160& a, const uint160& b) { return (base_uint160)a >= (base_uint160)b; }
515 inline bool operator==(const uint160& a, const uint160& b) { return (base_uint160)a == (base_uint160)b; }
516 inline bool operator!=(const uint160& a, const uint160& b) { return (base_uint160)a != (base_uint160)b; }
517 inline const uint160 operator^(const uint160& a, const uint160& b) { return (base_uint160)a ^ (base_uint160)b; }
518 inline const uint160 operator&(const uint160& a, const uint160& b) { return (base_uint160)a & (base_uint160)b; }
519 inline const uint160 operator|(const uint160& a, const uint160& b) { return (base_uint160)a | (base_uint160)b; }
520 inline const uint160 operator+(const uint160& a, const uint160& b) { return (base_uint160)a + (base_uint160)b; }
521 inline const uint160 operator-(const uint160& a, const uint160& b) { return (base_uint160)a - (base_uint160)b; }
525 //////////////////////////////////////////////////////////////////////////////
530 /** 256-bit unsigned integer */
531 class uint256 : public base_uint256
534 typedef base_uint256 basetype;
538 for (int i = 0; i < WIDTH; i++)
542 uint256(const basetype& b)
544 for (int i = 0; i < WIDTH; i++)
548 uint256& operator=(const basetype& b)
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++)
563 uint256& operator=(uint64_t b)
565 pn[0] = (unsigned int)b;
566 pn[1] = (unsigned int)(b >> 32);
567 for (int i = 2; i < WIDTH; i++)
572 explicit uint256(const std::string& str)
577 explicit uint256(const std::vector<unsigned char>& vch)
579 if (vch.size() == sizeof(pn))
580 memcpy(pn, &vch[0], sizeof(pn));
586 inline bool operator==(const uint256& a, uint64_t b) { return (base_uint256)a == b; }
587 inline bool operator!=(const uint256& a, uint64_t b) { return (base_uint256)a != b; }
588 inline const uint256 operator<<(const base_uint256& a, unsigned int shift) { return uint256(a) <<= shift; }
589 inline const uint256 operator>>(const base_uint256& a, unsigned int shift) { return uint256(a) >>= shift; }
590 inline const uint256 operator<<(const uint256& a, unsigned int shift) { return uint256(a) <<= shift; }
591 inline const uint256 operator>>(const uint256& a, unsigned int shift) { return uint256(a) >>= shift; }
593 inline const uint256 operator^(const base_uint256& a, const base_uint256& b) { return uint256(a) ^= b; }
594 inline const uint256 operator&(const base_uint256& a, const base_uint256& b) { return uint256(a) &= b; }
595 inline const uint256 operator|(const base_uint256& a, const base_uint256& b) { return uint256(a) |= b; }
596 inline const uint256 operator+(const base_uint256& a, const base_uint256& b) { return uint256(a) += b; }
597 inline const uint256 operator-(const base_uint256& a, const base_uint256& b) { return uint256(a) -= b; }
599 inline bool operator<(const base_uint256& a, const uint256& b) { return (base_uint256)a < (base_uint256)b; }
600 inline bool operator<=(const base_uint256& a, const uint256& b) { return (base_uint256)a <= (base_uint256)b; }
601 inline bool operator>(const base_uint256& a, const uint256& b) { return (base_uint256)a > (base_uint256)b; }
602 inline bool operator>=(const base_uint256& a, const uint256& b) { return (base_uint256)a >= (base_uint256)b; }
603 inline bool operator==(const base_uint256& a, const uint256& b) { return (base_uint256)a == (base_uint256)b; }
604 inline bool operator!=(const base_uint256& a, const uint256& b) { return (base_uint256)a != (base_uint256)b; }
605 inline const uint256 operator^(const base_uint256& a, const uint256& b) { return (base_uint256)a ^ (base_uint256)b; }
606 inline const uint256 operator&(const base_uint256& a, const uint256& b) { return (base_uint256)a & (base_uint256)b; }
607 inline const uint256 operator|(const base_uint256& a, const uint256& b) { return (base_uint256)a | (base_uint256)b; }
608 inline const uint256 operator+(const base_uint256& a, const uint256& b) { return (base_uint256)a + (base_uint256)b; }
609 inline const uint256 operator-(const base_uint256& a, const uint256& b) { return (base_uint256)a - (base_uint256)b; }
611 inline bool operator<(const uint256& a, const base_uint256& b) { return (base_uint256)a < (base_uint256)b; }
612 inline bool operator<=(const uint256& a, const base_uint256& b) { return (base_uint256)a <= (base_uint256)b; }
613 inline bool operator>(const uint256& a, const base_uint256& b) { return (base_uint256)a > (base_uint256)b; }
614 inline bool operator>=(const uint256& a, const base_uint256& b) { return (base_uint256)a >= (base_uint256)b; }
615 inline bool operator==(const uint256& a, const base_uint256& b) { return (base_uint256)a == (base_uint256)b; }
616 inline bool operator!=(const uint256& a, const base_uint256& b) { return (base_uint256)a != (base_uint256)b; }
617 inline const uint256 operator^(const uint256& a, const base_uint256& b) { return (base_uint256)a ^ (base_uint256)b; }
618 inline const uint256 operator&(const uint256& a, const base_uint256& b) { return (base_uint256)a & (base_uint256)b; }
619 inline const uint256 operator|(const uint256& a, const base_uint256& b) { return (base_uint256)a | (base_uint256)b; }
620 inline const uint256 operator+(const uint256& a, const base_uint256& b) { return (base_uint256)a + (base_uint256)b; }
621 inline const uint256 operator-(const uint256& a, const base_uint256& b) { return (base_uint256)a - (base_uint256)b; }
623 inline bool operator<(const uint256& a, const uint256& b) { return (base_uint256)a < (base_uint256)b; }
624 inline bool operator<=(const uint256& a, const uint256& b) { return (base_uint256)a <= (base_uint256)b; }
625 inline bool operator>(const uint256& a, const uint256& b) { return (base_uint256)a > (base_uint256)b; }
626 inline bool operator>=(const uint256& a, const uint256& b) { return (base_uint256)a >= (base_uint256)b; }
627 inline bool operator==(const uint256& a, const uint256& b) { return (base_uint256)a == (base_uint256)b; }
628 inline bool operator!=(const uint256& a, const uint256& b) { return (base_uint256)a != (base_uint256)b; }
629 inline const uint256 operator^(const uint256& a, const uint256& b) { return (base_uint256)a ^ (base_uint256)b; }
630 inline const uint256 operator&(const uint256& a, const uint256& b) { return (base_uint256)a & (base_uint256)b; }
631 inline const uint256 operator|(const uint256& a, const uint256& b) { return (base_uint256)a | (base_uint256)b; }
632 inline const uint256 operator+(const uint256& a, const uint256& b) { return (base_uint256)a + (base_uint256)b; }
633 inline const uint256 operator-(const uint256& a, const uint256& b) { return (base_uint256)a - (base_uint256)b; }