2010-07-14 17:54:31 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2012-02-07 17:28:30 +01:00
|
|
|
// Copyright (c) 2009-2012 The Bitcoin developers
|
2010-07-14 17:54:31 +02:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
2012-05-18 16:02:28 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2011-05-15 09:11:04 +02:00
|
|
|
#ifndef BITCOIN_UINT256_H
|
|
|
|
#define BITCOIN_UINT256_H
|
|
|
|
|
2010-07-14 17:54:31 +02:00
|
|
|
#include <limits.h>
|
2012-04-20 12:50:57 +02:00
|
|
|
#include <stdio.h>
|
2012-04-16 00:20:05 +02:00
|
|
|
#include <string.h>
|
2010-07-14 17:54:31 +02:00
|
|
|
#include <string>
|
2011-05-15 09:11:04 +02:00
|
|
|
#include <vector>
|
|
|
|
|
2011-12-21 22:33:19 +01:00
|
|
|
typedef long long int64;
|
|
|
|
typedef unsigned long long uint64;
|
|
|
|
|
2010-07-14 17:54:31 +02:00
|
|
|
|
2011-05-15 09:11:04 +02:00
|
|
|
inline int Testuint256AdHoc(std::vector<std::string> vArg);
|
2010-07-14 17:54:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-26 16:48:23 +02:00
|
|
|
/** Base class without constructors for uint256 and uint160.
|
|
|
|
* This makes the compiler let u use it in a union.
|
|
|
|
*/
|
2010-07-14 17:54:31 +02:00
|
|
|
template<unsigned int BITS>
|
|
|
|
class base_uint
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
enum { WIDTH=BITS/32 };
|
|
|
|
unsigned int pn[WIDTH];
|
|
|
|
public:
|
|
|
|
|
|
|
|
bool operator!() const
|
|
|
|
{
|
|
|
|
for (int i = 0; i < WIDTH; i++)
|
|
|
|
if (pn[i] != 0)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const base_uint operator~() const
|
|
|
|
{
|
|
|
|
base_uint ret;
|
|
|
|
for (int i = 0; i < WIDTH; i++)
|
|
|
|
ret.pn[i] = ~pn[i];
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
const base_uint operator-() const
|
|
|
|
{
|
|
|
|
base_uint ret;
|
|
|
|
for (int i = 0; i < WIDTH; i++)
|
|
|
|
ret.pn[i] = ~pn[i];
|
|
|
|
ret++;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-21 22:33:19 +01:00
|
|
|
base_uint& operator=(uint64 b)
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
|
|
|
pn[0] = (unsigned int)b;
|
|
|
|
pn[1] = (unsigned int)(b >> 32);
|
|
|
|
for (int i = 2; i < WIDTH; i++)
|
|
|
|
pn[i] = 0;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
base_uint& operator^=(const base_uint& b)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < WIDTH; i++)
|
|
|
|
pn[i] ^= b.pn[i];
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
base_uint& operator&=(const base_uint& b)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < WIDTH; i++)
|
|
|
|
pn[i] &= b.pn[i];
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
base_uint& operator|=(const base_uint& b)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < WIDTH; i++)
|
|
|
|
pn[i] |= b.pn[i];
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-12-21 22:33:19 +01:00
|
|
|
base_uint& operator^=(uint64 b)
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
|
|
|
pn[0] ^= (unsigned int)b;
|
|
|
|
pn[1] ^= (unsigned int)(b >> 32);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-12-21 22:33:19 +01:00
|
|
|
base_uint& operator|=(uint64 b)
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
|
|
|
pn[0] |= (unsigned int)b;
|
|
|
|
pn[1] |= (unsigned int)(b >> 32);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
base_uint& operator<<=(unsigned int shift)
|
|
|
|
{
|
|
|
|
base_uint a(*this);
|
|
|
|
for (int i = 0; i < WIDTH; i++)
|
|
|
|
pn[i] = 0;
|
|
|
|
int k = shift / 32;
|
|
|
|
shift = shift % 32;
|
|
|
|
for (int i = 0; i < WIDTH; i++)
|
|
|
|
{
|
|
|
|
if (i+k+1 < WIDTH && shift != 0)
|
|
|
|
pn[i+k+1] |= (a.pn[i] >> (32-shift));
|
|
|
|
if (i+k < WIDTH)
|
|
|
|
pn[i+k] |= (a.pn[i] << shift);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
base_uint& operator>>=(unsigned int shift)
|
|
|
|
{
|
|
|
|
base_uint a(*this);
|
|
|
|
for (int i = 0; i < WIDTH; i++)
|
|
|
|
pn[i] = 0;
|
|
|
|
int k = shift / 32;
|
|
|
|
shift = shift % 32;
|
|
|
|
for (int i = 0; i < WIDTH; i++)
|
|
|
|
{
|
|
|
|
if (i-k-1 >= 0 && shift != 0)
|
|
|
|
pn[i-k-1] |= (a.pn[i] << (32-shift));
|
|
|
|
if (i-k >= 0)
|
|
|
|
pn[i-k] |= (a.pn[i] >> shift);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
base_uint& operator+=(const base_uint& b)
|
|
|
|
{
|
2011-12-21 22:33:19 +01:00
|
|
|
uint64 carry = 0;
|
2010-07-14 17:54:31 +02:00
|
|
|
for (int i = 0; i < WIDTH; i++)
|
|
|
|
{
|
2011-12-21 22:33:19 +01:00
|
|
|
uint64 n = carry + pn[i] + b.pn[i];
|
2010-07-14 17:54:31 +02:00
|
|
|
pn[i] = n & 0xffffffff;
|
|
|
|
carry = n >> 32;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
base_uint& operator-=(const base_uint& b)
|
|
|
|
{
|
|
|
|
*this += -b;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-12-21 22:33:19 +01:00
|
|
|
base_uint& operator+=(uint64 b64)
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
|
|
|
base_uint b;
|
|
|
|
b = b64;
|
|
|
|
*this += b;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-12-21 22:33:19 +01:00
|
|
|
base_uint& operator-=(uint64 b64)
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
|
|
|
base_uint b;
|
|
|
|
b = b64;
|
|
|
|
*this += -b;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
base_uint& operator++()
|
|
|
|
{
|
|
|
|
// prefix operator
|
|
|
|
int i = 0;
|
|
|
|
while (++pn[i] == 0 && i < WIDTH-1)
|
|
|
|
i++;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const base_uint operator++(int)
|
|
|
|
{
|
|
|
|
// postfix operator
|
|
|
|
const base_uint ret = *this;
|
|
|
|
++(*this);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
base_uint& operator--()
|
|
|
|
{
|
|
|
|
// prefix operator
|
|
|
|
int i = 0;
|
|
|
|
while (--pn[i] == -1 && i < WIDTH-1)
|
|
|
|
i++;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const base_uint operator--(int)
|
|
|
|
{
|
|
|
|
// postfix operator
|
|
|
|
const base_uint ret = *this;
|
|
|
|
--(*this);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
friend inline bool operator<(const base_uint& a, const base_uint& b)
|
|
|
|
{
|
|
|
|
for (int i = base_uint::WIDTH-1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if (a.pn[i] < b.pn[i])
|
|
|
|
return true;
|
|
|
|
else if (a.pn[i] > b.pn[i])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend inline bool operator<=(const base_uint& a, const base_uint& b)
|
|
|
|
{
|
|
|
|
for (int i = base_uint::WIDTH-1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if (a.pn[i] < b.pn[i])
|
|
|
|
return true;
|
|
|
|
else if (a.pn[i] > b.pn[i])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend inline bool operator>(const base_uint& a, const base_uint& b)
|
|
|
|
{
|
|
|
|
for (int i = base_uint::WIDTH-1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if (a.pn[i] > b.pn[i])
|
|
|
|
return true;
|
|
|
|
else if (a.pn[i] < b.pn[i])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend inline bool operator>=(const base_uint& a, const base_uint& b)
|
|
|
|
{
|
|
|
|
for (int i = base_uint::WIDTH-1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if (a.pn[i] > b.pn[i])
|
|
|
|
return true;
|
|
|
|
else if (a.pn[i] < b.pn[i])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend inline bool operator==(const base_uint& a, const base_uint& b)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < base_uint::WIDTH; i++)
|
|
|
|
if (a.pn[i] != b.pn[i])
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-21 22:33:19 +01:00
|
|
|
friend inline bool operator==(const base_uint& a, uint64 b)
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
|
|
|
if (a.pn[0] != (unsigned int)b)
|
|
|
|
return false;
|
|
|
|
if (a.pn[1] != (unsigned int)(b >> 32))
|
|
|
|
return false;
|
|
|
|
for (int i = 2; i < base_uint::WIDTH; i++)
|
|
|
|
if (a.pn[i] != 0)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend inline bool operator!=(const base_uint& a, const base_uint& b)
|
|
|
|
{
|
|
|
|
return (!(a == b));
|
|
|
|
}
|
|
|
|
|
2011-12-21 22:33:19 +01:00
|
|
|
friend inline bool operator!=(const base_uint& a, uint64 b)
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
|
|
|
return (!(a == b));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::string GetHex() const
|
|
|
|
{
|
|
|
|
char psz[sizeof(pn)*2 + 1];
|
2012-04-15 22:52:09 +02:00
|
|
|
for (unsigned int i = 0; i < sizeof(pn); i++)
|
2010-07-14 17:54:31 +02:00
|
|
|
sprintf(psz + i*2, "%02x", ((unsigned char*)pn)[sizeof(pn) - i - 1]);
|
2011-05-15 09:11:04 +02:00
|
|
|
return std::string(psz, psz + sizeof(pn)*2);
|
2010-07-14 17:54:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetHex(const char* psz)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < WIDTH; i++)
|
|
|
|
pn[i] = 0;
|
|
|
|
|
|
|
|
// skip leading spaces
|
|
|
|
while (isspace(*psz))
|
|
|
|
psz++;
|
|
|
|
|
|
|
|
// skip 0x
|
|
|
|
if (psz[0] == '0' && tolower(psz[1]) == 'x')
|
|
|
|
psz += 2;
|
|
|
|
|
|
|
|
// hex string to uint
|
2012-09-09 14:52:07 +02:00
|
|
|
static const unsigned 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 };
|
2010-07-14 17:54:31 +02:00
|
|
|
const char* pbegin = psz;
|
2012-04-15 12:22:30 +02:00
|
|
|
while (phexdigit[(unsigned char)*psz] || *psz == '0')
|
2010-07-14 17:54:31 +02:00
|
|
|
psz++;
|
|
|
|
psz--;
|
|
|
|
unsigned char* p1 = (unsigned char*)pn;
|
|
|
|
unsigned char* pend = p1 + WIDTH * 4;
|
|
|
|
while (psz >= pbegin && p1 < pend)
|
|
|
|
{
|
|
|
|
*p1 = phexdigit[(unsigned char)*psz--];
|
|
|
|
if (psz >= pbegin)
|
|
|
|
{
|
|
|
|
*p1 |= (phexdigit[(unsigned char)*psz--] << 4);
|
|
|
|
p1++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetHex(const std::string& str)
|
|
|
|
{
|
|
|
|
SetHex(str.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ToString() const
|
|
|
|
{
|
|
|
|
return (GetHex());
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char* begin()
|
|
|
|
{
|
|
|
|
return (unsigned char*)&pn[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char* end()
|
|
|
|
{
|
|
|
|
return (unsigned char*)&pn[WIDTH];
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int size()
|
|
|
|
{
|
|
|
|
return sizeof(pn);
|
|
|
|
}
|
|
|
|
|
2012-01-04 23:39:45 +01:00
|
|
|
uint64 Get64(int n=0) const
|
|
|
|
{
|
|
|
|
return pn[2*n] | (uint64)pn[2*n+1] << 32;
|
|
|
|
}
|
2010-07-14 17:54:31 +02:00
|
|
|
|
2012-04-16 14:56:45 +02:00
|
|
|
// unsigned int GetSerializeSize(int nType=0, int nVersion=PROTOCOL_VERSION) const
|
|
|
|
unsigned int GetSerializeSize(int nType, int nVersion) const
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
|
|
|
return sizeof(pn);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Stream>
|
2012-04-16 14:56:45 +02:00
|
|
|
// void Serialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION) const
|
|
|
|
void Serialize(Stream& s, int nType, int nVersion) const
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
|
|
|
s.write((char*)pn, sizeof(pn));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Stream>
|
2012-04-16 14:56:45 +02:00
|
|
|
// void Unserialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION)
|
|
|
|
void Unserialize(Stream& s, int nType, int nVersion)
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
|
|
|
s.read((char*)pn, sizeof(pn));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
friend class uint160;
|
|
|
|
friend class uint256;
|
2011-05-15 09:11:04 +02:00
|
|
|
friend inline int Testuint256AdHoc(std::vector<std::string> vArg);
|
2010-07-14 17:54:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef base_uint<160> base_uint160;
|
|
|
|
typedef base_uint<256> base_uint256;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// uint160 and uint256 could be implemented as templates, but to keep
|
|
|
|
// compile errors and debugging cleaner, they're copy and pasted.
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// uint160
|
|
|
|
//
|
|
|
|
|
2012-03-26 16:48:23 +02:00
|
|
|
/** 160-bit unsigned integer */
|
2010-07-14 17:54:31 +02:00
|
|
|
class uint160 : public base_uint160
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef base_uint160 basetype;
|
|
|
|
|
|
|
|
uint160()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < WIDTH; i++)
|
|
|
|
pn[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint160(const basetype& b)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < WIDTH; i++)
|
|
|
|
pn[i] = b.pn[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
uint160& operator=(const basetype& b)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < WIDTH; i++)
|
|
|
|
pn[i] = b.pn[i];
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-12-21 22:33:19 +01:00
|
|
|
uint160(uint64 b)
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
|
|
|
pn[0] = (unsigned int)b;
|
|
|
|
pn[1] = (unsigned int)(b >> 32);
|
|
|
|
for (int i = 2; i < WIDTH; i++)
|
|
|
|
pn[i] = 0;
|
|
|
|
}
|
|
|
|
|
2011-12-21 22:33:19 +01:00
|
|
|
uint160& operator=(uint64 b)
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
|
|
|
pn[0] = (unsigned int)b;
|
|
|
|
pn[1] = (unsigned int)(b >> 32);
|
|
|
|
for (int i = 2; i < WIDTH; i++)
|
|
|
|
pn[i] = 0;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit uint160(const std::string& str)
|
|
|
|
{
|
|
|
|
SetHex(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit uint160(const std::vector<unsigned char>& vch)
|
|
|
|
{
|
|
|
|
if (vch.size() == sizeof(pn))
|
|
|
|
memcpy(pn, &vch[0], sizeof(pn));
|
|
|
|
else
|
|
|
|
*this = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-21 22:33:19 +01:00
|
|
|
inline bool operator==(const uint160& a, uint64 b) { return (base_uint160)a == b; }
|
|
|
|
inline bool operator!=(const uint160& a, uint64 b) { return (base_uint160)a != b; }
|
2010-07-14 17:54:31 +02:00
|
|
|
inline const uint160 operator<<(const base_uint160& a, unsigned int shift) { return uint160(a) <<= shift; }
|
|
|
|
inline const uint160 operator>>(const base_uint160& a, unsigned int shift) { return uint160(a) >>= shift; }
|
|
|
|
inline const uint160 operator<<(const uint160& a, unsigned int shift) { return uint160(a) <<= shift; }
|
|
|
|
inline const uint160 operator>>(const uint160& a, unsigned int shift) { return uint160(a) >>= shift; }
|
|
|
|
|
|
|
|
inline const uint160 operator^(const base_uint160& a, const base_uint160& b) { return uint160(a) ^= b; }
|
|
|
|
inline const uint160 operator&(const base_uint160& a, const base_uint160& b) { return uint160(a) &= b; }
|
|
|
|
inline const uint160 operator|(const base_uint160& a, const base_uint160& b) { return uint160(a) |= b; }
|
|
|
|
inline const uint160 operator+(const base_uint160& a, const base_uint160& b) { return uint160(a) += b; }
|
|
|
|
inline const uint160 operator-(const base_uint160& a, const base_uint160& b) { return uint160(a) -= b; }
|
|
|
|
|
|
|
|
inline bool operator<(const base_uint160& a, const uint160& b) { return (base_uint160)a < (base_uint160)b; }
|
|
|
|
inline bool operator<=(const base_uint160& a, const uint160& b) { return (base_uint160)a <= (base_uint160)b; }
|
|
|
|
inline bool operator>(const base_uint160& a, const uint160& b) { return (base_uint160)a > (base_uint160)b; }
|
|
|
|
inline bool operator>=(const base_uint160& a, const uint160& b) { return (base_uint160)a >= (base_uint160)b; }
|
|
|
|
inline bool operator==(const base_uint160& a, const uint160& b) { return (base_uint160)a == (base_uint160)b; }
|
|
|
|
inline bool operator!=(const base_uint160& a, const uint160& b) { return (base_uint160)a != (base_uint160)b; }
|
|
|
|
inline const uint160 operator^(const base_uint160& a, const uint160& b) { return (base_uint160)a ^ (base_uint160)b; }
|
|
|
|
inline const uint160 operator&(const base_uint160& a, const uint160& b) { return (base_uint160)a & (base_uint160)b; }
|
|
|
|
inline const uint160 operator|(const base_uint160& a, const uint160& b) { return (base_uint160)a | (base_uint160)b; }
|
|
|
|
inline const uint160 operator+(const base_uint160& a, const uint160& b) { return (base_uint160)a + (base_uint160)b; }
|
|
|
|
inline const uint160 operator-(const base_uint160& a, const uint160& b) { return (base_uint160)a - (base_uint160)b; }
|
|
|
|
|
|
|
|
inline bool operator<(const uint160& a, const base_uint160& b) { return (base_uint160)a < (base_uint160)b; }
|
|
|
|
inline bool operator<=(const uint160& a, const base_uint160& b) { return (base_uint160)a <= (base_uint160)b; }
|
|
|
|
inline bool operator>(const uint160& a, const base_uint160& b) { return (base_uint160)a > (base_uint160)b; }
|
|
|
|
inline bool operator>=(const uint160& a, const base_uint160& b) { return (base_uint160)a >= (base_uint160)b; }
|
|
|
|
inline bool operator==(const uint160& a, const base_uint160& b) { return (base_uint160)a == (base_uint160)b; }
|
|
|
|
inline bool operator!=(const uint160& a, const base_uint160& b) { return (base_uint160)a != (base_uint160)b; }
|
|
|
|
inline const uint160 operator^(const uint160& a, const base_uint160& b) { return (base_uint160)a ^ (base_uint160)b; }
|
|
|
|
inline const uint160 operator&(const uint160& a, const base_uint160& b) { return (base_uint160)a & (base_uint160)b; }
|
|
|
|
inline const uint160 operator|(const uint160& a, const base_uint160& b) { return (base_uint160)a | (base_uint160)b; }
|
|
|
|
inline const uint160 operator+(const uint160& a, const base_uint160& b) { return (base_uint160)a + (base_uint160)b; }
|
|
|
|
inline const uint160 operator-(const uint160& a, const base_uint160& b) { return (base_uint160)a - (base_uint160)b; }
|
|
|
|
|
|
|
|
inline bool operator<(const uint160& a, const uint160& b) { return (base_uint160)a < (base_uint160)b; }
|
|
|
|
inline bool operator<=(const uint160& a, const uint160& b) { return (base_uint160)a <= (base_uint160)b; }
|
|
|
|
inline bool operator>(const uint160& a, const uint160& b) { return (base_uint160)a > (base_uint160)b; }
|
|
|
|
inline bool operator>=(const uint160& a, const uint160& b) { return (base_uint160)a >= (base_uint160)b; }
|
|
|
|
inline bool operator==(const uint160& a, const uint160& b) { return (base_uint160)a == (base_uint160)b; }
|
|
|
|
inline bool operator!=(const uint160& a, const uint160& b) { return (base_uint160)a != (base_uint160)b; }
|
|
|
|
inline const uint160 operator^(const uint160& a, const uint160& b) { return (base_uint160)a ^ (base_uint160)b; }
|
|
|
|
inline const uint160 operator&(const uint160& a, const uint160& b) { return (base_uint160)a & (base_uint160)b; }
|
|
|
|
inline const uint160 operator|(const uint160& a, const uint160& b) { return (base_uint160)a | (base_uint160)b; }
|
|
|
|
inline const uint160 operator+(const uint160& a, const uint160& b) { return (base_uint160)a + (base_uint160)b; }
|
|
|
|
inline const uint160 operator-(const uint160& a, const uint160& b) { return (base_uint160)a - (base_uint160)b; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// uint256
|
|
|
|
//
|
|
|
|
|
2012-03-26 16:48:23 +02:00
|
|
|
/** 256-bit unsigned integer */
|
2010-07-14 17:54:31 +02:00
|
|
|
class uint256 : public base_uint256
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef base_uint256 basetype;
|
|
|
|
|
|
|
|
uint256()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < WIDTH; i++)
|
|
|
|
pn[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint256(const basetype& b)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < WIDTH; i++)
|
|
|
|
pn[i] = b.pn[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
uint256& operator=(const basetype& b)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < WIDTH; i++)
|
|
|
|
pn[i] = b.pn[i];
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-12-21 22:33:19 +01:00
|
|
|
uint256(uint64 b)
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
|
|
|
pn[0] = (unsigned int)b;
|
|
|
|
pn[1] = (unsigned int)(b >> 32);
|
|
|
|
for (int i = 2; i < WIDTH; i++)
|
|
|
|
pn[i] = 0;
|
|
|
|
}
|
|
|
|
|
2011-12-21 22:33:19 +01:00
|
|
|
uint256& operator=(uint64 b)
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
|
|
|
pn[0] = (unsigned int)b;
|
|
|
|
pn[1] = (unsigned int)(b >> 32);
|
|
|
|
for (int i = 2; i < WIDTH; i++)
|
|
|
|
pn[i] = 0;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit uint256(const std::string& str)
|
|
|
|
{
|
|
|
|
SetHex(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit uint256(const std::vector<unsigned char>& vch)
|
|
|
|
{
|
|
|
|
if (vch.size() == sizeof(pn))
|
|
|
|
memcpy(pn, &vch[0], sizeof(pn));
|
|
|
|
else
|
|
|
|
*this = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-21 22:33:19 +01:00
|
|
|
inline bool operator==(const uint256& a, uint64 b) { return (base_uint256)a == b; }
|
|
|
|
inline bool operator!=(const uint256& a, uint64 b) { return (base_uint256)a != b; }
|
2010-07-14 17:54:31 +02:00
|
|
|
inline const uint256 operator<<(const base_uint256& a, unsigned int shift) { return uint256(a) <<= shift; }
|
|
|
|
inline const uint256 operator>>(const base_uint256& a, unsigned int shift) { return uint256(a) >>= shift; }
|
|
|
|
inline const uint256 operator<<(const uint256& a, unsigned int shift) { return uint256(a) <<= shift; }
|
|
|
|
inline const uint256 operator>>(const uint256& a, unsigned int shift) { return uint256(a) >>= shift; }
|
|
|
|
|
|
|
|
inline const uint256 operator^(const base_uint256& a, const base_uint256& b) { return uint256(a) ^= b; }
|
|
|
|
inline const uint256 operator&(const base_uint256& a, const base_uint256& b) { return uint256(a) &= b; }
|
|
|
|
inline const uint256 operator|(const base_uint256& a, const base_uint256& b) { return uint256(a) |= b; }
|
|
|
|
inline const uint256 operator+(const base_uint256& a, const base_uint256& b) { return uint256(a) += b; }
|
|
|
|
inline const uint256 operator-(const base_uint256& a, const base_uint256& b) { return uint256(a) -= b; }
|
|
|
|
|
|
|
|
inline bool operator<(const base_uint256& a, const uint256& b) { return (base_uint256)a < (base_uint256)b; }
|
|
|
|
inline bool operator<=(const base_uint256& a, const uint256& b) { return (base_uint256)a <= (base_uint256)b; }
|
|
|
|
inline bool operator>(const base_uint256& a, const uint256& b) { return (base_uint256)a > (base_uint256)b; }
|
|
|
|
inline bool operator>=(const base_uint256& a, const uint256& b) { return (base_uint256)a >= (base_uint256)b; }
|
|
|
|
inline bool operator==(const base_uint256& a, const uint256& b) { return (base_uint256)a == (base_uint256)b; }
|
|
|
|
inline bool operator!=(const base_uint256& a, const uint256& b) { return (base_uint256)a != (base_uint256)b; }
|
|
|
|
inline const uint256 operator^(const base_uint256& a, const uint256& b) { return (base_uint256)a ^ (base_uint256)b; }
|
|
|
|
inline const uint256 operator&(const base_uint256& a, const uint256& b) { return (base_uint256)a & (base_uint256)b; }
|
|
|
|
inline const uint256 operator|(const base_uint256& a, const uint256& b) { return (base_uint256)a | (base_uint256)b; }
|
|
|
|
inline const uint256 operator+(const base_uint256& a, const uint256& b) { return (base_uint256)a + (base_uint256)b; }
|
|
|
|
inline const uint256 operator-(const base_uint256& a, const uint256& b) { return (base_uint256)a - (base_uint256)b; }
|
|
|
|
|
|
|
|
inline bool operator<(const uint256& a, const base_uint256& b) { return (base_uint256)a < (base_uint256)b; }
|
|
|
|
inline bool operator<=(const uint256& a, const base_uint256& b) { return (base_uint256)a <= (base_uint256)b; }
|
|
|
|
inline bool operator>(const uint256& a, const base_uint256& b) { return (base_uint256)a > (base_uint256)b; }
|
|
|
|
inline bool operator>=(const uint256& a, const base_uint256& b) { return (base_uint256)a >= (base_uint256)b; }
|
|
|
|
inline bool operator==(const uint256& a, const base_uint256& b) { return (base_uint256)a == (base_uint256)b; }
|
|
|
|
inline bool operator!=(const uint256& a, const base_uint256& b) { return (base_uint256)a != (base_uint256)b; }
|
|
|
|
inline const uint256 operator^(const uint256& a, const base_uint256& b) { return (base_uint256)a ^ (base_uint256)b; }
|
|
|
|
inline const uint256 operator&(const uint256& a, const base_uint256& b) { return (base_uint256)a & (base_uint256)b; }
|
|
|
|
inline const uint256 operator|(const uint256& a, const base_uint256& b) { return (base_uint256)a | (base_uint256)b; }
|
|
|
|
inline const uint256 operator+(const uint256& a, const base_uint256& b) { return (base_uint256)a + (base_uint256)b; }
|
|
|
|
inline const uint256 operator-(const uint256& a, const base_uint256& b) { return (base_uint256)a - (base_uint256)b; }
|
|
|
|
|
|
|
|
inline bool operator<(const uint256& a, const uint256& b) { return (base_uint256)a < (base_uint256)b; }
|
|
|
|
inline bool operator<=(const uint256& a, const uint256& b) { return (base_uint256)a <= (base_uint256)b; }
|
|
|
|
inline bool operator>(const uint256& a, const uint256& b) { return (base_uint256)a > (base_uint256)b; }
|
|
|
|
inline bool operator>=(const uint256& a, const uint256& b) { return (base_uint256)a >= (base_uint256)b; }
|
|
|
|
inline bool operator==(const uint256& a, const uint256& b) { return (base_uint256)a == (base_uint256)b; }
|
|
|
|
inline bool operator!=(const uint256& a, const uint256& b) { return (base_uint256)a != (base_uint256)b; }
|
|
|
|
inline const uint256 operator^(const uint256& a, const uint256& b) { return (base_uint256)a ^ (base_uint256)b; }
|
|
|
|
inline const uint256 operator&(const uint256& a, const uint256& b) { return (base_uint256)a & (base_uint256)b; }
|
|
|
|
inline const uint256 operator|(const uint256& a, const uint256& b) { return (base_uint256)a | (base_uint256)b; }
|
|
|
|
inline const uint256 operator+(const uint256& a, const uint256& b) { return (base_uint256)a + (base_uint256)b; }
|
|
|
|
inline const uint256 operator-(const uint256& a, const uint256& b) { return (base_uint256)a - (base_uint256)b; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-03 23:33:31 +01:00
|
|
|
#ifdef TEST_UINT256
|
2010-07-14 17:54:31 +02:00
|
|
|
|
2011-05-15 09:11:04 +02:00
|
|
|
inline int Testuint256AdHoc(std::vector<std::string> vArg)
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
|
|
|
uint256 g(0);
|
|
|
|
|
|
|
|
|
|
|
|
printf("%s\n", g.ToString().c_str());
|
|
|
|
g--; printf("g--\n");
|
|
|
|
printf("%s\n", g.ToString().c_str());
|
|
|
|
g--; printf("g--\n");
|
|
|
|
printf("%s\n", g.ToString().c_str());
|
|
|
|
g++; printf("g++\n");
|
|
|
|
printf("%s\n", g.ToString().c_str());
|
|
|
|
g++; printf("g++\n");
|
|
|
|
printf("%s\n", g.ToString().c_str());
|
|
|
|
g++; printf("g++\n");
|
|
|
|
printf("%s\n", g.ToString().c_str());
|
|
|
|
g++; printf("g++\n");
|
|
|
|
printf("%s\n", g.ToString().c_str());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint256 a(7);
|
|
|
|
printf("a=7\n");
|
|
|
|
printf("%s\n", a.ToString().c_str());
|
|
|
|
|
|
|
|
uint256 b;
|
|
|
|
printf("b undefined\n");
|
|
|
|
printf("%s\n", b.ToString().c_str());
|
|
|
|
int c = 3;
|
|
|
|
|
|
|
|
a = c;
|
|
|
|
a.pn[3] = 15;
|
|
|
|
printf("%s\n", a.ToString().c_str());
|
|
|
|
uint256 k(c);
|
|
|
|
|
|
|
|
a = 5;
|
|
|
|
a.pn[3] = 15;
|
|
|
|
printf("%s\n", a.ToString().c_str());
|
|
|
|
b = 1;
|
|
|
|
b <<= 52;
|
|
|
|
|
|
|
|
a |= b;
|
|
|
|
|
|
|
|
a ^= 0x500;
|
|
|
|
|
|
|
|
printf("a %s\n", a.ToString().c_str());
|
|
|
|
|
|
|
|
a = a | b | (uint256)0x1000;
|
|
|
|
|
|
|
|
|
|
|
|
printf("a %s\n", a.ToString().c_str());
|
|
|
|
printf("b %s\n", b.ToString().c_str());
|
|
|
|
|
|
|
|
a = 0xfffffffe;
|
|
|
|
a.pn[4] = 9;
|
|
|
|
|
|
|
|
printf("%s\n", a.ToString().c_str());
|
|
|
|
a++;
|
|
|
|
printf("%s\n", a.ToString().c_str());
|
|
|
|
a++;
|
|
|
|
printf("%s\n", a.ToString().c_str());
|
|
|
|
a++;
|
|
|
|
printf("%s\n", a.ToString().c_str());
|
|
|
|
a++;
|
|
|
|
printf("%s\n", a.ToString().c_str());
|
|
|
|
|
|
|
|
a--;
|
|
|
|
printf("%s\n", a.ToString().c_str());
|
|
|
|
a--;
|
|
|
|
printf("%s\n", a.ToString().c_str());
|
|
|
|
a--;
|
|
|
|
printf("%s\n", a.ToString().c_str());
|
|
|
|
uint256 d = a--;
|
|
|
|
printf("%s\n", d.ToString().c_str());
|
|
|
|
printf("%s\n", a.ToString().c_str());
|
|
|
|
a--;
|
|
|
|
printf("%s\n", a.ToString().c_str());
|
|
|
|
a--;
|
|
|
|
printf("%s\n", a.ToString().c_str());
|
|
|
|
|
|
|
|
d = a;
|
|
|
|
|
|
|
|
printf("%s\n", d.ToString().c_str());
|
|
|
|
for (int i = uint256::WIDTH-1; i >= 0; i--) printf("%08x", d.pn[i]); printf("\n");
|
|
|
|
|
|
|
|
uint256 neg = d;
|
|
|
|
neg = ~neg;
|
|
|
|
printf("%s\n", neg.ToString().c_str());
|
|
|
|
|
|
|
|
|
|
|
|
uint256 e = uint256("0xABCDEF123abcdef12345678909832180000011111111");
|
|
|
|
printf("\n");
|
|
|
|
printf("%s\n", e.ToString().c_str());
|
|
|
|
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
uint256 x1 = uint256("0xABCDEF123abcdef12345678909832180000011111111");
|
|
|
|
uint256 x2;
|
|
|
|
printf("%s\n", x1.ToString().c_str());
|
|
|
|
for (int i = 0; i < 270; i += 4)
|
|
|
|
{
|
|
|
|
x2 = x1 << i;
|
|
|
|
printf("%s\n", x2.ToString().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
printf("%s\n", x1.ToString().c_str());
|
|
|
|
for (int i = 0; i < 270; i += 4)
|
|
|
|
{
|
|
|
|
x2 = x1;
|
|
|
|
x2 >>= i;
|
|
|
|
printf("%s\n", x2.ToString().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 100; i++)
|
|
|
|
{
|
|
|
|
uint256 k = (~uint256(0) >> i);
|
|
|
|
printf("%s\n", k.ToString().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 100; i++)
|
|
|
|
{
|
|
|
|
uint256 k = (~uint256(0) << i);
|
|
|
|
printf("%s\n", k.ToString().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
2011-05-15 09:11:04 +02:00
|
|
|
|
|
|
|
#endif
|
2012-01-03 23:33:31 +01:00
|
|
|
|
|
|
|
#endif
|