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