raw
experimental-genesis    1 //  /****************************\
experimental-genesis 2 // * EXPERIMENTAL BRANCH. *
experimental-genesis 3 // * FOR LABORATORY USE ONLY. *
experimental-genesis 4 // ********************************
experimental-genesis 5 // ************
experimental-genesis 6 // **************
experimental-genesis 7 // ****************
experimental-genesis 8 // **** **** ****
experimental-genesis 9 // *** *** ***
experimental-genesis 10 // *** *** ***
experimental-genesis 11 // *** * * **
experimental-genesis 12 // ******** ********
experimental-genesis 13 // ******* ******
experimental-genesis 14 // *** **
experimental-genesis 15 // * ******* **
experimental-genesis 16 // ** * * * * *
experimental-genesis 17 // ** * * ***
experimental-genesis 18 // **** * * * * ****
experimental-genesis 19 // **** *** * * ** ***
experimental-genesis 20 // **** ********* ******
experimental-genesis 21 // ******* ***** *******
experimental-genesis 22 // ********* ****** **
experimental-genesis 23 // ** ****** ******
experimental-genesis 24 // ** ******* **
experimental-genesis 25 // ** ******* ***
experimental-genesis 26 // **** ******** ************
experimental-genesis 27 // ************ ************
experimental-genesis 28 // ******** *******
experimental-genesis 29 // ****** ****
experimental-genesis 30 // *** ***
experimental-genesis 31 // ********************************
experimental-genesis 32 // Copyright (c) 2009-2010 Satoshi Nakamoto
experimental-genesis 33 // Copyright (c) 2011 The Bitcoin developers
experimental-genesis 34 // Distributed under the MIT/X11 software license, see the accompanying
experimental-genesis 35 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
experimental-genesis 36 #ifndef BITCOIN_UINT256_H
experimental-genesis 37 #define BITCOIN_UINT256_H
experimental-genesis 38
experimental-genesis 39 #include "serialize.h"
experimental-genesis 40
experimental-genesis 41 #include <limits.h>
experimental-genesis 42 #include <string>
experimental-genesis 43 #include <vector>
experimental-genesis 44
experimental-genesis 45 #if defined(_MSC_VER) || defined(__BORLANDC__)
experimental-genesis 46 typedef __int64 int64;
experimental-genesis 47 typedef unsigned __int64 uint64;
experimental-genesis 48 #else
experimental-genesis 49 typedef long long int64;
experimental-genesis 50 typedef unsigned long long uint64;
experimental-genesis 51 #endif
experimental-genesis 52 #if defined(_MSC_VER) && _MSC_VER < 1300
experimental-genesis 53 #define for if (false) ; else for
experimental-genesis 54 #endif
experimental-genesis 55
experimental-genesis 56
experimental-genesis 57 inline int Testuint256AdHoc(std::vector<std::string> vArg);
experimental-genesis 58
experimental-genesis 59
experimental-genesis 60
experimental-genesis 61 // We have to keep a separate base class without constructors
experimental-genesis 62 // so the compiler will let us use it in a union
experimental-genesis 63 template<unsigned int BITS>
experimental-genesis 64 class base_uint
experimental-genesis 65 {
experimental-genesis 66 protected:
experimental-genesis 67 enum { WIDTH=BITS/32 };
experimental-genesis 68 unsigned int pn[WIDTH];
experimental-genesis 69 public:
experimental-genesis 70
experimental-genesis 71 bool operator!() const
experimental-genesis 72 {
experimental-genesis 73 for (int i = 0; i < WIDTH; i++)
experimental-genesis 74 if (pn[i] != 0)
experimental-genesis 75 return false;
experimental-genesis 76 return true;
experimental-genesis 77 }
experimental-genesis 78
experimental-genesis 79 const base_uint operator~() const
experimental-genesis 80 {
experimental-genesis 81 base_uint ret;
experimental-genesis 82 for (int i = 0; i < WIDTH; i++)
experimental-genesis 83 ret.pn[i] = ~pn[i];
experimental-genesis 84 return ret;
experimental-genesis 85 }
experimental-genesis 86
experimental-genesis 87 const base_uint operator-() const
experimental-genesis 88 {
experimental-genesis 89 base_uint ret;
experimental-genesis 90 for (int i = 0; i < WIDTH; i++)
experimental-genesis 91 ret.pn[i] = ~pn[i];
experimental-genesis 92 ret++;
experimental-genesis 93 return ret;
experimental-genesis 94 }
experimental-genesis 95
experimental-genesis 96
experimental-genesis 97 base_uint& operator=(uint64 b)
experimental-genesis 98 {
experimental-genesis 99 pn[0] = (unsigned int)b;
experimental-genesis 100 pn[1] = (unsigned int)(b >> 32);
experimental-genesis 101 for (int i = 2; i < WIDTH; i++)
experimental-genesis 102 pn[i] = 0;
experimental-genesis 103 return *this;
experimental-genesis 104 }
experimental-genesis 105
experimental-genesis 106 base_uint& operator^=(const base_uint& b)
experimental-genesis 107 {
experimental-genesis 108 for (int i = 0; i < WIDTH; i++)
experimental-genesis 109 pn[i] ^= b.pn[i];
experimental-genesis 110 return *this;
experimental-genesis 111 }
experimental-genesis 112
experimental-genesis 113 base_uint& operator&=(const base_uint& b)
experimental-genesis 114 {
experimental-genesis 115 for (int i = 0; i < WIDTH; i++)
experimental-genesis 116 pn[i] &= b.pn[i];
experimental-genesis 117 return *this;
experimental-genesis 118 }
experimental-genesis 119
experimental-genesis 120 base_uint& operator|=(const base_uint& b)
experimental-genesis 121 {
experimental-genesis 122 for (int i = 0; i < WIDTH; i++)
experimental-genesis 123 pn[i] |= b.pn[i];
experimental-genesis 124 return *this;
experimental-genesis 125 }
experimental-genesis 126
experimental-genesis 127 base_uint& operator^=(uint64 b)
experimental-genesis 128 {
experimental-genesis 129 pn[0] ^= (unsigned int)b;
experimental-genesis 130 pn[1] ^= (unsigned int)(b >> 32);
experimental-genesis 131 return *this;
experimental-genesis 132 }
experimental-genesis 133
experimental-genesis 134 base_uint& operator|=(uint64 b)
experimental-genesis 135 {
experimental-genesis 136 pn[0] |= (unsigned int)b;
experimental-genesis 137 pn[1] |= (unsigned int)(b >> 32);
experimental-genesis 138 return *this;
experimental-genesis 139 }
experimental-genesis 140
experimental-genesis 141 base_uint& operator<<=(unsigned int shift)
experimental-genesis 142 {
experimental-genesis 143 base_uint a(*this);
experimental-genesis 144 for (int i = 0; i < WIDTH; i++)
experimental-genesis 145 pn[i] = 0;
experimental-genesis 146 int k = shift / 32;
experimental-genesis 147 shift = shift % 32;
experimental-genesis 148 for (int i = 0; i < WIDTH; i++)
experimental-genesis 149 {
experimental-genesis 150 if (i+k+1 < WIDTH && shift != 0)
experimental-genesis 151 pn[i+k+1] |= (a.pn[i] >> (32-shift));
experimental-genesis 152 if (i+k < WIDTH)
experimental-genesis 153 pn[i+k] |= (a.pn[i] << shift);
experimental-genesis 154 }
experimental-genesis 155 return *this;
experimental-genesis 156 }
experimental-genesis 157
experimental-genesis 158 base_uint& operator>>=(unsigned int shift)
experimental-genesis 159 {
experimental-genesis 160 base_uint a(*this);
experimental-genesis 161 for (int i = 0; i < WIDTH; i++)
experimental-genesis 162 pn[i] = 0;
experimental-genesis 163 int k = shift / 32;
experimental-genesis 164 shift = shift % 32;
experimental-genesis 165 for (int i = 0; i < WIDTH; i++)
experimental-genesis 166 {
experimental-genesis 167 if (i-k-1 >= 0 && shift != 0)
experimental-genesis 168 pn[i-k-1] |= (a.pn[i] << (32-shift));
experimental-genesis 169 if (i-k >= 0)
experimental-genesis 170 pn[i-k] |= (a.pn[i] >> shift);
experimental-genesis 171 }
experimental-genesis 172 return *this;
experimental-genesis 173 }
experimental-genesis 174
experimental-genesis 175 base_uint& operator+=(const base_uint& b)
experimental-genesis 176 {
experimental-genesis 177 uint64 carry = 0;
experimental-genesis 178 for (int i = 0; i < WIDTH; i++)
experimental-genesis 179 {
experimental-genesis 180 uint64 n = carry + pn[i] + b.pn[i];
experimental-genesis 181 pn[i] = n & 0xffffffff;
experimental-genesis 182 carry = n >> 32;
experimental-genesis 183 }
experimental-genesis 184 return *this;
experimental-genesis 185 }
experimental-genesis 186
experimental-genesis 187 base_uint& operator-=(const base_uint& b)
experimental-genesis 188 {
experimental-genesis 189 *this += -b;
experimental-genesis 190 return *this;
experimental-genesis 191 }
experimental-genesis 192
experimental-genesis 193 base_uint& operator+=(uint64 b64)
experimental-genesis 194 {
experimental-genesis 195 base_uint b;
experimental-genesis 196 b = b64;
experimental-genesis 197 *this += b;
experimental-genesis 198 return *this;
experimental-genesis 199 }
experimental-genesis 200
experimental-genesis 201 base_uint& operator-=(uint64 b64)
experimental-genesis 202 {
experimental-genesis 203 base_uint b;
experimental-genesis 204 b = b64;
experimental-genesis 205 *this += -b;
experimental-genesis 206 return *this;
experimental-genesis 207 }
experimental-genesis 208
experimental-genesis 209
experimental-genesis 210 base_uint& operator++()
experimental-genesis 211 {
experimental-genesis 212 // prefix operator
experimental-genesis 213 int i = 0;
experimental-genesis 214 while (++pn[i] == 0 && i < WIDTH-1)
experimental-genesis 215 i++;
experimental-genesis 216 return *this;
experimental-genesis 217 }
experimental-genesis 218
experimental-genesis 219 const base_uint operator++(int)
experimental-genesis 220 {
experimental-genesis 221 // postfix operator
experimental-genesis 222 const base_uint ret = *this;
experimental-genesis 223 ++(*this);
experimental-genesis 224 return ret;
experimental-genesis 225 }
experimental-genesis 226
experimental-genesis 227 base_uint& operator--()
experimental-genesis 228 {
experimental-genesis 229 // prefix operator
experimental-genesis 230 int i = 0;
experimental-genesis 231 while (--pn[i] == -1 && i < WIDTH-1)
experimental-genesis 232 i++;
experimental-genesis 233 return *this;
experimental-genesis 234 }
experimental-genesis 235
experimental-genesis 236 const base_uint operator--(int)
experimental-genesis 237 {
experimental-genesis 238 // postfix operator
experimental-genesis 239 const base_uint ret = *this;
experimental-genesis 240 --(*this);
experimental-genesis 241 return ret;
experimental-genesis 242 }
experimental-genesis 243
experimental-genesis 244
experimental-genesis 245 friend inline bool operator<(const base_uint& a, const base_uint& b)
experimental-genesis 246 {
experimental-genesis 247 for (int i = base_uint::WIDTH-1; i >= 0; i--)
experimental-genesis 248 {
experimental-genesis 249 if (a.pn[i] < b.pn[i])
experimental-genesis 250 return true;
experimental-genesis 251 else if (a.pn[i] > b.pn[i])
experimental-genesis 252 return false;
experimental-genesis 253 }
experimental-genesis 254 return false;
experimental-genesis 255 }
experimental-genesis 256
experimental-genesis 257 friend inline bool operator<=(const base_uint& a, const base_uint& b)
experimental-genesis 258 {
experimental-genesis 259 for (int i = base_uint::WIDTH-1; i >= 0; i--)
experimental-genesis 260 {
experimental-genesis 261 if (a.pn[i] < b.pn[i])
experimental-genesis 262 return true;
experimental-genesis 263 else if (a.pn[i] > b.pn[i])
experimental-genesis 264 return false;
experimental-genesis 265 }
experimental-genesis 266 return true;
experimental-genesis 267 }
experimental-genesis 268
experimental-genesis 269 friend inline bool operator>(const base_uint& a, const base_uint& b)
experimental-genesis 270 {
experimental-genesis 271 for (int i = base_uint::WIDTH-1; i >= 0; i--)
experimental-genesis 272 {
experimental-genesis 273 if (a.pn[i] > b.pn[i])
experimental-genesis 274 return true;
experimental-genesis 275 else if (a.pn[i] < b.pn[i])
experimental-genesis 276 return false;
experimental-genesis 277 }
experimental-genesis 278 return false;
experimental-genesis 279 }
experimental-genesis 280
experimental-genesis 281 friend inline bool operator>=(const base_uint& a, const base_uint& b)
experimental-genesis 282 {
experimental-genesis 283 for (int i = base_uint::WIDTH-1; i >= 0; i--)
experimental-genesis 284 {
experimental-genesis 285 if (a.pn[i] > b.pn[i])
experimental-genesis 286 return true;
experimental-genesis 287 else if (a.pn[i] < b.pn[i])
experimental-genesis 288 return false;
experimental-genesis 289 }
experimental-genesis 290 return true;
experimental-genesis 291 }
experimental-genesis 292
experimental-genesis 293 friend inline bool operator==(const base_uint& a, const base_uint& b)
experimental-genesis 294 {
experimental-genesis 295 for (int i = 0; i < base_uint::WIDTH; i++)
experimental-genesis 296 if (a.pn[i] != b.pn[i])
experimental-genesis 297 return false;
experimental-genesis 298 return true;
experimental-genesis 299 }
experimental-genesis 300
experimental-genesis 301 friend inline bool operator==(const base_uint& a, uint64 b)
experimental-genesis 302 {
experimental-genesis 303 if (a.pn[0] != (unsigned int)b)
experimental-genesis 304 return false;
experimental-genesis 305 if (a.pn[1] != (unsigned int)(b >> 32))
experimental-genesis 306 return false;
experimental-genesis 307 for (int i = 2; i < base_uint::WIDTH; i++)
experimental-genesis 308 if (a.pn[i] != 0)
experimental-genesis 309 return false;
experimental-genesis 310 return true;
experimental-genesis 311 }
experimental-genesis 312
experimental-genesis 313 friend inline bool operator!=(const base_uint& a, const base_uint& b)
experimental-genesis 314 {
experimental-genesis 315 return (!(a == b));
experimental-genesis 316 }
experimental-genesis 317
experimental-genesis 318 friend inline bool operator!=(const base_uint& a, uint64 b)
experimental-genesis 319 {
experimental-genesis 320 return (!(a == b));
experimental-genesis 321 }
experimental-genesis 322
experimental-genesis 323
experimental-genesis 324
experimental-genesis 325 std::string GetHex() const
experimental-genesis 326 {
experimental-genesis 327 char psz[sizeof(pn)*2 + 1];
experimental-genesis 328 for (int i = 0; i < sizeof(pn); i++)
experimental-genesis 329 sprintf(psz + i*2, "%02x", ((unsigned char*)pn)[sizeof(pn) - i - 1]);
experimental-genesis 330 return std::string(psz, psz + sizeof(pn)*2);
experimental-genesis 331 }
experimental-genesis 332
experimental-genesis 333 void SetHex(const char* psz)
experimental-genesis 334 {
experimental-genesis 335 for (int i = 0; i < WIDTH; i++)
experimental-genesis 336 pn[i] = 0;
experimental-genesis 337
experimental-genesis 338 // skip leading spaces
experimental-genesis 339 while (isspace(*psz))
experimental-genesis 340 psz++;
experimental-genesis 341
experimental-genesis 342 // skip 0x
experimental-genesis 343 if (psz[0] == '0' && tolower(psz[1]) == 'x')
experimental-genesis 344 psz += 2;
experimental-genesis 345
experimental-genesis 346 // hex string to uint
experimental-genesis 347 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 };
experimental-genesis 348 const char* pbegin = psz;
experimental-genesis 349 while (phexdigit[*psz] || *psz == '0')
experimental-genesis 350 psz++;
experimental-genesis 351 psz--;
experimental-genesis 352 unsigned char* p1 = (unsigned char*)pn;
experimental-genesis 353 unsigned char* pend = p1 + WIDTH * 4;
experimental-genesis 354 while (psz >= pbegin && p1 < pend)
experimental-genesis 355 {
experimental-genesis 356 *p1 = phexdigit[(unsigned char)*psz--];
experimental-genesis 357 if (psz >= pbegin)
experimental-genesis 358 {
experimental-genesis 359 *p1 |= (phexdigit[(unsigned char)*psz--] << 4);
experimental-genesis 360 p1++;
experimental-genesis 361 }
experimental-genesis 362 }
experimental-genesis 363 }
experimental-genesis 364
experimental-genesis 365 void SetHex(const std::string& str)
experimental-genesis 366 {
experimental-genesis 367 SetHex(str.c_str());
experimental-genesis 368 }
experimental-genesis 369
experimental-genesis 370 std::string ToString() const
experimental-genesis 371 {
experimental-genesis 372 return (GetHex());
experimental-genesis 373 }
experimental-genesis 374
experimental-genesis 375 unsigned char* begin()
experimental-genesis 376 {
experimental-genesis 377 return (unsigned char*)&pn[0];
experimental-genesis 378 }
experimental-genesis 379
experimental-genesis 380 unsigned char* end()
experimental-genesis 381 {
experimental-genesis 382 return (unsigned char*)&pn[WIDTH];
experimental-genesis 383 }
experimental-genesis 384
experimental-genesis 385 unsigned int size()
experimental-genesis 386 {
experimental-genesis 387 return sizeof(pn);
experimental-genesis 388 }
experimental-genesis 389
experimental-genesis 390
experimental-genesis 391 unsigned int GetSerializeSize(int nType=0, int nVersion=VERSION) const
experimental-genesis 392 {
experimental-genesis 393 return sizeof(pn);
experimental-genesis 394 }
experimental-genesis 395
experimental-genesis 396 template<typename Stream>
experimental-genesis 397 void Serialize(Stream& s, int nType=0, int nVersion=VERSION) const
experimental-genesis 398 {
experimental-genesis 399 s.write((char*)pn, sizeof(pn));
experimental-genesis 400 }
experimental-genesis 401
experimental-genesis 402 template<typename Stream>
experimental-genesis 403 void Unserialize(Stream& s, int nType=0, int nVersion=VERSION)
experimental-genesis 404 {
experimental-genesis 405 s.read((char*)pn, sizeof(pn));
experimental-genesis 406 }
experimental-genesis 407
experimental-genesis 408
experimental-genesis 409 friend class uint160;
experimental-genesis 410 friend class uint256;
experimental-genesis 411 friend inline int Testuint256AdHoc(std::vector<std::string> vArg);
experimental-genesis 412 };
experimental-genesis 413
experimental-genesis 414 typedef base_uint<160> base_uint160;
experimental-genesis 415 typedef base_uint<256> base_uint256;
experimental-genesis 416
experimental-genesis 417
experimental-genesis 418
experimental-genesis 419 //
experimental-genesis 420 // uint160 and uint256 could be implemented as templates, but to keep
experimental-genesis 421 // compile errors and debugging cleaner, they're copy and pasted.
experimental-genesis 422 //
experimental-genesis 423
experimental-genesis 424
experimental-genesis 425
experimental-genesis 426 //////////////////////////////////////////////////////////////////////////////
experimental-genesis 427 //
experimental-genesis 428 // uint160
experimental-genesis 429 //
experimental-genesis 430
experimental-genesis 431 class uint160 : public base_uint160
experimental-genesis 432 {
experimental-genesis 433 public:
experimental-genesis 434 typedef base_uint160 basetype;
experimental-genesis 435
experimental-genesis 436 uint160()
experimental-genesis 437 {
experimental-genesis 438 for (int i = 0; i < WIDTH; i++)
experimental-genesis 439 pn[i] = 0;
experimental-genesis 440 }
experimental-genesis 441
experimental-genesis 442 uint160(const basetype& b)
experimental-genesis 443 {
experimental-genesis 444 for (int i = 0; i < WIDTH; i++)
experimental-genesis 445 pn[i] = b.pn[i];
experimental-genesis 446 }
experimental-genesis 447
experimental-genesis 448 uint160& operator=(const basetype& b)
experimental-genesis 449 {
experimental-genesis 450 for (int i = 0; i < WIDTH; i++)
experimental-genesis 451 pn[i] = b.pn[i];
experimental-genesis 452 return *this;
experimental-genesis 453 }
experimental-genesis 454
experimental-genesis 455 uint160(uint64 b)
experimental-genesis 456 {
experimental-genesis 457 pn[0] = (unsigned int)b;
experimental-genesis 458 pn[1] = (unsigned int)(b >> 32);
experimental-genesis 459 for (int i = 2; i < WIDTH; i++)
experimental-genesis 460 pn[i] = 0;
experimental-genesis 461 }
experimental-genesis 462
experimental-genesis 463 uint160& operator=(uint64 b)
experimental-genesis 464 {
experimental-genesis 465 pn[0] = (unsigned int)b;
experimental-genesis 466 pn[1] = (unsigned int)(b >> 32);
experimental-genesis 467 for (int i = 2; i < WIDTH; i++)
experimental-genesis 468 pn[i] = 0;
experimental-genesis 469 return *this;
experimental-genesis 470 }
experimental-genesis 471
experimental-genesis 472 explicit uint160(const std::string& str)
experimental-genesis 473 {
experimental-genesis 474 SetHex(str);
experimental-genesis 475 }
experimental-genesis 476
experimental-genesis 477 explicit uint160(const std::vector<unsigned char>& vch)
experimental-genesis 478 {
experimental-genesis 479 if (vch.size() == sizeof(pn))
experimental-genesis 480 memcpy(pn, &vch[0], sizeof(pn));
experimental-genesis 481 else
experimental-genesis 482 *this = 0;
experimental-genesis 483 }
experimental-genesis 484 };
experimental-genesis 485
experimental-genesis 486 inline bool operator==(const uint160& a, uint64 b) { return (base_uint160)a == b; }
experimental-genesis 487 inline bool operator!=(const uint160& a, uint64 b) { return (base_uint160)a != b; }
experimental-genesis 488 inline const uint160 operator<<(const base_uint160& a, unsigned int shift) { return uint160(a) <<= shift; }
experimental-genesis 489 inline const uint160 operator>>(const base_uint160& a, unsigned int shift) { return uint160(a) >>= shift; }
experimental-genesis 490 inline const uint160 operator<<(const uint160& a, unsigned int shift) { return uint160(a) <<= shift; }
experimental-genesis 491 inline const uint160 operator>>(const uint160& a, unsigned int shift) { return uint160(a) >>= shift; }
experimental-genesis 492
experimental-genesis 493 inline const uint160 operator^(const base_uint160& a, const base_uint160& b) { return uint160(a) ^= b; }
experimental-genesis 494 inline const uint160 operator&(const base_uint160& a, const base_uint160& b) { return uint160(a) &= b; }
experimental-genesis 495 inline const uint160 operator|(const base_uint160& a, const base_uint160& b) { return uint160(a) |= b; }
experimental-genesis 496 inline const uint160 operator+(const base_uint160& a, const base_uint160& b) { return uint160(a) += b; }
experimental-genesis 497 inline const uint160 operator-(const base_uint160& a, const base_uint160& b) { return uint160(a) -= b; }
experimental-genesis 498
experimental-genesis 499 inline bool operator<(const base_uint160& a, const uint160& b) { return (base_uint160)a < (base_uint160)b; }
experimental-genesis 500 inline bool operator<=(const base_uint160& a, const uint160& b) { return (base_uint160)a <= (base_uint160)b; }
experimental-genesis 501 inline bool operator>(const base_uint160& a, const uint160& b) { return (base_uint160)a > (base_uint160)b; }
experimental-genesis 502 inline bool operator>=(const base_uint160& a, const uint160& b) { return (base_uint160)a >= (base_uint160)b; }
experimental-genesis 503 inline bool operator==(const base_uint160& a, const uint160& b) { return (base_uint160)a == (base_uint160)b; }
experimental-genesis 504 inline bool operator!=(const base_uint160& a, const uint160& b) { return (base_uint160)a != (base_uint160)b; }
experimental-genesis 505 inline const uint160 operator^(const base_uint160& a, const uint160& b) { return (base_uint160)a ^ (base_uint160)b; }
experimental-genesis 506 inline const uint160 operator&(const base_uint160& a, const uint160& b) { return (base_uint160)a & (base_uint160)b; }
experimental-genesis 507 inline const uint160 operator|(const base_uint160& a, const uint160& b) { return (base_uint160)a | (base_uint160)b; }
experimental-genesis 508 inline const uint160 operator+(const base_uint160& a, const uint160& b) { return (base_uint160)a + (base_uint160)b; }
experimental-genesis 509 inline const uint160 operator-(const base_uint160& a, const uint160& b) { return (base_uint160)a - (base_uint160)b; }
experimental-genesis 510
experimental-genesis 511 inline bool operator<(const uint160& a, const base_uint160& b) { return (base_uint160)a < (base_uint160)b; }
experimental-genesis 512 inline bool operator<=(const uint160& a, const base_uint160& b) { return (base_uint160)a <= (base_uint160)b; }
experimental-genesis 513 inline bool operator>(const uint160& a, const base_uint160& b) { return (base_uint160)a > (base_uint160)b; }
experimental-genesis 514 inline bool operator>=(const uint160& a, const base_uint160& b) { return (base_uint160)a >= (base_uint160)b; }
experimental-genesis 515 inline bool operator==(const uint160& a, const base_uint160& b) { return (base_uint160)a == (base_uint160)b; }
experimental-genesis 516 inline bool operator!=(const uint160& a, const base_uint160& b) { return (base_uint160)a != (base_uint160)b; }
experimental-genesis 517 inline const uint160 operator^(const uint160& a, const base_uint160& b) { return (base_uint160)a ^ (base_uint160)b; }
experimental-genesis 518 inline const uint160 operator&(const uint160& a, const base_uint160& b) { return (base_uint160)a & (base_uint160)b; }
experimental-genesis 519 inline const uint160 operator|(const uint160& a, const base_uint160& b) { return (base_uint160)a | (base_uint160)b; }
experimental-genesis 520 inline const uint160 operator+(const uint160& a, const base_uint160& b) { return (base_uint160)a + (base_uint160)b; }
experimental-genesis 521 inline const uint160 operator-(const uint160& a, const base_uint160& b) { return (base_uint160)a - (base_uint160)b; }
experimental-genesis 522
experimental-genesis 523 inline bool operator<(const uint160& a, const uint160& b) { return (base_uint160)a < (base_uint160)b; }
experimental-genesis 524 inline bool operator<=(const uint160& a, const uint160& b) { return (base_uint160)a <= (base_uint160)b; }
experimental-genesis 525 inline bool operator>(const uint160& a, const uint160& b) { return (base_uint160)a > (base_uint160)b; }
experimental-genesis 526 inline bool operator>=(const uint160& a, const uint160& b) { return (base_uint160)a >= (base_uint160)b; }
experimental-genesis 527 inline bool operator==(const uint160& a, const uint160& b) { return (base_uint160)a == (base_uint160)b; }
experimental-genesis 528 inline bool operator!=(const uint160& a, const uint160& b) { return (base_uint160)a != (base_uint160)b; }
experimental-genesis 529 inline const uint160 operator^(const uint160& a, const uint160& b) { return (base_uint160)a ^ (base_uint160)b; }
experimental-genesis 530 inline const uint160 operator&(const uint160& a, const uint160& b) { return (base_uint160)a & (base_uint160)b; }
experimental-genesis 531 inline const uint160 operator|(const uint160& a, const uint160& b) { return (base_uint160)a | (base_uint160)b; }
experimental-genesis 532 inline const uint160 operator+(const uint160& a, const uint160& b) { return (base_uint160)a + (base_uint160)b; }
experimental-genesis 533 inline const uint160 operator-(const uint160& a, const uint160& b) { return (base_uint160)a - (base_uint160)b; }
experimental-genesis 534
experimental-genesis 535
experimental-genesis 536
experimental-genesis 537
experimental-genesis 538
experimental-genesis 539
experimental-genesis 540 //////////////////////////////////////////////////////////////////////////////
experimental-genesis 541 //
experimental-genesis 542 // uint256
experimental-genesis 543 //
experimental-genesis 544
experimental-genesis 545 class uint256 : public base_uint256
experimental-genesis 546 {
experimental-genesis 547 public:
experimental-genesis 548 typedef base_uint256 basetype;
experimental-genesis 549
experimental-genesis 550 uint256()
experimental-genesis 551 {
experimental-genesis 552 for (int i = 0; i < WIDTH; i++)
experimental-genesis 553 pn[i] = 0;
experimental-genesis 554 }
experimental-genesis 555
experimental-genesis 556 uint256(const basetype& b)
experimental-genesis 557 {
experimental-genesis 558 for (int i = 0; i < WIDTH; i++)
experimental-genesis 559 pn[i] = b.pn[i];
experimental-genesis 560 }
experimental-genesis 561
experimental-genesis 562 uint256& operator=(const basetype& b)
experimental-genesis 563 {
experimental-genesis 564 for (int i = 0; i < WIDTH; i++)
experimental-genesis 565 pn[i] = b.pn[i];
experimental-genesis 566 return *this;
experimental-genesis 567 }
experimental-genesis 568
experimental-genesis 569 uint256(uint64 b)
experimental-genesis 570 {
experimental-genesis 571 pn[0] = (unsigned int)b;
experimental-genesis 572 pn[1] = (unsigned int)(b >> 32);
experimental-genesis 573 for (int i = 2; i < WIDTH; i++)
experimental-genesis 574 pn[i] = 0;
experimental-genesis 575 }
experimental-genesis 576
experimental-genesis 577 uint256& operator=(uint64 b)
experimental-genesis 578 {
experimental-genesis 579 pn[0] = (unsigned int)b;
experimental-genesis 580 pn[1] = (unsigned int)(b >> 32);
experimental-genesis 581 for (int i = 2; i < WIDTH; i++)
experimental-genesis 582 pn[i] = 0;
experimental-genesis 583 return *this;
experimental-genesis 584 }
experimental-genesis 585
experimental-genesis 586 explicit uint256(const std::string& str)
experimental-genesis 587 {
experimental-genesis 588 SetHex(str);
experimental-genesis 589 }
experimental-genesis 590
experimental-genesis 591 explicit uint256(const std::vector<unsigned char>& vch)
experimental-genesis 592 {
experimental-genesis 593 if (vch.size() == sizeof(pn))
experimental-genesis 594 memcpy(pn, &vch[0], sizeof(pn));
experimental-genesis 595 else
experimental-genesis 596 *this = 0;
experimental-genesis 597 }
experimental-genesis 598 };
experimental-genesis 599
experimental-genesis 600 inline bool operator==(const uint256& a, uint64 b) { return (base_uint256)a == b; }
experimental-genesis 601 inline bool operator!=(const uint256& a, uint64 b) { return (base_uint256)a != b; }
experimental-genesis 602 inline const uint256 operator<<(const base_uint256& a, unsigned int shift) { return uint256(a) <<= shift; }
experimental-genesis 603 inline const uint256 operator>>(const base_uint256& a, unsigned int shift) { return uint256(a) >>= shift; }
experimental-genesis 604 inline const uint256 operator<<(const uint256& a, unsigned int shift) { return uint256(a) <<= shift; }
experimental-genesis 605 inline const uint256 operator>>(const uint256& a, unsigned int shift) { return uint256(a) >>= shift; }
experimental-genesis 606
experimental-genesis 607 inline const uint256 operator^(const base_uint256& a, const base_uint256& b) { return uint256(a) ^= b; }
experimental-genesis 608 inline const uint256 operator&(const base_uint256& a, const base_uint256& b) { return uint256(a) &= b; }
experimental-genesis 609 inline const uint256 operator|(const base_uint256& a, const base_uint256& b) { return uint256(a) |= b; }
experimental-genesis 610 inline const uint256 operator+(const base_uint256& a, const base_uint256& b) { return uint256(a) += b; }
experimental-genesis 611 inline const uint256 operator-(const base_uint256& a, const base_uint256& b) { return uint256(a) -= b; }
experimental-genesis 612
experimental-genesis 613 inline bool operator<(const base_uint256& a, const uint256& b) { return (base_uint256)a < (base_uint256)b; }
experimental-genesis 614 inline bool operator<=(const base_uint256& a, const uint256& b) { return (base_uint256)a <= (base_uint256)b; }
experimental-genesis 615 inline bool operator>(const base_uint256& a, const uint256& b) { return (base_uint256)a > (base_uint256)b; }
experimental-genesis 616 inline bool operator>=(const base_uint256& a, const uint256& b) { return (base_uint256)a >= (base_uint256)b; }
experimental-genesis 617 inline bool operator==(const base_uint256& a, const uint256& b) { return (base_uint256)a == (base_uint256)b; }
experimental-genesis 618 inline bool operator!=(const base_uint256& a, const uint256& b) { return (base_uint256)a != (base_uint256)b; }
experimental-genesis 619 inline const uint256 operator^(const base_uint256& a, const uint256& b) { return (base_uint256)a ^ (base_uint256)b; }
experimental-genesis 620 inline const uint256 operator&(const base_uint256& a, const uint256& b) { return (base_uint256)a & (base_uint256)b; }
experimental-genesis 621 inline const uint256 operator|(const base_uint256& a, const uint256& b) { return (base_uint256)a | (base_uint256)b; }
experimental-genesis 622 inline const uint256 operator+(const base_uint256& a, const uint256& b) { return (base_uint256)a + (base_uint256)b; }
experimental-genesis 623 inline const uint256 operator-(const base_uint256& a, const uint256& b) { return (base_uint256)a - (base_uint256)b; }
experimental-genesis 624
experimental-genesis 625 inline bool operator<(const uint256& a, const base_uint256& b) { return (base_uint256)a < (base_uint256)b; }
experimental-genesis 626 inline bool operator<=(const uint256& a, const base_uint256& b) { return (base_uint256)a <= (base_uint256)b; }
experimental-genesis 627 inline bool operator>(const uint256& a, const base_uint256& b) { return (base_uint256)a > (base_uint256)b; }
experimental-genesis 628 inline bool operator>=(const uint256& a, const base_uint256& b) { return (base_uint256)a >= (base_uint256)b; }
experimental-genesis 629 inline bool operator==(const uint256& a, const base_uint256& b) { return (base_uint256)a == (base_uint256)b; }
experimental-genesis 630 inline bool operator!=(const uint256& a, const base_uint256& b) { return (base_uint256)a != (base_uint256)b; }
experimental-genesis 631 inline const uint256 operator^(const uint256& a, const base_uint256& b) { return (base_uint256)a ^ (base_uint256)b; }
experimental-genesis 632 inline const uint256 operator&(const uint256& a, const base_uint256& b) { return (base_uint256)a & (base_uint256)b; }
experimental-genesis 633 inline const uint256 operator|(const uint256& a, const base_uint256& b) { return (base_uint256)a | (base_uint256)b; }
experimental-genesis 634 inline const uint256 operator+(const uint256& a, const base_uint256& b) { return (base_uint256)a + (base_uint256)b; }
experimental-genesis 635 inline const uint256 operator-(const uint256& a, const base_uint256& b) { return (base_uint256)a - (base_uint256)b; }
experimental-genesis 636
experimental-genesis 637 inline bool operator<(const uint256& a, const uint256& b) { return (base_uint256)a < (base_uint256)b; }
experimental-genesis 638 inline bool operator<=(const uint256& a, const uint256& b) { return (base_uint256)a <= (base_uint256)b; }
experimental-genesis 639 inline bool operator>(const uint256& a, const uint256& b) { return (base_uint256)a > (base_uint256)b; }
experimental-genesis 640 inline bool operator>=(const uint256& a, const uint256& b) { return (base_uint256)a >= (base_uint256)b; }
experimental-genesis 641 inline bool operator==(const uint256& a, const uint256& b) { return (base_uint256)a == (base_uint256)b; }
experimental-genesis 642 inline bool operator!=(const uint256& a, const uint256& b) { return (base_uint256)a != (base_uint256)b; }
experimental-genesis 643 inline const uint256 operator^(const uint256& a, const uint256& b) { return (base_uint256)a ^ (base_uint256)b; }
experimental-genesis 644 inline const uint256 operator&(const uint256& a, const uint256& b) { return (base_uint256)a & (base_uint256)b; }
experimental-genesis 645 inline const uint256 operator|(const uint256& a, const uint256& b) { return (base_uint256)a | (base_uint256)b; }
experimental-genesis 646 inline const uint256 operator+(const uint256& a, const uint256& b) { return (base_uint256)a + (base_uint256)b; }
experimental-genesis 647 inline const uint256 operator-(const uint256& a, const uint256& b) { return (base_uint256)a - (base_uint256)b; }
experimental-genesis 648
experimental-genesis 649
experimental-genesis 650
experimental-genesis 651
experimental-genesis 652
experimental-genesis 653
experimental-genesis 654
experimental-genesis 655
experimental-genesis 656
experimental-genesis 657
experimental-genesis 658
experimental-genesis 659
experimental-genesis 660 inline int Testuint256AdHoc(std::vector<std::string> vArg)
experimental-genesis 661 {
experimental-genesis 662 uint256 g(0);
experimental-genesis 663
experimental-genesis 664
experimental-genesis 665 printf("%s\n", g.ToString().c_str());
experimental-genesis 666 g--; printf("g--\n");
experimental-genesis 667 printf("%s\n", g.ToString().c_str());
experimental-genesis 668 g--; printf("g--\n");
experimental-genesis 669 printf("%s\n", g.ToString().c_str());
experimental-genesis 670 g++; printf("g++\n");
experimental-genesis 671 printf("%s\n", g.ToString().c_str());
experimental-genesis 672 g++; printf("g++\n");
experimental-genesis 673 printf("%s\n", g.ToString().c_str());
experimental-genesis 674 g++; printf("g++\n");
experimental-genesis 675 printf("%s\n", g.ToString().c_str());
experimental-genesis 676 g++; printf("g++\n");
experimental-genesis 677 printf("%s\n", g.ToString().c_str());
experimental-genesis 678
experimental-genesis 679
experimental-genesis 680
experimental-genesis 681 uint256 a(7);
experimental-genesis 682 printf("a=7\n");
experimental-genesis 683 printf("%s\n", a.ToString().c_str());
experimental-genesis 684
experimental-genesis 685 uint256 b;
experimental-genesis 686 printf("b undefined\n");
experimental-genesis 687 printf("%s\n", b.ToString().c_str());
experimental-genesis 688 int c = 3;
experimental-genesis 689
experimental-genesis 690 a = c;
experimental-genesis 691 a.pn[3] = 15;
experimental-genesis 692 printf("%s\n", a.ToString().c_str());
experimental-genesis 693 uint256 k(c);
experimental-genesis 694
experimental-genesis 695 a = 5;
experimental-genesis 696 a.pn[3] = 15;
experimental-genesis 697 printf("%s\n", a.ToString().c_str());
experimental-genesis 698 b = 1;
experimental-genesis 699 b <<= 52;
experimental-genesis 700
experimental-genesis 701 a |= b;
experimental-genesis 702
experimental-genesis 703 a ^= 0x500;
experimental-genesis 704
experimental-genesis 705 printf("a %s\n", a.ToString().c_str());
experimental-genesis 706
experimental-genesis 707 a = a | b | (uint256)0x1000;
experimental-genesis 708
experimental-genesis 709
experimental-genesis 710 printf("a %s\n", a.ToString().c_str());
experimental-genesis 711 printf("b %s\n", b.ToString().c_str());
experimental-genesis 712
experimental-genesis 713 a = 0xfffffffe;
experimental-genesis 714 a.pn[4] = 9;
experimental-genesis 715
experimental-genesis 716 printf("%s\n", a.ToString().c_str());
experimental-genesis 717 a++;
experimental-genesis 718 printf("%s\n", a.ToString().c_str());
experimental-genesis 719 a++;
experimental-genesis 720 printf("%s\n", a.ToString().c_str());
experimental-genesis 721 a++;
experimental-genesis 722 printf("%s\n", a.ToString().c_str());
experimental-genesis 723 a++;
experimental-genesis 724 printf("%s\n", a.ToString().c_str());
experimental-genesis 725
experimental-genesis 726 a--;
experimental-genesis 727 printf("%s\n", a.ToString().c_str());
experimental-genesis 728 a--;
experimental-genesis 729 printf("%s\n", a.ToString().c_str());
experimental-genesis 730 a--;
experimental-genesis 731 printf("%s\n", a.ToString().c_str());
experimental-genesis 732 uint256 d = a--;
experimental-genesis 733 printf("%s\n", d.ToString().c_str());
experimental-genesis 734 printf("%s\n", a.ToString().c_str());
experimental-genesis 735 a--;
experimental-genesis 736 printf("%s\n", a.ToString().c_str());
experimental-genesis 737 a--;
experimental-genesis 738 printf("%s\n", a.ToString().c_str());
experimental-genesis 739
experimental-genesis 740 d = a;
experimental-genesis 741
experimental-genesis 742 printf("%s\n", d.ToString().c_str());
experimental-genesis 743 for (int i = uint256::WIDTH-1; i >= 0; i--) printf("%08x", d.pn[i]); printf("\n");
experimental-genesis 744
experimental-genesis 745 uint256 neg = d;
experimental-genesis 746 neg = ~neg;
experimental-genesis 747 printf("%s\n", neg.ToString().c_str());
experimental-genesis 748
experimental-genesis 749
experimental-genesis 750 uint256 e = uint256("0xABCDEF123abcdef12345678909832180000011111111");
experimental-genesis 751 printf("\n");
experimental-genesis 752 printf("%s\n", e.ToString().c_str());
experimental-genesis 753
experimental-genesis 754
experimental-genesis 755 printf("\n");
experimental-genesis 756 uint256 x1 = uint256("0xABCDEF123abcdef12345678909832180000011111111");
experimental-genesis 757 uint256 x2;
experimental-genesis 758 printf("%s\n", x1.ToString().c_str());
experimental-genesis 759 for (int i = 0; i < 270; i += 4)
experimental-genesis 760 {
experimental-genesis 761 x2 = x1 << i;
experimental-genesis 762 printf("%s\n", x2.ToString().c_str());
experimental-genesis 763 }
experimental-genesis 764
experimental-genesis 765 printf("\n");
experimental-genesis 766 printf("%s\n", x1.ToString().c_str());
experimental-genesis 767 for (int i = 0; i < 270; i += 4)
experimental-genesis 768 {
experimental-genesis 769 x2 = x1;
experimental-genesis 770 x2 >>= i;
experimental-genesis 771 printf("%s\n", x2.ToString().c_str());
experimental-genesis 772 }
experimental-genesis 773
experimental-genesis 774
experimental-genesis 775 for (int i = 0; i < 100; i++)
experimental-genesis 776 {
experimental-genesis 777 uint256 k = (~uint256(0) >> i);
experimental-genesis 778 printf("%s\n", k.ToString().c_str());
experimental-genesis 779 }
experimental-genesis 780
experimental-genesis 781 for (int i = 0; i < 100; i++)
experimental-genesis 782 {
experimental-genesis 783 uint256 k = (~uint256(0) << i);
experimental-genesis 784 printf("%s\n", k.ToString().c_str());
experimental-genesis 785 }
experimental-genesis 786
experimental-genesis 787 return (0);
experimental-genesis 788 }
experimental-genesis 789
experimental-genesis 790 #endif