-
+ 6EE817AD8DB157912DF3B85A6A38BE1015C6DE8CADA460F1B164C4A54F7D689ECAC17309A70196AEA2D5C32E0B38C1BF9CA7EBB87A1BA4CEF97862AE0DEFD1FAbitcoin/src/uint256.h(0 . 0)(1 . 790)
23854 // /****************************\
23855 // * EXPERIMENTAL BRANCH. *
23856 // * FOR LABORATORY USE ONLY. *
23857 // ********************************
23858 // ************
23859 // **************
23860 // ****************
23861 // **** **** ****
23862 // *** *** ***
23863 // *** *** ***
23864 // *** * * **
23865 // ******** ********
23866 // ******* ******
23867 // *** **
23868 // * ******* **
23869 // ** * * * * *
23870 // ** * * ***
23871 // **** * * * * ****
23872 // **** *** * * ** ***
23873 // **** ********* ******
23874 // ******* ***** *******
23875 // ********* ****** **
23876 // ** ****** ******
23877 // ** ******* **
23878 // ** ******* ***
23879 // **** ******** ************
23880 // ************ ************
23881 // ******** *******
23882 // ****** ****
23883 // *** ***
23884 // ********************************
23885 // Copyright (c) 2009-2010 Satoshi Nakamoto
23886 // Copyright (c) 2011 The Bitcoin developers
23887 // Distributed under the MIT/X11 software license, see the accompanying
23888 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
23889 #ifndef BITCOIN_UINT256_H
23890 #define BITCOIN_UINT256_H
23891
23892 #include "serialize.h"
23893
23894 #include <limits.h>
23895 #include <string>
23896 #include <vector>
23897
23898 #if defined(_MSC_VER) || defined(__BORLANDC__)
23899 typedef __int64 int64;
23900 typedef unsigned __int64 uint64;
23901 #else
23902 typedef long long int64;
23903 typedef unsigned long long uint64;
23904 #endif
23905 #if defined(_MSC_VER) && _MSC_VER < 1300
23906 #define for if (false) ; else for
23907 #endif
23908
23909
23910 inline int Testuint256AdHoc(std::vector<std::string> vArg);
23911
23912
23913
23914 // We have to keep a separate base class without constructors
23915 // so the compiler will let us use it in a union
23916 template<unsigned int BITS>
23917 class base_uint
23918 {
23919 protected:
23920 enum { WIDTH=BITS/32 };
23921 unsigned int pn[WIDTH];
23922 public:
23923
23924 bool operator!() const
23925 {
23926 for (int i = 0; i < WIDTH; i++)
23927 if (pn[i] != 0)
23928 return false;
23929 return true;
23930 }
23931
23932 const base_uint operator~() const
23933 {
23934 base_uint ret;
23935 for (int i = 0; i < WIDTH; i++)
23936 ret.pn[i] = ~pn[i];
23937 return ret;
23938 }
23939
23940 const base_uint operator-() const
23941 {
23942 base_uint ret;
23943 for (int i = 0; i < WIDTH; i++)
23944 ret.pn[i] = ~pn[i];
23945 ret++;
23946 return ret;
23947 }
23948
23949
23950 base_uint& operator=(uint64 b)
23951 {
23952 pn[0] = (unsigned int)b;
23953 pn[1] = (unsigned int)(b >> 32);
23954 for (int i = 2; i < WIDTH; i++)
23955 pn[i] = 0;
23956 return *this;
23957 }
23958
23959 base_uint& operator^=(const base_uint& b)
23960 {
23961 for (int i = 0; i < WIDTH; i++)
23962 pn[i] ^= b.pn[i];
23963 return *this;
23964 }
23965
23966 base_uint& operator&=(const base_uint& b)
23967 {
23968 for (int i = 0; i < WIDTH; i++)
23969 pn[i] &= b.pn[i];
23970 return *this;
23971 }
23972
23973 base_uint& operator|=(const base_uint& b)
23974 {
23975 for (int i = 0; i < WIDTH; i++)
23976 pn[i] |= b.pn[i];
23977 return *this;
23978 }
23979
23980 base_uint& operator^=(uint64 b)
23981 {
23982 pn[0] ^= (unsigned int)b;
23983 pn[1] ^= (unsigned int)(b >> 32);
23984 return *this;
23985 }
23986
23987 base_uint& operator|=(uint64 b)
23988 {
23989 pn[0] |= (unsigned int)b;
23990 pn[1] |= (unsigned int)(b >> 32);
23991 return *this;
23992 }
23993
23994 base_uint& operator<<=(unsigned int shift)
23995 {
23996 base_uint a(*this);
23997 for (int i = 0; i < WIDTH; i++)
23998 pn[i] = 0;
23999 int k = shift / 32;
24000 shift = shift % 32;
24001 for (int i = 0; i < WIDTH; i++)
24002 {
24003 if (i+k+1 < WIDTH && shift != 0)
24004 pn[i+k+1] |= (a.pn[i] >> (32-shift));
24005 if (i+k < WIDTH)
24006 pn[i+k] |= (a.pn[i] << shift);
24007 }
24008 return *this;
24009 }
24010
24011 base_uint& operator>>=(unsigned int shift)
24012 {
24013 base_uint a(*this);
24014 for (int i = 0; i < WIDTH; i++)
24015 pn[i] = 0;
24016 int k = shift / 32;
24017 shift = shift % 32;
24018 for (int i = 0; i < WIDTH; i++)
24019 {
24020 if (i-k-1 >= 0 && shift != 0)
24021 pn[i-k-1] |= (a.pn[i] << (32-shift));
24022 if (i-k >= 0)
24023 pn[i-k] |= (a.pn[i] >> shift);
24024 }
24025 return *this;
24026 }
24027
24028 base_uint& operator+=(const base_uint& b)
24029 {
24030 uint64 carry = 0;
24031 for (int i = 0; i < WIDTH; i++)
24032 {
24033 uint64 n = carry + pn[i] + b.pn[i];
24034 pn[i] = n & 0xffffffff;
24035 carry = n >> 32;
24036 }
24037 return *this;
24038 }
24039
24040 base_uint& operator-=(const base_uint& b)
24041 {
24042 *this += -b;
24043 return *this;
24044 }
24045
24046 base_uint& operator+=(uint64 b64)
24047 {
24048 base_uint b;
24049 b = b64;
24050 *this += b;
24051 return *this;
24052 }
24053
24054 base_uint& operator-=(uint64 b64)
24055 {
24056 base_uint b;
24057 b = b64;
24058 *this += -b;
24059 return *this;
24060 }
24061
24062
24063 base_uint& operator++()
24064 {
24065 // prefix operator
24066 int i = 0;
24067 while (++pn[i] == 0 && i < WIDTH-1)
24068 i++;
24069 return *this;
24070 }
24071
24072 const base_uint operator++(int)
24073 {
24074 // postfix operator
24075 const base_uint ret = *this;
24076 ++(*this);
24077 return ret;
24078 }
24079
24080 base_uint& operator--()
24081 {
24082 // prefix operator
24083 int i = 0;
24084 while (--pn[i] == -1 && i < WIDTH-1)
24085 i++;
24086 return *this;
24087 }
24088
24089 const base_uint operator--(int)
24090 {
24091 // postfix operator
24092 const base_uint ret = *this;
24093 --(*this);
24094 return ret;
24095 }
24096
24097
24098 friend inline bool operator<(const base_uint& a, const base_uint& b)
24099 {
24100 for (int i = base_uint::WIDTH-1; i >= 0; i--)
24101 {
24102 if (a.pn[i] < b.pn[i])
24103 return true;
24104 else if (a.pn[i] > b.pn[i])
24105 return false;
24106 }
24107 return false;
24108 }
24109
24110 friend inline bool operator<=(const base_uint& a, const base_uint& b)
24111 {
24112 for (int i = base_uint::WIDTH-1; i >= 0; i--)
24113 {
24114 if (a.pn[i] < b.pn[i])
24115 return true;
24116 else if (a.pn[i] > b.pn[i])
24117 return false;
24118 }
24119 return true;
24120 }
24121
24122 friend inline bool operator>(const base_uint& a, const base_uint& b)
24123 {
24124 for (int i = base_uint::WIDTH-1; i >= 0; i--)
24125 {
24126 if (a.pn[i] > b.pn[i])
24127 return true;
24128 else if (a.pn[i] < b.pn[i])
24129 return false;
24130 }
24131 return false;
24132 }
24133
24134 friend inline bool operator>=(const base_uint& a, const base_uint& b)
24135 {
24136 for (int i = base_uint::WIDTH-1; i >= 0; i--)
24137 {
24138 if (a.pn[i] > b.pn[i])
24139 return true;
24140 else if (a.pn[i] < b.pn[i])
24141 return false;
24142 }
24143 return true;
24144 }
24145
24146 friend inline bool operator==(const base_uint& a, const base_uint& b)
24147 {
24148 for (int i = 0; i < base_uint::WIDTH; i++)
24149 if (a.pn[i] != b.pn[i])
24150 return false;
24151 return true;
24152 }
24153
24154 friend inline bool operator==(const base_uint& a, uint64 b)
24155 {
24156 if (a.pn[0] != (unsigned int)b)
24157 return false;
24158 if (a.pn[1] != (unsigned int)(b >> 32))
24159 return false;
24160 for (int i = 2; i < base_uint::WIDTH; i++)
24161 if (a.pn[i] != 0)
24162 return false;
24163 return true;
24164 }
24165
24166 friend inline bool operator!=(const base_uint& a, const base_uint& b)
24167 {
24168 return (!(a == b));
24169 }
24170
24171 friend inline bool operator!=(const base_uint& a, uint64 b)
24172 {
24173 return (!(a == b));
24174 }
24175
24176
24177
24178 std::string GetHex() const
24179 {
24180 char psz[sizeof(pn)*2 + 1];
24181 for (int i = 0; i < sizeof(pn); i++)
24182 sprintf(psz + i*2, "%02x", ((unsigned char*)pn)[sizeof(pn) - i - 1]);
24183 return std::string(psz, psz + sizeof(pn)*2);
24184 }
24185
24186 void SetHex(const char* psz)
24187 {
24188 for (int i = 0; i < WIDTH; i++)
24189 pn[i] = 0;
24190
24191 // skip leading spaces
24192 while (isspace(*psz))
24193 psz++;
24194
24195 // skip 0x
24196 if (psz[0] == '0' && tolower(psz[1]) == 'x')
24197 psz += 2;
24198
24199 // hex string to uint
24200 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 };
24201 const char* pbegin = psz;
24202 while (phexdigit[*psz] || *psz == '0')
24203 psz++;
24204 psz--;
24205 unsigned char* p1 = (unsigned char*)pn;
24206 unsigned char* pend = p1 + WIDTH * 4;
24207 while (psz >= pbegin && p1 < pend)
24208 {
24209 *p1 = phexdigit[(unsigned char)*psz--];
24210 if (psz >= pbegin)
24211 {
24212 *p1 |= (phexdigit[(unsigned char)*psz--] << 4);
24213 p1++;
24214 }
24215 }
24216 }
24217
24218 void SetHex(const std::string& str)
24219 {
24220 SetHex(str.c_str());
24221 }
24222
24223 std::string ToString() const
24224 {
24225 return (GetHex());
24226 }
24227
24228 unsigned char* begin()
24229 {
24230 return (unsigned char*)&pn[0];
24231 }
24232
24233 unsigned char* end()
24234 {
24235 return (unsigned char*)&pn[WIDTH];
24236 }
24237
24238 unsigned int size()
24239 {
24240 return sizeof(pn);
24241 }
24242
24243
24244 unsigned int GetSerializeSize(int nType=0, int nVersion=VERSION) const
24245 {
24246 return sizeof(pn);
24247 }
24248
24249 template<typename Stream>
24250 void Serialize(Stream& s, int nType=0, int nVersion=VERSION) const
24251 {
24252 s.write((char*)pn, sizeof(pn));
24253 }
24254
24255 template<typename Stream>
24256 void Unserialize(Stream& s, int nType=0, int nVersion=VERSION)
24257 {
24258 s.read((char*)pn, sizeof(pn));
24259 }
24260
24261
24262 friend class uint160;
24263 friend class uint256;
24264 friend inline int Testuint256AdHoc(std::vector<std::string> vArg);
24265 };
24266
24267 typedef base_uint<160> base_uint160;
24268 typedef base_uint<256> base_uint256;
24269
24270
24271
24272 //
24273 // uint160 and uint256 could be implemented as templates, but to keep
24274 // compile errors and debugging cleaner, they're copy and pasted.
24275 //
24276
24277
24278
24279 //////////////////////////////////////////////////////////////////////////////
24280 //
24281 // uint160
24282 //
24283
24284 class uint160 : public base_uint160
24285 {
24286 public:
24287 typedef base_uint160 basetype;
24288
24289 uint160()
24290 {
24291 for (int i = 0; i < WIDTH; i++)
24292 pn[i] = 0;
24293 }
24294
24295 uint160(const basetype& b)
24296 {
24297 for (int i = 0; i < WIDTH; i++)
24298 pn[i] = b.pn[i];
24299 }
24300
24301 uint160& operator=(const basetype& b)
24302 {
24303 for (int i = 0; i < WIDTH; i++)
24304 pn[i] = b.pn[i];
24305 return *this;
24306 }
24307
24308 uint160(uint64 b)
24309 {
24310 pn[0] = (unsigned int)b;
24311 pn[1] = (unsigned int)(b >> 32);
24312 for (int i = 2; i < WIDTH; i++)
24313 pn[i] = 0;
24314 }
24315
24316 uint160& operator=(uint64 b)
24317 {
24318 pn[0] = (unsigned int)b;
24319 pn[1] = (unsigned int)(b >> 32);
24320 for (int i = 2; i < WIDTH; i++)
24321 pn[i] = 0;
24322 return *this;
24323 }
24324
24325 explicit uint160(const std::string& str)
24326 {
24327 SetHex(str);
24328 }
24329
24330 explicit uint160(const std::vector<unsigned char>& vch)
24331 {
24332 if (vch.size() == sizeof(pn))
24333 memcpy(pn, &vch[0], sizeof(pn));
24334 else
24335 *this = 0;
24336 }
24337 };
24338
24339 inline bool operator==(const uint160& a, uint64 b) { return (base_uint160)a == b; }
24340 inline bool operator!=(const uint160& a, uint64 b) { return (base_uint160)a != b; }
24341 inline const uint160 operator<<(const base_uint160& a, unsigned int shift) { return uint160(a) <<= shift; }
24342 inline const uint160 operator>>(const base_uint160& a, unsigned int shift) { return uint160(a) >>= shift; }
24343 inline const uint160 operator<<(const uint160& a, unsigned int shift) { return uint160(a) <<= shift; }
24344 inline const uint160 operator>>(const uint160& a, unsigned int shift) { return uint160(a) >>= shift; }
24345
24346 inline const uint160 operator^(const base_uint160& a, const base_uint160& b) { return uint160(a) ^= b; }
24347 inline const uint160 operator&(const base_uint160& a, const base_uint160& b) { return uint160(a) &= b; }
24348 inline const uint160 operator|(const base_uint160& a, const base_uint160& b) { return uint160(a) |= b; }
24349 inline const uint160 operator+(const base_uint160& a, const base_uint160& b) { return uint160(a) += b; }
24350 inline const uint160 operator-(const base_uint160& a, const base_uint160& b) { return uint160(a) -= b; }
24351
24352 inline bool operator<(const base_uint160& a, const uint160& b) { return (base_uint160)a < (base_uint160)b; }
24353 inline bool operator<=(const base_uint160& a, const uint160& b) { return (base_uint160)a <= (base_uint160)b; }
24354 inline bool operator>(const base_uint160& a, const uint160& b) { return (base_uint160)a > (base_uint160)b; }
24355 inline bool operator>=(const base_uint160& a, const uint160& b) { return (base_uint160)a >= (base_uint160)b; }
24356 inline bool operator==(const base_uint160& a, const uint160& b) { return (base_uint160)a == (base_uint160)b; }
24357 inline bool operator!=(const base_uint160& a, const uint160& b) { return (base_uint160)a != (base_uint160)b; }
24358 inline const uint160 operator^(const base_uint160& a, const uint160& b) { return (base_uint160)a ^ (base_uint160)b; }
24359 inline const uint160 operator&(const base_uint160& a, const uint160& b) { return (base_uint160)a & (base_uint160)b; }
24360 inline const uint160 operator|(const base_uint160& a, const uint160& b) { return (base_uint160)a | (base_uint160)b; }
24361 inline const uint160 operator+(const base_uint160& a, const uint160& b) { return (base_uint160)a + (base_uint160)b; }
24362 inline const uint160 operator-(const base_uint160& a, const uint160& b) { return (base_uint160)a - (base_uint160)b; }
24363
24364 inline bool operator<(const uint160& a, const base_uint160& b) { return (base_uint160)a < (base_uint160)b; }
24365 inline bool operator<=(const uint160& a, const base_uint160& b) { return (base_uint160)a <= (base_uint160)b; }
24366 inline bool operator>(const uint160& a, const base_uint160& b) { return (base_uint160)a > (base_uint160)b; }
24367 inline bool operator>=(const uint160& a, const base_uint160& b) { return (base_uint160)a >= (base_uint160)b; }
24368 inline bool operator==(const uint160& a, const base_uint160& b) { return (base_uint160)a == (base_uint160)b; }
24369 inline bool operator!=(const uint160& a, const base_uint160& b) { return (base_uint160)a != (base_uint160)b; }
24370 inline const uint160 operator^(const uint160& a, const base_uint160& b) { return (base_uint160)a ^ (base_uint160)b; }
24371 inline const uint160 operator&(const uint160& a, const base_uint160& b) { return (base_uint160)a & (base_uint160)b; }
24372 inline const uint160 operator|(const uint160& a, const base_uint160& b) { return (base_uint160)a | (base_uint160)b; }
24373 inline const uint160 operator+(const uint160& a, const base_uint160& b) { return (base_uint160)a + (base_uint160)b; }
24374 inline const uint160 operator-(const uint160& a, const base_uint160& b) { return (base_uint160)a - (base_uint160)b; }
24375
24376 inline bool operator<(const uint160& a, const uint160& b) { return (base_uint160)a < (base_uint160)b; }
24377 inline bool operator<=(const uint160& a, const uint160& b) { return (base_uint160)a <= (base_uint160)b; }
24378 inline bool operator>(const uint160& a, const uint160& b) { return (base_uint160)a > (base_uint160)b; }
24379 inline bool operator>=(const uint160& a, const uint160& b) { return (base_uint160)a >= (base_uint160)b; }
24380 inline bool operator==(const uint160& a, const uint160& b) { return (base_uint160)a == (base_uint160)b; }
24381 inline bool operator!=(const uint160& a, const uint160& b) { return (base_uint160)a != (base_uint160)b; }
24382 inline const uint160 operator^(const uint160& a, const uint160& b) { return (base_uint160)a ^ (base_uint160)b; }
24383 inline const uint160 operator&(const uint160& a, const uint160& b) { return (base_uint160)a & (base_uint160)b; }
24384 inline const uint160 operator|(const uint160& a, const uint160& b) { return (base_uint160)a | (base_uint160)b; }
24385 inline const uint160 operator+(const uint160& a, const uint160& b) { return (base_uint160)a + (base_uint160)b; }
24386 inline const uint160 operator-(const uint160& a, const uint160& b) { return (base_uint160)a - (base_uint160)b; }
24387
24388
24389
24390
24391
24392
24393 //////////////////////////////////////////////////////////////////////////////
24394 //
24395 // uint256
24396 //
24397
24398 class uint256 : public base_uint256
24399 {
24400 public:
24401 typedef base_uint256 basetype;
24402
24403 uint256()
24404 {
24405 for (int i = 0; i < WIDTH; i++)
24406 pn[i] = 0;
24407 }
24408
24409 uint256(const basetype& b)
24410 {
24411 for (int i = 0; i < WIDTH; i++)
24412 pn[i] = b.pn[i];
24413 }
24414
24415 uint256& operator=(const basetype& b)
24416 {
24417 for (int i = 0; i < WIDTH; i++)
24418 pn[i] = b.pn[i];
24419 return *this;
24420 }
24421
24422 uint256(uint64 b)
24423 {
24424 pn[0] = (unsigned int)b;
24425 pn[1] = (unsigned int)(b >> 32);
24426 for (int i = 2; i < WIDTH; i++)
24427 pn[i] = 0;
24428 }
24429
24430 uint256& operator=(uint64 b)
24431 {
24432 pn[0] = (unsigned int)b;
24433 pn[1] = (unsigned int)(b >> 32);
24434 for (int i = 2; i < WIDTH; i++)
24435 pn[i] = 0;
24436 return *this;
24437 }
24438
24439 explicit uint256(const std::string& str)
24440 {
24441 SetHex(str);
24442 }
24443
24444 explicit uint256(const std::vector<unsigned char>& vch)
24445 {
24446 if (vch.size() == sizeof(pn))
24447 memcpy(pn, &vch[0], sizeof(pn));
24448 else
24449 *this = 0;
24450 }
24451 };
24452
24453 inline bool operator==(const uint256& a, uint64 b) { return (base_uint256)a == b; }
24454 inline bool operator!=(const uint256& a, uint64 b) { return (base_uint256)a != b; }
24455 inline const uint256 operator<<(const base_uint256& a, unsigned int shift) { return uint256(a) <<= shift; }
24456 inline const uint256 operator>>(const base_uint256& a, unsigned int shift) { return uint256(a) >>= shift; }
24457 inline const uint256 operator<<(const uint256& a, unsigned int shift) { return uint256(a) <<= shift; }
24458 inline const uint256 operator>>(const uint256& a, unsigned int shift) { return uint256(a) >>= shift; }
24459
24460 inline const uint256 operator^(const base_uint256& a, const base_uint256& b) { return uint256(a) ^= b; }
24461 inline const uint256 operator&(const base_uint256& a, const base_uint256& b) { return uint256(a) &= b; }
24462 inline const uint256 operator|(const base_uint256& a, const base_uint256& b) { return uint256(a) |= b; }
24463 inline const uint256 operator+(const base_uint256& a, const base_uint256& b) { return uint256(a) += b; }
24464 inline const uint256 operator-(const base_uint256& a, const base_uint256& b) { return uint256(a) -= b; }
24465
24466 inline bool operator<(const base_uint256& a, const uint256& b) { return (base_uint256)a < (base_uint256)b; }
24467 inline bool operator<=(const base_uint256& a, const uint256& b) { return (base_uint256)a <= (base_uint256)b; }
24468 inline bool operator>(const base_uint256& a, const uint256& b) { return (base_uint256)a > (base_uint256)b; }
24469 inline bool operator>=(const base_uint256& a, const uint256& b) { return (base_uint256)a >= (base_uint256)b; }
24470 inline bool operator==(const base_uint256& a, const uint256& b) { return (base_uint256)a == (base_uint256)b; }
24471 inline bool operator!=(const base_uint256& a, const uint256& b) { return (base_uint256)a != (base_uint256)b; }
24472 inline const uint256 operator^(const base_uint256& a, const uint256& b) { return (base_uint256)a ^ (base_uint256)b; }
24473 inline const uint256 operator&(const base_uint256& a, const uint256& b) { return (base_uint256)a & (base_uint256)b; }
24474 inline const uint256 operator|(const base_uint256& a, const uint256& b) { return (base_uint256)a | (base_uint256)b; }
24475 inline const uint256 operator+(const base_uint256& a, const uint256& b) { return (base_uint256)a + (base_uint256)b; }
24476 inline const uint256 operator-(const base_uint256& a, const uint256& b) { return (base_uint256)a - (base_uint256)b; }
24477
24478 inline bool operator<(const uint256& a, const base_uint256& b) { return (base_uint256)a < (base_uint256)b; }
24479 inline bool operator<=(const uint256& a, const base_uint256& b) { return (base_uint256)a <= (base_uint256)b; }
24480 inline bool operator>(const uint256& a, const base_uint256& b) { return (base_uint256)a > (base_uint256)b; }
24481 inline bool operator>=(const uint256& a, const base_uint256& b) { return (base_uint256)a >= (base_uint256)b; }
24482 inline bool operator==(const uint256& a, const base_uint256& b) { return (base_uint256)a == (base_uint256)b; }
24483 inline bool operator!=(const uint256& a, const base_uint256& b) { return (base_uint256)a != (base_uint256)b; }
24484 inline const uint256 operator^(const uint256& a, const base_uint256& b) { return (base_uint256)a ^ (base_uint256)b; }
24485 inline const uint256 operator&(const uint256& a, const base_uint256& b) { return (base_uint256)a & (base_uint256)b; }
24486 inline const uint256 operator|(const uint256& a, const base_uint256& b) { return (base_uint256)a | (base_uint256)b; }
24487 inline const uint256 operator+(const uint256& a, const base_uint256& b) { return (base_uint256)a + (base_uint256)b; }
24488 inline const uint256 operator-(const uint256& a, const base_uint256& b) { return (base_uint256)a - (base_uint256)b; }
24489
24490 inline bool operator<(const uint256& a, const uint256& b) { return (base_uint256)a < (base_uint256)b; }
24491 inline bool operator<=(const uint256& a, const uint256& b) { return (base_uint256)a <= (base_uint256)b; }
24492 inline bool operator>(const uint256& a, const uint256& b) { return (base_uint256)a > (base_uint256)b; }
24493 inline bool operator>=(const uint256& a, const uint256& b) { return (base_uint256)a >= (base_uint256)b; }
24494 inline bool operator==(const uint256& a, const uint256& b) { return (base_uint256)a == (base_uint256)b; }
24495 inline bool operator!=(const uint256& a, const uint256& b) { return (base_uint256)a != (base_uint256)b; }
24496 inline const uint256 operator^(const uint256& a, const uint256& b) { return (base_uint256)a ^ (base_uint256)b; }
24497 inline const uint256 operator&(const uint256& a, const uint256& b) { return (base_uint256)a & (base_uint256)b; }
24498 inline const uint256 operator|(const uint256& a, const uint256& b) { return (base_uint256)a | (base_uint256)b; }
24499 inline const uint256 operator+(const uint256& a, const uint256& b) { return (base_uint256)a + (base_uint256)b; }
24500 inline const uint256 operator-(const uint256& a, const uint256& b) { return (base_uint256)a - (base_uint256)b; }
24501
24502
24503
24504
24505
24506
24507
24508
24509
24510
24511
24512
24513 inline int Testuint256AdHoc(std::vector<std::string> vArg)
24514 {
24515 uint256 g(0);
24516
24517
24518 printf("%s\n", g.ToString().c_str());
24519 g--; printf("g--\n");
24520 printf("%s\n", g.ToString().c_str());
24521 g--; printf("g--\n");
24522 printf("%s\n", g.ToString().c_str());
24523 g++; printf("g++\n");
24524 printf("%s\n", g.ToString().c_str());
24525 g++; printf("g++\n");
24526 printf("%s\n", g.ToString().c_str());
24527 g++; printf("g++\n");
24528 printf("%s\n", g.ToString().c_str());
24529 g++; printf("g++\n");
24530 printf("%s\n", g.ToString().c_str());
24531
24532
24533
24534 uint256 a(7);
24535 printf("a=7\n");
24536 printf("%s\n", a.ToString().c_str());
24537
24538 uint256 b;
24539 printf("b undefined\n");
24540 printf("%s\n", b.ToString().c_str());
24541 int c = 3;
24542
24543 a = c;
24544 a.pn[3] = 15;
24545 printf("%s\n", a.ToString().c_str());
24546 uint256 k(c);
24547
24548 a = 5;
24549 a.pn[3] = 15;
24550 printf("%s\n", a.ToString().c_str());
24551 b = 1;
24552 b <<= 52;
24553
24554 a |= b;
24555
24556 a ^= 0x500;
24557
24558 printf("a %s\n", a.ToString().c_str());
24559
24560 a = a | b | (uint256)0x1000;
24561
24562
24563 printf("a %s\n", a.ToString().c_str());
24564 printf("b %s\n", b.ToString().c_str());
24565
24566 a = 0xfffffffe;
24567 a.pn[4] = 9;
24568
24569 printf("%s\n", a.ToString().c_str());
24570 a++;
24571 printf("%s\n", a.ToString().c_str());
24572 a++;
24573 printf("%s\n", a.ToString().c_str());
24574 a++;
24575 printf("%s\n", a.ToString().c_str());
24576 a++;
24577 printf("%s\n", a.ToString().c_str());
24578
24579 a--;
24580 printf("%s\n", a.ToString().c_str());
24581 a--;
24582 printf("%s\n", a.ToString().c_str());
24583 a--;
24584 printf("%s\n", a.ToString().c_str());
24585 uint256 d = a--;
24586 printf("%s\n", d.ToString().c_str());
24587 printf("%s\n", a.ToString().c_str());
24588 a--;
24589 printf("%s\n", a.ToString().c_str());
24590 a--;
24591 printf("%s\n", a.ToString().c_str());
24592
24593 d = a;
24594
24595 printf("%s\n", d.ToString().c_str());
24596 for (int i = uint256::WIDTH-1; i >= 0; i--) printf("%08x", d.pn[i]); printf("\n");
24597
24598 uint256 neg = d;
24599 neg = ~neg;
24600 printf("%s\n", neg.ToString().c_str());
24601
24602
24603 uint256 e = uint256("0xABCDEF123abcdef12345678909832180000011111111");
24604 printf("\n");
24605 printf("%s\n", e.ToString().c_str());
24606
24607
24608 printf("\n");
24609 uint256 x1 = uint256("0xABCDEF123abcdef12345678909832180000011111111");
24610 uint256 x2;
24611 printf("%s\n", x1.ToString().c_str());
24612 for (int i = 0; i < 270; i += 4)
24613 {
24614 x2 = x1 << i;
24615 printf("%s\n", x2.ToString().c_str());
24616 }
24617
24618 printf("\n");
24619 printf("%s\n", x1.ToString().c_str());
24620 for (int i = 0; i < 270; i += 4)
24621 {
24622 x2 = x1;
24623 x2 >>= i;
24624 printf("%s\n", x2.ToString().c_str());
24625 }
24626
24627
24628 for (int i = 0; i < 100; i++)
24629 {
24630 uint256 k = (~uint256(0) >> i);
24631 printf("%s\n", k.ToString().c_str());
24632 }
24633
24634 for (int i = 0; i < 100; i++)
24635 {
24636 uint256 k = (~uint256(0) << i);
24637 printf("%s\n", k.ToString().c_str());
24638 }
24639
24640 return (0);
24641 }
24642
24643 #endif