2010-07-14 17:54:31 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2014-04-19 23:02:47 +02:00
|
|
|
// Copyright (c) 2009-2014 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.
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2011-05-15 09:11:04 +02:00
|
|
|
#ifndef BITCOIN_UINT256_H
|
|
|
|
#define BITCOIN_UINT256_H
|
|
|
|
|
2014-04-19 23:25:44 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdexcept>
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <stdint.h>
|
2010-07-14 17:54:31 +02:00
|
|
|
#include <string>
|
2014-09-04 02:03:39 +02:00
|
|
|
#include <cstring>
|
2011-05-15 09:11:04 +02:00
|
|
|
#include <vector>
|
|
|
|
|
2014-04-19 23:25:44 +02:00
|
|
|
class uint_error : public std::runtime_error {
|
|
|
|
public:
|
|
|
|
explicit uint_error(const std::string& str) : std::runtime_error(str) {}
|
|
|
|
};
|
|
|
|
|
2014-04-19 23:02:47 +02:00
|
|
|
/** Template base class for unsigned big integers. */
|
2010-07-14 17:54:31 +02:00
|
|
|
template<unsigned int BITS>
|
|
|
|
class base_uint
|
|
|
|
{
|
2014-07-09 17:25:09 +02:00
|
|
|
protected:
|
2010-07-14 17:54:31 +02:00
|
|
|
enum { WIDTH=BITS/32 };
|
2013-05-08 03:03:17 +02:00
|
|
|
uint32_t pn[WIDTH];
|
2010-07-14 17:54:31 +02:00
|
|
|
public:
|
|
|
|
|
2014-04-19 23:02:47 +02:00
|
|
|
base_uint()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < WIDTH; i++)
|
|
|
|
pn[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
base_uint(const base_uint& b)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < WIDTH; i++)
|
|
|
|
pn[i] = b.pn[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
base_uint& operator=(const base_uint& b)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < WIDTH; i++)
|
|
|
|
pn[i] = b.pn[i];
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
base_uint(uint64_t b)
|
|
|
|
{
|
|
|
|
pn[0] = (unsigned int)b;
|
|
|
|
pn[1] = (unsigned int)(b >> 32);
|
|
|
|
for (int i = 2; i < WIDTH; i++)
|
|
|
|
pn[i] = 0;
|
|
|
|
}
|
|
|
|
|
2014-06-28 17:35:22 +02:00
|
|
|
explicit base_uint(const std::string& str);
|
|
|
|
explicit base_uint(const std::vector<unsigned char>& vch);
|
2014-04-19 23:02:47 +02:00
|
|
|
|
2010-07-14 17:54:31 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-06-28 17:35:22 +02:00
|
|
|
double getdouble() const;
|
2010-07-14 17:54:31 +02:00
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
base_uint& operator=(uint64_t 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;
|
|
|
|
}
|
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
base_uint& operator^=(uint64_t b)
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
|
|
|
pn[0] ^= (unsigned int)b;
|
|
|
|
pn[1] ^= (unsigned int)(b >> 32);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
base_uint& operator|=(uint64_t b)
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
|
|
|
pn[0] |= (unsigned int)b;
|
|
|
|
pn[1] |= (unsigned int)(b >> 32);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-06-28 17:35:22 +02:00
|
|
|
base_uint& operator<<=(unsigned int shift);
|
|
|
|
base_uint& operator>>=(unsigned int shift);
|
2010-07-14 17:54:31 +02:00
|
|
|
|
|
|
|
base_uint& operator+=(const base_uint& b)
|
|
|
|
{
|
2013-04-13 07:13:08 +02:00
|
|
|
uint64_t carry = 0;
|
2010-07-14 17:54:31 +02:00
|
|
|
for (int i = 0; i < WIDTH; i++)
|
|
|
|
{
|
2013-04-13 07:13:08 +02:00
|
|
|
uint64_t 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;
|
|
|
|
}
|
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
base_uint& operator+=(uint64_t b64)
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
|
|
|
base_uint b;
|
|
|
|
b = b64;
|
|
|
|
*this += b;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
base_uint& operator-=(uint64_t b64)
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
|
|
|
base_uint b;
|
|
|
|
b = b64;
|
|
|
|
*this += -b;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-06-28 17:35:22 +02:00
|
|
|
base_uint& operator*=(uint32_t b32);
|
|
|
|
base_uint& operator*=(const base_uint& b);
|
|
|
|
base_uint& operator/=(const base_uint& b);
|
2010-07-14 17:54:31 +02:00
|
|
|
|
|
|
|
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;
|
2013-12-21 16:50:49 +01:00
|
|
|
while (--pn[i] == (uint32_t)-1 && i < WIDTH-1)
|
2010-07-14 17:54:31 +02:00
|
|
|
i++;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const base_uint operator--(int)
|
|
|
|
{
|
|
|
|
// postfix operator
|
|
|
|
const base_uint ret = *this;
|
|
|
|
--(*this);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-06-28 17:35:22 +02:00
|
|
|
int CompareTo(const base_uint& b) const;
|
|
|
|
bool EqualTo(uint64_t b) const;
|
2010-07-14 17:54:31 +02:00
|
|
|
|
2014-04-19 23:02:47 +02:00
|
|
|
friend inline const base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; }
|
|
|
|
friend inline const base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; }
|
2014-04-20 01:03:19 +02:00
|
|
|
friend inline const base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; }
|
|
|
|
friend inline const base_uint operator/(const base_uint& a, const base_uint& b) { return base_uint(a) /= b; }
|
2014-04-19 23:02:47 +02:00
|
|
|
friend inline const base_uint operator|(const base_uint& a, const base_uint& b) { return base_uint(a) |= b; }
|
|
|
|
friend inline const base_uint operator&(const base_uint& a, const base_uint& b) { return base_uint(a) &= b; }
|
|
|
|
friend inline const base_uint operator^(const base_uint& a, const base_uint& b) { return base_uint(a) ^= b; }
|
|
|
|
friend inline const base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; }
|
|
|
|
friend inline const base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
|
2014-04-20 01:03:19 +02:00
|
|
|
friend inline const base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
|
2014-09-04 02:03:39 +02:00
|
|
|
friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }
|
|
|
|
friend inline bool operator!=(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) != 0; }
|
2014-04-21 08:28:43 +02:00
|
|
|
friend inline bool operator>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) > 0; }
|
|
|
|
friend inline bool operator<(const base_uint& a, const base_uint& b) { return a.CompareTo(b) < 0; }
|
|
|
|
friend inline bool operator>=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) >= 0; }
|
|
|
|
friend inline bool operator<=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <= 0; }
|
|
|
|
friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); }
|
|
|
|
friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); }
|
2010-07-14 17:54:31 +02:00
|
|
|
|
2014-06-28 17:35:22 +02:00
|
|
|
std::string GetHex() const;
|
|
|
|
void SetHex(const char* psz);
|
|
|
|
void SetHex(const std::string& str);
|
|
|
|
std::string ToString() const;
|
2010-07-14 17:54:31 +02:00
|
|
|
|
|
|
|
unsigned char* begin()
|
|
|
|
{
|
|
|
|
return (unsigned char*)&pn[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char* end()
|
|
|
|
{
|
|
|
|
return (unsigned char*)&pn[WIDTH];
|
|
|
|
}
|
|
|
|
|
2012-08-13 05:26:25 +02:00
|
|
|
const unsigned char* begin() const
|
|
|
|
{
|
|
|
|
return (unsigned char*)&pn[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned char* end() const
|
|
|
|
{
|
|
|
|
return (unsigned char*)&pn[WIDTH];
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int size() const
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
|
|
|
return sizeof(pn);
|
|
|
|
}
|
|
|
|
|
2014-04-20 01:03:19 +02:00
|
|
|
// Returns the position of the highest bit set plus one, or zero if the
|
|
|
|
// value is zero.
|
2014-06-28 17:35:22 +02:00
|
|
|
unsigned int bits() const;
|
2014-04-20 01:03:19 +02:00
|
|
|
|
2013-12-21 16:50:49 +01:00
|
|
|
uint64_t GetLow64() const
|
2012-01-04 23:39:45 +01:00
|
|
|
{
|
2013-12-21 16:50:49 +01:00
|
|
|
assert(WIDTH >= 2);
|
|
|
|
return pn[0] | (uint64_t)pn[1] << 32;
|
2012-01-04 23:39:45 +01:00
|
|
|
}
|
2010-07-14 17:54:31 +02:00
|
|
|
|
2012-04-16 14:56:45 +02:00
|
|
|
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, 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, int nVersion)
|
2010-07-14 17:54:31 +02:00
|
|
|
{
|
|
|
|
s.read((char*)pn, sizeof(pn));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-19 23:02:47 +02:00
|
|
|
/** 160-bit unsigned big integer. */
|
|
|
|
class uint160 : public base_uint<160> {
|
2010-07-14 17:54:31 +02:00
|
|
|
public:
|
2014-04-19 23:02:47 +02:00
|
|
|
uint160() {}
|
|
|
|
uint160(const base_uint<160>& b) : base_uint<160>(b) {}
|
|
|
|
uint160(uint64_t b) : base_uint<160>(b) {}
|
|
|
|
explicit uint160(const std::string& str) : base_uint<160>(str) {}
|
|
|
|
explicit uint160(const std::vector<unsigned char>& vch) : base_uint<160>(vch) {}
|
2010-07-14 17:54:31 +02:00
|
|
|
};
|
|
|
|
|
2014-04-19 23:02:47 +02:00
|
|
|
/** 256-bit unsigned big integer. */
|
|
|
|
class uint256 : public base_uint<256> {
|
2010-07-14 17:54:31 +02:00
|
|
|
public:
|
2014-04-19 23:02:47 +02:00
|
|
|
uint256() {}
|
|
|
|
uint256(const base_uint<256>& b) : base_uint<256>(b) {}
|
|
|
|
uint256(uint64_t b) : base_uint<256>(b) {}
|
|
|
|
explicit uint256(const std::string& str) : base_uint<256>(str) {}
|
|
|
|
explicit uint256(const std::vector<unsigned char>& vch) : base_uint<256>(vch) {}
|
2014-04-20 03:19:20 +02:00
|
|
|
|
|
|
|
// The "compact" format is a representation of a whole
|
|
|
|
// number N using an unsigned 32bit number similar to a
|
|
|
|
// floating point format.
|
|
|
|
// The most significant 8 bits are the unsigned exponent of base 256.
|
|
|
|
// This exponent can be thought of as "number of bytes of N".
|
|
|
|
// The lower 23 bits are the mantissa.
|
|
|
|
// Bit number 24 (0x800000) represents the sign of N.
|
|
|
|
// N = (-1^sign) * mantissa * 256^(exponent-3)
|
|
|
|
//
|
|
|
|
// Satoshi's original implementation used BN_bn2mpi() and BN_mpi2bn().
|
|
|
|
// MPI uses the most significant bit of the first byte as sign.
|
|
|
|
// Thus 0x1234560000 is compact (0x05123456)
|
|
|
|
// and 0xc0de000000 is compact (0x0600c0de)
|
|
|
|
// (0x05c0de00) would be -0x40de000000
|
|
|
|
//
|
|
|
|
// Bitcoin only uses this "compact" format for encoding difficulty
|
|
|
|
// targets, which are unsigned 256bit quantities. Thus, all the
|
|
|
|
// complexities of the sign bit and using base 256 are probably an
|
|
|
|
// implementation accident.
|
2014-06-28 17:35:22 +02:00
|
|
|
uint256& SetCompact(uint32_t nCompact, bool *pfNegative = NULL, bool *pfOverflow = NULL);
|
|
|
|
uint32_t GetCompact(bool fNegative = false) const;
|
2014-07-09 17:25:09 +02:00
|
|
|
|
|
|
|
uint64_t GetHash(const uint256& salt) const;
|
2010-07-14 17:54:31 +02:00
|
|
|
};
|
|
|
|
|
2014-08-28 22:21:03 +02:00
|
|
|
#endif // BITCOIN_UINT256_H
|