Back to home page

Bitcoin sources

 
 

    


0001 // Copyright (c) 2009-2010 Satoshi Nakamoto
0002 // Copyright (c) 2011 The Bitcoin developers
0003 // Distributed under the MIT/X11 software license, see the accompanying
0004 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
0005 #ifndef BITCOIN_UINT256_H
0006 #define BITCOIN_UINT256_H
0007 
0008 #include "serialize.h"
0009 
0010 #include <limits.h>
0011 #include <string>
0012 #include <vector>
0013 
0014 typedef long long  int64;
0015 typedef unsigned long long  uint64;
0016 
0017 inline int Testuint256AdHoc(std::vector<std::string> vArg);
0018 
0019 
0020 
0021 // We have to keep a separate base class without constructors
0022 // so the compiler will let us use it in a union
0023 template<unsigned int BITS>
0024 class base_uint
0025 {
0026 protected:
0027     enum { WIDTH=BITS/32 };
0028     unsigned int pn[WIDTH];
0029 public:
0030 
0031     bool operator!() const
0032     {
0033         for (int i = 0; i < WIDTH; i++)
0034             if (pn[i] != 0)
0035                 return false;
0036         return true;
0037     }
0038 
0039     const base_uint operator~() const
0040     {
0041         base_uint ret;
0042         for (int i = 0; i < WIDTH; i++)
0043             ret.pn[i] = ~pn[i];
0044         return ret;
0045     }
0046 
0047     const base_uint operator-() const
0048     {
0049         base_uint ret;
0050         for (int i = 0; i < WIDTH; i++)
0051             ret.pn[i] = ~pn[i];
0052         ret++;
0053         return ret;
0054     }
0055 
0056 
0057     base_uint& operator=(uint64 b)
0058     {
0059         pn[0] = (unsigned int)b;
0060         pn[1] = (unsigned int)(b >> 32);
0061         for (int i = 2; i < WIDTH; i++)
0062             pn[i] = 0;
0063         return *this;
0064     }
0065 
0066     base_uint& operator^=(const base_uint& b)
0067     {
0068         for (int i = 0; i < WIDTH; i++)
0069             pn[i] ^= b.pn[i];
0070         return *this;
0071     }
0072 
0073     base_uint& operator&=(const base_uint& b)
0074     {
0075         for (int i = 0; i < WIDTH; i++)
0076             pn[i] &= b.pn[i];
0077         return *this;
0078     }
0079 
0080     base_uint& operator|=(const base_uint& b)
0081     {
0082         for (int i = 0; i < WIDTH; i++)
0083             pn[i] |= b.pn[i];
0084         return *this;
0085     }
0086 
0087     base_uint& operator^=(uint64 b)
0088     {
0089         pn[0] ^= (unsigned int)b;
0090         pn[1] ^= (unsigned int)(b >> 32);
0091         return *this;
0092     }
0093 
0094     base_uint& operator|=(uint64 b)
0095     {
0096         pn[0] |= (unsigned int)b;
0097         pn[1] |= (unsigned int)(b >> 32);
0098         return *this;
0099     }
0100 
0101     base_uint& operator<<=(unsigned int shift)
0102     {
0103         base_uint a(*this);
0104         for (int i = 0; i < WIDTH; i++)
0105             pn[i] = 0;
0106         int k = shift / 32;
0107         shift = shift % 32;
0108         for (int i = 0; i < WIDTH; i++)
0109         {
0110             if (i+k+1 < WIDTH && shift != 0)
0111                 pn[i+k+1] |= (a.pn[i] >> (32-shift));
0112             if (i+k < WIDTH)
0113                 pn[i+k] |= (a.pn[i] << shift);
0114         }
0115         return *this;
0116     }
0117 
0118     base_uint& operator>>=(unsigned int shift)
0119     {
0120         base_uint a(*this);
0121         for (int i = 0; i < WIDTH; i++)
0122             pn[i] = 0;
0123         int k = shift / 32;
0124         shift = shift % 32;
0125         for (int i = 0; i < WIDTH; i++)
0126         {
0127             if (i-k-1 >= 0 && shift != 0)
0128                 pn[i-k-1] |= (a.pn[i] << (32-shift));
0129             if (i-k >= 0)
0130                 pn[i-k] |= (a.pn[i] >> shift);
0131         }
0132         return *this;
0133     }
0134 
0135     base_uint& operator+=(const base_uint& b)
0136     {
0137         uint64 carry = 0;
0138         for (int i = 0; i < WIDTH; i++)
0139         {
0140             uint64 n = carry + pn[i] + b.pn[i];
0141             pn[i] = n & 0xffffffff;
0142             carry = n >> 32;
0143         }
0144         return *this;
0145     }
0146 
0147     base_uint& operator-=(const base_uint& b)
0148     {
0149         *this += -b;
0150         return *this;
0151     }
0152 
0153     base_uint& operator+=(uint64 b64)
0154     {
0155         base_uint b;
0156         b = b64;
0157         *this += b;
0158         return *this;
0159     }
0160 
0161     base_uint& operator-=(uint64 b64)
0162     {
0163         base_uint b;
0164         b = b64;
0165         *this += -b;
0166         return *this;
0167     }
0168 
0169 
0170     base_uint& operator++()
0171     {
0172         // prefix operator
0173         int i = 0;
0174         while (++pn[i] == 0 && i < WIDTH-1)
0175             i++;
0176         return *this;
0177     }
0178 
0179     const base_uint operator++(int)
0180     {
0181         // postfix operator
0182         const base_uint ret = *this;
0183         ++(*this);
0184         return ret;
0185     }
0186 
0187     base_uint& operator--()
0188     {
0189         // prefix operator
0190         int i = 0;
0191         while (--pn[i] == -1 && i < WIDTH-1)
0192             i++;
0193         return *this;
0194     }
0195 
0196     const base_uint operator--(int)
0197     {
0198         // postfix operator
0199         const base_uint ret = *this;
0200         --(*this);
0201         return ret;
0202     }
0203 
0204 
0205     friend inline bool operator<(const base_uint& a, const base_uint& b)
0206     {
0207         for (int i = base_uint::WIDTH-1; i >= 0; i--)
0208         {
0209             if (a.pn[i] < b.pn[i])
0210                 return true;
0211             else if (a.pn[i] > b.pn[i])
0212                 return false;
0213         }
0214         return false;
0215     }
0216 
0217     friend inline bool operator<=(const base_uint& a, const base_uint& b)
0218     {
0219         for (int i = base_uint::WIDTH-1; i >= 0; i--)
0220         {
0221             if (a.pn[i] < b.pn[i])
0222                 return true;
0223             else if (a.pn[i] > b.pn[i])
0224                 return false;
0225         }
0226         return true;
0227     }
0228 
0229     friend inline bool operator>(const base_uint& a, const base_uint& b)
0230     {
0231         for (int i = base_uint::WIDTH-1; i >= 0; i--)
0232         {
0233             if (a.pn[i] > b.pn[i])
0234                 return true;
0235             else if (a.pn[i] < b.pn[i])
0236                 return false;
0237         }
0238         return false;
0239     }
0240 
0241     friend inline bool operator>=(const base_uint& a, const base_uint& b)
0242     {
0243         for (int i = base_uint::WIDTH-1; i >= 0; i--)
0244         {
0245             if (a.pn[i] > b.pn[i])
0246                 return true;
0247             else if (a.pn[i] < b.pn[i])
0248                 return false;
0249         }
0250         return true;
0251     }
0252 
0253     friend inline bool operator==(const base_uint& a, const base_uint& b)
0254     {
0255         for (int i = 0; i < base_uint::WIDTH; i++)
0256             if (a.pn[i] != b.pn[i])
0257                 return false;
0258         return true;
0259     }
0260 
0261     friend inline bool operator==(const base_uint& a, uint64 b)
0262     {
0263         if (a.pn[0] != (unsigned int)b)
0264             return false;
0265         if (a.pn[1] != (unsigned int)(b >> 32))
0266             return false;
0267         for (int i = 2; i < base_uint::WIDTH; i++)
0268             if (a.pn[i] != 0)
0269                 return false;
0270         return true;
0271     }
0272 
0273     friend inline bool operator!=(const base_uint& a, const base_uint& b)
0274     {
0275         return (!(a == b));
0276     }
0277 
0278     friend inline bool operator!=(const base_uint& a, uint64 b)
0279     {
0280         return (!(a == b));
0281     }
0282 
0283 
0284 
0285     std::string GetHex() const
0286     {
0287         char psz[sizeof(pn)*2 + 1];
0288         for (int i = 0; i < sizeof(pn); i++)
0289             sprintf(psz + i*2, "%02x", ((unsigned char*)pn)[sizeof(pn) - i - 1]);
0290         return std::string(psz, psz + sizeof(pn)*2);
0291     }
0292 
0293     void SetHex(const char* psz)
0294     {
0295         for (int i = 0; i < WIDTH; i++)
0296             pn[i] = 0;
0297 
0298         // skip leading spaces
0299         while (isspace(*psz))
0300             psz++;
0301 
0302         // skip 0x
0303         if (psz[0] == '0' && tolower(psz[1]) == 'x')
0304             psz += 2;
0305 
0306         // hex string to uint
0307         static char phexdigit[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0 };
0308         const char* pbegin = psz;
0309         while (phexdigit[*psz] || *psz == '0')
0310             psz++;
0311         psz--;
0312         unsigned char* p1 = (unsigned char*)pn;
0313         unsigned char* pend = p1 + WIDTH * 4;
0314         while (psz >= pbegin && p1 < pend)
0315         {
0316             *p1 = phexdigit[(unsigned char)*psz--];
0317             if (psz >= pbegin)
0318             {
0319                 *p1 |= (phexdigit[(unsigned char)*psz--] << 4);
0320                 p1++;
0321             }
0322         }
0323     }
0324 
0325     void SetHex(const std::string& str)
0326     {
0327         SetHex(str.c_str());
0328     }
0329 
0330     std::string ToString() const
0331     {
0332         return (GetHex());
0333     }
0334 
0335     unsigned char* begin()
0336     {
0337         return (unsigned char*)&pn[0];
0338     }
0339 
0340     unsigned char* end()
0341     {
0342         return (unsigned char*)&pn[WIDTH];
0343     }
0344 
0345     unsigned int size()
0346     {
0347         return sizeof(pn);
0348     }
0349 
0350 
0351     unsigned int GetSerializeSize(int nType=0, int nVersion=VERSION) const
0352     {
0353         return sizeof(pn);
0354     }
0355 
0356     template<typename Stream>
0357     void Serialize(Stream& s, int nType=0, int nVersion=VERSION) const
0358     {
0359         s.write((char*)pn, sizeof(pn));
0360     }
0361 
0362     template<typename Stream>
0363     void Unserialize(Stream& s, int nType=0, int nVersion=VERSION)
0364     {
0365         s.read((char*)pn, sizeof(pn));
0366     }
0367 
0368 
0369     friend class uint160;
0370     friend class uint256;
0371     friend inline int Testuint256AdHoc(std::vector<std::string> vArg);
0372 };
0373 
0374 typedef base_uint<160> base_uint160;
0375 typedef base_uint<256> base_uint256;
0376 
0377 
0378 
0379 //
0380 // uint160 and uint256 could be implemented as templates, but to keep
0381 // compile errors and debugging cleaner, they're copy and pasted.
0382 //
0383 
0384 
0385 
0386 //////////////////////////////////////////////////////////////////////////////
0387 //
0388 // uint160
0389 //
0390 
0391 class uint160 : public base_uint160
0392 {
0393 public:
0394     typedef base_uint160 basetype;
0395 
0396     uint160()
0397     {
0398         for (int i = 0; i < WIDTH; i++)
0399             pn[i] = 0;
0400     }
0401 
0402     uint160(const basetype& b)
0403     {
0404         for (int i = 0; i < WIDTH; i++)
0405             pn[i] = b.pn[i];
0406     }
0407 
0408     uint160& operator=(const basetype& b)
0409     {
0410         for (int i = 0; i < WIDTH; i++)
0411             pn[i] = b.pn[i];
0412         return *this;
0413     }
0414 
0415     uint160(uint64 b)
0416     {
0417         pn[0] = (unsigned int)b;
0418         pn[1] = (unsigned int)(b >> 32);
0419         for (int i = 2; i < WIDTH; i++)
0420             pn[i] = 0;
0421     }
0422 
0423     uint160& operator=(uint64 b)
0424     {
0425         pn[0] = (unsigned int)b;
0426         pn[1] = (unsigned int)(b >> 32);
0427         for (int i = 2; i < WIDTH; i++)
0428             pn[i] = 0;
0429         return *this;
0430     }
0431 
0432     explicit uint160(const std::string& str)
0433     {
0434         SetHex(str);
0435     }
0436 
0437     explicit uint160(const std::vector<unsigned char>& vch)
0438     {
0439         if (vch.size() == sizeof(pn))
0440             memcpy(pn, &vch[0], sizeof(pn));
0441         else
0442             *this = 0;
0443     }
0444 };
0445 
0446 inline bool operator==(const uint160& a, uint64 b)                           { return (base_uint160)a == b; }
0447 inline bool operator!=(const uint160& a, uint64 b)                           { return (base_uint160)a != b; }
0448 inline const uint160 operator<<(const base_uint160& a, unsigned int shift)   { return uint160(a) <<= shift; }
0449 inline const uint160 operator>>(const base_uint160& a, unsigned int shift)   { return uint160(a) >>= shift; }
0450 inline const uint160 operator<<(const uint160& a, unsigned int shift)        { return uint160(a) <<= shift; }
0451 inline const uint160 operator>>(const uint160& a, unsigned int shift)        { return uint160(a) >>= shift; }
0452 
0453 inline const uint160 operator^(const base_uint160& a, const base_uint160& b) { return uint160(a) ^= b; }
0454 inline const uint160 operator&(const base_uint160& a, const base_uint160& b) { return uint160(a) &= b; }
0455 inline const uint160 operator|(const base_uint160& a, const base_uint160& b) { return uint160(a) |= b; }
0456 inline const uint160 operator+(const base_uint160& a, const base_uint160& b) { return uint160(a) += b; }
0457 inline const uint160 operator-(const base_uint160& a, const base_uint160& b) { return uint160(a) -= b; }
0458 
0459 inline bool operator<(const base_uint160& a, const uint160& b)          { return (base_uint160)a <  (base_uint160)b; }
0460 inline bool operator<=(const base_uint160& a, const uint160& b)         { return (base_uint160)a <= (base_uint160)b; }
0461 inline bool operator>(const base_uint160& a, const uint160& b)          { return (base_uint160)a >  (base_uint160)b; }
0462 inline bool operator>=(const base_uint160& a, const uint160& b)         { return (base_uint160)a >= (base_uint160)b; }
0463 inline bool operator==(const base_uint160& a, const uint160& b)         { return (base_uint160)a == (base_uint160)b; }
0464 inline bool operator!=(const base_uint160& a, const uint160& b)         { return (base_uint160)a != (base_uint160)b; }
0465 inline const uint160 operator^(const base_uint160& a, const uint160& b) { return (base_uint160)a ^  (base_uint160)b; }
0466 inline const uint160 operator&(const base_uint160& a, const uint160& b) { return (base_uint160)a &  (base_uint160)b; }
0467 inline const uint160 operator|(const base_uint160& a, const uint160& b) { return (base_uint160)a |  (base_uint160)b; }
0468 inline const uint160 operator+(const base_uint160& a, const uint160& b) { return (base_uint160)a +  (base_uint160)b; }
0469 inline const uint160 operator-(const base_uint160& a, const uint160& b) { return (base_uint160)a -  (base_uint160)b; }
0470 
0471 inline bool operator<(const uint160& a, const base_uint160& b)          { return (base_uint160)a <  (base_uint160)b; }
0472 inline bool operator<=(const uint160& a, const base_uint160& b)         { return (base_uint160)a <= (base_uint160)b; }
0473 inline bool operator>(const uint160& a, const base_uint160& b)          { return (base_uint160)a >  (base_uint160)b; }
0474 inline bool operator>=(const uint160& a, const base_uint160& b)         { return (base_uint160)a >= (base_uint160)b; }
0475 inline bool operator==(const uint160& a, const base_uint160& b)         { return (base_uint160)a == (base_uint160)b; }
0476 inline bool operator!=(const uint160& a, const base_uint160& b)         { return (base_uint160)a != (base_uint160)b; }
0477 inline const uint160 operator^(const uint160& a, const base_uint160& b) { return (base_uint160)a ^  (base_uint160)b; }
0478 inline const uint160 operator&(const uint160& a, const base_uint160& b) { return (base_uint160)a &  (base_uint160)b; }
0479 inline const uint160 operator|(const uint160& a, const base_uint160& b) { return (base_uint160)a |  (base_uint160)b; }
0480 inline const uint160 operator+(const uint160& a, const base_uint160& b) { return (base_uint160)a +  (base_uint160)b; }
0481 inline const uint160 operator-(const uint160& a, const base_uint160& b) { return (base_uint160)a -  (base_uint160)b; }
0482 
0483 inline bool operator<(const uint160& a, const uint160& b)               { return (base_uint160)a <  (base_uint160)b; }
0484 inline bool operator<=(const uint160& a, const uint160& b)              { return (base_uint160)a <= (base_uint160)b; }
0485 inline bool operator>(const uint160& a, const uint160& b)               { return (base_uint160)a >  (base_uint160)b; }
0486 inline bool operator>=(const uint160& a, const uint160& b)              { return (base_uint160)a >= (base_uint160)b; }
0487 inline bool operator==(const uint160& a, const uint160& b)              { return (base_uint160)a == (base_uint160)b; }
0488 inline bool operator!=(const uint160& a, const uint160& b)              { return (base_uint160)a != (base_uint160)b; }
0489 inline const uint160 operator^(const uint160& a, const uint160& b)      { return (base_uint160)a ^  (base_uint160)b; }
0490 inline const uint160 operator&(const uint160& a, const uint160& b)      { return (base_uint160)a &  (base_uint160)b; }
0491 inline const uint160 operator|(const uint160& a, const uint160& b)      { return (base_uint160)a |  (base_uint160)b; }
0492 inline const uint160 operator+(const uint160& a, const uint160& b)      { return (base_uint160)a +  (base_uint160)b; }
0493 inline const uint160 operator-(const uint160& a, const uint160& b)      { return (base_uint160)a -  (base_uint160)b; }
0494 
0495 
0496 
0497 
0498 
0499 
0500 //////////////////////////////////////////////////////////////////////////////
0501 //
0502 // uint256
0503 //
0504 
0505 class uint256 : public base_uint256
0506 {
0507 public:
0508     typedef base_uint256 basetype;
0509 
0510     uint256()
0511     {
0512         for (int i = 0; i < WIDTH; i++)
0513             pn[i] = 0;
0514     }
0515 
0516     uint256(const basetype& b)
0517     {
0518         for (int i = 0; i < WIDTH; i++)
0519             pn[i] = b.pn[i];
0520     }
0521 
0522     uint256& operator=(const basetype& b)
0523     {
0524         for (int i = 0; i < WIDTH; i++)
0525             pn[i] = b.pn[i];
0526         return *this;
0527     }
0528 
0529     uint256(uint64 b)
0530     {
0531         pn[0] = (unsigned int)b;
0532         pn[1] = (unsigned int)(b >> 32);
0533         for (int i = 2; i < WIDTH; i++)
0534             pn[i] = 0;
0535     }
0536 
0537     uint256& operator=(uint64 b)
0538     {
0539         pn[0] = (unsigned int)b;
0540         pn[1] = (unsigned int)(b >> 32);
0541         for (int i = 2; i < WIDTH; i++)
0542             pn[i] = 0;
0543         return *this;
0544     }
0545 
0546     explicit uint256(const std::string& str)
0547     {
0548         SetHex(str);
0549     }
0550 
0551     explicit uint256(const std::vector<unsigned char>& vch)
0552     {
0553         if (vch.size() == sizeof(pn))
0554             memcpy(pn, &vch[0], sizeof(pn));
0555         else
0556             *this = 0;
0557     }
0558 };
0559 
0560 inline bool operator==(const uint256& a, uint64 b)                           { return (base_uint256)a == b; }
0561 inline bool operator!=(const uint256& a, uint64 b)                           { return (base_uint256)a != b; }
0562 inline const uint256 operator<<(const base_uint256& a, unsigned int shift)   { return uint256(a) <<= shift; }
0563 inline const uint256 operator>>(const base_uint256& a, unsigned int shift)   { return uint256(a) >>= shift; }
0564 inline const uint256 operator<<(const uint256& a, unsigned int shift)        { return uint256(a) <<= shift; }
0565 inline const uint256 operator>>(const uint256& a, unsigned int shift)        { return uint256(a) >>= shift; }
0566 
0567 inline const uint256 operator^(const base_uint256& a, const base_uint256& b) { return uint256(a) ^= b; }
0568 inline const uint256 operator&(const base_uint256& a, const base_uint256& b) { return uint256(a) &= b; }
0569 inline const uint256 operator|(const base_uint256& a, const base_uint256& b) { return uint256(a) |= b; }
0570 inline const uint256 operator+(const base_uint256& a, const base_uint256& b) { return uint256(a) += b; }
0571 inline const uint256 operator-(const base_uint256& a, const base_uint256& b) { return uint256(a) -= b; }
0572 
0573 inline bool operator<(const base_uint256& a, const uint256& b)          { return (base_uint256)a <  (base_uint256)b; }
0574 inline bool operator<=(const base_uint256& a, const uint256& b)         { return (base_uint256)a <= (base_uint256)b; }
0575 inline bool operator>(const base_uint256& a, const uint256& b)          { return (base_uint256)a >  (base_uint256)b; }
0576 inline bool operator>=(const base_uint256& a, const uint256& b)         { return (base_uint256)a >= (base_uint256)b; }
0577 inline bool operator==(const base_uint256& a, const uint256& b)         { return (base_uint256)a == (base_uint256)b; }
0578 inline bool operator!=(const base_uint256& a, const uint256& b)         { return (base_uint256)a != (base_uint256)b; }
0579 inline const uint256 operator^(const base_uint256& a, const uint256& b) { return (base_uint256)a ^  (base_uint256)b; }
0580 inline const uint256 operator&(const base_uint256& a, const uint256& b) { return (base_uint256)a &  (base_uint256)b; }
0581 inline const uint256 operator|(const base_uint256& a, const uint256& b) { return (base_uint256)a |  (base_uint256)b; }
0582 inline const uint256 operator+(const base_uint256& a, const uint256& b) { return (base_uint256)a +  (base_uint256)b; }
0583 inline const uint256 operator-(const base_uint256& a, const uint256& b) { return (base_uint256)a -  (base_uint256)b; }
0584 
0585 inline bool operator<(const uint256& a, const base_uint256& b)          { return (base_uint256)a <  (base_uint256)b; }
0586 inline bool operator<=(const uint256& a, const base_uint256& b)         { return (base_uint256)a <= (base_uint256)b; }
0587 inline bool operator>(const uint256& a, const base_uint256& b)          { return (base_uint256)a >  (base_uint256)b; }
0588 inline bool operator>=(const uint256& a, const base_uint256& b)         { return (base_uint256)a >= (base_uint256)b; }
0589 inline bool operator==(const uint256& a, const base_uint256& b)         { return (base_uint256)a == (base_uint256)b; }
0590 inline bool operator!=(const uint256& a, const base_uint256& b)         { return (base_uint256)a != (base_uint256)b; }
0591 inline const uint256 operator^(const uint256& a, const base_uint256& b) { return (base_uint256)a ^  (base_uint256)b; }
0592 inline const uint256 operator&(const uint256& a, const base_uint256& b) { return (base_uint256)a &  (base_uint256)b; }
0593 inline const uint256 operator|(const uint256& a, const base_uint256& b) { return (base_uint256)a |  (base_uint256)b; }
0594 inline const uint256 operator+(const uint256& a, const base_uint256& b) { return (base_uint256)a +  (base_uint256)b; }
0595 inline const uint256 operator-(const uint256& a, const base_uint256& b) { return (base_uint256)a -  (base_uint256)b; }
0596 
0597 inline bool operator<(const uint256& a, const uint256& b)               { return (base_uint256)a <  (base_uint256)b; }
0598 inline bool operator<=(const uint256& a, const uint256& b)              { return (base_uint256)a <= (base_uint256)b; }
0599 inline bool operator>(const uint256& a, const uint256& b)               { return (base_uint256)a >  (base_uint256)b; }
0600 inline bool operator>=(const uint256& a, const uint256& b)              { return (base_uint256)a >= (base_uint256)b; }
0601 inline bool operator==(const uint256& a, const uint256& b)              { return (base_uint256)a == (base_uint256)b; }
0602 inline bool operator!=(const uint256& a, const uint256& b)              { return (base_uint256)a != (base_uint256)b; }
0603 inline const uint256 operator^(const uint256& a, const uint256& b)      { return (base_uint256)a ^  (base_uint256)b; }
0604 inline const uint256 operator&(const uint256& a, const uint256& b)      { return (base_uint256)a &  (base_uint256)b; }
0605 inline const uint256 operator|(const uint256& a, const uint256& b)      { return (base_uint256)a |  (base_uint256)b; }
0606 inline const uint256 operator+(const uint256& a, const uint256& b)      { return (base_uint256)a +  (base_uint256)b; }
0607 inline const uint256 operator-(const uint256& a, const uint256& b)      { return (base_uint256)a -  (base_uint256)b; }
0608 
0609 
0610 
0611 
0612 
0613 
0614 
0615 
0616 
0617 
0618 
0619 
0620 inline int Testuint256AdHoc(std::vector<std::string> vArg)
0621 {
0622     uint256 g(0);
0623 
0624 
0625     printf("%s\n", g.ToString().c_str());
0626     g--;  printf("g--\n");
0627     printf("%s\n", g.ToString().c_str());
0628     g--;  printf("g--\n");
0629     printf("%s\n", g.ToString().c_str());
0630     g++;  printf("g++\n");
0631     printf("%s\n", g.ToString().c_str());
0632     g++;  printf("g++\n");
0633     printf("%s\n", g.ToString().c_str());
0634     g++;  printf("g++\n");
0635     printf("%s\n", g.ToString().c_str());
0636     g++;  printf("g++\n");
0637     printf("%s\n", g.ToString().c_str());
0638 
0639 
0640 
0641     uint256 a(7);
0642     printf("a=7\n");
0643     printf("%s\n", a.ToString().c_str());
0644 
0645     uint256 b;
0646     printf("b undefined\n");
0647     printf("%s\n", b.ToString().c_str());
0648     int c = 3;
0649 
0650     a = c;
0651     a.pn[3] = 15;
0652     printf("%s\n", a.ToString().c_str());
0653     uint256 k(c);
0654 
0655     a = 5;
0656     a.pn[3] = 15;
0657     printf("%s\n", a.ToString().c_str());
0658     b = 1;
0659     b <<= 52;
0660 
0661     a |= b;
0662 
0663     a ^= 0x500;
0664 
0665     printf("a %s\n", a.ToString().c_str());
0666 
0667     a = a | b | (uint256)0x1000;
0668 
0669 
0670     printf("a %s\n", a.ToString().c_str());
0671     printf("b %s\n", b.ToString().c_str());
0672 
0673     a = 0xfffffffe;
0674     a.pn[4] = 9;
0675 
0676     printf("%s\n", a.ToString().c_str());
0677     a++;
0678     printf("%s\n", a.ToString().c_str());
0679     a++;
0680     printf("%s\n", a.ToString().c_str());
0681     a++;
0682     printf("%s\n", a.ToString().c_str());
0683     a++;
0684     printf("%s\n", a.ToString().c_str());
0685 
0686     a--;
0687     printf("%s\n", a.ToString().c_str());
0688     a--;
0689     printf("%s\n", a.ToString().c_str());
0690     a--;
0691     printf("%s\n", a.ToString().c_str());
0692     uint256 d = a--;
0693     printf("%s\n", d.ToString().c_str());
0694     printf("%s\n", a.ToString().c_str());
0695     a--;
0696     printf("%s\n", a.ToString().c_str());
0697     a--;
0698     printf("%s\n", a.ToString().c_str());
0699 
0700     d = a;
0701 
0702     printf("%s\n", d.ToString().c_str());
0703     for (int i = uint256::WIDTH-1; i >= 0; i--) printf("%08x", d.pn[i]); printf("\n");
0704 
0705     uint256 neg = d;
0706     neg = ~neg;
0707     printf("%s\n", neg.ToString().c_str());
0708 
0709 
0710     uint256 e = uint256("0xABCDEF123abcdef12345678909832180000011111111");
0711     printf("\n");
0712     printf("%s\n", e.ToString().c_str());
0713 
0714 
0715     printf("\n");
0716     uint256 x1 = uint256("0xABCDEF123abcdef12345678909832180000011111111");
0717     uint256 x2;
0718     printf("%s\n", x1.ToString().c_str());
0719     for (int i = 0; i < 270; i += 4)
0720     {
0721         x2 = x1 << i;
0722         printf("%s\n", x2.ToString().c_str());
0723     }
0724 
0725     printf("\n");
0726     printf("%s\n", x1.ToString().c_str());
0727     for (int i = 0; i < 270; i += 4)
0728     {
0729         x2 = x1;
0730         x2 >>= i;
0731         printf("%s\n", x2.ToString().c_str());
0732     }
0733 
0734 
0735     for (int i = 0; i < 100; i++)
0736     {
0737         uint256 k = (~uint256(0) >> i);
0738         printf("%s\n", k.ToString().c_str());
0739     }
0740 
0741     for (int i = 0; i < 100; i++)
0742     {
0743         uint256 k = (~uint256(0) << i);
0744         printf("%s\n", k.ToString().c_str());
0745     }
0746 
0747     return (0);
0748 }
0749 
0750 #endif