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