You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

arith_uint256_tests.cpp 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. // Copyright (c) 2011-2013 The Bitcoin Core developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include <boost/test/unit_test.hpp>
  5. #include <stdint.h>
  6. #include <sstream>
  7. #include <iomanip>
  8. #include <limits>
  9. #include <cmath>
  10. #include "uint256.h"
  11. #include "arith_uint256.h"
  12. #include <string>
  13. #include "version.h"
  14. #include "test/test_bitcoin.h"
  15. BOOST_FIXTURE_TEST_SUITE(arith_uint256_tests, BasicTestingSetup)
  16. /// Convert vector to arith_uint256, via uint256 blob
  17. inline arith_uint256 arith_uint256V(const std::vector<unsigned char>& vch)
  18. {
  19. return UintToArith256(uint256(vch));
  20. }
  21. const unsigned char R1Array[] =
  22. "\x9c\x52\x4a\xdb\xcf\x56\x11\x12\x2b\x29\x12\x5e\x5d\x35\xd2\xd2"
  23. "\x22\x81\xaa\xb5\x33\xf0\x08\x32\xd5\x56\xb1\xf9\xea\xe5\x1d\x7d";
  24. const char R1ArrayHex[] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c";
  25. const double R1Ldouble = 0.4887374590559308955; // R1L equals roughly R1Ldouble * 2^256
  26. const arith_uint256 R1L = arith_uint256V(std::vector<unsigned char>(R1Array,R1Array+32));
  27. const uint64_t R1LLow64 = 0x121156cfdb4a529cULL;
  28. const unsigned char R2Array[] =
  29. "\x70\x32\x1d\x7c\x47\xa5\x6b\x40\x26\x7e\x0a\xc3\xa6\x9c\xb6\xbf"
  30. "\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7";
  31. const arith_uint256 R2L = arith_uint256V(std::vector<unsigned char>(R2Array,R2Array+32));
  32. const char R1LplusR2L[] = "549FB09FEA236A1EA3E31D4D58F1B1369288D204211CA751527CFC175767850C";
  33. const unsigned char ZeroArray[] =
  34. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  35. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  36. const arith_uint256 ZeroL = arith_uint256V(std::vector<unsigned char>(ZeroArray,ZeroArray+32));
  37. const unsigned char OneArray[] =
  38. "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  39. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  40. const arith_uint256 OneL = arith_uint256V(std::vector<unsigned char>(OneArray,OneArray+32));
  41. const unsigned char MaxArray[] =
  42. "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
  43. "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
  44. const arith_uint256 MaxL = arith_uint256V(std::vector<unsigned char>(MaxArray,MaxArray+32));
  45. const arith_uint256 HalfL = (OneL << 255);
  46. std::string ArrayToString(const unsigned char A[], unsigned int width)
  47. {
  48. std::stringstream Stream;
  49. Stream << std::hex;
  50. for (unsigned int i = 0; i < width; ++i)
  51. {
  52. Stream<<std::setw(2)<<std::setfill('0')<<(unsigned int)A[width-i-1];
  53. }
  54. return Stream.str();
  55. }
  56. BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
  57. {
  58. BOOST_CHECK(1 == 0+1);
  59. // constructor arith_uint256(vector<char>):
  60. BOOST_CHECK(R1L.ToString() == ArrayToString(R1Array,32));
  61. BOOST_CHECK(R2L.ToString() == ArrayToString(R2Array,32));
  62. BOOST_CHECK(ZeroL.ToString() == ArrayToString(ZeroArray,32));
  63. BOOST_CHECK(OneL.ToString() == ArrayToString(OneArray,32));
  64. BOOST_CHECK(MaxL.ToString() == ArrayToString(MaxArray,32));
  65. BOOST_CHECK(OneL.ToString() != ArrayToString(ZeroArray,32));
  66. // == and !=
  67. BOOST_CHECK(R1L != R2L);
  68. BOOST_CHECK(ZeroL != OneL);
  69. BOOST_CHECK(OneL != ZeroL);
  70. BOOST_CHECK(MaxL != ZeroL);
  71. BOOST_CHECK(~MaxL == ZeroL);
  72. BOOST_CHECK( ((R1L ^ R2L) ^ R1L) == R2L);
  73. uint64_t Tmp64 = 0xc4dab720d9c7acaaULL;
  74. for (unsigned int i = 0; i < 256; ++i)
  75. {
  76. BOOST_CHECK(ZeroL != (OneL << i));
  77. BOOST_CHECK((OneL << i) != ZeroL);
  78. BOOST_CHECK(R1L != (R1L ^ (OneL << i)));
  79. BOOST_CHECK(((arith_uint256(Tmp64) ^ (OneL << i) ) != Tmp64 ));
  80. }
  81. BOOST_CHECK(ZeroL == (OneL << 256));
  82. // String Constructor and Copy Constructor
  83. BOOST_CHECK(arith_uint256("0x"+R1L.ToString()) == R1L);
  84. BOOST_CHECK(arith_uint256("0x"+R2L.ToString()) == R2L);
  85. BOOST_CHECK(arith_uint256("0x"+ZeroL.ToString()) == ZeroL);
  86. BOOST_CHECK(arith_uint256("0x"+OneL.ToString()) == OneL);
  87. BOOST_CHECK(arith_uint256("0x"+MaxL.ToString()) == MaxL);
  88. BOOST_CHECK(arith_uint256(R1L.ToString()) == R1L);
  89. BOOST_CHECK(arith_uint256(" 0x"+R1L.ToString()+" ") == R1L);
  90. BOOST_CHECK(arith_uint256("") == ZeroL);
  91. BOOST_CHECK(R1L == arith_uint256(R1ArrayHex));
  92. BOOST_CHECK(arith_uint256(R1L) == R1L);
  93. BOOST_CHECK((arith_uint256(R1L^R2L)^R2L) == R1L);
  94. BOOST_CHECK(arith_uint256(ZeroL) == ZeroL);
  95. BOOST_CHECK(arith_uint256(OneL) == OneL);
  96. // uint64_t constructor
  97. BOOST_CHECK( (R1L & arith_uint256("0xffffffffffffffff")) == arith_uint256(R1LLow64));
  98. BOOST_CHECK(ZeroL == arith_uint256(0));
  99. BOOST_CHECK(OneL == arith_uint256(1));
  100. BOOST_CHECK(arith_uint256("0xffffffffffffffff") = arith_uint256(0xffffffffffffffffULL));
  101. // Assignment (from base_uint)
  102. arith_uint256 tmpL = ~ZeroL; BOOST_CHECK(tmpL == ~ZeroL);
  103. tmpL = ~OneL; BOOST_CHECK(tmpL == ~OneL);
  104. tmpL = ~R1L; BOOST_CHECK(tmpL == ~R1L);
  105. tmpL = ~R2L; BOOST_CHECK(tmpL == ~R2L);
  106. tmpL = ~MaxL; BOOST_CHECK(tmpL == ~MaxL);
  107. }
  108. void shiftArrayRight(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift)
  109. {
  110. for (unsigned int T=0; T < arrayLength; ++T)
  111. {
  112. unsigned int F = (T+bitsToShift/8);
  113. if (F < arrayLength)
  114. to[T] = from[F] >> (bitsToShift%8);
  115. else
  116. to[T] = 0;
  117. if (F + 1 < arrayLength)
  118. to[T] |= from[(F+1)] << (8-bitsToShift%8);
  119. }
  120. }
  121. void shiftArrayLeft(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift)
  122. {
  123. for (unsigned int T=0; T < arrayLength; ++T)
  124. {
  125. if (T >= bitsToShift/8)
  126. {
  127. unsigned int F = T-bitsToShift/8;
  128. to[T] = from[F] << (bitsToShift%8);
  129. if (T >= bitsToShift/8+1)
  130. to[T] |= from[F-1] >> (8-bitsToShift%8);
  131. }
  132. else {
  133. to[T] = 0;
  134. }
  135. }
  136. }
  137. BOOST_AUTO_TEST_CASE( shifts ) { // "<<" ">>" "<<=" ">>="
  138. unsigned char TmpArray[32];
  139. arith_uint256 TmpL;
  140. for (unsigned int i = 0; i < 256; ++i)
  141. {
  142. shiftArrayLeft(TmpArray, OneArray, 32, i);
  143. BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (OneL << i));
  144. TmpL = OneL; TmpL <<= i;
  145. BOOST_CHECK(TmpL == (OneL << i));
  146. BOOST_CHECK((HalfL >> (255-i)) == (OneL << i));
  147. TmpL = HalfL; TmpL >>= (255-i);
  148. BOOST_CHECK(TmpL == (OneL << i));
  149. shiftArrayLeft(TmpArray, R1Array, 32, i);
  150. BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L << i));
  151. TmpL = R1L; TmpL <<= i;
  152. BOOST_CHECK(TmpL == (R1L << i));
  153. shiftArrayRight(TmpArray, R1Array, 32, i);
  154. BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L >> i));
  155. TmpL = R1L; TmpL >>= i;
  156. BOOST_CHECK(TmpL == (R1L >> i));
  157. shiftArrayLeft(TmpArray, MaxArray, 32, i);
  158. BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL << i));
  159. TmpL = MaxL; TmpL <<= i;
  160. BOOST_CHECK(TmpL == (MaxL << i));
  161. shiftArrayRight(TmpArray, MaxArray, 32, i);
  162. BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL >> i));
  163. TmpL = MaxL; TmpL >>= i;
  164. BOOST_CHECK(TmpL == (MaxL >> i));
  165. }
  166. arith_uint256 c1L = arith_uint256(0x0123456789abcdefULL);
  167. arith_uint256 c2L = c1L << 128;
  168. for (unsigned int i = 0; i < 128; ++i) {
  169. BOOST_CHECK((c1L << i) == (c2L >> (128-i)));
  170. }
  171. for (unsigned int i = 128; i < 256; ++i) {
  172. BOOST_CHECK((c1L << i) == (c2L << (i-128)));
  173. }
  174. }
  175. BOOST_AUTO_TEST_CASE( unaryOperators ) // ! ~ -
  176. {
  177. BOOST_CHECK(!ZeroL);
  178. BOOST_CHECK(!(!OneL));
  179. for (unsigned int i = 0; i < 256; ++i)
  180. BOOST_CHECK(!(!(OneL<<i)));
  181. BOOST_CHECK(!(!R1L));
  182. BOOST_CHECK(!(!MaxL));
  183. BOOST_CHECK(~ZeroL == MaxL);
  184. unsigned char TmpArray[32];
  185. for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = ~R1Array[i]; }
  186. BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (~R1L));
  187. BOOST_CHECK(-ZeroL == ZeroL);
  188. BOOST_CHECK(-R1L == (~R1L)+1);
  189. for (unsigned int i = 0; i < 256; ++i)
  190. BOOST_CHECK(-(OneL<<i) == (MaxL << i));
  191. }
  192. // Check if doing _A_ _OP_ _B_ results in the same as applying _OP_ onto each
  193. // element of Aarray and Barray, and then converting the result into a arith_uint256.
  194. #define CHECKBITWISEOPERATOR(_A_,_B_,_OP_) \
  195. for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = _A_##Array[i] _OP_ _B_##Array[i]; } \
  196. BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (_A_##L _OP_ _B_##L));
  197. #define CHECKASSIGNMENTOPERATOR(_A_,_B_,_OP_) \
  198. TmpL = _A_##L; TmpL _OP_##= _B_##L; BOOST_CHECK(TmpL == (_A_##L _OP_ _B_##L));
  199. BOOST_AUTO_TEST_CASE( bitwiseOperators )
  200. {
  201. unsigned char TmpArray[32];
  202. CHECKBITWISEOPERATOR(R1,R2,|)
  203. CHECKBITWISEOPERATOR(R1,R2,^)
  204. CHECKBITWISEOPERATOR(R1,R2,&)
  205. CHECKBITWISEOPERATOR(R1,Zero,|)
  206. CHECKBITWISEOPERATOR(R1,Zero,^)
  207. CHECKBITWISEOPERATOR(R1,Zero,&)
  208. CHECKBITWISEOPERATOR(R1,Max,|)
  209. CHECKBITWISEOPERATOR(R1,Max,^)
  210. CHECKBITWISEOPERATOR(R1,Max,&)
  211. CHECKBITWISEOPERATOR(Zero,R1,|)
  212. CHECKBITWISEOPERATOR(Zero,R1,^)
  213. CHECKBITWISEOPERATOR(Zero,R1,&)
  214. CHECKBITWISEOPERATOR(Max,R1,|)
  215. CHECKBITWISEOPERATOR(Max,R1,^)
  216. CHECKBITWISEOPERATOR(Max,R1,&)
  217. arith_uint256 TmpL;
  218. CHECKASSIGNMENTOPERATOR(R1,R2,|)
  219. CHECKASSIGNMENTOPERATOR(R1,R2,^)
  220. CHECKASSIGNMENTOPERATOR(R1,R2,&)
  221. CHECKASSIGNMENTOPERATOR(R1,Zero,|)
  222. CHECKASSIGNMENTOPERATOR(R1,Zero,^)
  223. CHECKASSIGNMENTOPERATOR(R1,Zero,&)
  224. CHECKASSIGNMENTOPERATOR(R1,Max,|)
  225. CHECKASSIGNMENTOPERATOR(R1,Max,^)
  226. CHECKASSIGNMENTOPERATOR(R1,Max,&)
  227. CHECKASSIGNMENTOPERATOR(Zero,R1,|)
  228. CHECKASSIGNMENTOPERATOR(Zero,R1,^)
  229. CHECKASSIGNMENTOPERATOR(Zero,R1,&)
  230. CHECKASSIGNMENTOPERATOR(Max,R1,|)
  231. CHECKASSIGNMENTOPERATOR(Max,R1,^)
  232. CHECKASSIGNMENTOPERATOR(Max,R1,&)
  233. uint64_t Tmp64 = 0xe1db685c9a0b47a2ULL;
  234. TmpL = R1L; TmpL |= Tmp64; BOOST_CHECK(TmpL == (R1L | arith_uint256(Tmp64)));
  235. TmpL = R1L; TmpL |= 0; BOOST_CHECK(TmpL == R1L);
  236. TmpL ^= 0; BOOST_CHECK(TmpL == R1L);
  237. TmpL ^= Tmp64; BOOST_CHECK(TmpL == (R1L ^ arith_uint256(Tmp64)));
  238. }
  239. BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
  240. {
  241. arith_uint256 TmpL;
  242. for (unsigned int i = 0; i < 256; ++i) {
  243. TmpL= OneL<< i;
  244. BOOST_CHECK( TmpL >= ZeroL && TmpL > ZeroL && ZeroL < TmpL && ZeroL <= TmpL);
  245. BOOST_CHECK( TmpL >= 0 && TmpL > 0 && 0 < TmpL && 0 <= TmpL);
  246. TmpL |= R1L;
  247. BOOST_CHECK( TmpL >= R1L ); BOOST_CHECK( (TmpL == R1L) != (TmpL > R1L)); BOOST_CHECK( (TmpL == R1L) || !( TmpL <= R1L));
  248. BOOST_CHECK( R1L <= TmpL ); BOOST_CHECK( (R1L == TmpL) != (R1L < TmpL)); BOOST_CHECK( (TmpL == R1L) || !( R1L >= TmpL));
  249. BOOST_CHECK(! (TmpL < R1L)); BOOST_CHECK(! (R1L > TmpL));
  250. }
  251. }
  252. BOOST_AUTO_TEST_CASE( plusMinus )
  253. {
  254. arith_uint256 TmpL = 0;
  255. BOOST_CHECK(R1L+R2L == arith_uint256(R1LplusR2L));
  256. TmpL += R1L;
  257. BOOST_CHECK(TmpL == R1L);
  258. TmpL += R2L;
  259. BOOST_CHECK(TmpL == R1L + R2L);
  260. BOOST_CHECK(OneL+MaxL == ZeroL);
  261. BOOST_CHECK(MaxL+OneL == ZeroL);
  262. for (unsigned int i = 1; i < 256; ++i) {
  263. BOOST_CHECK( (MaxL >> i) + OneL == (HalfL >> (i-1)) );
  264. BOOST_CHECK( OneL + (MaxL >> i) == (HalfL >> (i-1)) );
  265. TmpL = (MaxL>>i); TmpL += OneL;
  266. BOOST_CHECK( TmpL == (HalfL >> (i-1)) );
  267. TmpL = (MaxL>>i); TmpL += 1;
  268. BOOST_CHECK( TmpL == (HalfL >> (i-1)) );
  269. TmpL = (MaxL>>i);
  270. BOOST_CHECK( TmpL++ == (MaxL>>i) );
  271. BOOST_CHECK( TmpL == (HalfL >> (i-1)));
  272. }
  273. BOOST_CHECK(arith_uint256(0xbedc77e27940a7ULL) + 0xee8d836fce66fbULL == arith_uint256(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL));
  274. TmpL = arith_uint256(0xbedc77e27940a7ULL); TmpL += 0xee8d836fce66fbULL;
  275. BOOST_CHECK(TmpL == arith_uint256(0xbedc77e27940a7ULL+0xee8d836fce66fbULL));
  276. TmpL -= 0xee8d836fce66fbULL; BOOST_CHECK(TmpL == 0xbedc77e27940a7ULL);
  277. TmpL = R1L;
  278. BOOST_CHECK(++TmpL == R1L+1);
  279. BOOST_CHECK(R1L -(-R2L) == R1L+R2L);
  280. BOOST_CHECK(R1L -(-OneL) == R1L+OneL);
  281. BOOST_CHECK(R1L - OneL == R1L+(-OneL));
  282. for (unsigned int i = 1; i < 256; ++i) {
  283. BOOST_CHECK((MaxL>>i) - (-OneL) == (HalfL >> (i-1)));
  284. BOOST_CHECK((HalfL >> (i-1)) - OneL == (MaxL>>i));
  285. TmpL = (HalfL >> (i-1));
  286. BOOST_CHECK(TmpL-- == (HalfL >> (i-1)));
  287. BOOST_CHECK(TmpL == (MaxL >> i));
  288. TmpL = (HalfL >> (i-1));
  289. BOOST_CHECK(--TmpL == (MaxL >> i));
  290. }
  291. TmpL = R1L;
  292. BOOST_CHECK(--TmpL == R1L-1);
  293. }
  294. BOOST_AUTO_TEST_CASE( multiply )
  295. {
  296. BOOST_CHECK((R1L * R1L).ToString() == "62a38c0486f01e45879d7910a7761bf30d5237e9873f9bff3642a732c4d84f10");
  297. BOOST_CHECK((R1L * R2L).ToString() == "de37805e9986996cfba76ff6ba51c008df851987d9dd323f0e5de07760529c40");
  298. BOOST_CHECK((R1L * ZeroL) == ZeroL);
  299. BOOST_CHECK((R1L * OneL) == R1L);
  300. BOOST_CHECK((R1L * MaxL) == -R1L);
  301. BOOST_CHECK((R2L * R1L) == (R1L * R2L));
  302. BOOST_CHECK((R2L * R2L).ToString() == "ac8c010096767d3cae5005dec28bb2b45a1d85ab7996ccd3e102a650f74ff100");
  303. BOOST_CHECK((R2L * ZeroL) == ZeroL);
  304. BOOST_CHECK((R2L * OneL) == R2L);
  305. BOOST_CHECK((R2L * MaxL) == -R2L);
  306. BOOST_CHECK(MaxL * MaxL == OneL);
  307. BOOST_CHECK((R1L * 0) == 0);
  308. BOOST_CHECK((R1L * 1) == R1L);
  309. BOOST_CHECK((R1L * 3).ToString() == "7759b1c0ed14047f961ad09b20ff83687876a0181a367b813634046f91def7d4");
  310. BOOST_CHECK((R2L * 0x87654321UL).ToString() == "23f7816e30c4ae2017257b7a0fa64d60402f5234d46e746b61c960d09a26d070");
  311. }
  312. BOOST_AUTO_TEST_CASE( divide )
  313. {
  314. arith_uint256 D1L("AD7133AC1977FA2B7");
  315. arith_uint256 D2L("ECD751716");
  316. BOOST_CHECK((R1L / D1L).ToString() == "00000000000000000b8ac01106981635d9ed112290f8895545a7654dde28fb3a");
  317. BOOST_CHECK((R1L / D2L).ToString() == "000000000873ce8efec5b67150bad3aa8c5fcb70e947586153bf2cec7c37c57a");
  318. BOOST_CHECK(R1L / OneL == R1L);
  319. BOOST_CHECK(R1L / MaxL == ZeroL);
  320. BOOST_CHECK(MaxL / R1L == 2);
  321. BOOST_CHECK_THROW(R1L / ZeroL, uint_error);
  322. BOOST_CHECK((R2L / D1L).ToString() == "000000000000000013e1665895a1cc981de6d93670105a6b3ec3b73141b3a3c5");
  323. BOOST_CHECK((R2L / D2L).ToString() == "000000000e8f0abe753bb0afe2e9437ee85d280be60882cf0bd1aaf7fa3cc2c4");
  324. BOOST_CHECK(R2L / OneL == R2L);
  325. BOOST_CHECK(R2L / MaxL == ZeroL);
  326. BOOST_CHECK(MaxL / R2L == 1);
  327. BOOST_CHECK_THROW(R2L / ZeroL, uint_error);
  328. }
  329. bool almostEqual(double d1, double d2)
  330. {
  331. return fabs(d1-d2) <= 4*fabs(d1)*std::numeric_limits<double>::epsilon();
  332. }
  333. BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex size() GetLow64 GetSerializeSize, Serialize, Unserialize
  334. {
  335. BOOST_CHECK(R1L.GetHex() == R1L.ToString());
  336. BOOST_CHECK(R2L.GetHex() == R2L.ToString());
  337. BOOST_CHECK(OneL.GetHex() == OneL.ToString());
  338. BOOST_CHECK(MaxL.GetHex() == MaxL.ToString());
  339. arith_uint256 TmpL(R1L);
  340. BOOST_CHECK(TmpL == R1L);
  341. TmpL.SetHex(R2L.ToString()); BOOST_CHECK(TmpL == R2L);
  342. TmpL.SetHex(ZeroL.ToString()); BOOST_CHECK(TmpL == 0);
  343. TmpL.SetHex(HalfL.ToString()); BOOST_CHECK(TmpL == HalfL);
  344. TmpL.SetHex(R1L.ToString());
  345. BOOST_CHECK(R1L.size() == 32);
  346. BOOST_CHECK(R2L.size() == 32);
  347. BOOST_CHECK(ZeroL.size() == 32);
  348. BOOST_CHECK(MaxL.size() == 32);
  349. BOOST_CHECK(R1L.GetLow64() == R1LLow64);
  350. BOOST_CHECK(HalfL.GetLow64() ==0x0000000000000000ULL);
  351. BOOST_CHECK(OneL.GetLow64() ==0x0000000000000001ULL);
  352. for (unsigned int i = 0; i < 255; ++i)
  353. {
  354. BOOST_CHECK((OneL << i).getdouble() == ldexp(1.0,i));
  355. }
  356. BOOST_CHECK(ZeroL.getdouble() == 0.0);
  357. for (int i = 256; i > 53; --i)
  358. BOOST_CHECK(almostEqual((R1L>>(256-i)).getdouble(), ldexp(R1Ldouble,i)));
  359. uint64_t R1L64part = (R1L>>192).GetLow64();
  360. for (int i = 53; i > 0; --i) // doubles can store all integers in {0,...,2^54-1} exactly
  361. {
  362. BOOST_CHECK((R1L>>(256-i)).getdouble() == (double)(R1L64part >> (64-i)));
  363. }
  364. }
  365. BOOST_AUTO_TEST_CASE(bignum_SetCompact)
  366. {
  367. arith_uint256 num;
  368. bool fNegative;
  369. bool fOverflow;
  370. num.SetCompact(0, &fNegative, &fOverflow);
  371. BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
  372. BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
  373. BOOST_CHECK_EQUAL(fNegative, false);
  374. BOOST_CHECK_EQUAL(fOverflow, false);
  375. num.SetCompact(0x00123456, &fNegative, &fOverflow);
  376. BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
  377. BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
  378. BOOST_CHECK_EQUAL(fNegative, false);
  379. BOOST_CHECK_EQUAL(fOverflow, false);
  380. num.SetCompact(0x01003456, &fNegative, &fOverflow);
  381. BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
  382. BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
  383. BOOST_CHECK_EQUAL(fNegative, false);
  384. BOOST_CHECK_EQUAL(fOverflow, false);
  385. num.SetCompact(0x02000056, &fNegative, &fOverflow);
  386. BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
  387. BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
  388. BOOST_CHECK_EQUAL(fNegative, false);
  389. BOOST_CHECK_EQUAL(fOverflow, false);
  390. num.SetCompact(0x03000000, &fNegative, &fOverflow);
  391. BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
  392. BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
  393. BOOST_CHECK_EQUAL(fNegative, false);
  394. BOOST_CHECK_EQUAL(fOverflow, false);
  395. num.SetCompact(0x04000000, &fNegative, &fOverflow);
  396. BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
  397. BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
  398. BOOST_CHECK_EQUAL(fNegative, false);
  399. BOOST_CHECK_EQUAL(fOverflow, false);
  400. num.SetCompact(0x00923456, &fNegative, &fOverflow);
  401. BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
  402. BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
  403. BOOST_CHECK_EQUAL(fNegative, false);
  404. BOOST_CHECK_EQUAL(fOverflow, false);
  405. num.SetCompact(0x01803456, &fNegative, &fOverflow);
  406. BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
  407. BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
  408. BOOST_CHECK_EQUAL(fNegative, false);
  409. BOOST_CHECK_EQUAL(fOverflow, false);
  410. num.SetCompact(0x02800056, &fNegative, &fOverflow);
  411. BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
  412. BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
  413. BOOST_CHECK_EQUAL(fNegative, false);
  414. BOOST_CHECK_EQUAL(fOverflow, false);
  415. num.SetCompact(0x03800000, &fNegative, &fOverflow);
  416. BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
  417. BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
  418. BOOST_CHECK_EQUAL(fNegative, false);
  419. BOOST_CHECK_EQUAL(fOverflow, false);
  420. num.SetCompact(0x04800000, &fNegative, &fOverflow);
  421. BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
  422. BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
  423. BOOST_CHECK_EQUAL(fNegative, false);
  424. BOOST_CHECK_EQUAL(fOverflow, false);
  425. num.SetCompact(0x01123456, &fNegative, &fOverflow);
  426. BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000012");
  427. BOOST_CHECK_EQUAL(num.GetCompact(), 0x01120000U);
  428. BOOST_CHECK_EQUAL(fNegative, false);
  429. BOOST_CHECK_EQUAL(fOverflow, false);
  430. // Make sure that we don't generate compacts with the 0x00800000 bit set
  431. num = 0x80;
  432. BOOST_CHECK_EQUAL(num.GetCompact(), 0x02008000U);
  433. num.SetCompact(0x01fedcba, &fNegative, &fOverflow);
  434. BOOST_CHECK_EQUAL(num.GetHex(), "000000000000000000000000000000000000000000000000000000000000007e");
  435. BOOST_CHECK_EQUAL(num.GetCompact(true), 0x01fe0000U);
  436. BOOST_CHECK_EQUAL(fNegative, true);
  437. BOOST_CHECK_EQUAL(fOverflow, false);
  438. num.SetCompact(0x02123456, &fNegative, &fOverflow);
  439. BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000001234");
  440. BOOST_CHECK_EQUAL(num.GetCompact(), 0x02123400U);
  441. BOOST_CHECK_EQUAL(fNegative, false);
  442. BOOST_CHECK_EQUAL(fOverflow, false);
  443. num.SetCompact(0x03123456, &fNegative, &fOverflow);
  444. BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000123456");
  445. BOOST_CHECK_EQUAL(num.GetCompact(), 0x03123456U);
  446. BOOST_CHECK_EQUAL(fNegative, false);
  447. BOOST_CHECK_EQUAL(fOverflow, false);
  448. num.SetCompact(0x04123456, &fNegative, &fOverflow);
  449. BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000012345600");
  450. BOOST_CHECK_EQUAL(num.GetCompact(), 0x04123456U);
  451. BOOST_CHECK_EQUAL(fNegative, false);
  452. BOOST_CHECK_EQUAL(fOverflow, false);
  453. num.SetCompact(0x04923456, &fNegative, &fOverflow);
  454. BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000012345600");
  455. BOOST_CHECK_EQUAL(num.GetCompact(true), 0x04923456U);
  456. BOOST_CHECK_EQUAL(fNegative, true);
  457. BOOST_CHECK_EQUAL(fOverflow, false);
  458. num.SetCompact(0x05009234, &fNegative, &fOverflow);
  459. BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000092340000");
  460. BOOST_CHECK_EQUAL(num.GetCompact(), 0x05009234U);
  461. BOOST_CHECK_EQUAL(fNegative, false);
  462. BOOST_CHECK_EQUAL(fOverflow, false);
  463. num.SetCompact(0x20123456, &fNegative, &fOverflow);
  464. BOOST_CHECK_EQUAL(num.GetHex(), "1234560000000000000000000000000000000000000000000000000000000000");
  465. BOOST_CHECK_EQUAL(num.GetCompact(), 0x20123456U);
  466. BOOST_CHECK_EQUAL(fNegative, false);
  467. BOOST_CHECK_EQUAL(fOverflow, false);
  468. num.SetCompact(0xff123456, &fNegative, &fOverflow);
  469. BOOST_CHECK_EQUAL(fNegative, false);
  470. BOOST_CHECK_EQUAL(fOverflow, true);
  471. }
  472. BOOST_AUTO_TEST_CASE( getmaxcoverage ) // some more tests just to get 100% coverage
  473. {
  474. // ~R1L give a base_uint<256>
  475. BOOST_CHECK((~~R1L >> 10) == (R1L >> 10));
  476. BOOST_CHECK((~~R1L << 10) == (R1L << 10));
  477. BOOST_CHECK(!(~~R1L < R1L));
  478. BOOST_CHECK(~~R1L <= R1L);
  479. BOOST_CHECK(!(~~R1L > R1L));
  480. BOOST_CHECK(~~R1L >= R1L);
  481. BOOST_CHECK(!(R1L < ~~R1L));
  482. BOOST_CHECK(R1L <= ~~R1L);
  483. BOOST_CHECK(!(R1L > ~~R1L));
  484. BOOST_CHECK(R1L >= ~~R1L);
  485. BOOST_CHECK(~~R1L + R2L == R1L + ~~R2L);
  486. BOOST_CHECK(~~R1L - R2L == R1L - ~~R2L);
  487. BOOST_CHECK(~R1L != R1L); BOOST_CHECK(R1L != ~R1L);
  488. unsigned char TmpArray[32];
  489. CHECKBITWISEOPERATOR(~R1,R2,|)
  490. CHECKBITWISEOPERATOR(~R1,R2,^)
  491. CHECKBITWISEOPERATOR(~R1,R2,&)
  492. CHECKBITWISEOPERATOR(R1,~R2,|)
  493. CHECKBITWISEOPERATOR(R1,~R2,^)
  494. CHECKBITWISEOPERATOR(R1,~R2,&)
  495. }
  496. BOOST_AUTO_TEST_SUITE_END()