-
+ 821F3AF7A2CE7BB860659E1B5833DA377C539C5E6207FFB48C042673DADA79B37002E6BD336E54C940CEE120BDB132B62966D30732E4AFEFDD46261EFF889BF8bitcoin/src/uint256.h(0 . 0)(1 . 759)
21901 // Copyright (c) 2009-2010 Satoshi Nakamoto
21902 // Copyright (c) 2011 The Bitcoin developers
21903 // Distributed under the MIT/X11 software license, see the accompanying
21904 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
21905 #ifndef BITCOIN_UINT256_H
21906 #define BITCOIN_UINT256_H
21907
21908 #include "serialize.h"
21909
21910 #include <limits.h>
21911 #include <string>
21912 #include <vector>
21913
21914 #if defined(_MSC_VER) || defined(__BORLANDC__)
21915 typedef __int64 int64;
21916 typedef unsigned __int64 uint64;
21917 #else
21918 typedef long long int64;
21919 typedef unsigned long long uint64;
21920 #endif
21921 #if defined(_MSC_VER) && _MSC_VER < 1300
21922 #define for if (false) ; else for
21923 #endif
21924
21925
21926 inline int Testuint256AdHoc(std::vector<std::string> vArg);
21927
21928
21929
21930 // We have to keep a separate base class without constructors
21931 // so the compiler will let us use it in a union
21932 template<unsigned int BITS>
21933 class base_uint
21934 {
21935 protected:
21936 enum { WIDTH=BITS/32 };
21937 unsigned int pn[WIDTH];
21938 public:
21939
21940 bool operator!() const
21941 {
21942 for (int i = 0; i < WIDTH; i++)
21943 if (pn[i] != 0)
21944 return false;
21945 return true;
21946 }
21947
21948 const base_uint operator~() const
21949 {
21950 base_uint ret;
21951 for (int i = 0; i < WIDTH; i++)
21952 ret.pn[i] = ~pn[i];
21953 return ret;
21954 }
21955
21956 const base_uint operator-() const
21957 {
21958 base_uint ret;
21959 for (int i = 0; i < WIDTH; i++)
21960 ret.pn[i] = ~pn[i];
21961 ret++;
21962 return ret;
21963 }
21964
21965
21966 base_uint& operator=(uint64 b)
21967 {
21968 pn[0] = (unsigned int)b;
21969 pn[1] = (unsigned int)(b >> 32);
21970 for (int i = 2; i < WIDTH; i++)
21971 pn[i] = 0;
21972 return *this;
21973 }
21974
21975 base_uint& operator^=(const base_uint& b)
21976 {
21977 for (int i = 0; i < WIDTH; i++)
21978 pn[i] ^= b.pn[i];
21979 return *this;
21980 }
21981
21982 base_uint& operator&=(const base_uint& b)
21983 {
21984 for (int i = 0; i < WIDTH; i++)
21985 pn[i] &= b.pn[i];
21986 return *this;
21987 }
21988
21989 base_uint& operator|=(const base_uint& b)
21990 {
21991 for (int i = 0; i < WIDTH; i++)
21992 pn[i] |= b.pn[i];
21993 return *this;
21994 }
21995
21996 base_uint& operator^=(uint64 b)
21997 {
21998 pn[0] ^= (unsigned int)b;
21999 pn[1] ^= (unsigned int)(b >> 32);
22000 return *this;
22001 }
22002
22003 base_uint& operator|=(uint64 b)
22004 {
22005 pn[0] |= (unsigned int)b;
22006 pn[1] |= (unsigned int)(b >> 32);
22007 return *this;
22008 }
22009
22010 base_uint& operator<<=(unsigned int shift)
22011 {
22012 base_uint a(*this);
22013 for (int i = 0; i < WIDTH; i++)
22014 pn[i] = 0;
22015 int k = shift / 32;
22016 shift = shift % 32;
22017 for (int i = 0; i < WIDTH; i++)
22018 {
22019 if (i+k+1 < WIDTH && shift != 0)
22020 pn[i+k+1] |= (a.pn[i] >> (32-shift));
22021 if (i+k < WIDTH)
22022 pn[i+k] |= (a.pn[i] << shift);
22023 }
22024 return *this;
22025 }
22026
22027 base_uint& operator>>=(unsigned int shift)
22028 {
22029 base_uint a(*this);
22030 for (int i = 0; i < WIDTH; i++)
22031 pn[i] = 0;
22032 int k = shift / 32;
22033 shift = shift % 32;
22034 for (int i = 0; i < WIDTH; i++)
22035 {
22036 if (i-k-1 >= 0 && shift != 0)
22037 pn[i-k-1] |= (a.pn[i] << (32-shift));
22038 if (i-k >= 0)
22039 pn[i-k] |= (a.pn[i] >> shift);
22040 }
22041 return *this;
22042 }
22043
22044 base_uint& operator+=(const base_uint& b)
22045 {
22046 uint64 carry = 0;
22047 for (int i = 0; i < WIDTH; i++)
22048 {
22049 uint64 n = carry + pn[i] + b.pn[i];
22050 pn[i] = n & 0xffffffff;
22051 carry = n >> 32;
22052 }
22053 return *this;
22054 }
22055
22056 base_uint& operator-=(const base_uint& b)
22057 {
22058 *this += -b;
22059 return *this;
22060 }
22061
22062 base_uint& operator+=(uint64 b64)
22063 {
22064 base_uint b;
22065 b = b64;
22066 *this += b;
22067 return *this;
22068 }
22069
22070 base_uint& operator-=(uint64 b64)
22071 {
22072 base_uint b;
22073 b = b64;
22074 *this += -b;
22075 return *this;
22076 }
22077
22078
22079 base_uint& operator++()
22080 {
22081 // prefix operator
22082 int i = 0;
22083 while (++pn[i] == 0 && i < WIDTH-1)
22084 i++;
22085 return *this;
22086 }
22087
22088 const base_uint operator++(int)
22089 {
22090 // postfix operator
22091 const base_uint ret = *this;
22092 ++(*this);
22093 return ret;
22094 }
22095
22096 base_uint& operator--()
22097 {
22098 // prefix operator
22099 int i = 0;
22100 while (--pn[i] == -1 && i < WIDTH-1)
22101 i++;
22102 return *this;
22103 }
22104
22105 const base_uint operator--(int)
22106 {
22107 // postfix operator
22108 const base_uint ret = *this;
22109 --(*this);
22110 return ret;
22111 }
22112
22113
22114 friend inline bool operator<(const base_uint& a, const base_uint& b)
22115 {
22116 for (int i = base_uint::WIDTH-1; i >= 0; i--)
22117 {
22118 if (a.pn[i] < b.pn[i])
22119 return true;
22120 else if (a.pn[i] > b.pn[i])
22121 return false;
22122 }
22123 return false;
22124 }
22125
22126 friend inline bool operator<=(const base_uint& a, const base_uint& b)
22127 {
22128 for (int i = base_uint::WIDTH-1; i >= 0; i--)
22129 {
22130 if (a.pn[i] < b.pn[i])
22131 return true;
22132 else if (a.pn[i] > b.pn[i])
22133 return false;
22134 }
22135 return true;
22136 }
22137
22138 friend inline bool operator>(const base_uint& a, const base_uint& b)
22139 {
22140 for (int i = base_uint::WIDTH-1; i >= 0; i--)
22141 {
22142 if (a.pn[i] > b.pn[i])
22143 return true;
22144 else if (a.pn[i] < b.pn[i])
22145 return false;
22146 }
22147 return false;
22148 }
22149
22150 friend inline bool operator>=(const base_uint& a, const base_uint& b)
22151 {
22152 for (int i = base_uint::WIDTH-1; i >= 0; i--)
22153 {
22154 if (a.pn[i] > b.pn[i])
22155 return true;
22156 else if (a.pn[i] < b.pn[i])
22157 return false;
22158 }
22159 return true;
22160 }
22161
22162 friend inline bool operator==(const base_uint& a, const base_uint& b)
22163 {
22164 for (int i = 0; i < base_uint::WIDTH; i++)
22165 if (a.pn[i] != b.pn[i])
22166 return false;
22167 return true;
22168 }
22169
22170 friend inline bool operator==(const base_uint& a, uint64 b)
22171 {
22172 if (a.pn[0] != (unsigned int)b)
22173 return false;
22174 if (a.pn[1] != (unsigned int)(b >> 32))
22175 return false;
22176 for (int i = 2; i < base_uint::WIDTH; i++)
22177 if (a.pn[i] != 0)
22178 return false;
22179 return true;
22180 }
22181
22182 friend inline bool operator!=(const base_uint& a, const base_uint& b)
22183 {
22184 return (!(a == b));
22185 }
22186
22187 friend inline bool operator!=(const base_uint& a, uint64 b)
22188 {
22189 return (!(a == b));
22190 }
22191
22192
22193
22194 std::string GetHex() const
22195 {
22196 char psz[sizeof(pn)*2 + 1];
22197 for (int i = 0; i < sizeof(pn); i++)
22198 sprintf(psz + i*2, "%02x", ((unsigned char*)pn)[sizeof(pn) - i - 1]);
22199 return std::string(psz, psz + sizeof(pn)*2);
22200 }
22201
22202 void SetHex(const char* psz)
22203 {
22204 for (int i = 0; i < WIDTH; i++)
22205 pn[i] = 0;
22206
22207 // skip leading spaces
22208 while (isspace(*psz))
22209 psz++;
22210
22211 // skip 0x
22212 if (psz[0] == '0' && tolower(psz[1]) == 'x')
22213 psz += 2;
22214
22215 // hex string to uint
22216 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 };
22217 const char* pbegin = psz;
22218 while (phexdigit[*psz] || *psz == '0')
22219 psz++;
22220 psz--;
22221 unsigned char* p1 = (unsigned char*)pn;
22222 unsigned char* pend = p1 + WIDTH * 4;
22223 while (psz >= pbegin && p1 < pend)
22224 {
22225 *p1 = phexdigit[(unsigned char)*psz--];
22226 if (psz >= pbegin)
22227 {
22228 *p1 |= (phexdigit[(unsigned char)*psz--] << 4);
22229 p1++;
22230 }
22231 }
22232 }
22233
22234 void SetHex(const std::string& str)
22235 {
22236 SetHex(str.c_str());
22237 }
22238
22239 std::string ToString() const
22240 {
22241 return (GetHex());
22242 }
22243
22244 unsigned char* begin()
22245 {
22246 return (unsigned char*)&pn[0];
22247 }
22248
22249 unsigned char* end()
22250 {
22251 return (unsigned char*)&pn[WIDTH];
22252 }
22253
22254 unsigned int size()
22255 {
22256 return sizeof(pn);
22257 }
22258
22259
22260 unsigned int GetSerializeSize(int nType=0, int nVersion=VERSION) const
22261 {
22262 return sizeof(pn);
22263 }
22264
22265 template<typename Stream>
22266 void Serialize(Stream& s, int nType=0, int nVersion=VERSION) const
22267 {
22268 s.write((char*)pn, sizeof(pn));
22269 }
22270
22271 template<typename Stream>
22272 void Unserialize(Stream& s, int nType=0, int nVersion=VERSION)
22273 {
22274 s.read((char*)pn, sizeof(pn));
22275 }
22276
22277
22278 friend class uint160;
22279 friend class uint256;
22280 friend inline int Testuint256AdHoc(std::vector<std::string> vArg);
22281 };
22282
22283 typedef base_uint<160> base_uint160;
22284 typedef base_uint<256> base_uint256;
22285
22286
22287
22288 //
22289 // uint160 and uint256 could be implemented as templates, but to keep
22290 // compile errors and debugging cleaner, they're copy and pasted.
22291 //
22292
22293
22294
22295 //////////////////////////////////////////////////////////////////////////////
22296 //
22297 // uint160
22298 //
22299
22300 class uint160 : public base_uint160
22301 {
22302 public:
22303 typedef base_uint160 basetype;
22304
22305 uint160()
22306 {
22307 for (int i = 0; i < WIDTH; i++)
22308 pn[i] = 0;
22309 }
22310
22311 uint160(const basetype& b)
22312 {
22313 for (int i = 0; i < WIDTH; i++)
22314 pn[i] = b.pn[i];
22315 }
22316
22317 uint160& operator=(const basetype& b)
22318 {
22319 for (int i = 0; i < WIDTH; i++)
22320 pn[i] = b.pn[i];
22321 return *this;
22322 }
22323
22324 uint160(uint64 b)
22325 {
22326 pn[0] = (unsigned int)b;
22327 pn[1] = (unsigned int)(b >> 32);
22328 for (int i = 2; i < WIDTH; i++)
22329 pn[i] = 0;
22330 }
22331
22332 uint160& operator=(uint64 b)
22333 {
22334 pn[0] = (unsigned int)b;
22335 pn[1] = (unsigned int)(b >> 32);
22336 for (int i = 2; i < WIDTH; i++)
22337 pn[i] = 0;
22338 return *this;
22339 }
22340
22341 explicit uint160(const std::string& str)
22342 {
22343 SetHex(str);
22344 }
22345
22346 explicit uint160(const std::vector<unsigned char>& vch)
22347 {
22348 if (vch.size() == sizeof(pn))
22349 memcpy(pn, &vch[0], sizeof(pn));
22350 else
22351 *this = 0;
22352 }
22353 };
22354
22355 inline bool operator==(const uint160& a, uint64 b) { return (base_uint160)a == b; }
22356 inline bool operator!=(const uint160& a, uint64 b) { return (base_uint160)a != b; }
22357 inline const uint160 operator<<(const base_uint160& a, unsigned int shift) { return uint160(a) <<= shift; }
22358 inline const uint160 operator>>(const base_uint160& a, unsigned int shift) { return uint160(a) >>= shift; }
22359 inline const uint160 operator<<(const uint160& a, unsigned int shift) { return uint160(a) <<= shift; }
22360 inline const uint160 operator>>(const uint160& a, unsigned int shift) { return uint160(a) >>= shift; }
22361
22362 inline const uint160 operator^(const base_uint160& a, const base_uint160& b) { return uint160(a) ^= b; }
22363 inline const uint160 operator&(const base_uint160& a, const base_uint160& b) { return uint160(a) &= b; }
22364 inline const uint160 operator|(const base_uint160& a, const base_uint160& b) { return uint160(a) |= b; }
22365 inline const uint160 operator+(const base_uint160& a, const base_uint160& b) { return uint160(a) += b; }
22366 inline const uint160 operator-(const base_uint160& a, const base_uint160& b) { return uint160(a) -= b; }
22367
22368 inline bool operator<(const base_uint160& a, const uint160& b) { return (base_uint160)a < (base_uint160)b; }
22369 inline bool operator<=(const base_uint160& a, const uint160& b) { return (base_uint160)a <= (base_uint160)b; }
22370 inline bool operator>(const base_uint160& a, const uint160& b) { return (base_uint160)a > (base_uint160)b; }
22371 inline bool operator>=(const base_uint160& a, const uint160& b) { return (base_uint160)a >= (base_uint160)b; }
22372 inline bool operator==(const base_uint160& a, const uint160& b) { return (base_uint160)a == (base_uint160)b; }
22373 inline bool operator!=(const base_uint160& a, const uint160& b) { return (base_uint160)a != (base_uint160)b; }
22374 inline const uint160 operator^(const base_uint160& a, const uint160& b) { return (base_uint160)a ^ (base_uint160)b; }
22375 inline const uint160 operator&(const base_uint160& a, const uint160& b) { return (base_uint160)a & (base_uint160)b; }
22376 inline const uint160 operator|(const base_uint160& a, const uint160& b) { return (base_uint160)a | (base_uint160)b; }
22377 inline const uint160 operator+(const base_uint160& a, const uint160& b) { return (base_uint160)a + (base_uint160)b; }
22378 inline const uint160 operator-(const base_uint160& a, const uint160& b) { return (base_uint160)a - (base_uint160)b; }
22379
22380 inline bool operator<(const uint160& a, const base_uint160& b) { return (base_uint160)a < (base_uint160)b; }
22381 inline bool operator<=(const uint160& a, const base_uint160& b) { return (base_uint160)a <= (base_uint160)b; }
22382 inline bool operator>(const uint160& a, const base_uint160& b) { return (base_uint160)a > (base_uint160)b; }
22383 inline bool operator>=(const uint160& a, const base_uint160& b) { return (base_uint160)a >= (base_uint160)b; }
22384 inline bool operator==(const uint160& a, const base_uint160& b) { return (base_uint160)a == (base_uint160)b; }
22385 inline bool operator!=(const uint160& a, const base_uint160& b) { return (base_uint160)a != (base_uint160)b; }
22386 inline const uint160 operator^(const uint160& a, const base_uint160& b) { return (base_uint160)a ^ (base_uint160)b; }
22387 inline const uint160 operator&(const uint160& a, const base_uint160& b) { return (base_uint160)a & (base_uint160)b; }
22388 inline const uint160 operator|(const uint160& a, const base_uint160& b) { return (base_uint160)a | (base_uint160)b; }
22389 inline const uint160 operator+(const uint160& a, const base_uint160& b) { return (base_uint160)a + (base_uint160)b; }
22390 inline const uint160 operator-(const uint160& a, const base_uint160& b) { return (base_uint160)a - (base_uint160)b; }
22391
22392 inline bool operator<(const uint160& a, const uint160& b) { return (base_uint160)a < (base_uint160)b; }
22393 inline bool operator<=(const uint160& a, const uint160& b) { return (base_uint160)a <= (base_uint160)b; }
22394 inline bool operator>(const uint160& a, const uint160& b) { return (base_uint160)a > (base_uint160)b; }
22395 inline bool operator>=(const uint160& a, const uint160& b) { return (base_uint160)a >= (base_uint160)b; }
22396 inline bool operator==(const uint160& a, const uint160& b) { return (base_uint160)a == (base_uint160)b; }
22397 inline bool operator!=(const uint160& a, const uint160& b) { return (base_uint160)a != (base_uint160)b; }
22398 inline const uint160 operator^(const uint160& a, const uint160& b) { return (base_uint160)a ^ (base_uint160)b; }
22399 inline const uint160 operator&(const uint160& a, const uint160& b) { return (base_uint160)a & (base_uint160)b; }
22400 inline const uint160 operator|(const uint160& a, const uint160& b) { return (base_uint160)a | (base_uint160)b; }
22401 inline const uint160 operator+(const uint160& a, const uint160& b) { return (base_uint160)a + (base_uint160)b; }
22402 inline const uint160 operator-(const uint160& a, const uint160& b) { return (base_uint160)a - (base_uint160)b; }
22403
22404
22405
22406
22407
22408
22409 //////////////////////////////////////////////////////////////////////////////
22410 //
22411 // uint256
22412 //
22413
22414 class uint256 : public base_uint256
22415 {
22416 public:
22417 typedef base_uint256 basetype;
22418
22419 uint256()
22420 {
22421 for (int i = 0; i < WIDTH; i++)
22422 pn[i] = 0;
22423 }
22424
22425 uint256(const basetype& b)
22426 {
22427 for (int i = 0; i < WIDTH; i++)
22428 pn[i] = b.pn[i];
22429 }
22430
22431 uint256& operator=(const basetype& b)
22432 {
22433 for (int i = 0; i < WIDTH; i++)
22434 pn[i] = b.pn[i];
22435 return *this;
22436 }
22437
22438 uint256(uint64 b)
22439 {
22440 pn[0] = (unsigned int)b;
22441 pn[1] = (unsigned int)(b >> 32);
22442 for (int i = 2; i < WIDTH; i++)
22443 pn[i] = 0;
22444 }
22445
22446 uint256& operator=(uint64 b)
22447 {
22448 pn[0] = (unsigned int)b;
22449 pn[1] = (unsigned int)(b >> 32);
22450 for (int i = 2; i < WIDTH; i++)
22451 pn[i] = 0;
22452 return *this;
22453 }
22454
22455 explicit uint256(const std::string& str)
22456 {
22457 SetHex(str);
22458 }
22459
22460 explicit uint256(const std::vector<unsigned char>& vch)
22461 {
22462 if (vch.size() == sizeof(pn))
22463 memcpy(pn, &vch[0], sizeof(pn));
22464 else
22465 *this = 0;
22466 }
22467 };
22468
22469 inline bool operator==(const uint256& a, uint64 b) { return (base_uint256)a == b; }
22470 inline bool operator!=(const uint256& a, uint64 b) { return (base_uint256)a != b; }
22471 inline const uint256 operator<<(const base_uint256& a, unsigned int shift) { return uint256(a) <<= shift; }
22472 inline const uint256 operator>>(const base_uint256& a, unsigned int shift) { return uint256(a) >>= shift; }
22473 inline const uint256 operator<<(const uint256& a, unsigned int shift) { return uint256(a) <<= shift; }
22474 inline const uint256 operator>>(const uint256& a, unsigned int shift) { return uint256(a) >>= shift; }
22475
22476 inline const uint256 operator^(const base_uint256& a, const base_uint256& b) { return uint256(a) ^= b; }
22477 inline const uint256 operator&(const base_uint256& a, const base_uint256& b) { return uint256(a) &= b; }
22478 inline const uint256 operator|(const base_uint256& a, const base_uint256& b) { return uint256(a) |= b; }
22479 inline const uint256 operator+(const base_uint256& a, const base_uint256& b) { return uint256(a) += b; }
22480 inline const uint256 operator-(const base_uint256& a, const base_uint256& b) { return uint256(a) -= b; }
22481
22482 inline bool operator<(const base_uint256& a, const uint256& b) { return (base_uint256)a < (base_uint256)b; }
22483 inline bool operator<=(const base_uint256& a, const uint256& b) { return (base_uint256)a <= (base_uint256)b; }
22484 inline bool operator>(const base_uint256& a, const uint256& b) { return (base_uint256)a > (base_uint256)b; }
22485 inline bool operator>=(const base_uint256& a, const uint256& b) { return (base_uint256)a >= (base_uint256)b; }
22486 inline bool operator==(const base_uint256& a, const uint256& b) { return (base_uint256)a == (base_uint256)b; }
22487 inline bool operator!=(const base_uint256& a, const uint256& b) { return (base_uint256)a != (base_uint256)b; }
22488 inline const uint256 operator^(const base_uint256& a, const uint256& b) { return (base_uint256)a ^ (base_uint256)b; }
22489 inline const uint256 operator&(const base_uint256& a, const uint256& b) { return (base_uint256)a & (base_uint256)b; }
22490 inline const uint256 operator|(const base_uint256& a, const uint256& b) { return (base_uint256)a | (base_uint256)b; }
22491 inline const uint256 operator+(const base_uint256& a, const uint256& b) { return (base_uint256)a + (base_uint256)b; }
22492 inline const uint256 operator-(const base_uint256& a, const uint256& b) { return (base_uint256)a - (base_uint256)b; }
22493
22494 inline bool operator<(const uint256& a, const base_uint256& b) { return (base_uint256)a < (base_uint256)b; }
22495 inline bool operator<=(const uint256& a, const base_uint256& b) { return (base_uint256)a <= (base_uint256)b; }
22496 inline bool operator>(const uint256& a, const base_uint256& b) { return (base_uint256)a > (base_uint256)b; }
22497 inline bool operator>=(const uint256& a, const base_uint256& b) { return (base_uint256)a >= (base_uint256)b; }
22498 inline bool operator==(const uint256& a, const base_uint256& b) { return (base_uint256)a == (base_uint256)b; }
22499 inline bool operator!=(const uint256& a, const base_uint256& b) { return (base_uint256)a != (base_uint256)b; }
22500 inline const uint256 operator^(const uint256& a, const base_uint256& b) { return (base_uint256)a ^ (base_uint256)b; }
22501 inline const uint256 operator&(const uint256& a, const base_uint256& b) { return (base_uint256)a & (base_uint256)b; }
22502 inline const uint256 operator|(const uint256& a, const base_uint256& b) { return (base_uint256)a | (base_uint256)b; }
22503 inline const uint256 operator+(const uint256& a, const base_uint256& b) { return (base_uint256)a + (base_uint256)b; }
22504 inline const uint256 operator-(const uint256& a, const base_uint256& b) { return (base_uint256)a - (base_uint256)b; }
22505
22506 inline bool operator<(const uint256& a, const uint256& b) { return (base_uint256)a < (base_uint256)b; }
22507 inline bool operator<=(const uint256& a, const uint256& b) { return (base_uint256)a <= (base_uint256)b; }
22508 inline bool operator>(const uint256& a, const uint256& b) { return (base_uint256)a > (base_uint256)b; }
22509 inline bool operator>=(const uint256& a, const uint256& b) { return (base_uint256)a >= (base_uint256)b; }
22510 inline bool operator==(const uint256& a, const uint256& b) { return (base_uint256)a == (base_uint256)b; }
22511 inline bool operator!=(const uint256& a, const uint256& b) { return (base_uint256)a != (base_uint256)b; }
22512 inline const uint256 operator^(const uint256& a, const uint256& b) { return (base_uint256)a ^ (base_uint256)b; }
22513 inline const uint256 operator&(const uint256& a, const uint256& b) { return (base_uint256)a & (base_uint256)b; }
22514 inline const uint256 operator|(const uint256& a, const uint256& b) { return (base_uint256)a | (base_uint256)b; }
22515 inline const uint256 operator+(const uint256& a, const uint256& b) { return (base_uint256)a + (base_uint256)b; }
22516 inline const uint256 operator-(const uint256& a, const uint256& b) { return (base_uint256)a - (base_uint256)b; }
22517
22518
22519
22520
22521
22522
22523
22524
22525
22526
22527
22528
22529 inline int Testuint256AdHoc(std::vector<std::string> vArg)
22530 {
22531 uint256 g(0);
22532
22533
22534 printf("%s\n", g.ToString().c_str());
22535 g--; printf("g--\n");
22536 printf("%s\n", g.ToString().c_str());
22537 g--; printf("g--\n");
22538 printf("%s\n", g.ToString().c_str());
22539 g++; printf("g++\n");
22540 printf("%s\n", g.ToString().c_str());
22541 g++; printf("g++\n");
22542 printf("%s\n", g.ToString().c_str());
22543 g++; printf("g++\n");
22544 printf("%s\n", g.ToString().c_str());
22545 g++; printf("g++\n");
22546 printf("%s\n", g.ToString().c_str());
22547
22548
22549
22550 uint256 a(7);
22551 printf("a=7\n");
22552 printf("%s\n", a.ToString().c_str());
22553
22554 uint256 b;
22555 printf("b undefined\n");
22556 printf("%s\n", b.ToString().c_str());
22557 int c = 3;
22558
22559 a = c;
22560 a.pn[3] = 15;
22561 printf("%s\n", a.ToString().c_str());
22562 uint256 k(c);
22563
22564 a = 5;
22565 a.pn[3] = 15;
22566 printf("%s\n", a.ToString().c_str());
22567 b = 1;
22568 b <<= 52;
22569
22570 a |= b;
22571
22572 a ^= 0x500;
22573
22574 printf("a %s\n", a.ToString().c_str());
22575
22576 a = a | b | (uint256)0x1000;
22577
22578
22579 printf("a %s\n", a.ToString().c_str());
22580 printf("b %s\n", b.ToString().c_str());
22581
22582 a = 0xfffffffe;
22583 a.pn[4] = 9;
22584
22585 printf("%s\n", a.ToString().c_str());
22586 a++;
22587 printf("%s\n", a.ToString().c_str());
22588 a++;
22589 printf("%s\n", a.ToString().c_str());
22590 a++;
22591 printf("%s\n", a.ToString().c_str());
22592 a++;
22593 printf("%s\n", a.ToString().c_str());
22594
22595 a--;
22596 printf("%s\n", a.ToString().c_str());
22597 a--;
22598 printf("%s\n", a.ToString().c_str());
22599 a--;
22600 printf("%s\n", a.ToString().c_str());
22601 uint256 d = a--;
22602 printf("%s\n", d.ToString().c_str());
22603 printf("%s\n", a.ToString().c_str());
22604 a--;
22605 printf("%s\n", a.ToString().c_str());
22606 a--;
22607 printf("%s\n", a.ToString().c_str());
22608
22609 d = a;
22610
22611 printf("%s\n", d.ToString().c_str());
22612 for (int i = uint256::WIDTH-1; i >= 0; i--) printf("%08x", d.pn[i]); printf("\n");
22613
22614 uint256 neg = d;
22615 neg = ~neg;
22616 printf("%s\n", neg.ToString().c_str());
22617
22618
22619 uint256 e = uint256("0xABCDEF123abcdef12345678909832180000011111111");
22620 printf("\n");
22621 printf("%s\n", e.ToString().c_str());
22622
22623
22624 printf("\n");
22625 uint256 x1 = uint256("0xABCDEF123abcdef12345678909832180000011111111");
22626 uint256 x2;
22627 printf("%s\n", x1.ToString().c_str());
22628 for (int i = 0; i < 270; i += 4)
22629 {
22630 x2 = x1 << i;
22631 printf("%s\n", x2.ToString().c_str());
22632 }
22633
22634 printf("\n");
22635 printf("%s\n", x1.ToString().c_str());
22636 for (int i = 0; i < 270; i += 4)
22637 {
22638 x2 = x1;
22639 x2 >>= i;
22640 printf("%s\n", x2.ToString().c_str());
22641 }
22642
22643
22644 for (int i = 0; i < 100; i++)
22645 {
22646 uint256 k = (~uint256(0) >> i);
22647 printf("%s\n", k.ToString().c_str());
22648 }
22649
22650 for (int i = 0; i < 100; i++)
22651 {
22652 uint256 k = (~uint256(0) << i);
22653 printf("%s\n", k.ToString().c_str());
22654 }
22655
22656 return (0);
22657 }
22658
22659 #endif