uint256->arith_uint256 blob256->uint256
Introduce new opaque implementation of `uint256`, move old "arithmetic" implementation to `arith_uint256.
This commit is contained in:
parent
734f85c4f0
commit
bfc6070342
7 changed files with 990 additions and 706 deletions
|
@ -74,11 +74,12 @@ BITCOIN_CORE_H = \
|
||||||
alert.h \
|
alert.h \
|
||||||
allocators.h \
|
allocators.h \
|
||||||
amount.h \
|
amount.h \
|
||||||
|
arith_uint256.h \
|
||||||
base58.h \
|
base58.h \
|
||||||
bloom.h \
|
bloom.h \
|
||||||
chain.h \
|
chain.h \
|
||||||
chainparams.h \
|
|
||||||
chainparamsbase.h \
|
chainparamsbase.h \
|
||||||
|
chainparams.h \
|
||||||
chainparamsseeds.h \
|
chainparamsseeds.h \
|
||||||
checkpoints.h \
|
checkpoints.h \
|
||||||
checkqueue.h \
|
checkqueue.h \
|
||||||
|
@ -87,8 +88,6 @@ BITCOIN_CORE_H = \
|
||||||
coins.h \
|
coins.h \
|
||||||
compat.h \
|
compat.h \
|
||||||
compressor.h \
|
compressor.h \
|
||||||
primitives/block.h \
|
|
||||||
primitives/transaction.h \
|
|
||||||
core_io.h \
|
core_io.h \
|
||||||
crypter.h \
|
crypter.h \
|
||||||
db.h \
|
db.h \
|
||||||
|
@ -108,6 +107,8 @@ BITCOIN_CORE_H = \
|
||||||
net.h \
|
net.h \
|
||||||
noui.h \
|
noui.h \
|
||||||
pow.h \
|
pow.h \
|
||||||
|
primitives/block.h \
|
||||||
|
primitives/transaction.h \
|
||||||
protocol.h \
|
protocol.h \
|
||||||
pubkey.h \
|
pubkey.h \
|
||||||
random.h \
|
random.h \
|
||||||
|
@ -115,11 +116,11 @@ BITCOIN_CORE_H = \
|
||||||
rpcprotocol.h \
|
rpcprotocol.h \
|
||||||
rpcserver.h \
|
rpcserver.h \
|
||||||
script/interpreter.h \
|
script/interpreter.h \
|
||||||
|
script/script_error.h \
|
||||||
script/script.h \
|
script/script.h \
|
||||||
script/sigcache.h \
|
script/sigcache.h \
|
||||||
script/sign.h \
|
script/sign.h \
|
||||||
script/standard.h \
|
script/standard.h \
|
||||||
script/script_error.h \
|
|
||||||
serialize.h \
|
serialize.h \
|
||||||
streams.h \
|
streams.h \
|
||||||
sync.h \
|
sync.h \
|
||||||
|
@ -132,13 +133,13 @@ BITCOIN_CORE_H = \
|
||||||
uint256.h \
|
uint256.h \
|
||||||
undo.h \
|
undo.h \
|
||||||
util.h \
|
util.h \
|
||||||
utilstrencodings.h \
|
|
||||||
utilmoneystr.h \
|
utilmoneystr.h \
|
||||||
|
utilstrencodings.h \
|
||||||
utiltime.h \
|
utiltime.h \
|
||||||
version.h \
|
version.h \
|
||||||
|
walletdb.h \
|
||||||
wallet.h \
|
wallet.h \
|
||||||
wallet_ismine.h \
|
wallet_ismine.h \
|
||||||
walletdb.h \
|
|
||||||
compat/sanity.h
|
compat/sanity.h
|
||||||
|
|
||||||
JSON_H = \
|
JSON_H = \
|
||||||
|
@ -261,18 +262,19 @@ libbitcoin_common_a_SOURCES = \
|
||||||
# backward-compatibility objects and their sanity checks are linked.
|
# backward-compatibility objects and their sanity checks are linked.
|
||||||
libbitcoin_util_a_CPPFLAGS = $(BITCOIN_INCLUDES)
|
libbitcoin_util_a_CPPFLAGS = $(BITCOIN_INCLUDES)
|
||||||
libbitcoin_util_a_SOURCES = \
|
libbitcoin_util_a_SOURCES = \
|
||||||
compat/strnlen.cpp \
|
arith_uint256.cpp \
|
||||||
compat/glibc_sanity.cpp \
|
|
||||||
compat/glibcxx_sanity.cpp \
|
|
||||||
chainparamsbase.cpp \
|
chainparamsbase.cpp \
|
||||||
clientversion.cpp \
|
clientversion.cpp \
|
||||||
|
compat/glibc_sanity.cpp \
|
||||||
|
compat/glibcxx_sanity.cpp \
|
||||||
|
compat/strnlen.cpp \
|
||||||
random.cpp \
|
random.cpp \
|
||||||
rpcprotocol.cpp \
|
rpcprotocol.cpp \
|
||||||
sync.cpp \
|
sync.cpp \
|
||||||
uint256.cpp \
|
uint256.cpp \
|
||||||
util.cpp \
|
util.cpp \
|
||||||
utilstrencodings.cpp \
|
|
||||||
utilmoneystr.cpp \
|
utilmoneystr.cpp \
|
||||||
|
utilstrencodings.cpp \
|
||||||
utiltime.cpp \
|
utiltime.cpp \
|
||||||
$(BITCOIN_CORE_H)
|
$(BITCOIN_CORE_H)
|
||||||
|
|
||||||
|
@ -352,19 +354,20 @@ bitcoin_cli_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
|
||||||
if BUILD_BITCOIN_LIBS
|
if BUILD_BITCOIN_LIBS
|
||||||
include_HEADERS = script/bitcoinconsensus.h
|
include_HEADERS = script/bitcoinconsensus.h
|
||||||
libbitcoinconsensus_la_SOURCES = \
|
libbitcoinconsensus_la_SOURCES = \
|
||||||
primitives/transaction.cpp \
|
arith_uint256.cpp \
|
||||||
crypto/hmac_sha512.cpp \
|
crypto/hmac_sha512.cpp \
|
||||||
|
crypto/ripemd160.cpp \
|
||||||
crypto/sha1.cpp \
|
crypto/sha1.cpp \
|
||||||
crypto/sha256.cpp \
|
crypto/sha256.cpp \
|
||||||
crypto/sha512.cpp \
|
crypto/sha512.cpp \
|
||||||
crypto/ripemd160.cpp \
|
|
||||||
eccryptoverify.cpp \
|
eccryptoverify.cpp \
|
||||||
ecwrapper.cpp \
|
ecwrapper.cpp \
|
||||||
hash.cpp \
|
hash.cpp \
|
||||||
|
primitives/transaction.cpp \
|
||||||
pubkey.cpp \
|
pubkey.cpp \
|
||||||
script/script.cpp \
|
|
||||||
script/interpreter.cpp \
|
|
||||||
script/bitcoinconsensus.cpp \
|
script/bitcoinconsensus.cpp \
|
||||||
|
script/interpreter.cpp \
|
||||||
|
script/script.cpp \
|
||||||
uint256.cpp \
|
uint256.cpp \
|
||||||
utilstrencodings.cpp
|
utilstrencodings.cpp
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,7 @@ RAW_TEST_FILES = test/data/alertTests.raw
|
||||||
GENERATED_TEST_FILES = $(JSON_TEST_FILES:.json=.json.h) $(RAW_TEST_FILES:.raw=.raw.h)
|
GENERATED_TEST_FILES = $(JSON_TEST_FILES:.json=.json.h) $(RAW_TEST_FILES:.raw=.raw.h)
|
||||||
|
|
||||||
BITCOIN_TESTS =\
|
BITCOIN_TESTS =\
|
||||||
|
test/arith_uint256_tests.cpp \
|
||||||
test/bignum.h \
|
test/bignum.h \
|
||||||
test/alert_tests.cpp \
|
test/alert_tests.cpp \
|
||||||
test/allocator_tests.cpp \
|
test/allocator_tests.cpp \
|
||||||
|
@ -67,7 +68,6 @@ BITCOIN_TESTS =\
|
||||||
test/test_bitcoin.cpp \
|
test/test_bitcoin.cpp \
|
||||||
test/timedata_tests.cpp \
|
test/timedata_tests.cpp \
|
||||||
test/transaction_tests.cpp \
|
test/transaction_tests.cpp \
|
||||||
test/uint256_tests.cpp \
|
|
||||||
test/univalue_tests.cpp \
|
test/univalue_tests.cpp \
|
||||||
test/util_tests.cpp
|
test/util_tests.cpp
|
||||||
|
|
||||||
|
|
357
src/arith_uint256.cpp
Normal file
357
src/arith_uint256.cpp
Normal file
|
@ -0,0 +1,357 @@
|
||||||
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
||||||
|
// Copyright (c) 2009-2014 The Bitcoin developers
|
||||||
|
// Distributed under the MIT software license, see the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
#include "arith_uint256.h"
|
||||||
|
|
||||||
|
#include "utilstrencodings.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
template <unsigned int BITS>
|
||||||
|
base_uint<BITS>::base_uint(const std::string& str)
|
||||||
|
{
|
||||||
|
SetHex(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <unsigned int BITS>
|
||||||
|
base_uint<BITS>::base_uint(const std::vector<unsigned char>& vch)
|
||||||
|
{
|
||||||
|
if (vch.size() != sizeof(pn))
|
||||||
|
throw uint_error("Converting vector of wrong size to base_uint");
|
||||||
|
memcpy(pn, &vch[0], sizeof(pn));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <unsigned int BITS>
|
||||||
|
base_uint<BITS>& base_uint<BITS>::operator<<=(unsigned int shift)
|
||||||
|
{
|
||||||
|
base_uint<BITS> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <unsigned int BITS>
|
||||||
|
base_uint<BITS>& base_uint<BITS>::operator>>=(unsigned int shift)
|
||||||
|
{
|
||||||
|
base_uint<BITS> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <unsigned int BITS>
|
||||||
|
base_uint<BITS>& base_uint<BITS>::operator*=(uint32_t b32)
|
||||||
|
{
|
||||||
|
uint64_t carry = 0;
|
||||||
|
for (int i = 0; i < WIDTH; i++) {
|
||||||
|
uint64_t n = carry + (uint64_t)b32 * pn[i];
|
||||||
|
pn[i] = n & 0xffffffff;
|
||||||
|
carry = n >> 32;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <unsigned int BITS>
|
||||||
|
base_uint<BITS>& base_uint<BITS>::operator*=(const base_uint& b)
|
||||||
|
{
|
||||||
|
base_uint<BITS> a = *this;
|
||||||
|
*this = 0;
|
||||||
|
for (int j = 0; j < WIDTH; j++) {
|
||||||
|
uint64_t carry = 0;
|
||||||
|
for (int i = 0; i + j < WIDTH; i++) {
|
||||||
|
uint64_t n = carry + pn[i + j] + (uint64_t)a.pn[j] * b.pn[i];
|
||||||
|
pn[i + j] = n & 0xffffffff;
|
||||||
|
carry = n >> 32;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <unsigned int BITS>
|
||||||
|
base_uint<BITS>& base_uint<BITS>::operator/=(const base_uint& b)
|
||||||
|
{
|
||||||
|
base_uint<BITS> div = b; // make a copy, so we can shift.
|
||||||
|
base_uint<BITS> num = *this; // make a copy, so we can subtract.
|
||||||
|
*this = 0; // the quotient.
|
||||||
|
int num_bits = num.bits();
|
||||||
|
int div_bits = div.bits();
|
||||||
|
if (div_bits == 0)
|
||||||
|
throw uint_error("Division by zero");
|
||||||
|
if (div_bits > num_bits) // the result is certainly 0.
|
||||||
|
return *this;
|
||||||
|
int shift = num_bits - div_bits;
|
||||||
|
div <<= shift; // shift so that div and num align.
|
||||||
|
while (shift >= 0) {
|
||||||
|
if (num >= div) {
|
||||||
|
num -= div;
|
||||||
|
pn[shift / 32] |= (1 << (shift & 31)); // set a bit of the result.
|
||||||
|
}
|
||||||
|
div >>= 1; // shift back.
|
||||||
|
shift--;
|
||||||
|
}
|
||||||
|
// num now contains the remainder of the division.
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <unsigned int BITS>
|
||||||
|
int base_uint<BITS>::CompareTo(const base_uint<BITS>& b) const
|
||||||
|
{
|
||||||
|
for (int i = WIDTH - 1; i >= 0; i--) {
|
||||||
|
if (pn[i] < b.pn[i])
|
||||||
|
return -1;
|
||||||
|
if (pn[i] > b.pn[i])
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <unsigned int BITS>
|
||||||
|
bool base_uint<BITS>::EqualTo(uint64_t b) const
|
||||||
|
{
|
||||||
|
for (int i = WIDTH - 1; i >= 2; i--) {
|
||||||
|
if (pn[i])
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (pn[1] != (b >> 32))
|
||||||
|
return false;
|
||||||
|
if (pn[0] != (b & 0xfffffffful))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <unsigned int BITS>
|
||||||
|
double base_uint<BITS>::getdouble() const
|
||||||
|
{
|
||||||
|
double ret = 0.0;
|
||||||
|
double fact = 1.0;
|
||||||
|
for (int i = 0; i < WIDTH; i++) {
|
||||||
|
ret += fact * pn[i];
|
||||||
|
fact *= 4294967296.0;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <unsigned int BITS>
|
||||||
|
std::string base_uint<BITS>::GetHex() const
|
||||||
|
{
|
||||||
|
char psz[sizeof(pn) * 2 + 1];
|
||||||
|
for (unsigned int i = 0; i < sizeof(pn); i++)
|
||||||
|
sprintf(psz + i * 2, "%02x", ((unsigned char*)pn)[sizeof(pn) - i - 1]);
|
||||||
|
return std::string(psz, psz + sizeof(pn) * 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <unsigned int BITS>
|
||||||
|
void base_uint<BITS>::SetHex(const char* psz)
|
||||||
|
{
|
||||||
|
memset(pn, 0, sizeof(pn));
|
||||||
|
|
||||||
|
// skip leading spaces
|
||||||
|
while (isspace(*psz))
|
||||||
|
psz++;
|
||||||
|
|
||||||
|
// skip 0x
|
||||||
|
if (psz[0] == '0' && tolower(psz[1]) == 'x')
|
||||||
|
psz += 2;
|
||||||
|
|
||||||
|
// hex string to uint
|
||||||
|
const char* pbegin = psz;
|
||||||
|
while (::HexDigit(*psz) != -1)
|
||||||
|
psz++;
|
||||||
|
psz--;
|
||||||
|
unsigned char* p1 = (unsigned char*)pn;
|
||||||
|
unsigned char* pend = p1 + WIDTH * 4;
|
||||||
|
while (psz >= pbegin && p1 < pend) {
|
||||||
|
*p1 = ::HexDigit(*psz--);
|
||||||
|
if (psz >= pbegin) {
|
||||||
|
*p1 |= ((unsigned char)::HexDigit(*psz--) << 4);
|
||||||
|
p1++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <unsigned int BITS>
|
||||||
|
void base_uint<BITS>::SetHex(const std::string& str)
|
||||||
|
{
|
||||||
|
SetHex(str.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <unsigned int BITS>
|
||||||
|
std::string base_uint<BITS>::ToString() const
|
||||||
|
{
|
||||||
|
return (GetHex());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <unsigned int BITS>
|
||||||
|
unsigned int base_uint<BITS>::bits() const
|
||||||
|
{
|
||||||
|
for (int pos = WIDTH - 1; pos >= 0; pos--) {
|
||||||
|
if (pn[pos]) {
|
||||||
|
for (int bits = 31; bits > 0; bits--) {
|
||||||
|
if (pn[pos] & 1 << bits)
|
||||||
|
return 32 * pos + bits + 1;
|
||||||
|
}
|
||||||
|
return 32 * pos + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Explicit instantiations for base_uint<160>
|
||||||
|
template base_uint<160>::base_uint(const std::string&);
|
||||||
|
template base_uint<160>::base_uint(const std::vector<unsigned char>&);
|
||||||
|
template base_uint<160>& base_uint<160>::operator<<=(unsigned int);
|
||||||
|
template base_uint<160>& base_uint<160>::operator>>=(unsigned int);
|
||||||
|
template base_uint<160>& base_uint<160>::operator*=(uint32_t b32);
|
||||||
|
template base_uint<160>& base_uint<160>::operator*=(const base_uint<160>& b);
|
||||||
|
template base_uint<160>& base_uint<160>::operator/=(const base_uint<160>& b);
|
||||||
|
template int base_uint<160>::CompareTo(const base_uint<160>&) const;
|
||||||
|
template bool base_uint<160>::EqualTo(uint64_t) const;
|
||||||
|
template double base_uint<160>::getdouble() const;
|
||||||
|
template std::string base_uint<160>::GetHex() const;
|
||||||
|
template std::string base_uint<160>::ToString() const;
|
||||||
|
template void base_uint<160>::SetHex(const char*);
|
||||||
|
template void base_uint<160>::SetHex(const std::string&);
|
||||||
|
template unsigned int base_uint<160>::bits() const;
|
||||||
|
|
||||||
|
// Explicit instantiations for base_uint<256>
|
||||||
|
template base_uint<256>::base_uint(const std::string&);
|
||||||
|
template base_uint<256>::base_uint(const std::vector<unsigned char>&);
|
||||||
|
template base_uint<256>& base_uint<256>::operator<<=(unsigned int);
|
||||||
|
template base_uint<256>& base_uint<256>::operator>>=(unsigned int);
|
||||||
|
template base_uint<256>& base_uint<256>::operator*=(uint32_t b32);
|
||||||
|
template base_uint<256>& base_uint<256>::operator*=(const base_uint<256>& b);
|
||||||
|
template base_uint<256>& base_uint<256>::operator/=(const base_uint<256>& b);
|
||||||
|
template int base_uint<256>::CompareTo(const base_uint<256>&) const;
|
||||||
|
template bool base_uint<256>::EqualTo(uint64_t) const;
|
||||||
|
template double base_uint<256>::getdouble() const;
|
||||||
|
template std::string base_uint<256>::GetHex() const;
|
||||||
|
template std::string base_uint<256>::ToString() const;
|
||||||
|
template void base_uint<256>::SetHex(const char*);
|
||||||
|
template void base_uint<256>::SetHex(const std::string&);
|
||||||
|
template unsigned int base_uint<256>::bits() const;
|
||||||
|
|
||||||
|
// This implementation directly uses shifts instead of going
|
||||||
|
// through an intermediate MPI representation.
|
||||||
|
arith_uint256& arith_uint256::SetCompact(uint32_t nCompact, bool* pfNegative, bool* pfOverflow)
|
||||||
|
{
|
||||||
|
int nSize = nCompact >> 24;
|
||||||
|
uint32_t nWord = nCompact & 0x007fffff;
|
||||||
|
if (nSize <= 3) {
|
||||||
|
nWord >>= 8 * (3 - nSize);
|
||||||
|
*this = nWord;
|
||||||
|
} else {
|
||||||
|
*this = nWord;
|
||||||
|
*this <<= 8 * (nSize - 3);
|
||||||
|
}
|
||||||
|
if (pfNegative)
|
||||||
|
*pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0;
|
||||||
|
if (pfOverflow)
|
||||||
|
*pfOverflow = nWord != 0 && ((nSize > 34) ||
|
||||||
|
(nWord > 0xff && nSize > 33) ||
|
||||||
|
(nWord > 0xffff && nSize > 32));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t arith_uint256::GetCompact(bool fNegative) const
|
||||||
|
{
|
||||||
|
int nSize = (bits() + 7) / 8;
|
||||||
|
uint32_t nCompact = 0;
|
||||||
|
if (nSize <= 3) {
|
||||||
|
nCompact = GetLow64() << 8 * (3 - nSize);
|
||||||
|
} else {
|
||||||
|
arith_uint256 bn = *this >> 8 * (nSize - 3);
|
||||||
|
nCompact = bn.GetLow64();
|
||||||
|
}
|
||||||
|
// The 0x00800000 bit denotes the sign.
|
||||||
|
// Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
|
||||||
|
if (nCompact & 0x00800000) {
|
||||||
|
nCompact >>= 8;
|
||||||
|
nSize++;
|
||||||
|
}
|
||||||
|
assert((nCompact & ~0x007fffff) == 0);
|
||||||
|
assert(nSize < 256);
|
||||||
|
nCompact |= nSize << 24;
|
||||||
|
nCompact |= (fNegative && (nCompact & 0x007fffff) ? 0x00800000 : 0);
|
||||||
|
return nCompact;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void inline HashMix(uint32_t& a, uint32_t& b, uint32_t& c)
|
||||||
|
{
|
||||||
|
// Taken from lookup3, by Bob Jenkins.
|
||||||
|
a -= c;
|
||||||
|
a ^= ((c << 4) | (c >> 28));
|
||||||
|
c += b;
|
||||||
|
b -= a;
|
||||||
|
b ^= ((a << 6) | (a >> 26));
|
||||||
|
a += c;
|
||||||
|
c -= b;
|
||||||
|
c ^= ((b << 8) | (b >> 24));
|
||||||
|
b += a;
|
||||||
|
a -= c;
|
||||||
|
a ^= ((c << 16) | (c >> 16));
|
||||||
|
c += b;
|
||||||
|
b -= a;
|
||||||
|
b ^= ((a << 19) | (a >> 13));
|
||||||
|
a += c;
|
||||||
|
c -= b;
|
||||||
|
c ^= ((b << 4) | (b >> 28));
|
||||||
|
b += a;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void inline HashFinal(uint32_t& a, uint32_t& b, uint32_t& c)
|
||||||
|
{
|
||||||
|
// Taken from lookup3, by Bob Jenkins.
|
||||||
|
c ^= b;
|
||||||
|
c -= ((b << 14) | (b >> 18));
|
||||||
|
a ^= c;
|
||||||
|
a -= ((c << 11) | (c >> 21));
|
||||||
|
b ^= a;
|
||||||
|
b -= ((a << 25) | (a >> 7));
|
||||||
|
c ^= b;
|
||||||
|
c -= ((b << 16) | (b >> 16));
|
||||||
|
a ^= c;
|
||||||
|
a -= ((c << 4) | (c >> 28));
|
||||||
|
b ^= a;
|
||||||
|
b -= ((a << 14) | (a >> 18));
|
||||||
|
c ^= b;
|
||||||
|
c -= ((b << 24) | (b >> 8));
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t arith_uint256::GetHash(const arith_uint256& salt) const
|
||||||
|
{
|
||||||
|
uint32_t a, b, c;
|
||||||
|
a = b = c = 0xdeadbeef + (WIDTH << 2);
|
||||||
|
|
||||||
|
a += pn[0] ^ salt.pn[0];
|
||||||
|
b += pn[1] ^ salt.pn[1];
|
||||||
|
c += pn[2] ^ salt.pn[2];
|
||||||
|
HashMix(a, b, c);
|
||||||
|
a += pn[3] ^ salt.pn[3];
|
||||||
|
b += pn[4] ^ salt.pn[4];
|
||||||
|
c += pn[5] ^ salt.pn[5];
|
||||||
|
HashMix(a, b, c);
|
||||||
|
a += pn[6] ^ salt.pn[6];
|
||||||
|
b += pn[7] ^ salt.pn[7];
|
||||||
|
HashFinal(a, b, c);
|
||||||
|
|
||||||
|
return ((((uint64_t)b) << 32) | c);
|
||||||
|
}
|
|
@ -1,19 +1,350 @@
|
||||||
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
||||||
|
// Copyright (c) 2009-2014 The Bitcoin developers
|
||||||
|
// Distributed under the MIT software license, see the accompanying
|
||||||
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
#ifndef BITCOIN_ARITH_UINT256_H
|
#ifndef BITCOIN_ARITH_UINT256_H
|
||||||
#define BITCOIN_ARITH_UINT256_H
|
#define BITCOIN_ARITH_UINT256_H
|
||||||
|
|
||||||
// Temporary for migration to opaque uint160/256
|
#include <assert.h>
|
||||||
#include "uint256.h"
|
#include <cstring>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class arith_uint256 : public uint256 {
|
class uint_error : public std::runtime_error {
|
||||||
public:
|
public:
|
||||||
arith_uint256() {}
|
explicit uint_error(const std::string& str) : std::runtime_error(str) {}
|
||||||
arith_uint256(const base_uint<256>& b) : uint256(b) {}
|
|
||||||
arith_uint256(uint64_t b) : uint256(b) {}
|
|
||||||
explicit arith_uint256(const std::string& str) : uint256(str) {}
|
|
||||||
explicit arith_uint256(const std::vector<unsigned char>& vch) : uint256(vch) {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#define ArithToUint256(x) (x)
|
/** Template base class for unsigned big integers. */
|
||||||
#define UintToArith256(x) (x)
|
template<unsigned int BITS>
|
||||||
|
class base_uint
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
enum { WIDTH=BITS/32 };
|
||||||
|
uint32_t pn[WIDTH];
|
||||||
|
public:
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
explicit base_uint(const std::string& str);
|
||||||
|
explicit base_uint(const std::vector<unsigned char>& vch);
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
double getdouble() const;
|
||||||
|
|
||||||
|
base_uint& operator=(uint64_t b)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
base_uint& operator^=(uint64_t b)
|
||||||
|
{
|
||||||
|
pn[0] ^= (unsigned int)b;
|
||||||
|
pn[1] ^= (unsigned int)(b >> 32);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
base_uint& operator|=(uint64_t b)
|
||||||
|
{
|
||||||
|
pn[0] |= (unsigned int)b;
|
||||||
|
pn[1] |= (unsigned int)(b >> 32);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
base_uint& operator<<=(unsigned int shift);
|
||||||
|
base_uint& operator>>=(unsigned int shift);
|
||||||
|
|
||||||
|
base_uint& operator+=(const base_uint& b)
|
||||||
|
{
|
||||||
|
uint64_t carry = 0;
|
||||||
|
for (int i = 0; i < WIDTH; i++)
|
||||||
|
{
|
||||||
|
uint64_t n = carry + pn[i] + b.pn[i];
|
||||||
|
pn[i] = n & 0xffffffff;
|
||||||
|
carry = n >> 32;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
base_uint& operator-=(const base_uint& b)
|
||||||
|
{
|
||||||
|
*this += -b;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
base_uint& operator+=(uint64_t b64)
|
||||||
|
{
|
||||||
|
base_uint b;
|
||||||
|
b = b64;
|
||||||
|
*this += b;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
base_uint& operator-=(uint64_t b64)
|
||||||
|
{
|
||||||
|
base_uint b;
|
||||||
|
b = b64;
|
||||||
|
*this += -b;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
base_uint& operator*=(uint32_t b32);
|
||||||
|
base_uint& operator*=(const base_uint& b);
|
||||||
|
base_uint& operator/=(const base_uint& b);
|
||||||
|
|
||||||
|
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] == (uint32_t)-1 && i < WIDTH-1)
|
||||||
|
i++;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
const base_uint operator--(int)
|
||||||
|
{
|
||||||
|
// postfix operator
|
||||||
|
const base_uint ret = *this;
|
||||||
|
--(*this);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int CompareTo(const base_uint& b) const;
|
||||||
|
bool EqualTo(uint64_t b) const;
|
||||||
|
|
||||||
|
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, 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, 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; }
|
||||||
|
friend inline const base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
|
||||||
|
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; }
|
||||||
|
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); }
|
||||||
|
|
||||||
|
std::string GetHex() const;
|
||||||
|
void SetHex(const char* psz);
|
||||||
|
void SetHex(const std::string& str);
|
||||||
|
std::string ToString() const;
|
||||||
|
|
||||||
|
unsigned char* begin()
|
||||||
|
{
|
||||||
|
return (unsigned char*)&pn[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char* end()
|
||||||
|
{
|
||||||
|
return (unsigned char*)&pn[WIDTH];
|
||||||
|
}
|
||||||
|
|
||||||
|
const unsigned char* begin() const
|
||||||
|
{
|
||||||
|
return (unsigned char*)&pn[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
const unsigned char* end() const
|
||||||
|
{
|
||||||
|
return (unsigned char*)&pn[WIDTH];
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int size() const
|
||||||
|
{
|
||||||
|
return sizeof(pn);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the position of the highest bit set plus one, or zero if the
|
||||||
|
* value is zero.
|
||||||
|
*/
|
||||||
|
unsigned int bits() const;
|
||||||
|
|
||||||
|
uint64_t GetLow64() const
|
||||||
|
{
|
||||||
|
assert(WIDTH >= 2);
|
||||||
|
return pn[0] | (uint64_t)pn[1] << 32;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int GetSerializeSize(int nType, int nVersion) const
|
||||||
|
{
|
||||||
|
return sizeof(pn);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Stream>
|
||||||
|
void Serialize(Stream& s, int nType, int nVersion) const
|
||||||
|
{
|
||||||
|
s.write((char*)pn, sizeof(pn));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Stream>
|
||||||
|
void Unserialize(Stream& s, int nType, int nVersion)
|
||||||
|
{
|
||||||
|
s.read((char*)pn, sizeof(pn));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Temporary for migration to blob160/256
|
||||||
|
uint64_t GetCheapHash() const
|
||||||
|
{
|
||||||
|
return GetLow64();
|
||||||
|
}
|
||||||
|
void SetNull()
|
||||||
|
{
|
||||||
|
memset(pn, 0, sizeof(pn));
|
||||||
|
}
|
||||||
|
bool IsNull() const
|
||||||
|
{
|
||||||
|
for (int i = 0; i < WIDTH; i++)
|
||||||
|
if (pn[i] != 0)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/** 160-bit unsigned big integer. */
|
||||||
|
class arith_uint160 : public base_uint<160> {
|
||||||
|
public:
|
||||||
|
arith_uint160() {}
|
||||||
|
arith_uint160(const base_uint<160>& b) : base_uint<160>(b) {}
|
||||||
|
arith_uint160(uint64_t b) : base_uint<160>(b) {}
|
||||||
|
explicit arith_uint160(const std::string& str) : base_uint<160>(str) {}
|
||||||
|
explicit arith_uint160(const std::vector<unsigned char>& vch) : base_uint<160>(vch) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
/** 256-bit unsigned big integer. */
|
||||||
|
class arith_uint256 : public base_uint<256> {
|
||||||
|
public:
|
||||||
|
arith_uint256() {}
|
||||||
|
arith_uint256(const base_uint<256>& b) : base_uint<256>(b) {}
|
||||||
|
arith_uint256(uint64_t b) : base_uint<256>(b) {}
|
||||||
|
explicit arith_uint256(const std::string& str) : base_uint<256>(str) {}
|
||||||
|
explicit arith_uint256(const std::vector<unsigned char>& vch) : base_uint<256>(vch) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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)
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = NULL, bool *pfOverflow = NULL);
|
||||||
|
uint32_t GetCompact(bool fNegative = false) const;
|
||||||
|
|
||||||
|
uint64_t GetHash(const arith_uint256& salt) const;
|
||||||
|
};
|
||||||
|
|
||||||
#endif // BITCOIN_UINT256_H
|
#endif // BITCOIN_UINT256_H
|
||||||
|
|
|
@ -8,11 +8,11 @@
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include "uint256.h"
|
#include "arith_uint256.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "version.h"
|
#include "version.h"
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE(uint256_tests)
|
BOOST_AUTO_TEST_SUITE(arith_uint256_tests)
|
||||||
|
|
||||||
const unsigned char R1Array[] =
|
const unsigned char R1Array[] =
|
||||||
"\x9c\x52\x4a\xdb\xcf\x56\x11\x12\x2b\x29\x12\x5e\x5d\x35\xd2\xd2"
|
"\x9c\x52\x4a\xdb\xcf\x56\x11\x12\x2b\x29\x12\x5e\x5d\x35\xd2\xd2"
|
||||||
|
@ -20,38 +20,38 @@ const unsigned char R1Array[] =
|
||||||
const char R1ArrayHex[] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c";
|
const char R1ArrayHex[] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c";
|
||||||
const double R1Ldouble = 0.4887374590559308955; // R1L equals roughly R1Ldouble * 2^256
|
const double R1Ldouble = 0.4887374590559308955; // R1L equals roughly R1Ldouble * 2^256
|
||||||
const double R1Sdouble = 0.7096329412477836074;
|
const double R1Sdouble = 0.7096329412477836074;
|
||||||
const uint256 R1L = uint256(std::vector<unsigned char>(R1Array,R1Array+32));
|
const arith_uint256 R1L = arith_uint256(std::vector<unsigned char>(R1Array,R1Array+32));
|
||||||
const uint160 R1S = uint160(std::vector<unsigned char>(R1Array,R1Array+20));
|
const arith_uint160 R1S = arith_uint160(std::vector<unsigned char>(R1Array,R1Array+20));
|
||||||
const uint64_t R1LLow64 = 0x121156cfdb4a529cULL;
|
const uint64_t R1LLow64 = 0x121156cfdb4a529cULL;
|
||||||
|
|
||||||
const unsigned char R2Array[] =
|
const unsigned char R2Array[] =
|
||||||
"\x70\x32\x1d\x7c\x47\xa5\x6b\x40\x26\x7e\x0a\xc3\xa6\x9c\xb6\xbf"
|
"\x70\x32\x1d\x7c\x47\xa5\x6b\x40\x26\x7e\x0a\xc3\xa6\x9c\xb6\xbf"
|
||||||
"\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7";
|
"\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7";
|
||||||
const uint256 R2L = uint256(std::vector<unsigned char>(R2Array,R2Array+32));
|
const arith_uint256 R2L = arith_uint256(std::vector<unsigned char>(R2Array,R2Array+32));
|
||||||
const uint160 R2S = uint160(std::vector<unsigned char>(R2Array,R2Array+20));
|
const arith_uint160 R2S = arith_uint160(std::vector<unsigned char>(R2Array,R2Array+20));
|
||||||
|
|
||||||
const char R1LplusR2L[] = "549FB09FEA236A1EA3E31D4D58F1B1369288D204211CA751527CFC175767850C";
|
const char R1LplusR2L[] = "549FB09FEA236A1EA3E31D4D58F1B1369288D204211CA751527CFC175767850C";
|
||||||
|
|
||||||
const unsigned char ZeroArray[] =
|
const unsigned char ZeroArray[] =
|
||||||
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
|
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
|
||||||
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
|
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
|
||||||
const uint256 ZeroL = uint256(std::vector<unsigned char>(ZeroArray,ZeroArray+32));
|
const arith_uint256 ZeroL = arith_uint256(std::vector<unsigned char>(ZeroArray,ZeroArray+32));
|
||||||
const uint160 ZeroS = uint160(std::vector<unsigned char>(ZeroArray,ZeroArray+20));
|
const arith_uint160 ZeroS = arith_uint160(std::vector<unsigned char>(ZeroArray,ZeroArray+20));
|
||||||
|
|
||||||
const unsigned char OneArray[] =
|
const unsigned char OneArray[] =
|
||||||
"\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
|
"\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
|
||||||
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
|
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
|
||||||
const uint256 OneL = uint256(std::vector<unsigned char>(OneArray,OneArray+32));
|
const arith_uint256 OneL = arith_uint256(std::vector<unsigned char>(OneArray,OneArray+32));
|
||||||
const uint160 OneS = uint160(std::vector<unsigned char>(OneArray,OneArray+20));
|
const arith_uint160 OneS = arith_uint160(std::vector<unsigned char>(OneArray,OneArray+20));
|
||||||
|
|
||||||
const unsigned char MaxArray[] =
|
const unsigned char MaxArray[] =
|
||||||
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
|
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
|
||||||
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
|
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
|
||||||
const uint256 MaxL = uint256(std::vector<unsigned char>(MaxArray,MaxArray+32));
|
const arith_uint256 MaxL = arith_uint256(std::vector<unsigned char>(MaxArray,MaxArray+32));
|
||||||
const uint160 MaxS = uint160(std::vector<unsigned char>(MaxArray,MaxArray+20));
|
const arith_uint160 MaxS = arith_uint160(std::vector<unsigned char>(MaxArray,MaxArray+20));
|
||||||
|
|
||||||
const uint256 HalfL = (OneL << 255);
|
const arith_uint256 HalfL = (OneL << 255);
|
||||||
const uint160 HalfS = (OneS << 159);
|
const arith_uint160 HalfS = (OneS << 159);
|
||||||
std::string ArrayToString(const unsigned char A[], unsigned int width)
|
std::string ArrayToString(const unsigned char A[], unsigned int width)
|
||||||
{
|
{
|
||||||
std::stringstream Stream;
|
std::stringstream Stream;
|
||||||
|
@ -66,7 +66,7 @@ std::string ArrayToString(const unsigned char A[], unsigned int width)
|
||||||
BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
|
BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
|
||||||
{
|
{
|
||||||
BOOST_CHECK(1 == 0+1);
|
BOOST_CHECK(1 == 0+1);
|
||||||
// constructor uint256(vector<char>):
|
// constructor arith_uint256(vector<char>):
|
||||||
BOOST_CHECK(R1L.ToString() == ArrayToString(R1Array,32));
|
BOOST_CHECK(R1L.ToString() == ArrayToString(R1Array,32));
|
||||||
BOOST_CHECK(R1S.ToString() == ArrayToString(R1Array,20));
|
BOOST_CHECK(R1S.ToString() == ArrayToString(R1Array,20));
|
||||||
BOOST_CHECK(R2L.ToString() == ArrayToString(R2Array,32));
|
BOOST_CHECK(R2L.ToString() == ArrayToString(R2Array,32));
|
||||||
|
@ -95,7 +95,7 @@ BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
|
||||||
BOOST_CHECK(ZeroL != (OneL << i));
|
BOOST_CHECK(ZeroL != (OneL << i));
|
||||||
BOOST_CHECK((OneL << i) != ZeroL);
|
BOOST_CHECK((OneL << i) != ZeroL);
|
||||||
BOOST_CHECK(R1L != (R1L ^ (OneL << i)));
|
BOOST_CHECK(R1L != (R1L ^ (OneL << i)));
|
||||||
BOOST_CHECK(((uint256(Tmp64) ^ (OneL << i) ) != Tmp64 ));
|
BOOST_CHECK(((arith_uint256(Tmp64) ^ (OneL << i) ) != Tmp64 ));
|
||||||
}
|
}
|
||||||
BOOST_CHECK(ZeroL == (OneL << 256));
|
BOOST_CHECK(ZeroL == (OneL << 256));
|
||||||
|
|
||||||
|
@ -104,67 +104,67 @@ BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
|
||||||
BOOST_CHECK(ZeroS != (OneS << i));
|
BOOST_CHECK(ZeroS != (OneS << i));
|
||||||
BOOST_CHECK((OneS << i) != ZeroS);
|
BOOST_CHECK((OneS << i) != ZeroS);
|
||||||
BOOST_CHECK(R1S != (R1S ^ (OneS << i)));
|
BOOST_CHECK(R1S != (R1S ^ (OneS << i)));
|
||||||
BOOST_CHECK(((uint160(Tmp64) ^ (OneS << i) ) != Tmp64 ));
|
BOOST_CHECK(((arith_uint160(Tmp64) ^ (OneS << i) ) != Tmp64 ));
|
||||||
}
|
}
|
||||||
BOOST_CHECK(ZeroS == (OneS << 256));
|
BOOST_CHECK(ZeroS == (OneS << 256));
|
||||||
|
|
||||||
// String Constructor and Copy Constructor
|
// String Constructor and Copy Constructor
|
||||||
BOOST_CHECK(uint256("0x"+R1L.ToString()) == R1L);
|
BOOST_CHECK(arith_uint256("0x"+R1L.ToString()) == R1L);
|
||||||
BOOST_CHECK(uint256("0x"+R2L.ToString()) == R2L);
|
BOOST_CHECK(arith_uint256("0x"+R2L.ToString()) == R2L);
|
||||||
BOOST_CHECK(uint256("0x"+ZeroL.ToString()) == ZeroL);
|
BOOST_CHECK(arith_uint256("0x"+ZeroL.ToString()) == ZeroL);
|
||||||
BOOST_CHECK(uint256("0x"+OneL.ToString()) == OneL);
|
BOOST_CHECK(arith_uint256("0x"+OneL.ToString()) == OneL);
|
||||||
BOOST_CHECK(uint256("0x"+MaxL.ToString()) == MaxL);
|
BOOST_CHECK(arith_uint256("0x"+MaxL.ToString()) == MaxL);
|
||||||
BOOST_CHECK(uint256(R1L.ToString()) == R1L);
|
BOOST_CHECK(arith_uint256(R1L.ToString()) == R1L);
|
||||||
BOOST_CHECK(uint256(" 0x"+R1L.ToString()+" ") == R1L);
|
BOOST_CHECK(arith_uint256(" 0x"+R1L.ToString()+" ") == R1L);
|
||||||
BOOST_CHECK(uint256("") == ZeroL);
|
BOOST_CHECK(arith_uint256("") == ZeroL);
|
||||||
BOOST_CHECK(R1L == uint256(R1ArrayHex));
|
BOOST_CHECK(R1L == arith_uint256(R1ArrayHex));
|
||||||
BOOST_CHECK(uint256(R1L) == R1L);
|
BOOST_CHECK(arith_uint256(R1L) == R1L);
|
||||||
BOOST_CHECK((uint256(R1L^R2L)^R2L) == R1L);
|
BOOST_CHECK((arith_uint256(R1L^R2L)^R2L) == R1L);
|
||||||
BOOST_CHECK(uint256(ZeroL) == ZeroL);
|
BOOST_CHECK(arith_uint256(ZeroL) == ZeroL);
|
||||||
BOOST_CHECK(uint256(OneL) == OneL);
|
BOOST_CHECK(arith_uint256(OneL) == OneL);
|
||||||
|
|
||||||
BOOST_CHECK(uint160("0x"+R1S.ToString()) == R1S);
|
BOOST_CHECK(arith_uint160("0x"+R1S.ToString()) == R1S);
|
||||||
BOOST_CHECK(uint160("0x"+R2S.ToString()) == R2S);
|
BOOST_CHECK(arith_uint160("0x"+R2S.ToString()) == R2S);
|
||||||
BOOST_CHECK(uint160("0x"+ZeroS.ToString()) == ZeroS);
|
BOOST_CHECK(arith_uint160("0x"+ZeroS.ToString()) == ZeroS);
|
||||||
BOOST_CHECK(uint160("0x"+OneS.ToString()) == OneS);
|
BOOST_CHECK(arith_uint160("0x"+OneS.ToString()) == OneS);
|
||||||
BOOST_CHECK(uint160("0x"+MaxS.ToString()) == MaxS);
|
BOOST_CHECK(arith_uint160("0x"+MaxS.ToString()) == MaxS);
|
||||||
BOOST_CHECK(uint160(R1S.ToString()) == R1S);
|
BOOST_CHECK(arith_uint160(R1S.ToString()) == R1S);
|
||||||
BOOST_CHECK(uint160(" 0x"+R1S.ToString()+" ") == R1S);
|
BOOST_CHECK(arith_uint160(" 0x"+R1S.ToString()+" ") == R1S);
|
||||||
BOOST_CHECK(uint160("") == ZeroS);
|
BOOST_CHECK(arith_uint160("") == ZeroS);
|
||||||
BOOST_CHECK(R1S == uint160(R1ArrayHex));
|
BOOST_CHECK(R1S == arith_uint160(R1ArrayHex));
|
||||||
|
|
||||||
BOOST_CHECK(uint160(R1S) == R1S);
|
BOOST_CHECK(arith_uint160(R1S) == R1S);
|
||||||
BOOST_CHECK((uint160(R1S^R2S)^R2S) == R1S);
|
BOOST_CHECK((arith_uint160(R1S^R2S)^R2S) == R1S);
|
||||||
BOOST_CHECK(uint160(ZeroS) == ZeroS);
|
BOOST_CHECK(arith_uint160(ZeroS) == ZeroS);
|
||||||
BOOST_CHECK(uint160(OneS) == OneS);
|
BOOST_CHECK(arith_uint160(OneS) == OneS);
|
||||||
|
|
||||||
// uint64_t constructor
|
// uint64_t constructor
|
||||||
BOOST_CHECK( (R1L & uint256("0xffffffffffffffff")) == uint256(R1LLow64));
|
BOOST_CHECK( (R1L & arith_uint256("0xffffffffffffffff")) == arith_uint256(R1LLow64));
|
||||||
BOOST_CHECK(ZeroL == uint256(0));
|
BOOST_CHECK(ZeroL == arith_uint256(0));
|
||||||
BOOST_CHECK(OneL == uint256(1));
|
BOOST_CHECK(OneL == arith_uint256(1));
|
||||||
BOOST_CHECK(uint256("0xffffffffffffffff") = uint256(0xffffffffffffffffULL));
|
BOOST_CHECK(arith_uint256("0xffffffffffffffff") = arith_uint256(0xffffffffffffffffULL));
|
||||||
BOOST_CHECK( (R1S & uint160("0xffffffffffffffff")) == uint160(R1LLow64));
|
BOOST_CHECK( (R1S & arith_uint160("0xffffffffffffffff")) == arith_uint160(R1LLow64));
|
||||||
BOOST_CHECK(ZeroS == uint160(0));
|
BOOST_CHECK(ZeroS == arith_uint160(0));
|
||||||
BOOST_CHECK(OneS == uint160(1));
|
BOOST_CHECK(OneS == arith_uint160(1));
|
||||||
BOOST_CHECK(uint160("0xffffffffffffffff") = uint160(0xffffffffffffffffULL));
|
BOOST_CHECK(arith_uint160("0xffffffffffffffff") = arith_uint160(0xffffffffffffffffULL));
|
||||||
|
|
||||||
// Assignment (from base_uint)
|
// Assignment (from base_uint)
|
||||||
uint256 tmpL = ~ZeroL; BOOST_CHECK(tmpL == ~ZeroL);
|
arith_uint256 tmpL = ~ZeroL; BOOST_CHECK(tmpL == ~ZeroL);
|
||||||
tmpL = ~OneL; BOOST_CHECK(tmpL == ~OneL);
|
tmpL = ~OneL; BOOST_CHECK(tmpL == ~OneL);
|
||||||
tmpL = ~R1L; BOOST_CHECK(tmpL == ~R1L);
|
tmpL = ~R1L; BOOST_CHECK(tmpL == ~R1L);
|
||||||
tmpL = ~R2L; BOOST_CHECK(tmpL == ~R2L);
|
tmpL = ~R2L; BOOST_CHECK(tmpL == ~R2L);
|
||||||
tmpL = ~MaxL; BOOST_CHECK(tmpL == ~MaxL);
|
tmpL = ~MaxL; BOOST_CHECK(tmpL == ~MaxL);
|
||||||
uint160 tmpS = ~ZeroS; BOOST_CHECK(tmpS == ~ZeroS);
|
arith_uint160 tmpS = ~ZeroS; BOOST_CHECK(tmpS == ~ZeroS);
|
||||||
tmpS = ~OneS; BOOST_CHECK(tmpS == ~OneS);
|
tmpS = ~OneS; BOOST_CHECK(tmpS == ~OneS);
|
||||||
tmpS = ~R1S; BOOST_CHECK(tmpS == ~R1S);
|
tmpS = ~R1S; BOOST_CHECK(tmpS == ~R1S);
|
||||||
tmpS = ~R2S; BOOST_CHECK(tmpS == ~R2S);
|
tmpS = ~R2S; BOOST_CHECK(tmpS == ~R2S);
|
||||||
tmpS = ~MaxS; BOOST_CHECK(tmpS == ~MaxS);
|
tmpS = ~MaxS; BOOST_CHECK(tmpS == ~MaxS);
|
||||||
|
|
||||||
// Wrong length must throw exception.
|
// Wrong length must throw exception.
|
||||||
BOOST_CHECK_THROW(uint256(std::vector<unsigned char>(OneArray,OneArray+31)), uint_error);
|
BOOST_CHECK_THROW(arith_uint256(std::vector<unsigned char>(OneArray,OneArray+31)), uint_error);
|
||||||
BOOST_CHECK_THROW(uint256(std::vector<unsigned char>(OneArray,OneArray+20)), uint_error);
|
BOOST_CHECK_THROW(arith_uint256(std::vector<unsigned char>(OneArray,OneArray+20)), uint_error);
|
||||||
BOOST_CHECK_THROW(uint160(std::vector<unsigned char>(OneArray,OneArray+32)), uint_error);
|
BOOST_CHECK_THROW(arith_uint160(std::vector<unsigned char>(OneArray,OneArray+32)), uint_error);
|
||||||
BOOST_CHECK_THROW(uint160(std::vector<unsigned char>(OneArray,OneArray+19)), uint_error);
|
BOOST_CHECK_THROW(arith_uint160(std::vector<unsigned char>(OneArray,OneArray+19)), uint_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
void shiftArrayRight(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift)
|
void shiftArrayRight(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift)
|
||||||
|
@ -200,11 +200,11 @@ void shiftArrayLeft(unsigned char* to, const unsigned char* from, unsigned int a
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE( shifts ) { // "<<" ">>" "<<=" ">>="
|
BOOST_AUTO_TEST_CASE( shifts ) { // "<<" ">>" "<<=" ">>="
|
||||||
unsigned char TmpArray[32];
|
unsigned char TmpArray[32];
|
||||||
uint256 TmpL;
|
arith_uint256 TmpL;
|
||||||
for (unsigned int i = 0; i < 256; ++i)
|
for (unsigned int i = 0; i < 256; ++i)
|
||||||
{
|
{
|
||||||
shiftArrayLeft(TmpArray, OneArray, 32, i);
|
shiftArrayLeft(TmpArray, OneArray, 32, i);
|
||||||
BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (OneL << i));
|
BOOST_CHECK(arith_uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (OneL << i));
|
||||||
TmpL = OneL; TmpL <<= i;
|
TmpL = OneL; TmpL <<= i;
|
||||||
BOOST_CHECK(TmpL == (OneL << i));
|
BOOST_CHECK(TmpL == (OneL << i));
|
||||||
BOOST_CHECK((HalfL >> (255-i)) == (OneL << i));
|
BOOST_CHECK((HalfL >> (255-i)) == (OneL << i));
|
||||||
|
@ -212,27 +212,27 @@ BOOST_AUTO_TEST_CASE( shifts ) { // "<<" ">>" "<<=" ">>="
|
||||||
BOOST_CHECK(TmpL == (OneL << i));
|
BOOST_CHECK(TmpL == (OneL << i));
|
||||||
|
|
||||||
shiftArrayLeft(TmpArray, R1Array, 32, i);
|
shiftArrayLeft(TmpArray, R1Array, 32, i);
|
||||||
BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L << i));
|
BOOST_CHECK(arith_uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L << i));
|
||||||
TmpL = R1L; TmpL <<= i;
|
TmpL = R1L; TmpL <<= i;
|
||||||
BOOST_CHECK(TmpL == (R1L << i));
|
BOOST_CHECK(TmpL == (R1L << i));
|
||||||
|
|
||||||
shiftArrayRight(TmpArray, R1Array, 32, i);
|
shiftArrayRight(TmpArray, R1Array, 32, i);
|
||||||
BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L >> i));
|
BOOST_CHECK(arith_uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L >> i));
|
||||||
TmpL = R1L; TmpL >>= i;
|
TmpL = R1L; TmpL >>= i;
|
||||||
BOOST_CHECK(TmpL == (R1L >> i));
|
BOOST_CHECK(TmpL == (R1L >> i));
|
||||||
|
|
||||||
shiftArrayLeft(TmpArray, MaxArray, 32, i);
|
shiftArrayLeft(TmpArray, MaxArray, 32, i);
|
||||||
BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL << i));
|
BOOST_CHECK(arith_uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL << i));
|
||||||
TmpL = MaxL; TmpL <<= i;
|
TmpL = MaxL; TmpL <<= i;
|
||||||
BOOST_CHECK(TmpL == (MaxL << i));
|
BOOST_CHECK(TmpL == (MaxL << i));
|
||||||
|
|
||||||
shiftArrayRight(TmpArray, MaxArray, 32, i);
|
shiftArrayRight(TmpArray, MaxArray, 32, i);
|
||||||
BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL >> i));
|
BOOST_CHECK(arith_uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL >> i));
|
||||||
TmpL = MaxL; TmpL >>= i;
|
TmpL = MaxL; TmpL >>= i;
|
||||||
BOOST_CHECK(TmpL == (MaxL >> i));
|
BOOST_CHECK(TmpL == (MaxL >> i));
|
||||||
}
|
}
|
||||||
uint256 c1L = uint256(0x0123456789abcdefULL);
|
arith_uint256 c1L = arith_uint256(0x0123456789abcdefULL);
|
||||||
uint256 c2L = c1L << 128;
|
arith_uint256 c2L = c1L << 128;
|
||||||
for (unsigned int i = 0; i < 128; ++i) {
|
for (unsigned int i = 0; i < 128; ++i) {
|
||||||
BOOST_CHECK((c1L << i) == (c2L >> (128-i)));
|
BOOST_CHECK((c1L << i) == (c2L >> (128-i)));
|
||||||
}
|
}
|
||||||
|
@ -240,11 +240,11 @@ BOOST_AUTO_TEST_CASE( shifts ) { // "<<" ">>" "<<=" ">>="
|
||||||
BOOST_CHECK((c1L << i) == (c2L << (i-128)));
|
BOOST_CHECK((c1L << i) == (c2L << (i-128)));
|
||||||
}
|
}
|
||||||
|
|
||||||
uint160 TmpS;
|
arith_uint160 TmpS;
|
||||||
for (unsigned int i = 0; i < 160; ++i)
|
for (unsigned int i = 0; i < 160; ++i)
|
||||||
{
|
{
|
||||||
shiftArrayLeft(TmpArray, OneArray, 20, i);
|
shiftArrayLeft(TmpArray, OneArray, 20, i);
|
||||||
BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (OneS << i));
|
BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (OneS << i));
|
||||||
TmpS = OneS; TmpS <<= i;
|
TmpS = OneS; TmpS <<= i;
|
||||||
BOOST_CHECK(TmpS == (OneS << i));
|
BOOST_CHECK(TmpS == (OneS << i));
|
||||||
BOOST_CHECK((HalfS >> (159-i)) == (OneS << i));
|
BOOST_CHECK((HalfS >> (159-i)) == (OneS << i));
|
||||||
|
@ -252,27 +252,27 @@ BOOST_AUTO_TEST_CASE( shifts ) { // "<<" ">>" "<<=" ">>="
|
||||||
BOOST_CHECK(TmpS == (OneS << i));
|
BOOST_CHECK(TmpS == (OneS << i));
|
||||||
|
|
||||||
shiftArrayLeft(TmpArray, R1Array, 20, i);
|
shiftArrayLeft(TmpArray, R1Array, 20, i);
|
||||||
BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (R1S << i));
|
BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (R1S << i));
|
||||||
TmpS = R1S; TmpS <<= i;
|
TmpS = R1S; TmpS <<= i;
|
||||||
BOOST_CHECK(TmpS == (R1S << i));
|
BOOST_CHECK(TmpS == (R1S << i));
|
||||||
|
|
||||||
shiftArrayRight(TmpArray, R1Array, 20, i);
|
shiftArrayRight(TmpArray, R1Array, 20, i);
|
||||||
BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (R1S >> i));
|
BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (R1S >> i));
|
||||||
TmpS = R1S; TmpS >>= i;
|
TmpS = R1S; TmpS >>= i;
|
||||||
BOOST_CHECK(TmpS == (R1S >> i));
|
BOOST_CHECK(TmpS == (R1S >> i));
|
||||||
|
|
||||||
shiftArrayLeft(TmpArray, MaxArray, 20, i);
|
shiftArrayLeft(TmpArray, MaxArray, 20, i);
|
||||||
BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (MaxS << i));
|
BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (MaxS << i));
|
||||||
TmpS = MaxS; TmpS <<= i;
|
TmpS = MaxS; TmpS <<= i;
|
||||||
BOOST_CHECK(TmpS == (MaxS << i));
|
BOOST_CHECK(TmpS == (MaxS << i));
|
||||||
|
|
||||||
shiftArrayRight(TmpArray, MaxArray, 20, i);
|
shiftArrayRight(TmpArray, MaxArray, 20, i);
|
||||||
BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (MaxS >> i));
|
BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (MaxS >> i));
|
||||||
TmpS = MaxS; TmpS >>= i;
|
TmpS = MaxS; TmpS >>= i;
|
||||||
BOOST_CHECK(TmpS == (MaxS >> i));
|
BOOST_CHECK(TmpS == (MaxS >> i));
|
||||||
}
|
}
|
||||||
uint160 c1S = uint160(0x0123456789abcdefULL);
|
arith_uint160 c1S = arith_uint160(0x0123456789abcdefULL);
|
||||||
uint160 c2S = c1S << 80;
|
arith_uint160 c2S = c1S << 80;
|
||||||
for (unsigned int i = 0; i < 80; ++i) {
|
for (unsigned int i = 0; i < 80; ++i) {
|
||||||
BOOST_CHECK((c1S << i) == (c2S >> (80-i)));
|
BOOST_CHECK((c1S << i) == (c2S >> (80-i)));
|
||||||
}
|
}
|
||||||
|
@ -297,8 +297,8 @@ BOOST_AUTO_TEST_CASE( unaryOperators ) // ! ~ -
|
||||||
|
|
||||||
unsigned char TmpArray[32];
|
unsigned char TmpArray[32];
|
||||||
for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = ~R1Array[i]; }
|
for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = ~R1Array[i]; }
|
||||||
BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (~R1L));
|
BOOST_CHECK(arith_uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (~R1L));
|
||||||
BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (~R1S));
|
BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (~R1S));
|
||||||
|
|
||||||
BOOST_CHECK(-ZeroL == ZeroL); BOOST_CHECK(-ZeroS == ZeroS);
|
BOOST_CHECK(-ZeroL == ZeroL); BOOST_CHECK(-ZeroS == ZeroS);
|
||||||
BOOST_CHECK(-R1L == (~R1L)+1);
|
BOOST_CHECK(-R1L == (~R1L)+1);
|
||||||
|
@ -311,12 +311,12 @@ BOOST_AUTO_TEST_CASE( unaryOperators ) // ! ~ -
|
||||||
|
|
||||||
|
|
||||||
// Check if doing _A_ _OP_ _B_ results in the same as applying _OP_ onto each
|
// Check if doing _A_ _OP_ _B_ results in the same as applying _OP_ onto each
|
||||||
// element of Aarray and Barray, and then converting the result into a uint256.
|
// element of Aarray and Barray, and then converting the result into a arith_uint256.
|
||||||
#define CHECKBITWISEOPERATOR(_A_,_B_,_OP_) \
|
#define CHECKBITWISEOPERATOR(_A_,_B_,_OP_) \
|
||||||
for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = _A_##Array[i] _OP_ _B_##Array[i]; } \
|
for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = _A_##Array[i] _OP_ _B_##Array[i]; } \
|
||||||
BOOST_CHECK(uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (_A_##L _OP_ _B_##L)); \
|
BOOST_CHECK(arith_uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (_A_##L _OP_ _B_##L)); \
|
||||||
for (unsigned int i = 0; i < 20; ++i) { TmpArray[i] = _A_##Array[i] _OP_ _B_##Array[i]; } \
|
for (unsigned int i = 0; i < 20; ++i) { TmpArray[i] = _A_##Array[i] _OP_ _B_##Array[i]; } \
|
||||||
BOOST_CHECK(uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (_A_##S _OP_ _B_##S));
|
BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (_A_##S _OP_ _B_##S));
|
||||||
|
|
||||||
#define CHECKASSIGNMENTOPERATOR(_A_,_B_,_OP_) \
|
#define CHECKASSIGNMENTOPERATOR(_A_,_B_,_OP_) \
|
||||||
TmpL = _A_##L; TmpL _OP_##= _B_##L; BOOST_CHECK(TmpL == (_A_##L _OP_ _B_##L)); \
|
TmpL = _A_##L; TmpL _OP_##= _B_##L; BOOST_CHECK(TmpL == (_A_##L _OP_ _B_##L)); \
|
||||||
|
@ -342,8 +342,8 @@ BOOST_AUTO_TEST_CASE( bitwiseOperators )
|
||||||
CHECKBITWISEOPERATOR(Max,R1,^)
|
CHECKBITWISEOPERATOR(Max,R1,^)
|
||||||
CHECKBITWISEOPERATOR(Max,R1,&)
|
CHECKBITWISEOPERATOR(Max,R1,&)
|
||||||
|
|
||||||
uint256 TmpL;
|
arith_uint256 TmpL;
|
||||||
uint160 TmpS;
|
arith_uint160 TmpS;
|
||||||
CHECKASSIGNMENTOPERATOR(R1,R2,|)
|
CHECKASSIGNMENTOPERATOR(R1,R2,|)
|
||||||
CHECKASSIGNMENTOPERATOR(R1,R2,^)
|
CHECKASSIGNMENTOPERATOR(R1,R2,^)
|
||||||
CHECKASSIGNMENTOPERATOR(R1,R2,&)
|
CHECKASSIGNMENTOPERATOR(R1,R2,&)
|
||||||
|
@ -361,19 +361,19 @@ BOOST_AUTO_TEST_CASE( bitwiseOperators )
|
||||||
CHECKASSIGNMENTOPERATOR(Max,R1,&)
|
CHECKASSIGNMENTOPERATOR(Max,R1,&)
|
||||||
|
|
||||||
uint64_t Tmp64 = 0xe1db685c9a0b47a2ULL;
|
uint64_t Tmp64 = 0xe1db685c9a0b47a2ULL;
|
||||||
TmpL = R1L; TmpL |= Tmp64; BOOST_CHECK(TmpL == (R1L | uint256(Tmp64)));
|
TmpL = R1L; TmpL |= Tmp64; BOOST_CHECK(TmpL == (R1L | arith_uint256(Tmp64)));
|
||||||
TmpS = R1S; TmpS |= Tmp64; BOOST_CHECK(TmpS == (R1S | uint160(Tmp64)));
|
TmpS = R1S; TmpS |= Tmp64; BOOST_CHECK(TmpS == (R1S | arith_uint160(Tmp64)));
|
||||||
TmpL = R1L; TmpL |= 0; BOOST_CHECK(TmpL == R1L);
|
TmpL = R1L; TmpL |= 0; BOOST_CHECK(TmpL == R1L);
|
||||||
TmpS = R1S; TmpS |= 0; BOOST_CHECK(TmpS == R1S);
|
TmpS = R1S; TmpS |= 0; BOOST_CHECK(TmpS == R1S);
|
||||||
TmpL ^= 0; BOOST_CHECK(TmpL == R1L);
|
TmpL ^= 0; BOOST_CHECK(TmpL == R1L);
|
||||||
TmpS ^= 0; BOOST_CHECK(TmpS == R1S);
|
TmpS ^= 0; BOOST_CHECK(TmpS == R1S);
|
||||||
TmpL ^= Tmp64; BOOST_CHECK(TmpL == (R1L ^ uint256(Tmp64)));
|
TmpL ^= Tmp64; BOOST_CHECK(TmpL == (R1L ^ arith_uint256(Tmp64)));
|
||||||
TmpS ^= Tmp64; BOOST_CHECK(TmpS == (R1S ^ uint160(Tmp64)));
|
TmpS ^= Tmp64; BOOST_CHECK(TmpS == (R1S ^ arith_uint160(Tmp64)));
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
|
BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
|
||||||
{
|
{
|
||||||
uint256 TmpL;
|
arith_uint256 TmpL;
|
||||||
for (unsigned int i = 0; i < 256; ++i) {
|
for (unsigned int i = 0; i < 256; ++i) {
|
||||||
TmpL= OneL<< i;
|
TmpL= OneL<< i;
|
||||||
BOOST_CHECK( TmpL >= ZeroL && TmpL > ZeroL && ZeroL < TmpL && ZeroL <= TmpL);
|
BOOST_CHECK( TmpL >= ZeroL && TmpL > ZeroL && ZeroL < TmpL && ZeroL <= TmpL);
|
||||||
|
@ -383,7 +383,7 @@ BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
|
||||||
BOOST_CHECK( R1L <= TmpL ); BOOST_CHECK( (R1L == TmpL) != (R1L < TmpL)); BOOST_CHECK( (TmpL == R1L) || !( R1L >= TmpL));
|
BOOST_CHECK( R1L <= TmpL ); BOOST_CHECK( (R1L == TmpL) != (R1L < TmpL)); BOOST_CHECK( (TmpL == R1L) || !( R1L >= TmpL));
|
||||||
BOOST_CHECK(! (TmpL < R1L)); BOOST_CHECK(! (R1L > TmpL));
|
BOOST_CHECK(! (TmpL < R1L)); BOOST_CHECK(! (R1L > TmpL));
|
||||||
}
|
}
|
||||||
uint160 TmpS;
|
arith_uint160 TmpS;
|
||||||
for (unsigned int i = 0; i < 160; ++i) {
|
for (unsigned int i = 0; i < 160; ++i) {
|
||||||
TmpS= OneS<< i;
|
TmpS= OneS<< i;
|
||||||
BOOST_CHECK( TmpS >= ZeroS && TmpS > ZeroS && ZeroS < TmpS && ZeroS <= TmpS);
|
BOOST_CHECK( TmpS >= ZeroS && TmpS > ZeroS && ZeroS < TmpS && ZeroS <= TmpS);
|
||||||
|
@ -397,8 +397,8 @@ BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE( plusMinus )
|
BOOST_AUTO_TEST_CASE( plusMinus )
|
||||||
{
|
{
|
||||||
uint256 TmpL = 0;
|
arith_uint256 TmpL = 0;
|
||||||
BOOST_CHECK(R1L+R2L == uint256(R1LplusR2L));
|
BOOST_CHECK(R1L+R2L == arith_uint256(R1LplusR2L));
|
||||||
TmpL += R1L;
|
TmpL += R1L;
|
||||||
BOOST_CHECK(TmpL == R1L);
|
BOOST_CHECK(TmpL == R1L);
|
||||||
TmpL += R2L;
|
TmpL += R2L;
|
||||||
|
@ -416,9 +416,9 @@ BOOST_AUTO_TEST_CASE( plusMinus )
|
||||||
BOOST_CHECK( TmpL++ == (MaxL>>i) );
|
BOOST_CHECK( TmpL++ == (MaxL>>i) );
|
||||||
BOOST_CHECK( TmpL == (HalfL >> (i-1)));
|
BOOST_CHECK( TmpL == (HalfL >> (i-1)));
|
||||||
}
|
}
|
||||||
BOOST_CHECK(uint256(0xbedc77e27940a7ULL) + 0xee8d836fce66fbULL == uint256(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL));
|
BOOST_CHECK(arith_uint256(0xbedc77e27940a7ULL) + 0xee8d836fce66fbULL == arith_uint256(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL));
|
||||||
TmpL = uint256(0xbedc77e27940a7ULL); TmpL += 0xee8d836fce66fbULL;
|
TmpL = arith_uint256(0xbedc77e27940a7ULL); TmpL += 0xee8d836fce66fbULL;
|
||||||
BOOST_CHECK(TmpL == uint256(0xbedc77e27940a7ULL+0xee8d836fce66fbULL));
|
BOOST_CHECK(TmpL == arith_uint256(0xbedc77e27940a7ULL+0xee8d836fce66fbULL));
|
||||||
TmpL -= 0xee8d836fce66fbULL; BOOST_CHECK(TmpL == 0xbedc77e27940a7ULL);
|
TmpL -= 0xee8d836fce66fbULL; BOOST_CHECK(TmpL == 0xbedc77e27940a7ULL);
|
||||||
TmpL = R1L;
|
TmpL = R1L;
|
||||||
BOOST_CHECK(++TmpL == R1L+1);
|
BOOST_CHECK(++TmpL == R1L+1);
|
||||||
|
@ -439,8 +439,8 @@ BOOST_AUTO_TEST_CASE( plusMinus )
|
||||||
BOOST_CHECK(--TmpL == R1L-1);
|
BOOST_CHECK(--TmpL == R1L-1);
|
||||||
|
|
||||||
// 160-bit; copy-pasted
|
// 160-bit; copy-pasted
|
||||||
uint160 TmpS = 0;
|
arith_uint160 TmpS = 0;
|
||||||
BOOST_CHECK(R1S+R2S == uint160(R1LplusR2L));
|
BOOST_CHECK(R1S+R2S == arith_uint160(R1LplusR2L));
|
||||||
TmpS += R1S;
|
TmpS += R1S;
|
||||||
BOOST_CHECK(TmpS == R1S);
|
BOOST_CHECK(TmpS == R1S);
|
||||||
TmpS += R2S;
|
TmpS += R2S;
|
||||||
|
@ -458,9 +458,9 @@ BOOST_AUTO_TEST_CASE( plusMinus )
|
||||||
BOOST_CHECK( TmpS++ == (MaxS>>i) );
|
BOOST_CHECK( TmpS++ == (MaxS>>i) );
|
||||||
BOOST_CHECK( TmpS == (HalfS >> (i-1)));
|
BOOST_CHECK( TmpS == (HalfS >> (i-1)));
|
||||||
}
|
}
|
||||||
BOOST_CHECK(uint160(0xbedc77e27940a7ULL) + 0xee8d836fce66fbULL == uint160(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL));
|
BOOST_CHECK(arith_uint160(0xbedc77e27940a7ULL) + 0xee8d836fce66fbULL == arith_uint160(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL));
|
||||||
TmpS = uint160(0xbedc77e27940a7ULL); TmpS += 0xee8d836fce66fbULL;
|
TmpS = arith_uint160(0xbedc77e27940a7ULL); TmpS += 0xee8d836fce66fbULL;
|
||||||
BOOST_CHECK(TmpS == uint160(0xbedc77e27940a7ULL+0xee8d836fce66fbULL));
|
BOOST_CHECK(TmpS == arith_uint160(0xbedc77e27940a7ULL+0xee8d836fce66fbULL));
|
||||||
TmpS -= 0xee8d836fce66fbULL; BOOST_CHECK(TmpS == 0xbedc77e27940a7ULL);
|
TmpS -= 0xee8d836fce66fbULL; BOOST_CHECK(TmpS == 0xbedc77e27940a7ULL);
|
||||||
TmpS = R1S;
|
TmpS = R1S;
|
||||||
BOOST_CHECK(++TmpS == R1S+1);
|
BOOST_CHECK(++TmpS == R1S+1);
|
||||||
|
@ -521,8 +521,8 @@ BOOST_AUTO_TEST_CASE( multiply )
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE( divide )
|
BOOST_AUTO_TEST_CASE( divide )
|
||||||
{
|
{
|
||||||
uint256 D1L("AD7133AC1977FA2B7");
|
arith_uint256 D1L("AD7133AC1977FA2B7");
|
||||||
uint256 D2L("ECD751716");
|
arith_uint256 D2L("ECD751716");
|
||||||
BOOST_CHECK((R1L / D1L).ToString() == "00000000000000000b8ac01106981635d9ed112290f8895545a7654dde28fb3a");
|
BOOST_CHECK((R1L / D1L).ToString() == "00000000000000000b8ac01106981635d9ed112290f8895545a7654dde28fb3a");
|
||||||
BOOST_CHECK((R1L / D2L).ToString() == "000000000873ce8efec5b67150bad3aa8c5fcb70e947586153bf2cec7c37c57a");
|
BOOST_CHECK((R1L / D2L).ToString() == "000000000873ce8efec5b67150bad3aa8c5fcb70e947586153bf2cec7c37c57a");
|
||||||
BOOST_CHECK(R1L / OneL == R1L);
|
BOOST_CHECK(R1L / OneL == R1L);
|
||||||
|
@ -536,8 +536,8 @@ BOOST_AUTO_TEST_CASE( divide )
|
||||||
BOOST_CHECK(MaxL / R2L == 1);
|
BOOST_CHECK(MaxL / R2L == 1);
|
||||||
BOOST_CHECK_THROW(R2L / ZeroL, uint_error);
|
BOOST_CHECK_THROW(R2L / ZeroL, uint_error);
|
||||||
|
|
||||||
uint160 D1S("D3C5EDCDEA54EB92679F0A4B4");
|
arith_uint160 D1S("D3C5EDCDEA54EB92679F0A4B4");
|
||||||
uint160 D2S("13037");
|
arith_uint160 D2S("13037");
|
||||||
BOOST_CHECK((R1S / D1S).ToString() == "0000000000000000000000000db9af3beade6c02");
|
BOOST_CHECK((R1S / D1S).ToString() == "0000000000000000000000000db9af3beade6c02");
|
||||||
BOOST_CHECK((R1S / D2S).ToString() == "000098dfb6cc40ca592bf74366794f298ada205c");
|
BOOST_CHECK((R1S / D2S).ToString() == "000098dfb6cc40ca592bf74366794f298ada205c");
|
||||||
BOOST_CHECK(R1S / OneS == R1S);
|
BOOST_CHECK(R1S / OneS == R1S);
|
||||||
|
@ -564,7 +564,7 @@ BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 G
|
||||||
BOOST_CHECK(R2L.GetHex() == R2L.ToString());
|
BOOST_CHECK(R2L.GetHex() == R2L.ToString());
|
||||||
BOOST_CHECK(OneL.GetHex() == OneL.ToString());
|
BOOST_CHECK(OneL.GetHex() == OneL.ToString());
|
||||||
BOOST_CHECK(MaxL.GetHex() == MaxL.ToString());
|
BOOST_CHECK(MaxL.GetHex() == MaxL.ToString());
|
||||||
uint256 TmpL(R1L);
|
arith_uint256 TmpL(R1L);
|
||||||
BOOST_CHECK(TmpL == R1L);
|
BOOST_CHECK(TmpL == R1L);
|
||||||
TmpL.SetHex(R2L.ToString()); BOOST_CHECK(TmpL == R2L);
|
TmpL.SetHex(R2L.ToString()); BOOST_CHECK(TmpL == R2L);
|
||||||
TmpL.SetHex(ZeroL.ToString()); BOOST_CHECK(TmpL == 0);
|
TmpL.SetHex(ZeroL.ToString()); BOOST_CHECK(TmpL == 0);
|
||||||
|
@ -612,7 +612,7 @@ BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 G
|
||||||
BOOST_CHECK(R2S.GetHex() == R2S.ToString());
|
BOOST_CHECK(R2S.GetHex() == R2S.ToString());
|
||||||
BOOST_CHECK(OneS.GetHex() == OneS.ToString());
|
BOOST_CHECK(OneS.GetHex() == OneS.ToString());
|
||||||
BOOST_CHECK(MaxS.GetHex() == MaxS.ToString());
|
BOOST_CHECK(MaxS.GetHex() == MaxS.ToString());
|
||||||
uint160 TmpS(R1S);
|
arith_uint160 TmpS(R1S);
|
||||||
BOOST_CHECK(TmpS == R1S);
|
BOOST_CHECK(TmpS == R1S);
|
||||||
TmpS.SetHex(R2S.ToString()); BOOST_CHECK(TmpS == R2S);
|
TmpS.SetHex(R2S.ToString()); BOOST_CHECK(TmpS == R2S);
|
||||||
TmpS.SetHex(ZeroS.ToString()); BOOST_CHECK(TmpS == 0);
|
TmpS.SetHex(ZeroS.ToString()); BOOST_CHECK(TmpS == 0);
|
||||||
|
@ -677,7 +677,7 @@ BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 G
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(bignum_SetCompact)
|
BOOST_AUTO_TEST_CASE(bignum_SetCompact)
|
||||||
{
|
{
|
||||||
uint256 num;
|
arith_uint256 num;
|
||||||
bool fNegative;
|
bool fNegative;
|
||||||
bool fOverflow;
|
bool fOverflow;
|
||||||
num.SetCompact(0, &fNegative, &fOverflow);
|
num.SetCompact(0, &fNegative, &fOverflow);
|
||||||
|
@ -834,4 +834,3 @@ BOOST_AUTO_TEST_CASE( getmaxcoverage ) // some more tests just to get 100% cover
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
281
src/uint256.cpp
281
src/uint256.cpp
|
@ -11,158 +11,25 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
template <unsigned int BITS>
|
template <unsigned int BITS>
|
||||||
base_uint<BITS>::base_uint(const std::string& str)
|
base_blob<BITS>::base_blob(const std::vector<unsigned char>& vch)
|
||||||
{
|
{
|
||||||
SetHex(str);
|
assert(vch.size() == sizeof(data));
|
||||||
|
memcpy(data, &vch[0], sizeof(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <unsigned int BITS>
|
template <unsigned int BITS>
|
||||||
base_uint<BITS>::base_uint(const std::vector<unsigned char>& vch)
|
std::string base_blob<BITS>::GetHex() const
|
||||||
{
|
{
|
||||||
if (vch.size() != sizeof(pn))
|
char psz[sizeof(data) * 2 + 1];
|
||||||
throw uint_error("Converting vector of wrong size to base_uint");
|
for (unsigned int i = 0; i < sizeof(data); i++)
|
||||||
memcpy(pn, &vch[0], sizeof(pn));
|
sprintf(psz + i * 2, "%02x", data[sizeof(data) - i - 1]);
|
||||||
|
return std::string(psz, psz + sizeof(data) * 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <unsigned int BITS>
|
template <unsigned int BITS>
|
||||||
base_uint<BITS>& base_uint<BITS>::operator<<=(unsigned int shift)
|
void base_blob<BITS>::SetHex(const char* psz)
|
||||||
{
|
{
|
||||||
base_uint<BITS> a(*this);
|
memset(data, 0, sizeof(data));
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <unsigned int BITS>
|
|
||||||
base_uint<BITS>& base_uint<BITS>::operator>>=(unsigned int shift)
|
|
||||||
{
|
|
||||||
base_uint<BITS> 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <unsigned int BITS>
|
|
||||||
base_uint<BITS>& base_uint<BITS>::operator*=(uint32_t b32)
|
|
||||||
{
|
|
||||||
uint64_t carry = 0;
|
|
||||||
for (int i = 0; i < WIDTH; i++) {
|
|
||||||
uint64_t n = carry + (uint64_t)b32 * pn[i];
|
|
||||||
pn[i] = n & 0xffffffff;
|
|
||||||
carry = n >> 32;
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <unsigned int BITS>
|
|
||||||
base_uint<BITS>& base_uint<BITS>::operator*=(const base_uint& b)
|
|
||||||
{
|
|
||||||
base_uint<BITS> a = *this;
|
|
||||||
*this = 0;
|
|
||||||
for (int j = 0; j < WIDTH; j++) {
|
|
||||||
uint64_t carry = 0;
|
|
||||||
for (int i = 0; i + j < WIDTH; i++) {
|
|
||||||
uint64_t n = carry + pn[i + j] + (uint64_t)a.pn[j] * b.pn[i];
|
|
||||||
pn[i + j] = n & 0xffffffff;
|
|
||||||
carry = n >> 32;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <unsigned int BITS>
|
|
||||||
base_uint<BITS>& base_uint<BITS>::operator/=(const base_uint& b)
|
|
||||||
{
|
|
||||||
base_uint<BITS> div = b; // make a copy, so we can shift.
|
|
||||||
base_uint<BITS> num = *this; // make a copy, so we can subtract.
|
|
||||||
*this = 0; // the quotient.
|
|
||||||
int num_bits = num.bits();
|
|
||||||
int div_bits = div.bits();
|
|
||||||
if (div_bits == 0)
|
|
||||||
throw uint_error("Division by zero");
|
|
||||||
if (div_bits > num_bits) // the result is certainly 0.
|
|
||||||
return *this;
|
|
||||||
int shift = num_bits - div_bits;
|
|
||||||
div <<= shift; // shift so that div and num align.
|
|
||||||
while (shift >= 0) {
|
|
||||||
if (num >= div) {
|
|
||||||
num -= div;
|
|
||||||
pn[shift / 32] |= (1 << (shift & 31)); // set a bit of the result.
|
|
||||||
}
|
|
||||||
div >>= 1; // shift back.
|
|
||||||
shift--;
|
|
||||||
}
|
|
||||||
// num now contains the remainder of the division.
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <unsigned int BITS>
|
|
||||||
int base_uint<BITS>::CompareTo(const base_uint<BITS>& b) const
|
|
||||||
{
|
|
||||||
for (int i = WIDTH - 1; i >= 0; i--) {
|
|
||||||
if (pn[i] < b.pn[i])
|
|
||||||
return -1;
|
|
||||||
if (pn[i] > b.pn[i])
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <unsigned int BITS>
|
|
||||||
bool base_uint<BITS>::EqualTo(uint64_t b) const
|
|
||||||
{
|
|
||||||
for (int i = WIDTH - 1; i >= 2; i--) {
|
|
||||||
if (pn[i])
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (pn[1] != (b >> 32))
|
|
||||||
return false;
|
|
||||||
if (pn[0] != (b & 0xfffffffful))
|
|
||||||
return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <unsigned int BITS>
|
|
||||||
double base_uint<BITS>::getdouble() const
|
|
||||||
{
|
|
||||||
double ret = 0.0;
|
|
||||||
double fact = 1.0;
|
|
||||||
for (int i = 0; i < WIDTH; i++) {
|
|
||||||
ret += fact * pn[i];
|
|
||||||
fact *= 4294967296.0;
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <unsigned int BITS>
|
|
||||||
std::string base_uint<BITS>::GetHex() const
|
|
||||||
{
|
|
||||||
char psz[sizeof(pn) * 2 + 1];
|
|
||||||
for (unsigned int i = 0; i < sizeof(pn); i++)
|
|
||||||
sprintf(psz + i * 2, "%02x", ((unsigned char*)pn)[sizeof(pn) - i - 1]);
|
|
||||||
return std::string(psz, psz + sizeof(pn) * 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <unsigned int BITS>
|
|
||||||
void base_uint<BITS>::SetHex(const char* psz)
|
|
||||||
{
|
|
||||||
memset(pn, 0, sizeof(pn));
|
|
||||||
|
|
||||||
// skip leading spaces
|
// skip leading spaces
|
||||||
while (isspace(*psz))
|
while (isspace(*psz))
|
||||||
|
@ -177,7 +44,7 @@ void base_uint<BITS>::SetHex(const char* psz)
|
||||||
while (::HexDigit(*psz) != -1)
|
while (::HexDigit(*psz) != -1)
|
||||||
psz++;
|
psz++;
|
||||||
psz--;
|
psz--;
|
||||||
unsigned char* p1 = (unsigned char*)pn;
|
unsigned char* p1 = (unsigned char*)data;
|
||||||
unsigned char* pend = p1 + WIDTH * 4;
|
unsigned char* pend = p1 + WIDTH * 4;
|
||||||
while (psz >= pbegin && p1 < pend) {
|
while (psz >= pbegin && p1 < pend) {
|
||||||
*p1 = ::HexDigit(*psz--);
|
*p1 = ::HexDigit(*psz--);
|
||||||
|
@ -189,110 +56,30 @@ void base_uint<BITS>::SetHex(const char* psz)
|
||||||
}
|
}
|
||||||
|
|
||||||
template <unsigned int BITS>
|
template <unsigned int BITS>
|
||||||
void base_uint<BITS>::SetHex(const std::string& str)
|
void base_blob<BITS>::SetHex(const std::string& str)
|
||||||
{
|
{
|
||||||
SetHex(str.c_str());
|
SetHex(str.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <unsigned int BITS>
|
template <unsigned int BITS>
|
||||||
std::string base_uint<BITS>::ToString() const
|
std::string base_blob<BITS>::ToString() const
|
||||||
{
|
{
|
||||||
return (GetHex());
|
return (GetHex());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <unsigned int BITS>
|
// Explicit instantiations for base_blob<160>
|
||||||
unsigned int base_uint<BITS>::bits() const
|
template base_blob<160>::base_blob(const std::vector<unsigned char>&);
|
||||||
{
|
template std::string base_blob<160>::GetHex() const;
|
||||||
for (int pos = WIDTH - 1; pos >= 0; pos--) {
|
template std::string base_blob<160>::ToString() const;
|
||||||
if (pn[pos]) {
|
template void base_blob<160>::SetHex(const char*);
|
||||||
for (int bits = 31; bits > 0; bits--) {
|
template void base_blob<160>::SetHex(const std::string&);
|
||||||
if (pn[pos] & 1 << bits)
|
|
||||||
return 32 * pos + bits + 1;
|
|
||||||
}
|
|
||||||
return 32 * pos + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Explicit instantiations for base_uint<160>
|
// Explicit instantiations for base_blob<256>
|
||||||
template base_uint<160>::base_uint(const std::string&);
|
template base_blob<256>::base_blob(const std::vector<unsigned char>&);
|
||||||
template base_uint<160>::base_uint(const std::vector<unsigned char>&);
|
template std::string base_blob<256>::GetHex() const;
|
||||||
template base_uint<160>& base_uint<160>::operator<<=(unsigned int);
|
template std::string base_blob<256>::ToString() const;
|
||||||
template base_uint<160>& base_uint<160>::operator>>=(unsigned int);
|
template void base_blob<256>::SetHex(const char*);
|
||||||
template base_uint<160>& base_uint<160>::operator*=(uint32_t b32);
|
template void base_blob<256>::SetHex(const std::string&);
|
||||||
template base_uint<160>& base_uint<160>::operator*=(const base_uint<160>& b);
|
|
||||||
template base_uint<160>& base_uint<160>::operator/=(const base_uint<160>& b);
|
|
||||||
template int base_uint<160>::CompareTo(const base_uint<160>&) const;
|
|
||||||
template bool base_uint<160>::EqualTo(uint64_t) const;
|
|
||||||
template double base_uint<160>::getdouble() const;
|
|
||||||
template std::string base_uint<160>::GetHex() const;
|
|
||||||
template std::string base_uint<160>::ToString() const;
|
|
||||||
template void base_uint<160>::SetHex(const char*);
|
|
||||||
template void base_uint<160>::SetHex(const std::string&);
|
|
||||||
template unsigned int base_uint<160>::bits() const;
|
|
||||||
|
|
||||||
// Explicit instantiations for base_uint<256>
|
|
||||||
template base_uint<256>::base_uint(const std::string&);
|
|
||||||
template base_uint<256>::base_uint(const std::vector<unsigned char>&);
|
|
||||||
template base_uint<256>& base_uint<256>::operator<<=(unsigned int);
|
|
||||||
template base_uint<256>& base_uint<256>::operator>>=(unsigned int);
|
|
||||||
template base_uint<256>& base_uint<256>::operator*=(uint32_t b32);
|
|
||||||
template base_uint<256>& base_uint<256>::operator*=(const base_uint<256>& b);
|
|
||||||
template base_uint<256>& base_uint<256>::operator/=(const base_uint<256>& b);
|
|
||||||
template int base_uint<256>::CompareTo(const base_uint<256>&) const;
|
|
||||||
template bool base_uint<256>::EqualTo(uint64_t) const;
|
|
||||||
template double base_uint<256>::getdouble() const;
|
|
||||||
template std::string base_uint<256>::GetHex() const;
|
|
||||||
template std::string base_uint<256>::ToString() const;
|
|
||||||
template void base_uint<256>::SetHex(const char*);
|
|
||||||
template void base_uint<256>::SetHex(const std::string&);
|
|
||||||
template unsigned int base_uint<256>::bits() const;
|
|
||||||
|
|
||||||
// This implementation directly uses shifts instead of going
|
|
||||||
// through an intermediate MPI representation.
|
|
||||||
uint256& uint256::SetCompact(uint32_t nCompact, bool* pfNegative, bool* pfOverflow)
|
|
||||||
{
|
|
||||||
int nSize = nCompact >> 24;
|
|
||||||
uint32_t nWord = nCompact & 0x007fffff;
|
|
||||||
if (nSize <= 3) {
|
|
||||||
nWord >>= 8 * (3 - nSize);
|
|
||||||
*this = nWord;
|
|
||||||
} else {
|
|
||||||
*this = nWord;
|
|
||||||
*this <<= 8 * (nSize - 3);
|
|
||||||
}
|
|
||||||
if (pfNegative)
|
|
||||||
*pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0;
|
|
||||||
if (pfOverflow)
|
|
||||||
*pfOverflow = nWord != 0 && ((nSize > 34) ||
|
|
||||||
(nWord > 0xff && nSize > 33) ||
|
|
||||||
(nWord > 0xffff && nSize > 32));
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t uint256::GetCompact(bool fNegative) const
|
|
||||||
{
|
|
||||||
int nSize = (bits() + 7) / 8;
|
|
||||||
uint32_t nCompact = 0;
|
|
||||||
if (nSize <= 3) {
|
|
||||||
nCompact = GetLow64() << 8 * (3 - nSize);
|
|
||||||
} else {
|
|
||||||
uint256 bn = *this >> 8 * (nSize - 3);
|
|
||||||
nCompact = bn.GetLow64();
|
|
||||||
}
|
|
||||||
// The 0x00800000 bit denotes the sign.
|
|
||||||
// Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
|
|
||||||
if (nCompact & 0x00800000) {
|
|
||||||
nCompact >>= 8;
|
|
||||||
nSize++;
|
|
||||||
}
|
|
||||||
assert((nCompact & ~0x007fffff) == 0);
|
|
||||||
assert(nSize < 256);
|
|
||||||
nCompact |= nSize << 24;
|
|
||||||
nCompact |= (fNegative && (nCompact & 0x007fffff) ? 0x00800000 : 0);
|
|
||||||
return nCompact;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void inline HashMix(uint32_t& a, uint32_t& b, uint32_t& c)
|
static void inline HashMix(uint32_t& a, uint32_t& b, uint32_t& c)
|
||||||
{
|
{
|
||||||
|
@ -339,18 +126,20 @@ static void inline HashFinal(uint32_t& a, uint32_t& b, uint32_t& c)
|
||||||
uint64_t uint256::GetHash(const uint256& salt) const
|
uint64_t uint256::GetHash(const uint256& salt) const
|
||||||
{
|
{
|
||||||
uint32_t a, b, c;
|
uint32_t a, b, c;
|
||||||
|
const uint32_t *pn = (const uint32_t*)data;
|
||||||
|
const uint32_t *salt_pn = (const uint32_t*)salt.data;
|
||||||
a = b = c = 0xdeadbeef + (WIDTH << 2);
|
a = b = c = 0xdeadbeef + (WIDTH << 2);
|
||||||
|
|
||||||
a += pn[0] ^ salt.pn[0];
|
a += pn[0] ^ salt_pn[0];
|
||||||
b += pn[1] ^ salt.pn[1];
|
b += pn[1] ^ salt_pn[1];
|
||||||
c += pn[2] ^ salt.pn[2];
|
c += pn[2] ^ salt_pn[2];
|
||||||
HashMix(a, b, c);
|
HashMix(a, b, c);
|
||||||
a += pn[3] ^ salt.pn[3];
|
a += pn[3] ^ salt_pn[3];
|
||||||
b += pn[4] ^ salt.pn[4];
|
b += pn[4] ^ salt_pn[4];
|
||||||
c += pn[5] ^ salt.pn[5];
|
c += pn[5] ^ salt_pn[5];
|
||||||
HashMix(a, b, c);
|
HashMix(a, b, c);
|
||||||
a += pn[6] ^ salt.pn[6];
|
a += pn[6] ^ salt_pn[6];
|
||||||
b += pn[7] ^ salt.pn[7];
|
b += pn[7] ^ salt_pn[7];
|
||||||
HashFinal(a, b, c);
|
HashFinal(a, b, c);
|
||||||
|
|
||||||
return ((((uint64_t)b) << 32) | c);
|
return ((((uint64_t)b) << 32) | c);
|
||||||
|
|
339
src/uint256.h
339
src/uint256.h
|
@ -13,217 +13,37 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
class uint_error : public std::runtime_error {
|
/** Template base class for fixed-sized opaque blobs. */
|
||||||
public:
|
|
||||||
explicit uint_error(const std::string& str) : std::runtime_error(str) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
/** Template base class for unsigned big integers. */
|
|
||||||
template<unsigned int BITS>
|
template<unsigned int BITS>
|
||||||
class base_uint
|
class base_blob
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
enum { WIDTH=BITS/32 };
|
enum { WIDTH=BITS/8 };
|
||||||
uint32_t pn[WIDTH];
|
uint8_t data[WIDTH];
|
||||||
public:
|
public:
|
||||||
|
base_blob()
|
||||||
base_uint()
|
|
||||||
{
|
{
|
||||||
for (int i = 0; i < WIDTH; i++)
|
memset(data, 0, sizeof(data));
|
||||||
pn[i] = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
base_uint(const base_uint& b)
|
explicit base_blob(const std::vector<unsigned char>& vch);
|
||||||
|
|
||||||
|
bool IsNull() const
|
||||||
{
|
{
|
||||||
for (int i = 0; i < WIDTH; i++)
|
for (int i = 0; i < WIDTH; i++)
|
||||||
pn[i] = b.pn[i];
|
if (data[i] != 0)
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
explicit base_uint(const std::string& str);
|
|
||||||
explicit base_uint(const std::vector<unsigned char>& vch);
|
|
||||||
|
|
||||||
bool operator!() const
|
|
||||||
{
|
|
||||||
for (int i = 0; i < WIDTH; i++)
|
|
||||||
if (pn[i] != 0)
|
|
||||||
return false;
|
return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const base_uint operator~() const
|
void SetNull()
|
||||||
{
|
{
|
||||||
base_uint ret;
|
memset(data, 0, sizeof(data));
|
||||||
for (int i = 0; i < WIDTH; i++)
|
|
||||||
ret.pn[i] = ~pn[i];
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const base_uint operator-() const
|
friend inline bool operator==(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) == 0; }
|
||||||
{
|
friend inline bool operator!=(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) != 0; }
|
||||||
base_uint ret;
|
friend inline bool operator<(const base_blob& a, const base_blob& b) { return memcmp(a.data, b.data, sizeof(a.data)) < 0; }
|
||||||
for (int i = 0; i < WIDTH; i++)
|
|
||||||
ret.pn[i] = ~pn[i];
|
|
||||||
ret++;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
double getdouble() const;
|
|
||||||
|
|
||||||
base_uint& operator=(uint64_t b)
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
base_uint& operator^=(uint64_t b)
|
|
||||||
{
|
|
||||||
pn[0] ^= (unsigned int)b;
|
|
||||||
pn[1] ^= (unsigned int)(b >> 32);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
base_uint& operator|=(uint64_t b)
|
|
||||||
{
|
|
||||||
pn[0] |= (unsigned int)b;
|
|
||||||
pn[1] |= (unsigned int)(b >> 32);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
base_uint& operator<<=(unsigned int shift);
|
|
||||||
base_uint& operator>>=(unsigned int shift);
|
|
||||||
|
|
||||||
base_uint& operator+=(const base_uint& b)
|
|
||||||
{
|
|
||||||
uint64_t carry = 0;
|
|
||||||
for (int i = 0; i < WIDTH; i++)
|
|
||||||
{
|
|
||||||
uint64_t n = carry + pn[i] + b.pn[i];
|
|
||||||
pn[i] = n & 0xffffffff;
|
|
||||||
carry = n >> 32;
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
base_uint& operator-=(const base_uint& b)
|
|
||||||
{
|
|
||||||
*this += -b;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
base_uint& operator+=(uint64_t b64)
|
|
||||||
{
|
|
||||||
base_uint b;
|
|
||||||
b = b64;
|
|
||||||
*this += b;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
base_uint& operator-=(uint64_t b64)
|
|
||||||
{
|
|
||||||
base_uint b;
|
|
||||||
b = b64;
|
|
||||||
*this += -b;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
base_uint& operator*=(uint32_t b32);
|
|
||||||
base_uint& operator*=(const base_uint& b);
|
|
||||||
base_uint& operator/=(const base_uint& b);
|
|
||||||
|
|
||||||
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] == (uint32_t)-1 && i < WIDTH-1)
|
|
||||||
i++;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
const base_uint operator--(int)
|
|
||||||
{
|
|
||||||
// postfix operator
|
|
||||||
const base_uint ret = *this;
|
|
||||||
--(*this);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int CompareTo(const base_uint& b) const;
|
|
||||||
bool EqualTo(uint64_t b) const;
|
|
||||||
|
|
||||||
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, 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, 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; }
|
|
||||||
friend inline const base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
|
|
||||||
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; }
|
|
||||||
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); }
|
|
||||||
|
|
||||||
std::string GetHex() const;
|
std::string GetHex() const;
|
||||||
void SetHex(const char* psz);
|
void SetHex(const char* psz);
|
||||||
|
@ -232,122 +52,107 @@ public:
|
||||||
|
|
||||||
unsigned char* begin()
|
unsigned char* begin()
|
||||||
{
|
{
|
||||||
return (unsigned char*)&pn[0];
|
return &data[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char* end()
|
unsigned char* end()
|
||||||
{
|
{
|
||||||
return (unsigned char*)&pn[WIDTH];
|
return &data[WIDTH];
|
||||||
}
|
}
|
||||||
|
|
||||||
const unsigned char* begin() const
|
const unsigned char* begin() const
|
||||||
{
|
{
|
||||||
return (unsigned char*)&pn[0];
|
return &data[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
const unsigned char* end() const
|
const unsigned char* end() const
|
||||||
{
|
{
|
||||||
return (unsigned char*)&pn[WIDTH];
|
return &data[WIDTH];
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int size() const
|
unsigned int size() const
|
||||||
{
|
{
|
||||||
return sizeof(pn);
|
return sizeof(data);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the position of the highest bit set plus one, or zero if the
|
|
||||||
* value is zero.
|
|
||||||
*/
|
|
||||||
unsigned int bits() const;
|
|
||||||
|
|
||||||
uint64_t GetLow64() const
|
|
||||||
{
|
|
||||||
assert(WIDTH >= 2);
|
|
||||||
return pn[0] | (uint64_t)pn[1] << 32;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int GetSerializeSize(int nType, int nVersion) const
|
unsigned int GetSerializeSize(int nType, int nVersion) const
|
||||||
{
|
{
|
||||||
return sizeof(pn);
|
return sizeof(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Serialize(Stream& s, int nType, int nVersion) const
|
void Serialize(Stream& s, int nType, int nVersion) const
|
||||||
{
|
{
|
||||||
s.write((char*)pn, sizeof(pn));
|
s.write((char*)data, sizeof(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Unserialize(Stream& s, int nType, int nVersion)
|
void Unserialize(Stream& s, int nType, int nVersion)
|
||||||
{
|
{
|
||||||
s.read((char*)pn, sizeof(pn));
|
s.read((char*)data, sizeof(data));
|
||||||
}
|
|
||||||
|
|
||||||
// Temporary for migration to opaque uint160/256
|
|
||||||
uint64_t GetCheapHash() const
|
|
||||||
{
|
|
||||||
return GetLow64();
|
|
||||||
}
|
|
||||||
void SetNull()
|
|
||||||
{
|
|
||||||
memset(pn, 0, sizeof(pn));
|
|
||||||
}
|
|
||||||
bool IsNull() const
|
|
||||||
{
|
|
||||||
for (int i = 0; i < WIDTH; i++)
|
|
||||||
if (pn[i] != 0)
|
|
||||||
return false;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/** 160-bit unsigned big integer. */
|
/** 160-bit opaque blob.
|
||||||
class uint160 : public base_uint<160> {
|
* @note This type is called uint160 for historical reasons only. It is an opaque
|
||||||
|
* blob of 160 bits and has no integer operations.
|
||||||
|
*/
|
||||||
|
class uint160 : public base_blob<160> {
|
||||||
public:
|
public:
|
||||||
uint160() {}
|
uint160() {}
|
||||||
uint160(const base_uint<160>& b) : base_uint<160>(b) {}
|
uint160(const base_blob<160>& b) : base_blob<160>(b) {}
|
||||||
uint160(uint64_t b) : base_uint<160>(b) {}
|
explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
|
||||||
explicit uint160(const std::string& str) : base_uint<160>(str) {}
|
|
||||||
explicit uint160(const std::vector<unsigned char>& vch) : base_uint<160>(vch) {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/** 256-bit unsigned big integer. */
|
/** 256-bit opaque blob.
|
||||||
class uint256 : public base_uint<256> {
|
* @note This type is called uint256 for historical reasons only. It is an
|
||||||
|
* opaque blob of 256 bits and has no integer operations. Use arith_uint256 if
|
||||||
|
* those are required.
|
||||||
|
*/
|
||||||
|
class uint256 : public base_blob<256> {
|
||||||
public:
|
public:
|
||||||
uint256() {}
|
uint256() {}
|
||||||
uint256(const base_uint<256>& b) : base_uint<256>(b) {}
|
uint256(const base_blob<256>& b) : base_blob<256>(b) {}
|
||||||
uint256(uint64_t b) : base_uint<256>(b) {}
|
explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
|
||||||
explicit uint256(const std::string& str) : base_uint<256>(str) {}
|
|
||||||
explicit uint256(const std::vector<unsigned char>& vch) : base_uint<256>(vch) {}
|
|
||||||
|
|
||||||
/**
|
/** A cheap hash function that just returns 64 bits from the result, it can be
|
||||||
* The "compact" format is a representation of a whole
|
* used when the contents are considered uniformly random. It is not appropriate
|
||||||
* number N using an unsigned 32bit number similar to a
|
* when the value can easily be influenced from outside as e.g. a network adversary could
|
||||||
* floating point format.
|
* provide values to trigger worst-case behavior.
|
||||||
* The most significant 8 bits are the unsigned exponent of base 256.
|
* @note The result of this function is not stable between little and big endian.
|
||||||
* 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)
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
*/
|
||||||
uint256& SetCompact(uint32_t nCompact, bool *pfNegative = NULL, bool *pfOverflow = NULL);
|
uint64_t GetCheapHash() const
|
||||||
uint32_t GetCompact(bool fNegative = false) const;
|
{
|
||||||
|
uint64_t result;
|
||||||
|
memcpy((void*)&result, (void*)data, 8);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A more secure, salted hash function.
|
||||||
|
* @note This hash is not stable between little and big endian.
|
||||||
|
*/
|
||||||
uint64_t GetHash(const uint256& salt) const;
|
uint64_t GetHash(const uint256& salt) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Temporary for migration to opaque uint160/256
|
/* uint256 from const char *.
|
||||||
inline uint256 uint256S(const std::string &x) { return uint256(x); }
|
* This is a separate function because the constructor uint256(const char*) can result
|
||||||
|
* in dangerously catching uint256(0).
|
||||||
|
*/
|
||||||
|
inline uint256 uint256S(const char *str)
|
||||||
|
{
|
||||||
|
uint256 rv;
|
||||||
|
rv.SetHex(str);
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
/* uint256 from std::string.
|
||||||
|
* This is a separate function because the constructor uint256(const std::string &str) can result
|
||||||
|
* in dangerously catching uint256(0) via std::string(const char*).
|
||||||
|
*/
|
||||||
|
inline uint256 uint256S(const std::string& str)
|
||||||
|
{
|
||||||
|
uint256 rv;
|
||||||
|
rv.SetHex(str);
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
|
||||||
#endif // BITCOIN_UINT256_H
|
#endif // BITCOIN_UINT256_H
|
||||||
|
|
Loading…
Reference in a new issue