LCOV - code coverage report
Current view: top level - src/test - arith_uint256_tests.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 634 634 100.0 %
Date: 2025-02-23 09:33:43 Functions: 26 26 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2011-2013 The Bitcoin Core developers
       2             : // Copyright (c) 2017-2021 The PIVX Core developers
       3             : // Distributed under the MIT software license, see the accompanying
       4             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5             : 
       6             : #include <boost/test/unit_test.hpp>
       7             : #include <stdint.h>
       8             : #include <sstream>
       9             : #include <iomanip>
      10             : #include <limits>
      11             : #include <cmath>
      12             : #include "uint256.h"
      13             : #include "arith_uint256.h"
      14             : #include <string>
      15             : #include "version.h"
      16             : #include "test/test_pivx.h"
      17             : 
      18             : BOOST_FIXTURE_TEST_SUITE(arith_uint256_tests, BasicTestingSetup)
      19             : 
      20             : /// Convert vector to arith_uint256, via uint256 blob
      21             : inline arith_uint256 arith_uint256V(const std::vector<unsigned char>& vch)
      22             : {
      23             :     return UintToArith256(uint256(vch));
      24             : }
      25             : 
      26             : const unsigned char R1Array[] =
      27             :     "\x9c\x52\x4a\xdb\xcf\x56\x11\x12\x2b\x29\x12\x5e\x5d\x35\xd2\xd2"
      28             :     "\x22\x81\xaa\xb5\x33\xf0\x08\x32\xd5\x56\xb1\xf9\xea\xe5\x1d\x7d";
      29             : const char R1ArrayHex[] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c";
      30             : const double R1Ldouble = 0.4887374590559308955; // R1L equals roughly R1Ldouble * 2^256
      31             : const double R1Sdouble = 0.7096329412477836074;
      32             : const arith_uint256 R1L = arith_uint256(std::vector<unsigned char>(R1Array,R1Array+32));
      33             : const arith_uint160 R1S = arith_uint160(std::vector<unsigned char>(R1Array,R1Array+20));
      34             : const uint64_t R1LLow64 = 0x121156cfdb4a529cULL;
      35             : 
      36             : const unsigned char R2Array[] =
      37             :     "\x70\x32\x1d\x7c\x47\xa5\x6b\x40\x26\x7e\x0a\xc3\xa6\x9c\xb6\xbf"
      38             :     "\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7";
      39             : const arith_uint256 R2L = arith_uint256(std::vector<unsigned char>(R2Array,R2Array+32));
      40             : const arith_uint160 R2S = arith_uint160(std::vector<unsigned char>(R2Array,R2Array+20));
      41             : 
      42             : const char R1LplusR2L[] = "549FB09FEA236A1EA3E31D4D58F1B1369288D204211CA751527CFC175767850C";
      43             : 
      44             : const unsigned char ZeroArray[] =
      45             :     "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      46             :     "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
      47             : const arith_uint256 ZeroL = arith_uint256(std::vector<unsigned char>(ZeroArray,ZeroArray+32));
      48             : const arith_uint160 ZeroS = arith_uint160(std::vector<unsigned char>(ZeroArray,ZeroArray+20));
      49             : 
      50             : const unsigned char OneArray[] =
      51             :     "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
      52             :     "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
      53             : const arith_uint256 OneL = arith_uint256(std::vector<unsigned char>(OneArray,OneArray+32));
      54             : const arith_uint160 OneS = arith_uint160(std::vector<unsigned char>(OneArray,OneArray+20));
      55             : 
      56             : const unsigned char MaxArray[] =
      57             :     "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
      58             :     "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
      59             : const arith_uint256 MaxL = arith_uint256(std::vector<unsigned char>(MaxArray,MaxArray+32));
      60             : const arith_uint160 MaxS = arith_uint160(std::vector<unsigned char>(MaxArray,MaxArray+20));
      61             : 
      62             : const arith_uint256 HalfL = (OneL << 255);
      63             : const arith_uint160 HalfS = (OneS << 159);
      64          12 : std::string ArrayToString(const unsigned char A[], unsigned int width)
      65             : {
      66          12 :     std::stringstream Stream;
      67          12 :     Stream << std::hex;
      68         324 :     for (unsigned int i = 0; i < width; ++i)
      69             :     {
      70         312 :         Stream<<std::setw(2)<<std::setfill('0')<<(unsigned int)A[width-i-1];
      71             :     }
      72          12 :     return Stream.str();
      73             : }
      74             : 
      75           2 : BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
      76             : {
      77           2 :     BOOST_CHECK(1 == 0+1);
      78             :     // constructor arith_uint256(vector<char>):
      79           4 :     BOOST_CHECK(R1L.ToString() == ArrayToString(R1Array,32));
      80           4 :     BOOST_CHECK(R1S.ToString() == ArrayToString(R1Array,20));
      81           4 :     BOOST_CHECK(R2L.ToString() == ArrayToString(R2Array,32));
      82           4 :     BOOST_CHECK(R2S.ToString() == ArrayToString(R2Array,20));
      83           4 :     BOOST_CHECK(ZeroL.ToString() == ArrayToString(ZeroArray,32));
      84           4 :     BOOST_CHECK(ZeroS.ToString() == ArrayToString(ZeroArray,20));
      85           4 :     BOOST_CHECK(OneL.ToString() == ArrayToString(OneArray,32));
      86           4 :     BOOST_CHECK(OneS.ToString() == ArrayToString(OneArray,20));
      87           4 :     BOOST_CHECK(MaxL.ToString() == ArrayToString(MaxArray,32));
      88           4 :     BOOST_CHECK(MaxS.ToString() == ArrayToString(MaxArray,20));
      89           2 :     BOOST_CHECK(OneL.ToString() != ArrayToString(ZeroArray,32));
      90           4 :     BOOST_CHECK(OneS.ToString() != ArrayToString(ZeroArray,20));
      91             : 
      92             :     // == and !=
      93           2 :     BOOST_CHECK(R1L != R2L && R1S != R2S);
      94           2 :     BOOST_CHECK(ZeroL != OneL && ZeroS != OneS);
      95           2 :     BOOST_CHECK(OneL != ZeroL && OneS != ZeroS);
      96           2 :     BOOST_CHECK(MaxL != ZeroL && MaxS != ZeroS);
      97          12 :     BOOST_CHECK(~MaxL == ZeroL && ~MaxS == ZeroS);
      98           4 :     BOOST_CHECK( ((R1L ^ R2L) ^ R1L) == R2L);
      99           4 :     BOOST_CHECK( ((R1S ^ R2S) ^ R1S) == R2S);
     100             : 
     101           1 :     uint64_t Tmp64 = 0xc4dab720d9c7acaaULL;
     102         257 :     for (unsigned int i = 0; i < 256; ++i)
     103             :     {
     104         512 :         BOOST_CHECK(ZeroL != (OneL << i));
     105         512 :         BOOST_CHECK((OneL << i) != ZeroL);
     106         768 :         BOOST_CHECK(R1L != (R1L ^ (OneL << i)));
     107        1024 :         BOOST_CHECK(((arith_uint256(Tmp64) ^ (OneL << i) ) != Tmp64 ));
     108             :     }
     109           2 :     BOOST_CHECK(ZeroL == (OneL << 256));
     110             : 
     111         161 :     for (unsigned int i = 0; i < 160; ++i)
     112             :     {
     113         320 :         BOOST_CHECK(ZeroS != (OneS << i));
     114         320 :         BOOST_CHECK((OneS << i) != ZeroS);
     115         480 :         BOOST_CHECK(R1S != (R1S ^ (OneS << i)));
     116         640 :         BOOST_CHECK(((arith_uint160(Tmp64) ^ (OneS << i) ) != Tmp64 ));
     117             :     }
     118           2 :     BOOST_CHECK(ZeroS == (OneS << 256));
     119             : 
     120             :     // String Constructor and Copy Constructor
     121           4 :     BOOST_CHECK(arith_uint256("0x"+R1L.ToString()) == R1L);
     122           4 :     BOOST_CHECK(arith_uint256("0x"+R2L.ToString()) == R2L);
     123           4 :     BOOST_CHECK(arith_uint256("0x"+ZeroL.ToString()) == ZeroL);
     124           4 :     BOOST_CHECK(arith_uint256("0x"+OneL.ToString()) == OneL);
     125           4 :     BOOST_CHECK(arith_uint256("0x"+MaxL.ToString()) == MaxL);
     126           4 :     BOOST_CHECK(arith_uint256(R1L.ToString()) == R1L);
     127           4 :     BOOST_CHECK(arith_uint256("   0x"+R1L.ToString()+"   ") == R1L);
     128           3 :     BOOST_CHECK(arith_uint256("") == ZeroL);
     129           4 :     BOOST_CHECK(R1L == arith_uint256(R1ArrayHex));
     130           3 :     BOOST_CHECK(arith_uint256(R1L) == R1L);
     131           5 :     BOOST_CHECK((arith_uint256(R1L^R2L)^R2L) == R1L);
     132           3 :     BOOST_CHECK(arith_uint256(ZeroL) == ZeroL);
     133           3 :     BOOST_CHECK(arith_uint256(OneL) == OneL);
     134             : 
     135           4 :     BOOST_CHECK(arith_uint160("0x"+R1S.ToString()) == R1S);
     136           4 :     BOOST_CHECK(arith_uint160("0x"+R2S.ToString()) == R2S);
     137           4 :     BOOST_CHECK(arith_uint160("0x"+ZeroS.ToString()) == ZeroS);
     138           4 :     BOOST_CHECK(arith_uint160("0x"+OneS.ToString()) == OneS);
     139           4 :     BOOST_CHECK(arith_uint160("0x"+MaxS.ToString()) == MaxS);
     140           4 :     BOOST_CHECK(arith_uint160(R1S.ToString()) == R1S);
     141           4 :     BOOST_CHECK(arith_uint160("   0x"+R1S.ToString()+"   ") == R1S);
     142           3 :     BOOST_CHECK(arith_uint160("") == ZeroS);
     143           4 :     BOOST_CHECK(R1S == arith_uint160(R1ArrayHex));
     144             : 
     145           3 :     BOOST_CHECK(arith_uint160(R1S) == R1S);
     146           5 :     BOOST_CHECK((arith_uint160(R1S^R2S)^R2S) == R1S);
     147           3 :     BOOST_CHECK(arith_uint160(ZeroS) == ZeroS);
     148           3 :     BOOST_CHECK(arith_uint160(OneS) == OneS);
     149             : 
     150             :     // uint64_t constructor
     151           6 :     BOOST_CHECK( (R1L & arith_uint256("0xffffffffffffffff")) == arith_uint256(R1LLow64));
     152           3 :     BOOST_CHECK(ZeroL == arith_uint256(0));
     153           3 :     BOOST_CHECK(OneL == arith_uint256(1));
     154          14 :     BOOST_CHECK(arith_uint256("0xffffffffffffffff") = arith_uint256(0xffffffffffffffffULL));
     155           6 :     BOOST_CHECK( (R1S & arith_uint160("0xffffffffffffffff")) == arith_uint160(R1LLow64));
     156           3 :     BOOST_CHECK(ZeroS == arith_uint160(0));
     157           3 :     BOOST_CHECK(OneS == arith_uint160(1));
     158          11 :     BOOST_CHECK(arith_uint160("0xffffffffffffffff") = arith_uint160(0xffffffffffffffffULL));
     159             : 
     160             :     // Assignment (from base_uint)
     161          21 :     arith_uint256 tmpL = ~ZeroL; BOOST_CHECK(tmpL == ~ZeroL);
     162          29 :     tmpL = ~OneL; BOOST_CHECK(tmpL == ~OneL);
     163          29 :     tmpL = ~R1L; BOOST_CHECK(tmpL == ~R1L);
     164          29 :     tmpL = ~R2L; BOOST_CHECK(tmpL == ~R2L);
     165          29 :     tmpL = ~MaxL; BOOST_CHECK(tmpL == ~MaxL);
     166          15 :     arith_uint160 tmpS = ~ZeroS; BOOST_CHECK(tmpS == ~ZeroS);
     167          20 :     tmpS = ~OneS; BOOST_CHECK(tmpS == ~OneS);
     168          20 :     tmpS = ~R1S; BOOST_CHECK(tmpS == ~R1S);
     169          20 :     tmpS = ~R2S; BOOST_CHECK(tmpS == ~R2S);
     170          20 :     tmpS = ~MaxS; BOOST_CHECK(tmpS == ~MaxS);
     171             : 
     172             :     // Wrong length must throw exception.
     173           3 :     BOOST_CHECK_THROW(arith_uint256(std::vector<unsigned char>(OneArray,OneArray+31)), uint_error);
     174           3 :     BOOST_CHECK_THROW(arith_uint256(std::vector<unsigned char>(OneArray,OneArray+20)), uint_error);
     175           3 :     BOOST_CHECK_THROW(arith_uint160(std::vector<unsigned char>(OneArray,OneArray+32)), uint_error);
     176           3 :     BOOST_CHECK_THROW(arith_uint160(std::vector<unsigned char>(OneArray,OneArray+19)), uint_error);
     177           1 : }
     178             : 
     179         832 : void shiftArrayRight(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift)
     180             : {
     181       23616 :     for (unsigned int T=0; T < arrayLength; ++T)
     182             :     {
     183       22784 :         unsigned int F = (T+bitsToShift/8);
     184       22784 :         if (F < arrayLength)
     185       11808 :             to[T]  = from[F] >> (bitsToShift%8);
     186             :         else
     187       10976 :             to[T] = 0;
     188       22784 :         if (F + 1 < arrayLength)
     189       10976 :             to[T] |= from[(F+1)] << (8-bitsToShift%8);
     190             :     }
     191         832 : }
     192             : 
     193        1248 : void shiftArrayLeft(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift)
     194             : {
     195       35424 :     for (unsigned int T=0; T < arrayLength; ++T)
     196             :     {
     197       34176 :         if (T >= bitsToShift/8)
     198             :         {
     199       17712 :             unsigned int F = T-bitsToShift/8;
     200       17712 :             to[T]  = from[F] << (bitsToShift%8);
     201       17712 :             if (T >= bitsToShift/8+1)
     202       16464 :                 to[T] |= from[F-1] >> (8-bitsToShift%8);
     203             :         }
     204             :         else {
     205       16464 :             to[T] = 0;
     206             :         }
     207             :     }
     208        1248 : }
     209             : 
     210           2 : BOOST_AUTO_TEST_CASE( shifts ) { // "<<"  ">>"  "<<="  ">>="
     211           1 :     unsigned char TmpArray[32];
     212           1 :     arith_uint256 TmpL;
     213         257 :     for (unsigned int i = 0; i < 256; ++i)
     214             :     {
     215         256 :         shiftArrayLeft(TmpArray, OneArray, 32, i);
     216        1024 :         BOOST_CHECK(arith_uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (OneL << i));
     217        2304 :         TmpL = OneL; TmpL <<= i;
     218         512 :         BOOST_CHECK(TmpL == (OneL << i));
     219         512 :         BOOST_CHECK((HalfL >> (255-i)) == (OneL << i));
     220        2304 :         TmpL = HalfL; TmpL >>= (255-i);
     221         512 :         BOOST_CHECK(TmpL == (OneL << i));
     222             : 
     223         256 :         shiftArrayLeft(TmpArray, R1Array, 32, i);
     224        1024 :         BOOST_CHECK(arith_uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L << i));
     225        2304 :         TmpL = R1L; TmpL <<= i;
     226         512 :         BOOST_CHECK(TmpL == (R1L << i));
     227             : 
     228         256 :         shiftArrayRight(TmpArray, R1Array, 32, i);
     229        1024 :         BOOST_CHECK(arith_uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L >> i));
     230        2304 :         TmpL = R1L; TmpL >>= i;
     231         512 :         BOOST_CHECK(TmpL == (R1L >> i));
     232             : 
     233         256 :         shiftArrayLeft(TmpArray, MaxArray, 32, i);
     234        1024 :         BOOST_CHECK(arith_uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL << i));
     235        2304 :         TmpL = MaxL; TmpL <<= i;
     236         512 :         BOOST_CHECK(TmpL == (MaxL << i));
     237             : 
     238         256 :         shiftArrayRight(TmpArray, MaxArray, 32, i);
     239        1024 :         BOOST_CHECK(arith_uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL >> i));
     240        2304 :         TmpL = MaxL; TmpL >>= i;
     241         512 :         BOOST_CHECK(TmpL == (MaxL >> i));
     242             :     }
     243           1 :     arith_uint256 c1L = arith_uint256(0x0123456789abcdefULL);
     244           1 :     arith_uint256 c2L = c1L << 128;
     245         129 :     for (unsigned int i = 0; i < 128; ++i) {
     246         256 :         BOOST_CHECK((c1L << i) == (c2L >> (128-i)));
     247             :     }
     248         129 :     for (unsigned int i = 128; i < 256; ++i) {
     249         256 :         BOOST_CHECK((c1L << i) == (c2L << (i-128)));
     250             :     }
     251             : 
     252           1 :     arith_uint160 TmpS;
     253         161 :     for (unsigned int i = 0; i < 160; ++i)
     254             :     {
     255         160 :         shiftArrayLeft(TmpArray, OneArray, 20, i);
     256         640 :         BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (OneS << i));
     257         960 :         TmpS = OneS; TmpS <<= i;
     258         320 :         BOOST_CHECK(TmpS == (OneS << i));
     259         320 :         BOOST_CHECK((HalfS >> (159-i)) == (OneS << i));
     260         960 :         TmpS = HalfS; TmpS >>= (159-i);
     261         320 :         BOOST_CHECK(TmpS == (OneS << i));
     262             : 
     263         160 :         shiftArrayLeft(TmpArray, R1Array, 20, i);
     264         640 :         BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (R1S << i));
     265         960 :         TmpS = R1S; TmpS <<= i;
     266         320 :         BOOST_CHECK(TmpS == (R1S << i));
     267             : 
     268         160 :         shiftArrayRight(TmpArray, R1Array, 20, i);
     269         640 :         BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (R1S >> i));
     270         960 :         TmpS = R1S; TmpS >>= i;
     271         320 :         BOOST_CHECK(TmpS == (R1S >> i));
     272             : 
     273         160 :         shiftArrayLeft(TmpArray, MaxArray, 20, i);
     274         640 :         BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (MaxS << i));
     275         960 :         TmpS = MaxS; TmpS <<= i;
     276         320 :         BOOST_CHECK(TmpS == (MaxS << i));
     277             : 
     278         160 :         shiftArrayRight(TmpArray, MaxArray, 20, i);
     279         640 :         BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (MaxS >> i));
     280         960 :         TmpS = MaxS; TmpS >>= i;
     281         320 :         BOOST_CHECK(TmpS == (MaxS >> i));
     282             :     }
     283           1 :     arith_uint160 c1S = arith_uint160(0x0123456789abcdefULL);
     284           1 :     arith_uint160 c2S = c1S << 80;
     285          81 :     for (unsigned int i = 0; i < 80; ++i) {
     286         160 :         BOOST_CHECK((c1S << i) == (c2S >> (80-i)));
     287             :     }
     288          81 :     for (unsigned int i = 80; i < 160; ++i) {
     289         160 :         BOOST_CHECK((c1S << i) == (c2S << (i-80)));
     290             :     }
     291           1 : }
     292             : 
     293           2 : BOOST_AUTO_TEST_CASE( unaryOperators ) // !    ~    -
     294             : {
     295          16 :     BOOST_CHECK(!ZeroL);  BOOST_CHECK(!ZeroS);
     296           5 :     BOOST_CHECK(!(!OneL));BOOST_CHECK(!(!OneS));
     297         257 :     for (unsigned int i = 0; i < 256; ++i)
     298         768 :         BOOST_CHECK(!(!(OneL<<i)));
     299         161 :     for (unsigned int i = 0; i < 160; ++i)
     300         480 :         BOOST_CHECK(!(!(OneS<<i)));
     301           5 :     BOOST_CHECK(!(!R1L));BOOST_CHECK(!(!R1S));
     302           5 :     BOOST_CHECK(!(!R2S));BOOST_CHECK(!(!R2S));
     303           5 :     BOOST_CHECK(!(!MaxL));BOOST_CHECK(!(!MaxS));
     304             : 
     305          18 :     BOOST_CHECK(~ZeroL == MaxL); BOOST_CHECK(~ZeroS == MaxS);
     306             : 
     307           1 :     unsigned char TmpArray[32];
     308          33 :     for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = ~R1Array[i]; }
     309          13 :     BOOST_CHECK(arith_uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (~R1L));
     310          10 :     BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (~R1S));
     311             : 
     312           3 :     BOOST_CHECK(-ZeroL == ZeroL); BOOST_CHECK(-ZeroS == ZeroS);
     313          13 :     BOOST_CHECK(-R1L == (~R1L)+1);
     314          10 :     BOOST_CHECK(-R1S == (~R1S)+1);
     315         257 :     for (unsigned int i = 0; i < 256; ++i)
     316         512 :         BOOST_CHECK(-(OneL<<i) == (MaxL << i));
     317         161 :     for (unsigned int i = 0; i < 160; ++i)
     318         320 :         BOOST_CHECK(-(OneS<<i) == (MaxS << i));
     319           1 : }
     320             : 
     321             : 
     322             : // Check if doing _A_ _OP_ _B_ results in the same as applying _OP_ onto each
     323             : // element of Aarray and Barray, and then converting the result into a arith_uint256.
     324             : #define CHECKBITWISEOPERATOR(_A_,_B_,_OP_)                              \
     325             :     for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = _A_##Array[i] _OP_ _B_##Array[i]; } \
     326             :     BOOST_CHECK(arith_uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (_A_##L _OP_ _B_##L)); \
     327             :     for (unsigned int i = 0; i < 20; ++i) { TmpArray[i] = _A_##Array[i] _OP_ _B_##Array[i]; } \
     328             :     BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (_A_##S _OP_ _B_##S));
     329             : 
     330             : #define CHECKASSIGNMENTOPERATOR(_A_,_B_,_OP_)                           \
     331             :     TmpL = _A_##L; TmpL _OP_##= _B_##L; BOOST_CHECK(TmpL == (_A_##L _OP_ _B_##L)); \
     332             :     TmpS = _A_##S; TmpS _OP_##= _B_##S; BOOST_CHECK(TmpS == (_A_##S _OP_ _B_##S));
     333             : 
     334           2 : BOOST_AUTO_TEST_CASE( bitwiseOperators )
     335             : {
     336           1 :     unsigned char TmpArray[32];
     337             : 
     338          61 :     CHECKBITWISEOPERATOR(R1,R2,|)
     339          61 :     CHECKBITWISEOPERATOR(R1,R2,^)
     340          61 :     CHECKBITWISEOPERATOR(R1,R2,&)
     341          61 :     CHECKBITWISEOPERATOR(R1,Zero,|)
     342          61 :     CHECKBITWISEOPERATOR(R1,Zero,^)
     343          61 :     CHECKBITWISEOPERATOR(R1,Zero,&)
     344          61 :     CHECKBITWISEOPERATOR(R1,Max,|)
     345          61 :     CHECKBITWISEOPERATOR(R1,Max,^)
     346          61 :     CHECKBITWISEOPERATOR(R1,Max,&)
     347          61 :     CHECKBITWISEOPERATOR(Zero,R1,|)
     348          61 :     CHECKBITWISEOPERATOR(Zero,R1,^)
     349          61 :     CHECKBITWISEOPERATOR(Zero,R1,&)
     350          61 :     CHECKBITWISEOPERATOR(Max,R1,|)
     351          61 :     CHECKBITWISEOPERATOR(Max,R1,^)
     352          61 :     CHECKBITWISEOPERATOR(Max,R1,&)
     353             : 
     354           1 :     arith_uint256 TmpL;
     355           1 :     arith_uint160 TmpS;
     356          31 :     CHECKASSIGNMENTOPERATOR(R1,R2,|)
     357          31 :     CHECKASSIGNMENTOPERATOR(R1,R2,^)
     358          31 :     CHECKASSIGNMENTOPERATOR(R1,R2,&)
     359          31 :     CHECKASSIGNMENTOPERATOR(R1,Zero,|)
     360          31 :     CHECKASSIGNMENTOPERATOR(R1,Zero,^)
     361          31 :     CHECKASSIGNMENTOPERATOR(R1,Zero,&)
     362          31 :     CHECKASSIGNMENTOPERATOR(R1,Max,|)
     363          31 :     CHECKASSIGNMENTOPERATOR(R1,Max,^)
     364          31 :     CHECKASSIGNMENTOPERATOR(R1,Max,&)
     365          31 :     CHECKASSIGNMENTOPERATOR(Zero,R1,|)
     366          31 :     CHECKASSIGNMENTOPERATOR(Zero,R1,^)
     367          31 :     CHECKASSIGNMENTOPERATOR(Zero,R1,&)
     368          31 :     CHECKASSIGNMENTOPERATOR(Max,R1,|)
     369          31 :     CHECKASSIGNMENTOPERATOR(Max,R1,^)
     370          31 :     CHECKASSIGNMENTOPERATOR(Max,R1,&)
     371             : 
     372           1 :     uint64_t Tmp64 = 0xe1db685c9a0b47a2ULL;
     373           5 :     TmpL = R1L; TmpL |= Tmp64;  BOOST_CHECK(TmpL == (R1L | arith_uint256(Tmp64)));
     374           9 :     TmpS = R1S; TmpS |= Tmp64;  BOOST_CHECK(TmpS == (R1S | arith_uint160(Tmp64)));
     375          10 :     TmpL = R1L; TmpL |= 0; BOOST_CHECK(TmpL == R1L);
     376           7 :     TmpS = R1S; TmpS |= 0; BOOST_CHECK(TmpS == R1S);
     377           2 :     TmpL ^= 0; BOOST_CHECK(TmpL == R1L);
     378           2 :     TmpS ^= 0; BOOST_CHECK(TmpS == R1S);
     379           4 :     TmpL ^= Tmp64;  BOOST_CHECK(TmpL == (R1L ^ arith_uint256(Tmp64)));
     380           4 :     TmpS ^= Tmp64;  BOOST_CHECK(TmpS == (R1S ^ arith_uint160(Tmp64)));
     381           1 : }
     382             : 
     383           2 : BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
     384             : {
     385           1 :     arith_uint256 TmpL;
     386         257 :     for (unsigned int i = 0; i < 256; ++i) {
     387        2560 :         TmpL= OneL<< i;
     388        1280 :         BOOST_CHECK( TmpL >= ZeroL && TmpL > ZeroL && ZeroL < TmpL && ZeroL <= TmpL);
     389        1536 :         BOOST_CHECK( TmpL >= 0 && TmpL > 0 && 0 < TmpL && 0 <= TmpL);
     390        2304 :         TmpL |= R1L;
     391        1286 :         BOOST_CHECK( TmpL >= R1L ); BOOST_CHECK( (TmpL == R1L) != (TmpL > R1L)); BOOST_CHECK( (TmpL == R1L) || !( TmpL <= R1L));
     392        1286 :         BOOST_CHECK( R1L <= TmpL ); BOOST_CHECK( (R1L == TmpL) != (R1L < TmpL)); BOOST_CHECK( (TmpL == R1L) || !( R1L >= TmpL));
     393         768 :         BOOST_CHECK(! (TmpL < R1L)); BOOST_CHECK(! (R1L > TmpL));
     394             :     }
     395           1 :     arith_uint160 TmpS;
     396         161 :     for (unsigned int i = 0; i < 160; ++i) {
     397        1120 :         TmpS= OneS<< i;
     398         800 :         BOOST_CHECK( TmpS >= ZeroS && TmpS > ZeroS && ZeroS < TmpS && ZeroS <= TmpS);
     399         960 :         BOOST_CHECK( TmpS >= 0 && TmpS > 0 && 0 < TmpS && 0 <= TmpS);
     400         960 :         TmpS |= R1S;
     401         812 :         BOOST_CHECK( TmpS >= R1S ); BOOST_CHECK( (TmpS == R1S) != (TmpS > R1S)); BOOST_CHECK( (TmpS == R1S) || !( TmpS <= R1S));
     402         812 :         BOOST_CHECK( R1S <= TmpS ); BOOST_CHECK( (R1S == TmpS) != (R1S < TmpS)); BOOST_CHECK( (TmpS == R1S) || !( R1S >= TmpS));
     403         480 :         BOOST_CHECK(! (TmpS < R1S)); BOOST_CHECK(! (R1S > TmpS));
     404             :     }
     405           1 : }
     406             : 
     407           2 : BOOST_AUTO_TEST_CASE( plusMinus )
     408             : {
     409           1 :     arith_uint256 TmpL = 0;
     410           5 :     BOOST_CHECK(R1L+R2L == arith_uint256(R1LplusR2L));
     411           9 :     TmpL += R1L;
     412           2 :     BOOST_CHECK(TmpL == R1L);
     413           9 :     TmpL += R2L;
     414           3 :     BOOST_CHECK(TmpL == R1L + R2L);
     415           3 :     BOOST_CHECK(OneL+MaxL == ZeroL);
     416           3 :     BOOST_CHECK(MaxL+OneL == ZeroL);
     417         256 :     for (unsigned int i = 1; i < 256; ++i) {
     418         765 :         BOOST_CHECK( (MaxL >> i) + OneL == (HalfL >> (i-1)) );
     419         765 :         BOOST_CHECK( OneL + (MaxL >> i) == (HalfL >> (i-1)) );
     420        2550 :         TmpL = (MaxL>>i); TmpL += OneL;
     421         510 :         BOOST_CHECK( TmpL == (HalfL >> (i-1)) );
     422        2550 :         TmpL = (MaxL>>i); TmpL += 1;
     423         510 :         BOOST_CHECK( TmpL == (HalfL >> (i-1)) );
     424        2550 :         TmpL = (MaxL>>i);
     425         765 :         BOOST_CHECK( TmpL++ == (MaxL>>i) );
     426         510 :         BOOST_CHECK( TmpL == (HalfL >> (i-1)));
     427             :     }
     428           6 :     BOOST_CHECK(arith_uint256(0xbedc77e27940a7ULL) + 0xee8d836fce66fbULL == arith_uint256(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL));
     429          10 :     TmpL = arith_uint256(0xbedc77e27940a7ULL); TmpL += 0xee8d836fce66fbULL;
     430           3 :     BOOST_CHECK(TmpL == arith_uint256(0xbedc77e27940a7ULL+0xee8d836fce66fbULL));
     431           2 :     TmpL -= 0xee8d836fce66fbULL;  BOOST_CHECK(TmpL == 0xbedc77e27940a7ULL);
     432           9 :     TmpL = R1L;
     433           4 :     BOOST_CHECK(++TmpL == R1L+1);
     434             : 
     435           3 :     BOOST_CHECK(R1L -(-R2L) == R1L+R2L);
     436           3 :     BOOST_CHECK(R1L -(-OneL) == R1L+OneL);
     437           3 :     BOOST_CHECK(R1L - OneL == R1L+(-OneL));
     438         256 :     for (unsigned int i = 1; i < 256; ++i) {
     439         510 :         BOOST_CHECK((MaxL>>i) - (-OneL)  == (HalfL >> (i-1)));
     440         510 :         BOOST_CHECK((HalfL >> (i-1)) - OneL == (MaxL>>i));
     441        2550 :         TmpL = (HalfL >> (i-1));
     442         765 :         BOOST_CHECK(TmpL-- == (HalfL >> (i-1)));
     443         510 :         BOOST_CHECK(TmpL == (MaxL >> i));
     444        2550 :         TmpL = (HalfL >> (i-1));
     445         765 :         BOOST_CHECK(--TmpL == (MaxL >> i));
     446             :     }
     447           9 :     TmpL = R1L;
     448           4 :     BOOST_CHECK(--TmpL == R1L-1);
     449             : 
     450             :     // 160-bit; copy-pasted
     451           1 :     arith_uint160 TmpS = 0;
     452           5 :     BOOST_CHECK(R1S+R2S == arith_uint160(R1LplusR2L));
     453           6 :     TmpS += R1S;
     454           2 :     BOOST_CHECK(TmpS == R1S);
     455           6 :     TmpS += R2S;
     456           3 :     BOOST_CHECK(TmpS == R1S + R2S);
     457           3 :     BOOST_CHECK(OneS+MaxS == ZeroS);
     458           3 :     BOOST_CHECK(MaxS+OneS == ZeroS);
     459         160 :     for (unsigned int i = 1; i < 160; ++i) {
     460         477 :         BOOST_CHECK( (MaxS >> i) + OneS == (HalfS >> (i-1)) );
     461         477 :         BOOST_CHECK( OneS + (MaxS >> i) == (HalfS >> (i-1)) );
     462        1113 :         TmpS = (MaxS>>i); TmpS += OneS;
     463         318 :         BOOST_CHECK( TmpS == (HalfS >> (i-1)) );
     464        1113 :         TmpS = (MaxS>>i); TmpS += 1;
     465         318 :         BOOST_CHECK( TmpS == (HalfS >> (i-1)) );
     466        1113 :         TmpS = (MaxS>>i);
     467         477 :         BOOST_CHECK( TmpS++ == (MaxS>>i) );
     468         318 :         BOOST_CHECK( TmpS == (HalfS >> (i-1)));
     469             :     }
     470           6 :     BOOST_CHECK(arith_uint160(0xbedc77e27940a7ULL) + 0xee8d836fce66fbULL == arith_uint160(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL));
     471           7 :     TmpS = arith_uint160(0xbedc77e27940a7ULL); TmpS += 0xee8d836fce66fbULL;
     472           3 :     BOOST_CHECK(TmpS == arith_uint160(0xbedc77e27940a7ULL+0xee8d836fce66fbULL));
     473           2 :     TmpS -= 0xee8d836fce66fbULL;  BOOST_CHECK(TmpS == 0xbedc77e27940a7ULL);
     474           6 :     TmpS = R1S;
     475           4 :     BOOST_CHECK(++TmpS == R1S+1);
     476             : 
     477           3 :     BOOST_CHECK(R1S -(-R2S) == R1S+R2S);
     478           3 :     BOOST_CHECK(R1S -(-OneS) == R1S+OneS);
     479           3 :     BOOST_CHECK(R1S - OneS == R1S+(-OneS));
     480         160 :     for (unsigned int i = 1; i < 160; ++i) {
     481         318 :         BOOST_CHECK((MaxS>>i) - (-OneS)  == (HalfS >> (i-1)));
     482         318 :         BOOST_CHECK((HalfS >> (i-1)) - OneS == (MaxS>>i));
     483        1113 :         TmpS = (HalfS >> (i-1));
     484         477 :         BOOST_CHECK(TmpS-- == (HalfS >> (i-1)));
     485         318 :         BOOST_CHECK(TmpS == (MaxS >> i));
     486        1113 :         TmpS = (HalfS >> (i-1));
     487         477 :         BOOST_CHECK(--TmpS == (MaxS >> i));
     488             :     }
     489           6 :     TmpS = R1S;
     490           4 :     BOOST_CHECK(--TmpS == R1S-1);
     491             : 
     492           1 : }
     493             : 
     494           2 : BOOST_AUTO_TEST_CASE( multiply )
     495             : {
     496           4 :     BOOST_CHECK((R1L * R1L).ToString() == "62a38c0486f01e45879d7910a7761bf30d5237e9873f9bff3642a732c4d84f10");
     497           4 :     BOOST_CHECK((R1L * R2L).ToString() == "de37805e9986996cfba76ff6ba51c008df851987d9dd323f0e5de07760529c40");
     498           2 :     BOOST_CHECK((R1L * ZeroL) == ZeroL);
     499           2 :     BOOST_CHECK((R1L * OneL) == R1L);
     500           2 :     BOOST_CHECK((R1L * MaxL) == -R1L);
     501           2 :     BOOST_CHECK((R2L * R1L) == (R1L * R2L));
     502           4 :     BOOST_CHECK((R2L * R2L).ToString() == "ac8c010096767d3cae5005dec28bb2b45a1d85ab7996ccd3e102a650f74ff100");
     503           2 :     BOOST_CHECK((R2L * ZeroL) == ZeroL);
     504           2 :     BOOST_CHECK((R2L * OneL) == R2L);
     505           2 :     BOOST_CHECK((R2L * MaxL) == -R2L);
     506             : 
     507           4 :     BOOST_CHECK((R1S * R1S).ToString() == "a7761bf30d5237e9873f9bff3642a732c4d84f10");
     508           4 :     BOOST_CHECK((R1S * R2S).ToString() == "ba51c008df851987d9dd323f0e5de07760529c40");
     509           2 :     BOOST_CHECK((R1S * ZeroS) == ZeroS);
     510           2 :     BOOST_CHECK((R1S * OneS) == R1S);
     511           2 :     BOOST_CHECK((R1S * MaxS) == -R1S);
     512           2 :     BOOST_CHECK((R2S * R1S) == (R1S * R2S));
     513           4 :     BOOST_CHECK((R2S * R2S).ToString() == "c28bb2b45a1d85ab7996ccd3e102a650f74ff100");
     514           2 :     BOOST_CHECK((R2S * ZeroS) == ZeroS);
     515           2 :     BOOST_CHECK((R2S * OneS) == R2S);
     516           2 :     BOOST_CHECK((R2S * MaxS) == -R2S);
     517             : 
     518           2 :     BOOST_CHECK(MaxL * MaxL == OneL);
     519           2 :     BOOST_CHECK(MaxS * MaxS == OneS);
     520             : 
     521           3 :     BOOST_CHECK((R1L * 0) == 0);
     522           2 :     BOOST_CHECK((R1L * 1) == R1L);
     523           4 :     BOOST_CHECK((R1L * 3).ToString() == "7759b1c0ed14047f961ad09b20ff83687876a0181a367b813634046f91def7d4");
     524           4 :     BOOST_CHECK((R2L * 0x87654321UL).ToString() == "23f7816e30c4ae2017257b7a0fa64d60402f5234d46e746b61c960d09a26d070");
     525           3 :     BOOST_CHECK((R1S * 0) == 0);
     526           2 :     BOOST_CHECK((R1S * 1) == R1S);
     527           4 :     BOOST_CHECK((R1S * 7).ToString() == "f7a987f3c3bf758d927f202d7e795faeff084244");
     528           4 :     BOOST_CHECK((R2S * 0xFFFFFFFFUL).ToString() == "1c6f6c930353e17f7d6127213bb18d2883e2cd90");
     529           1 : }
     530             : 
     531           2 : BOOST_AUTO_TEST_CASE( divide )
     532             : {
     533           2 :     arith_uint256 D1L("AD7133AC1977FA2B7");
     534           2 :     arith_uint256 D2L("ECD751716");
     535           4 :     BOOST_CHECK((R1L / D1L).ToString() == "00000000000000000b8ac01106981635d9ed112290f8895545a7654dde28fb3a");
     536           4 :     BOOST_CHECK((R1L / D2L).ToString() == "000000000873ce8efec5b67150bad3aa8c5fcb70e947586153bf2cec7c37c57a");
     537           2 :     BOOST_CHECK(R1L / OneL == R1L);
     538           2 :     BOOST_CHECK(R1L / MaxL == ZeroL);
     539           3 :     BOOST_CHECK(MaxL / R1L == 2);
     540           2 :     BOOST_CHECK_THROW(R1L / ZeroL, uint_error);
     541           4 :     BOOST_CHECK((R2L / D1L).ToString() == "000000000000000013e1665895a1cc981de6d93670105a6b3ec3b73141b3a3c5");
     542           4 :     BOOST_CHECK((R2L / D2L).ToString() == "000000000e8f0abe753bb0afe2e9437ee85d280be60882cf0bd1aaf7fa3cc2c4");
     543           2 :     BOOST_CHECK(R2L / OneL == R2L);
     544           2 :     BOOST_CHECK(R2L / MaxL == ZeroL);
     545           3 :     BOOST_CHECK(MaxL / R2L == 1);
     546           2 :     BOOST_CHECK_THROW(R2L / ZeroL, uint_error);
     547             : 
     548           2 :     arith_uint160 D1S("D3C5EDCDEA54EB92679F0A4B4");
     549           2 :     arith_uint160 D2S("13037");
     550           4 :     BOOST_CHECK((R1S / D1S).ToString() == "0000000000000000000000000db9af3beade6c02");
     551           4 :     BOOST_CHECK((R1S / D2S).ToString() == "000098dfb6cc40ca592bf74366794f298ada205c");
     552           2 :     BOOST_CHECK(R1S / OneS == R1S);
     553           2 :     BOOST_CHECK(R1S / MaxS == ZeroS);
     554           3 :     BOOST_CHECK(MaxS / R1S == 1);
     555           2 :     BOOST_CHECK_THROW(R1S / ZeroS, uint_error);
     556           4 :     BOOST_CHECK((R2S / D1S).ToString() == "0000000000000000000000000c5608e781182047");
     557           4 :     BOOST_CHECK((R2S / D2S).ToString() == "00008966751b7187c3c67c1fda5cea7db2c1c069");
     558           2 :     BOOST_CHECK(R2S / OneS == R2S);
     559           2 :     BOOST_CHECK(R2S / MaxS == ZeroS);
     560           3 :     BOOST_CHECK(MaxS / R2S == 1);
     561           2 :     BOOST_CHECK_THROW(R2S / ZeroS, uint_error);
     562           1 : }
     563             : 
     564             : 
     565         310 : bool almostEqual(double d1, double d2)
     566             : {
     567         310 :     return fabs(d1-d2) <= 4*fabs(d1)*std::numeric_limits<double>::epsilon();
     568             : }
     569             : 
     570           2 : BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 GetSerializeSize, Serialize, Unserialize
     571             : {
     572           4 :     BOOST_CHECK(R1L.GetHex() == R1L.ToString());
     573           4 :     BOOST_CHECK(R2L.GetHex() == R2L.ToString());
     574           4 :     BOOST_CHECK(OneL.GetHex() == OneL.ToString());
     575           4 :     BOOST_CHECK(MaxL.GetHex() == MaxL.ToString());
     576           1 :     arith_uint256 TmpL(R1L);
     577           2 :     BOOST_CHECK(TmpL == R1L);
     578           3 :     TmpL.SetHex(R2L.ToString());   BOOST_CHECK(TmpL == R2L);
     579           3 :     TmpL.SetHex(ZeroL.ToString()); BOOST_CHECK(TmpL == 0);
     580           3 :     TmpL.SetHex(HalfL.ToString()); BOOST_CHECK(TmpL == HalfL);
     581             : 
     582           1 :     TmpL.SetHex(R1L.ToString());
     583           2 :     BOOST_CHECK(memcmp(R1L.begin(), R1Array, 32)==0);
     584           2 :     BOOST_CHECK(memcmp(TmpL.begin(), R1Array, 32)==0);
     585           2 :     BOOST_CHECK(memcmp(R2L.begin(), R2Array, 32)==0);
     586           2 :     BOOST_CHECK(memcmp(ZeroL.begin(), ZeroArray, 32)==0);
     587           2 :     BOOST_CHECK(memcmp(OneL.begin(), OneArray, 32)==0);
     588           2 :     BOOST_CHECK(R1L.size() == 32);
     589           2 :     BOOST_CHECK(R2L.size() == 32);
     590           2 :     BOOST_CHECK(ZeroL.size() == 32);
     591           2 :     BOOST_CHECK(MaxL.size() == 32);
     592           2 :     BOOST_CHECK(R1L.begin() + 32 == R1L.end());
     593           2 :     BOOST_CHECK(R2L.begin() + 32 == R2L.end());
     594           2 :     BOOST_CHECK(OneL.begin() + 32 == OneL.end());
     595           2 :     BOOST_CHECK(MaxL.begin() + 32 == MaxL.end());
     596           2 :     BOOST_CHECK(TmpL.begin() + 32 == TmpL.end());
     597           2 :     BOOST_CHECK(R1L.GetLow64()  == R1LLow64);
     598           2 :     BOOST_CHECK(HalfL.GetLow64() ==0x0000000000000000ULL);
     599           2 :     BOOST_CHECK(OneL.GetLow64() ==0x0000000000000001ULL);
     600           2 :     BOOST_CHECK(GetSerializeSize(R1L, PROTOCOL_VERSION) == 32);
     601           2 :     BOOST_CHECK(GetSerializeSize(ZeroL, PROTOCOL_VERSION) == 32);
     602             : 
     603           1 :     CDataStream ss(0, PROTOCOL_VERSION);
     604           1 :     ss << R1L;
     605           5 :     BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+32));
     606           1 :     ss >> TmpL;
     607           2 :     BOOST_CHECK(R1L == TmpL);
     608           1 :     ss.clear();
     609           1 :     ss << ZeroL;
     610           5 :     BOOST_CHECK(ss.str() == std::string(ZeroArray,ZeroArray+32));
     611           1 :     ss >> TmpL;
     612           2 :     BOOST_CHECK(ZeroL == TmpL);
     613           1 :     ss.clear();
     614           1 :     ss << MaxL;
     615           5 :     BOOST_CHECK(ss.str() == std::string(MaxArray,MaxArray+32));
     616           1 :     ss >> TmpL;
     617           2 :     BOOST_CHECK(MaxL == TmpL);
     618           1 :     ss.clear();
     619             : 
     620           4 :     BOOST_CHECK(R1S.GetHex() == R1S.ToString());
     621           4 :     BOOST_CHECK(R2S.GetHex() == R2S.ToString());
     622           4 :     BOOST_CHECK(OneS.GetHex() == OneS.ToString());
     623           4 :     BOOST_CHECK(MaxS.GetHex() == MaxS.ToString());
     624           1 :     arith_uint160 TmpS(R1S);
     625           2 :     BOOST_CHECK(TmpS == R1S);
     626           3 :     TmpS.SetHex(R2S.ToString());   BOOST_CHECK(TmpS == R2S);
     627           3 :     TmpS.SetHex(ZeroS.ToString()); BOOST_CHECK(TmpS == 0);
     628           3 :     TmpS.SetHex(HalfS.ToString()); BOOST_CHECK(TmpS == HalfS);
     629             : 
     630           1 :     TmpS.SetHex(R1S.ToString());
     631           2 :     BOOST_CHECK(memcmp(R1S.begin(), R1Array, 20)==0);
     632           2 :     BOOST_CHECK(memcmp(TmpS.begin(), R1Array, 20)==0);
     633           2 :     BOOST_CHECK(memcmp(R2S.begin(), R2Array, 20)==0);
     634           2 :     BOOST_CHECK(memcmp(ZeroS.begin(), ZeroArray, 20)==0);
     635           2 :     BOOST_CHECK(memcmp(OneS.begin(), OneArray, 20)==0);
     636           2 :     BOOST_CHECK(R1S.size() == 20);
     637           2 :     BOOST_CHECK(R2S.size() == 20);
     638           2 :     BOOST_CHECK(ZeroS.size() == 20);
     639           2 :     BOOST_CHECK(MaxS.size() == 20);
     640           2 :     BOOST_CHECK(R1S.begin() + 20 == R1S.end());
     641           2 :     BOOST_CHECK(R2S.begin() + 20 == R2S.end());
     642           2 :     BOOST_CHECK(OneS.begin() + 20 == OneS.end());
     643           2 :     BOOST_CHECK(MaxS.begin() + 20 == MaxS.end());
     644           2 :     BOOST_CHECK(TmpS.begin() + 20 == TmpS.end());
     645           2 :     BOOST_CHECK(R1S.GetLow64()  == R1LLow64);
     646           2 :     BOOST_CHECK(HalfS.GetLow64() ==0x0000000000000000ULL);
     647           2 :     BOOST_CHECK(OneS.GetLow64() ==0x0000000000000001ULL);
     648           2 :     BOOST_CHECK(GetSerializeSize(R1S, PROTOCOL_VERSION) == 20);
     649           2 :     BOOST_CHECK(GetSerializeSize(ZeroS, PROTOCOL_VERSION) == 20);
     650             : 
     651           1 :     ss << R1S;
     652           5 :     BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+20));
     653           1 :     ss >> TmpS;
     654           2 :     BOOST_CHECK(R1S == TmpS);
     655           1 :     ss.clear();
     656           1 :     ss << ZeroS;
     657           5 :     BOOST_CHECK(ss.str() == std::string(ZeroArray,ZeroArray+20));
     658           1 :     ss >> TmpS;
     659           2 :     BOOST_CHECK(ZeroS == TmpS);
     660           1 :     ss.clear();
     661           1 :     ss << MaxS;
     662           5 :     BOOST_CHECK(ss.str() == std::string(MaxArray,MaxArray+20));
     663           1 :     ss >> TmpS;
     664           2 :     BOOST_CHECK(MaxS == TmpS);
     665           1 :     ss.clear();
     666             : 
     667         256 :     for (unsigned int i = 0; i < 255; ++i)
     668             :     {
     669         510 :         BOOST_CHECK((OneL << i).getdouble() == ldexp(1.0,i));
     670         415 :         if (i < 160) BOOST_CHECK((OneS << i).getdouble() == ldexp(1.0,i));
     671             :     }
     672           2 :     BOOST_CHECK(ZeroL.getdouble() == 0.0);
     673           2 :     BOOST_CHECK(ZeroS.getdouble() == 0.0);
     674         204 :     for (int i = 256; i > 53; --i)
     675         406 :         BOOST_CHECK(almostEqual((R1L>>(256-i)).getdouble(), ldexp(R1Ldouble,i)));
     676         108 :     for (int i = 160; i > 53; --i)
     677         214 :         BOOST_CHECK(almostEqual((R1S>>(160-i)).getdouble(), ldexp(R1Sdouble,i)));
     678           1 :     uint64_t R1L64part = (R1L>>192).GetLow64();
     679           1 :     uint64_t R1S64part = (R1S>>96).GetLow64();
     680          54 :     for (int i = 53; i > 0; --i) // doubles can store all integers in {0,...,2^54-1} exactly
     681             :     {
     682         106 :         BOOST_CHECK((R1L>>(256-i)).getdouble() == (double)(R1L64part >> (64-i)));
     683         106 :         BOOST_CHECK((R1S>>(160-i)).getdouble() == (double)(R1S64part >> (64-i)));
     684             :     }
     685           1 : }
     686             : 
     687           2 : BOOST_AUTO_TEST_CASE(bignum_SetCompact)
     688             : {
     689           1 :     arith_uint256 num;
     690           1 :     bool fNegative;
     691           1 :     bool fOverflow;
     692           1 :     num.SetCompact(0, &fNegative, &fOverflow);
     693           2 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
     694           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
     695           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     696           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     697             : 
     698           1 :     num.SetCompact(0x00123456, &fNegative, &fOverflow);
     699           2 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
     700           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
     701           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     702           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     703             : 
     704           1 :     num.SetCompact(0x01003456, &fNegative, &fOverflow);
     705           2 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
     706           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
     707           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     708           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     709             : 
     710           1 :     num.SetCompact(0x02000056, &fNegative, &fOverflow);
     711           2 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
     712           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
     713           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     714           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     715             : 
     716           1 :     num.SetCompact(0x03000000, &fNegative, &fOverflow);
     717           2 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
     718           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
     719           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     720           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     721             : 
     722           1 :     num.SetCompact(0x04000000, &fNegative, &fOverflow);
     723           2 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
     724           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
     725           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     726           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     727             : 
     728           1 :     num.SetCompact(0x00923456, &fNegative, &fOverflow);
     729           2 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
     730           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
     731           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     732           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     733             : 
     734           1 :     num.SetCompact(0x01803456, &fNegative, &fOverflow);
     735           2 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
     736           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
     737           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     738           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     739             : 
     740           1 :     num.SetCompact(0x02800056, &fNegative, &fOverflow);
     741           2 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
     742           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
     743           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     744           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     745             : 
     746           1 :     num.SetCompact(0x03800000, &fNegative, &fOverflow);
     747           2 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
     748           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
     749           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     750           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     751             : 
     752           1 :     num.SetCompact(0x04800000, &fNegative, &fOverflow);
     753           2 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
     754           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
     755           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     756           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     757             : 
     758           1 :     num.SetCompact(0x01123456, &fNegative, &fOverflow);
     759           2 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000012");
     760           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0x01120000U);
     761           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     762           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     763             : 
     764             :     // Make sure that we don't generate compacts with the 0x00800000 bit set
     765          10 :     num = 0x80;
     766           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0x02008000U);
     767             : 
     768           1 :     num.SetCompact(0x01fedcba, &fNegative, &fOverflow);
     769           2 :     BOOST_CHECK_EQUAL(num.GetHex(), "000000000000000000000000000000000000000000000000000000000000007e");
     770           1 :     BOOST_CHECK_EQUAL(num.GetCompact(true), 0x01fe0000U);
     771           1 :     BOOST_CHECK_EQUAL(fNegative, true);
     772           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     773             : 
     774           1 :     num.SetCompact(0x02123456, &fNegative, &fOverflow);
     775           2 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000001234");
     776           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0x02123400U);
     777           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     778           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     779             : 
     780           1 :     num.SetCompact(0x03123456, &fNegative, &fOverflow);
     781           2 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000123456");
     782           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0x03123456U);
     783           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     784           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     785             : 
     786           1 :     num.SetCompact(0x04123456, &fNegative, &fOverflow);
     787           2 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000012345600");
     788           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0x04123456U);
     789           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     790           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     791             : 
     792           1 :     num.SetCompact(0x04923456, &fNegative, &fOverflow);
     793           2 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000012345600");
     794           1 :     BOOST_CHECK_EQUAL(num.GetCompact(true), 0x04923456U);
     795           1 :     BOOST_CHECK_EQUAL(fNegative, true);
     796           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     797             : 
     798           1 :     num.SetCompact(0x05009234, &fNegative, &fOverflow);
     799           2 :     BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000092340000");
     800           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0x05009234U);
     801           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     802           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     803             : 
     804           1 :     num.SetCompact(0x20123456, &fNegative, &fOverflow);
     805           2 :     BOOST_CHECK_EQUAL(num.GetHex(), "1234560000000000000000000000000000000000000000000000000000000000");
     806           1 :     BOOST_CHECK_EQUAL(num.GetCompact(), 0x20123456U);
     807           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     808           1 :     BOOST_CHECK_EQUAL(fOverflow, false);
     809             : 
     810           1 :     num.SetCompact(0xff123456, &fNegative, &fOverflow);
     811           1 :     BOOST_CHECK_EQUAL(fNegative, false);
     812           1 :     BOOST_CHECK_EQUAL(fOverflow, true);
     813           1 : }
     814             : 
     815             : 
     816           2 : BOOST_AUTO_TEST_CASE( getmaxcoverage ) // some more tests just to get 100% coverage
     817             : {
     818             :     // ~R1L give a base_uint<256>
     819          20 :     BOOST_CHECK((~~R1L >> 10) == (R1L >> 10)); BOOST_CHECK((~~R1S >> 10) == (R1S >> 10));
     820          20 :     BOOST_CHECK((~~R1L << 10) == (R1L << 10)); BOOST_CHECK((~~R1S << 10) == (R1S << 10));
     821          33 :     BOOST_CHECK(!(~~R1L < R1L)); BOOST_CHECK(!(~~R1S < R1S));
     822          33 :     BOOST_CHECK(~~R1L <= R1L); BOOST_CHECK(~~R1S <= R1S);
     823          33 :     BOOST_CHECK(!(~~R1L > R1L)); BOOST_CHECK(!(~~R1S > R1S));
     824          33 :     BOOST_CHECK(~~R1L >= R1L); BOOST_CHECK(~~R1S >= R1S);
     825          33 :     BOOST_CHECK(!(R1L < ~~R1L)); BOOST_CHECK(!(R1S < ~~R1S));
     826          33 :     BOOST_CHECK(R1L <= ~~R1L); BOOST_CHECK(R1S <= ~~R1S);
     827          33 :     BOOST_CHECK(!(R1L > ~~R1L)); BOOST_CHECK(!(R1S > ~~R1S));
     828          33 :     BOOST_CHECK(R1L >= ~~R1L); BOOST_CHECK(R1S >= ~~R1S);
     829             : 
     830          40 :     BOOST_CHECK(~~R1L + R2L == R1L + ~~R2L);
     831          28 :     BOOST_CHECK(~~R1S + R2S == R1S + ~~R2S);
     832          30 :     BOOST_CHECK(~~R1L - R2L == R1L - ~~R2L);
     833          21 :     BOOST_CHECK(~~R1S - R2S == R1S - ~~R2S);
     834          21 :     BOOST_CHECK(~R1L != R1L); BOOST_CHECK(R1L != ~R1L);
     835          15 :     BOOST_CHECK(~R1S != R1S); BOOST_CHECK(R1S != ~R1S);
     836           1 :     unsigned char TmpArray[32];
     837          76 :     CHECKBITWISEOPERATOR(~R1,R2,|)
     838          76 :     CHECKBITWISEOPERATOR(~R1,R2,^)
     839          76 :     CHECKBITWISEOPERATOR(~R1,R2,&)
     840          76 :     CHECKBITWISEOPERATOR(R1,~R2,|)
     841          76 :     CHECKBITWISEOPERATOR(R1,~R2,^)
     842          76 :     CHECKBITWISEOPERATOR(R1,~R2,&)
     843           1 : }
     844             : 
     845             : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 1.14