Remove arith_uint160

We never do 160-bit arithmetic.
This commit is contained in:
Wladimir J. van der Laan 2014-12-16 15:44:57 +01:00
parent dba2e9141a
commit edc720479d
3 changed files with 23 additions and 294 deletions

View File

@ -216,23 +216,6 @@ unsigned int base_uint<BITS>::bits() const
return 0; 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> // Explicit instantiations for base_uint<256>
template base_uint<256>::base_uint(const std::string&); 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(const std::vector<unsigned char>&);

View File

@ -304,16 +304,6 @@ public:
} }
}; };
/** 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. */ /** 256-bit unsigned big integer. */
class arith_uint256 : public base_uint<256> { class arith_uint256 : public base_uint<256> {
public: public:

View File

@ -19,16 +19,13 @@ const unsigned char R1Array[] =
"\x22\x81\xaa\xb5\x33\xf0\x08\x32\xd5\x56\xb1\xf9\xea\xe5\x1d\x7d"; "\x22\x81\xaa\xb5\x33\xf0\x08\x32\xd5\x56\xb1\xf9\xea\xe5\x1d\x7d";
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 arith_uint256 R1L = arith_uint256(std::vector<unsigned char>(R1Array,R1Array+32)); const arith_uint256 R1L = arith_uint256(std::vector<unsigned char>(R1Array,R1Array+32));
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 arith_uint256 R2L = arith_uint256(std::vector<unsigned char>(R2Array,R2Array+32)); const arith_uint256 R2L = arith_uint256(std::vector<unsigned char>(R2Array,R2Array+32));
const arith_uint160 R2S = arith_uint160(std::vector<unsigned char>(R2Array,R2Array+20));
const char R1LplusR2L[] = "549FB09FEA236A1EA3E31D4D58F1B1369288D204211CA751527CFC175767850C"; const char R1LplusR2L[] = "549FB09FEA236A1EA3E31D4D58F1B1369288D204211CA751527CFC175767850C";
@ -36,22 +33,18 @@ 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 arith_uint256 ZeroL = arith_uint256(std::vector<unsigned char>(ZeroArray,ZeroArray+32)); const arith_uint256 ZeroL = arith_uint256(std::vector<unsigned char>(ZeroArray,ZeroArray+32));
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 arith_uint256 OneL = arith_uint256(std::vector<unsigned char>(OneArray,OneArray+32)); const arith_uint256 OneL = arith_uint256(std::vector<unsigned char>(OneArray,OneArray+32));
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 arith_uint256 MaxL = arith_uint256(std::vector<unsigned char>(MaxArray,MaxArray+32)); const arith_uint256 MaxL = arith_uint256(std::vector<unsigned char>(MaxArray,MaxArray+32));
const arith_uint160 MaxS = arith_uint160(std::vector<unsigned char>(MaxArray,MaxArray+20));
const arith_uint256 HalfL = (OneL << 255); const arith_uint256 HalfL = (OneL << 255);
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;
@ -68,26 +61,19 @@ BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
BOOST_CHECK(1 == 0+1); BOOST_CHECK(1 == 0+1);
// constructor arith_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(R2L.ToString() == ArrayToString(R2Array,32)); BOOST_CHECK(R2L.ToString() == ArrayToString(R2Array,32));
BOOST_CHECK(R2S.ToString() == ArrayToString(R2Array,20));
BOOST_CHECK(ZeroL.ToString() == ArrayToString(ZeroArray,32)); BOOST_CHECK(ZeroL.ToString() == ArrayToString(ZeroArray,32));
BOOST_CHECK(ZeroS.ToString() == ArrayToString(ZeroArray,20));
BOOST_CHECK(OneL.ToString() == ArrayToString(OneArray,32)); BOOST_CHECK(OneL.ToString() == ArrayToString(OneArray,32));
BOOST_CHECK(OneS.ToString() == ArrayToString(OneArray,20));
BOOST_CHECK(MaxL.ToString() == ArrayToString(MaxArray,32)); BOOST_CHECK(MaxL.ToString() == ArrayToString(MaxArray,32));
BOOST_CHECK(MaxS.ToString() == ArrayToString(MaxArray,20));
BOOST_CHECK(OneL.ToString() != ArrayToString(ZeroArray,32)); BOOST_CHECK(OneL.ToString() != ArrayToString(ZeroArray,32));
BOOST_CHECK(OneS.ToString() != ArrayToString(ZeroArray,20));
// == and != // == and !=
BOOST_CHECK(R1L != R2L && R1S != R2S); BOOST_CHECK(R1L != R2L);
BOOST_CHECK(ZeroL != OneL && ZeroS != OneS); BOOST_CHECK(ZeroL != OneL);
BOOST_CHECK(OneL != ZeroL && OneS != ZeroS); BOOST_CHECK(OneL != ZeroL);
BOOST_CHECK(MaxL != ZeroL && MaxS != ZeroS); BOOST_CHECK(MaxL != ZeroL);
BOOST_CHECK(~MaxL == ZeroL && ~MaxS == ZeroS); BOOST_CHECK(~MaxL == ZeroL);
BOOST_CHECK( ((R1L ^ R2L) ^ R1L) == R2L); BOOST_CHECK( ((R1L ^ R2L) ^ R1L) == R2L);
BOOST_CHECK( ((R1S ^ R2S) ^ R1S) == R2S);
uint64_t Tmp64 = 0xc4dab720d9c7acaaULL; uint64_t Tmp64 = 0xc4dab720d9c7acaaULL;
for (unsigned int i = 0; i < 256; ++i) for (unsigned int i = 0; i < 256; ++i)
@ -99,15 +85,6 @@ BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
} }
BOOST_CHECK(ZeroL == (OneL << 256)); BOOST_CHECK(ZeroL == (OneL << 256));
for (unsigned int i = 0; i < 160; ++i)
{
BOOST_CHECK(ZeroS != (OneS << i));
BOOST_CHECK((OneS << i) != ZeroS);
BOOST_CHECK(R1S != (R1S ^ (OneS << i)));
BOOST_CHECK(((arith_uint160(Tmp64) ^ (OneS << i) ) != Tmp64 ));
}
BOOST_CHECK(ZeroS == (OneS << 256));
// String Constructor and Copy Constructor // String Constructor and Copy Constructor
BOOST_CHECK(arith_uint256("0x"+R1L.ToString()) == R1L); BOOST_CHECK(arith_uint256("0x"+R1L.ToString()) == R1L);
BOOST_CHECK(arith_uint256("0x"+R2L.ToString()) == R2L); BOOST_CHECK(arith_uint256("0x"+R2L.ToString()) == R2L);
@ -123,30 +100,11 @@ BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
BOOST_CHECK(arith_uint256(ZeroL) == ZeroL); BOOST_CHECK(arith_uint256(ZeroL) == ZeroL);
BOOST_CHECK(arith_uint256(OneL) == OneL); BOOST_CHECK(arith_uint256(OneL) == OneL);
BOOST_CHECK(arith_uint160("0x"+R1S.ToString()) == R1S);
BOOST_CHECK(arith_uint160("0x"+R2S.ToString()) == R2S);
BOOST_CHECK(arith_uint160("0x"+ZeroS.ToString()) == ZeroS);
BOOST_CHECK(arith_uint160("0x"+OneS.ToString()) == OneS);
BOOST_CHECK(arith_uint160("0x"+MaxS.ToString()) == MaxS);
BOOST_CHECK(arith_uint160(R1S.ToString()) == R1S);
BOOST_CHECK(arith_uint160(" 0x"+R1S.ToString()+" ") == R1S);
BOOST_CHECK(arith_uint160("") == ZeroS);
BOOST_CHECK(R1S == arith_uint160(R1ArrayHex));
BOOST_CHECK(arith_uint160(R1S) == R1S);
BOOST_CHECK((arith_uint160(R1S^R2S)^R2S) == R1S);
BOOST_CHECK(arith_uint160(ZeroS) == ZeroS);
BOOST_CHECK(arith_uint160(OneS) == OneS);
// uint64_t constructor // uint64_t constructor
BOOST_CHECK( (R1L & arith_uint256("0xffffffffffffffff")) == arith_uint256(R1LLow64)); BOOST_CHECK( (R1L & arith_uint256("0xffffffffffffffff")) == arith_uint256(R1LLow64));
BOOST_CHECK(ZeroL == arith_uint256(0)); BOOST_CHECK(ZeroL == arith_uint256(0));
BOOST_CHECK(OneL == arith_uint256(1)); BOOST_CHECK(OneL == arith_uint256(1));
BOOST_CHECK(arith_uint256("0xffffffffffffffff") = arith_uint256(0xffffffffffffffffULL)); BOOST_CHECK(arith_uint256("0xffffffffffffffff") = arith_uint256(0xffffffffffffffffULL));
BOOST_CHECK( (R1S & arith_uint160("0xffffffffffffffff")) == arith_uint160(R1LLow64));
BOOST_CHECK(ZeroS == arith_uint160(0));
BOOST_CHECK(OneS == arith_uint160(1));
BOOST_CHECK(arith_uint160("0xffffffffffffffff") = arith_uint160(0xffffffffffffffffULL));
// Assignment (from base_uint) // Assignment (from base_uint)
arith_uint256 tmpL = ~ZeroL; BOOST_CHECK(tmpL == ~ZeroL); arith_uint256 tmpL = ~ZeroL; BOOST_CHECK(tmpL == ~ZeroL);
@ -154,17 +112,10 @@ BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
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);
arith_uint160 tmpS = ~ZeroS; BOOST_CHECK(tmpS == ~ZeroS);
tmpS = ~OneS; BOOST_CHECK(tmpS == ~OneS);
tmpS = ~R1S; BOOST_CHECK(tmpS == ~R1S);
tmpS = ~R2S; BOOST_CHECK(tmpS == ~R2S);
tmpS = ~MaxS; BOOST_CHECK(tmpS == ~MaxS);
// Wrong length must throw exception. // Wrong length must throw exception.
BOOST_CHECK_THROW(arith_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(arith_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(arith_uint160(std::vector<unsigned char>(OneArray,OneArray+32)), 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)
@ -239,74 +190,27 @@ BOOST_AUTO_TEST_CASE( shifts ) { // "<<" ">>" "<<=" ">>="
for (unsigned int i = 128; i < 256; ++i) { for (unsigned int i = 128; i < 256; ++i) {
BOOST_CHECK((c1L << i) == (c2L << (i-128))); BOOST_CHECK((c1L << i) == (c2L << (i-128)));
} }
arith_uint160 TmpS;
for (unsigned int i = 0; i < 160; ++i)
{
shiftArrayLeft(TmpArray, OneArray, 20, i);
BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (OneS << i));
TmpS = OneS; TmpS <<= i;
BOOST_CHECK(TmpS == (OneS << i));
BOOST_CHECK((HalfS >> (159-i)) == (OneS << i));
TmpS = HalfS; TmpS >>= (159-i);
BOOST_CHECK(TmpS == (OneS << i));
shiftArrayLeft(TmpArray, R1Array, 20, i);
BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (R1S << i));
TmpS = R1S; TmpS <<= i;
BOOST_CHECK(TmpS == (R1S << i));
shiftArrayRight(TmpArray, R1Array, 20, i);
BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (R1S >> i));
TmpS = R1S; TmpS >>= i;
BOOST_CHECK(TmpS == (R1S >> i));
shiftArrayLeft(TmpArray, MaxArray, 20, i);
BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (MaxS << i));
TmpS = MaxS; TmpS <<= i;
BOOST_CHECK(TmpS == (MaxS << i));
shiftArrayRight(TmpArray, MaxArray, 20, i);
BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (MaxS >> i));
TmpS = MaxS; TmpS >>= i;
BOOST_CHECK(TmpS == (MaxS >> i));
}
arith_uint160 c1S = arith_uint160(0x0123456789abcdefULL);
arith_uint160 c2S = c1S << 80;
for (unsigned int i = 0; i < 80; ++i) {
BOOST_CHECK((c1S << i) == (c2S >> (80-i)));
}
for (unsigned int i = 80; i < 160; ++i) {
BOOST_CHECK((c1S << i) == (c2S << (i-80)));
}
} }
BOOST_AUTO_TEST_CASE( unaryOperators ) // ! ~ - BOOST_AUTO_TEST_CASE( unaryOperators ) // ! ~ -
{ {
BOOST_CHECK(!ZeroL); BOOST_CHECK(!ZeroS); BOOST_CHECK(!ZeroL);
BOOST_CHECK(!(!OneL));BOOST_CHECK(!(!OneS)); BOOST_CHECK(!(!OneL));
for (unsigned int i = 0; i < 256; ++i) for (unsigned int i = 0; i < 256; ++i)
BOOST_CHECK(!(!(OneL<<i))); BOOST_CHECK(!(!(OneL<<i)));
for (unsigned int i = 0; i < 160; ++i) BOOST_CHECK(!(!R1L));
BOOST_CHECK(!(!(OneS<<i))); BOOST_CHECK(!(!MaxL));
BOOST_CHECK(!(!R1L));BOOST_CHECK(!(!R1S));
BOOST_CHECK(!(!R2S));BOOST_CHECK(!(!R2S));
BOOST_CHECK(!(!MaxL));BOOST_CHECK(!(!MaxS));
BOOST_CHECK(~ZeroL == MaxL); BOOST_CHECK(~ZeroS == MaxS); BOOST_CHECK(~ZeroL == MaxL);
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(arith_uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (~R1L)); BOOST_CHECK(arith_uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (~R1L));
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(-R1L == (~R1L)+1); BOOST_CHECK(-R1L == (~R1L)+1);
BOOST_CHECK(-R1S == (~R1S)+1);
for (unsigned int i = 0; i < 256; ++i) for (unsigned int i = 0; i < 256; ++i)
BOOST_CHECK(-(OneL<<i) == (MaxL << i)); BOOST_CHECK(-(OneL<<i) == (MaxL << i));
for (unsigned int i = 0; i < 160; ++i)
BOOST_CHECK(-(OneS<<i) == (MaxS << i));
} }
@ -314,13 +218,10 @@ BOOST_AUTO_TEST_CASE( unaryOperators ) // ! ~ -
// element of Aarray and Barray, and then converting the result into a arith_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(arith_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]; } \
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));
TmpS = _A_##S; TmpS _OP_##= _B_##S; BOOST_CHECK(TmpS == (_A_##S _OP_ _B_##S));
BOOST_AUTO_TEST_CASE( bitwiseOperators ) BOOST_AUTO_TEST_CASE( bitwiseOperators )
{ {
@ -343,7 +244,6 @@ BOOST_AUTO_TEST_CASE( bitwiseOperators )
CHECKBITWISEOPERATOR(Max,R1,&) CHECKBITWISEOPERATOR(Max,R1,&)
arith_uint256 TmpL; arith_uint256 TmpL;
arith_uint160 TmpS;
CHECKASSIGNMENTOPERATOR(R1,R2,|) CHECKASSIGNMENTOPERATOR(R1,R2,|)
CHECKASSIGNMENTOPERATOR(R1,R2,^) CHECKASSIGNMENTOPERATOR(R1,R2,^)
CHECKASSIGNMENTOPERATOR(R1,R2,&) CHECKASSIGNMENTOPERATOR(R1,R2,&)
@ -362,13 +262,9 @@ BOOST_AUTO_TEST_CASE( bitwiseOperators )
uint64_t Tmp64 = 0xe1db685c9a0b47a2ULL; uint64_t Tmp64 = 0xe1db685c9a0b47a2ULL;
TmpL = R1L; TmpL |= Tmp64; BOOST_CHECK(TmpL == (R1L | arith_uint256(Tmp64))); TmpL = R1L; TmpL |= Tmp64; BOOST_CHECK(TmpL == (R1L | arith_uint256(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);
TmpL ^= 0; BOOST_CHECK(TmpL == R1L); TmpL ^= 0; BOOST_CHECK(TmpL == R1L);
TmpS ^= 0; BOOST_CHECK(TmpS == R1S);
TmpL ^= Tmp64; BOOST_CHECK(TmpL == (R1L ^ arith_uint256(Tmp64))); TmpL ^= Tmp64; BOOST_CHECK(TmpL == (R1L ^ arith_uint256(Tmp64)));
TmpS ^= Tmp64; BOOST_CHECK(TmpS == (R1S ^ arith_uint160(Tmp64)));
} }
BOOST_AUTO_TEST_CASE( comparison ) // <= >= < > BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
@ -383,16 +279,6 @@ 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));
} }
arith_uint160 TmpS;
for (unsigned int i = 0; i < 160; ++i) {
TmpS= OneS<< i;
BOOST_CHECK( TmpS >= ZeroS && TmpS > ZeroS && ZeroS < TmpS && ZeroS <= TmpS);
BOOST_CHECK( TmpS >= 0 && TmpS > 0 && 0 < TmpS && 0 <= TmpS);
TmpS |= R1S;
BOOST_CHECK( TmpS >= R1S ); BOOST_CHECK( (TmpS == R1S) != (TmpS > R1S)); BOOST_CHECK( (TmpS == R1S) || !( TmpS <= R1S));
BOOST_CHECK( R1S <= TmpS ); BOOST_CHECK( (R1S == TmpS) != (R1S < TmpS)); BOOST_CHECK( (TmpS == R1S) || !( R1S >= TmpS));
BOOST_CHECK(! (TmpS < R1S)); BOOST_CHECK(! (R1S > TmpS));
}
} }
BOOST_AUTO_TEST_CASE( plusMinus ) BOOST_AUTO_TEST_CASE( plusMinus )
@ -437,49 +323,6 @@ BOOST_AUTO_TEST_CASE( plusMinus )
} }
TmpL = R1L; TmpL = R1L;
BOOST_CHECK(--TmpL == R1L-1); BOOST_CHECK(--TmpL == R1L-1);
// 160-bit; copy-pasted
arith_uint160 TmpS = 0;
BOOST_CHECK(R1S+R2S == arith_uint160(R1LplusR2L));
TmpS += R1S;
BOOST_CHECK(TmpS == R1S);
TmpS += R2S;
BOOST_CHECK(TmpS == R1S + R2S);
BOOST_CHECK(OneS+MaxS == ZeroS);
BOOST_CHECK(MaxS+OneS == ZeroS);
for (unsigned int i = 1; i < 160; ++i) {
BOOST_CHECK( (MaxS >> i) + OneS == (HalfS >> (i-1)) );
BOOST_CHECK( OneS + (MaxS >> i) == (HalfS >> (i-1)) );
TmpS = (MaxS>>i); TmpS += OneS;
BOOST_CHECK( TmpS == (HalfS >> (i-1)) );
TmpS = (MaxS>>i); TmpS += 1;
BOOST_CHECK( TmpS == (HalfS >> (i-1)) );
TmpS = (MaxS>>i);
BOOST_CHECK( TmpS++ == (MaxS>>i) );
BOOST_CHECK( TmpS == (HalfS >> (i-1)));
}
BOOST_CHECK(arith_uint160(0xbedc77e27940a7ULL) + 0xee8d836fce66fbULL == arith_uint160(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL));
TmpS = arith_uint160(0xbedc77e27940a7ULL); TmpS += 0xee8d836fce66fbULL;
BOOST_CHECK(TmpS == arith_uint160(0xbedc77e27940a7ULL+0xee8d836fce66fbULL));
TmpS -= 0xee8d836fce66fbULL; BOOST_CHECK(TmpS == 0xbedc77e27940a7ULL);
TmpS = R1S;
BOOST_CHECK(++TmpS == R1S+1);
BOOST_CHECK(R1S -(-R2S) == R1S+R2S);
BOOST_CHECK(R1S -(-OneS) == R1S+OneS);
BOOST_CHECK(R1S - OneS == R1S+(-OneS));
for (unsigned int i = 1; i < 160; ++i) {
BOOST_CHECK((MaxS>>i) - (-OneS) == (HalfS >> (i-1)));
BOOST_CHECK((HalfS >> (i-1)) - OneS == (MaxS>>i));
TmpS = (HalfS >> (i-1));
BOOST_CHECK(TmpS-- == (HalfS >> (i-1)));
BOOST_CHECK(TmpS == (MaxS >> i));
TmpS = (HalfS >> (i-1));
BOOST_CHECK(--TmpS == (MaxS >> i));
}
TmpS = R1S;
BOOST_CHECK(--TmpS == R1S-1);
} }
BOOST_AUTO_TEST_CASE( multiply ) BOOST_AUTO_TEST_CASE( multiply )
@ -495,28 +338,12 @@ BOOST_AUTO_TEST_CASE( multiply )
BOOST_CHECK((R2L * OneL) == R2L); BOOST_CHECK((R2L * OneL) == R2L);
BOOST_CHECK((R2L * MaxL) == -R2L); BOOST_CHECK((R2L * MaxL) == -R2L);
BOOST_CHECK((R1S * R1S).ToString() == "a7761bf30d5237e9873f9bff3642a732c4d84f10");
BOOST_CHECK((R1S * R2S).ToString() == "ba51c008df851987d9dd323f0e5de07760529c40");
BOOST_CHECK((R1S * ZeroS) == ZeroS);
BOOST_CHECK((R1S * OneS) == R1S);
BOOST_CHECK((R1S * MaxS) == -R1S);
BOOST_CHECK((R2S * R1S) == (R1S * R2S));
BOOST_CHECK((R2S * R2S).ToString() == "c28bb2b45a1d85ab7996ccd3e102a650f74ff100");
BOOST_CHECK((R2S * ZeroS) == ZeroS);
BOOST_CHECK((R2S * OneS) == R2S);
BOOST_CHECK((R2S * MaxS) == -R2S);
BOOST_CHECK(MaxL * MaxL == OneL); BOOST_CHECK(MaxL * MaxL == OneL);
BOOST_CHECK(MaxS * MaxS == OneS);
BOOST_CHECK((R1L * 0) == 0); BOOST_CHECK((R1L * 0) == 0);
BOOST_CHECK((R1L * 1) == R1L); BOOST_CHECK((R1L * 1) == R1L);
BOOST_CHECK((R1L * 3).ToString() == "7759b1c0ed14047f961ad09b20ff83687876a0181a367b813634046f91def7d4"); BOOST_CHECK((R1L * 3).ToString() == "7759b1c0ed14047f961ad09b20ff83687876a0181a367b813634046f91def7d4");
BOOST_CHECK((R2L * 0x87654321UL).ToString() == "23f7816e30c4ae2017257b7a0fa64d60402f5234d46e746b61c960d09a26d070"); BOOST_CHECK((R2L * 0x87654321UL).ToString() == "23f7816e30c4ae2017257b7a0fa64d60402f5234d46e746b61c960d09a26d070");
BOOST_CHECK((R1S * 0) == 0);
BOOST_CHECK((R1S * 1) == R1S);
BOOST_CHECK((R1S * 7).ToString() == "f7a987f3c3bf758d927f202d7e795faeff084244");
BOOST_CHECK((R2S * 0xFFFFFFFFUL).ToString() == "1c6f6c930353e17f7d6127213bb18d2883e2cd90");
} }
BOOST_AUTO_TEST_CASE( divide ) BOOST_AUTO_TEST_CASE( divide )
@ -535,21 +362,6 @@ BOOST_AUTO_TEST_CASE( divide )
BOOST_CHECK(R2L / MaxL == ZeroL); BOOST_CHECK(R2L / MaxL == ZeroL);
BOOST_CHECK(MaxL / R2L == 1); BOOST_CHECK(MaxL / R2L == 1);
BOOST_CHECK_THROW(R2L / ZeroL, uint_error); BOOST_CHECK_THROW(R2L / ZeroL, uint_error);
arith_uint160 D1S("D3C5EDCDEA54EB92679F0A4B4");
arith_uint160 D2S("13037");
BOOST_CHECK((R1S / D1S).ToString() == "0000000000000000000000000db9af3beade6c02");
BOOST_CHECK((R1S / D2S).ToString() == "000098dfb6cc40ca592bf74366794f298ada205c");
BOOST_CHECK(R1S / OneS == R1S);
BOOST_CHECK(R1S / MaxS == ZeroS);
BOOST_CHECK(MaxS / R1S == 1);
BOOST_CHECK_THROW(R1S / ZeroS, uint_error);
BOOST_CHECK((R2S / D1S).ToString() == "0000000000000000000000000c5608e781182047");
BOOST_CHECK((R2S / D2S).ToString() == "00008966751b7187c3c67c1fda5cea7db2c1c069");
BOOST_CHECK(R2S / OneS == R2S);
BOOST_CHECK(R2S / MaxS == ZeroS);
BOOST_CHECK(MaxS / R2S == 1);
BOOST_CHECK_THROW(R2S / ZeroS, uint_error);
} }
@ -608,70 +420,17 @@ BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 G
BOOST_CHECK(MaxL == TmpL); BOOST_CHECK(MaxL == TmpL);
ss.str(""); ss.str("");
BOOST_CHECK(R1S.GetHex() == R1S.ToString());
BOOST_CHECK(R2S.GetHex() == R2S.ToString());
BOOST_CHECK(OneS.GetHex() == OneS.ToString());
BOOST_CHECK(MaxS.GetHex() == MaxS.ToString());
arith_uint160 TmpS(R1S);
BOOST_CHECK(TmpS == R1S);
TmpS.SetHex(R2S.ToString()); BOOST_CHECK(TmpS == R2S);
TmpS.SetHex(ZeroS.ToString()); BOOST_CHECK(TmpS == 0);
TmpS.SetHex(HalfS.ToString()); BOOST_CHECK(TmpS == HalfS);
TmpS.SetHex(R1S.ToString());
BOOST_CHECK(memcmp(R1S.begin(), R1Array, 20)==0);
BOOST_CHECK(memcmp(TmpS.begin(), R1Array, 20)==0);
BOOST_CHECK(memcmp(R2S.begin(), R2Array, 20)==0);
BOOST_CHECK(memcmp(ZeroS.begin(), ZeroArray, 20)==0);
BOOST_CHECK(memcmp(OneS.begin(), OneArray, 20)==0);
BOOST_CHECK(R1S.size() == 20);
BOOST_CHECK(R2S.size() == 20);
BOOST_CHECK(ZeroS.size() == 20);
BOOST_CHECK(MaxS.size() == 20);
BOOST_CHECK(R1S.begin() + 20 == R1S.end());
BOOST_CHECK(R2S.begin() + 20 == R2S.end());
BOOST_CHECK(OneS.begin() + 20 == OneS.end());
BOOST_CHECK(MaxS.begin() + 20 == MaxS.end());
BOOST_CHECK(TmpS.begin() + 20 == TmpS.end());
BOOST_CHECK(R1S.GetLow64() == R1LLow64);
BOOST_CHECK(HalfS.GetLow64() ==0x0000000000000000ULL);
BOOST_CHECK(OneS.GetLow64() ==0x0000000000000001ULL);
BOOST_CHECK(R1S.GetSerializeSize(0,PROTOCOL_VERSION) == 20);
BOOST_CHECK(ZeroS.GetSerializeSize(0,PROTOCOL_VERSION) == 20);
R1S.Serialize(ss,0,PROTOCOL_VERSION);
BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+20));
TmpS.Unserialize(ss,0,PROTOCOL_VERSION);
BOOST_CHECK(R1S == TmpS);
ss.str("");
ZeroS.Serialize(ss,0,PROTOCOL_VERSION);
BOOST_CHECK(ss.str() == std::string(ZeroArray,ZeroArray+20));
TmpS.Unserialize(ss,0,PROTOCOL_VERSION);
BOOST_CHECK(ZeroS == TmpS);
ss.str("");
MaxS.Serialize(ss,0,PROTOCOL_VERSION);
BOOST_CHECK(ss.str() == std::string(MaxArray,MaxArray+20));
TmpS.Unserialize(ss,0,PROTOCOL_VERSION);
BOOST_CHECK(MaxS == TmpS);
ss.str("");
for (unsigned int i = 0; i < 255; ++i) for (unsigned int i = 0; i < 255; ++i)
{ {
BOOST_CHECK((OneL << i).getdouble() == ldexp(1.0,i)); BOOST_CHECK((OneL << i).getdouble() == ldexp(1.0,i));
if (i < 160) BOOST_CHECK((OneS << i).getdouble() == ldexp(1.0,i));
} }
BOOST_CHECK(ZeroL.getdouble() == 0.0); BOOST_CHECK(ZeroL.getdouble() == 0.0);
BOOST_CHECK(ZeroS.getdouble() == 0.0);
for (int i = 256; i > 53; --i) for (int i = 256; i > 53; --i)
BOOST_CHECK(almostEqual((R1L>>(256-i)).getdouble(), ldexp(R1Ldouble,i))); BOOST_CHECK(almostEqual((R1L>>(256-i)).getdouble(), ldexp(R1Ldouble,i)));
for (int i = 160; i > 53; --i)
BOOST_CHECK(almostEqual((R1S>>(160-i)).getdouble(), ldexp(R1Sdouble,i)));
uint64_t R1L64part = (R1L>>192).GetLow64(); uint64_t R1L64part = (R1L>>192).GetLow64();
uint64_t R1S64part = (R1S>>96).GetLow64();
for (int i = 53; i > 0; --i) // doubles can store all integers in {0,...,2^54-1} exactly for (int i = 53; i > 0; --i) // doubles can store all integers in {0,...,2^54-1} exactly
{ {
BOOST_CHECK((R1L>>(256-i)).getdouble() == (double)(R1L64part >> (64-i))); BOOST_CHECK((R1L>>(256-i)).getdouble() == (double)(R1L64part >> (64-i)));
BOOST_CHECK((R1S>>(160-i)).getdouble() == (double)(R1S64part >> (64-i)));
} }
} }
@ -807,23 +566,20 @@ BOOST_AUTO_TEST_CASE(bignum_SetCompact)
BOOST_AUTO_TEST_CASE( getmaxcoverage ) // some more tests just to get 100% coverage BOOST_AUTO_TEST_CASE( getmaxcoverage ) // some more tests just to get 100% coverage
{ {
// ~R1L give a base_uint<256> // ~R1L give a base_uint<256>
BOOST_CHECK((~~R1L >> 10) == (R1L >> 10)); BOOST_CHECK((~~R1S >> 10) == (R1S >> 10)); BOOST_CHECK((~~R1L >> 10) == (R1L >> 10));
BOOST_CHECK((~~R1L << 10) == (R1L << 10)); BOOST_CHECK((~~R1S << 10) == (R1S << 10)); BOOST_CHECK((~~R1L << 10) == (R1L << 10));
BOOST_CHECK(!(~~R1L < R1L)); BOOST_CHECK(!(~~R1S < R1S)); BOOST_CHECK(!(~~R1L < R1L));
BOOST_CHECK(~~R1L <= R1L); BOOST_CHECK(~~R1S <= R1S); BOOST_CHECK(~~R1L <= R1L);
BOOST_CHECK(!(~~R1L > R1L)); BOOST_CHECK(!(~~R1S > R1S)); BOOST_CHECK(!(~~R1L > R1L));
BOOST_CHECK(~~R1L >= R1L); BOOST_CHECK(~~R1S >= R1S); BOOST_CHECK(~~R1L >= R1L);
BOOST_CHECK(!(R1L < ~~R1L)); BOOST_CHECK(!(R1S < ~~R1S)); BOOST_CHECK(!(R1L < ~~R1L));
BOOST_CHECK(R1L <= ~~R1L); BOOST_CHECK(R1S <= ~~R1S); BOOST_CHECK(R1L <= ~~R1L);
BOOST_CHECK(!(R1L > ~~R1L)); BOOST_CHECK(!(R1S > ~~R1S)); BOOST_CHECK(!(R1L > ~~R1L));
BOOST_CHECK(R1L >= ~~R1L); BOOST_CHECK(R1S >= ~~R1S); BOOST_CHECK(R1L >= ~~R1L);
BOOST_CHECK(~~R1L + R2L == R1L + ~~R2L); BOOST_CHECK(~~R1L + R2L == R1L + ~~R2L);
BOOST_CHECK(~~R1S + R2S == R1S + ~~R2S);
BOOST_CHECK(~~R1L - R2L == R1L - ~~R2L); BOOST_CHECK(~~R1L - R2L == R1L - ~~R2L);
BOOST_CHECK(~~R1S - R2S == R1S - ~~R2S);
BOOST_CHECK(~R1L != R1L); BOOST_CHECK(R1L != ~R1L); BOOST_CHECK(~R1L != R1L); BOOST_CHECK(R1L != ~R1L);
BOOST_CHECK(~R1S != R1S); BOOST_CHECK(R1S != ~R1S);
unsigned char TmpArray[32]; unsigned char TmpArray[32];
CHECKBITWISEOPERATOR(~R1,R2,|) CHECKBITWISEOPERATOR(~R1,R2,|)
CHECKBITWISEOPERATOR(~R1,R2,^) CHECKBITWISEOPERATOR(~R1,R2,^)