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.

uint256_tests.cpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 "arith_uint256.h"
  5. #include "uint256.h"
  6. #include "version.h"
  7. #include "test/test_bitcoin.h"
  8. #include <boost/test/unit_test.hpp>
  9. #include <stdint.h>
  10. #include <sstream>
  11. #include <iomanip>
  12. #include <limits>
  13. #include <cmath>
  14. #include <string>
  15. #include <stdio.h>
  16. BOOST_FIXTURE_TEST_SUITE(uint256_tests, BasicTestingSetup)
  17. const unsigned char R1Array[] =
  18. "\x9c\x52\x4a\xdb\xcf\x56\x11\x12\x2b\x29\x12\x5e\x5d\x35\xd2\xd2"
  19. "\x22\x81\xaa\xb5\x33\xf0\x08\x32\xd5\x56\xb1\xf9\xea\xe5\x1d\x7d";
  20. const char R1ArrayHex[] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c";
  21. const uint256 R1L = uint256(std::vector<unsigned char>(R1Array,R1Array+32));
  22. const uint160 R1S = uint160(std::vector<unsigned char>(R1Array,R1Array+20));
  23. const unsigned char R2Array[] =
  24. "\x70\x32\x1d\x7c\x47\xa5\x6b\x40\x26\x7e\x0a\xc3\xa6\x9c\xb6\xbf"
  25. "\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7";
  26. const uint256 R2L = uint256(std::vector<unsigned char>(R2Array,R2Array+32));
  27. const uint160 R2S = uint160(std::vector<unsigned char>(R2Array,R2Array+20));
  28. const unsigned char ZeroArray[] =
  29. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  30. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  31. const uint256 ZeroL = uint256(std::vector<unsigned char>(ZeroArray,ZeroArray+32));
  32. const uint160 ZeroS = uint160(std::vector<unsigned char>(ZeroArray,ZeroArray+20));
  33. const unsigned char OneArray[] =
  34. "\x01\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 uint256 OneL = uint256(std::vector<unsigned char>(OneArray,OneArray+32));
  37. const uint160 OneS = uint160(std::vector<unsigned char>(OneArray,OneArray+20));
  38. const unsigned char MaxArray[] =
  39. "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
  40. "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
  41. const uint256 MaxL = uint256(std::vector<unsigned char>(MaxArray,MaxArray+32));
  42. const uint160 MaxS = uint160(std::vector<unsigned char>(MaxArray,MaxArray+20));
  43. std::string ArrayToString(const unsigned char A[], unsigned int width)
  44. {
  45. std::stringstream Stream;
  46. Stream << std::hex;
  47. for (unsigned int i = 0; i < width; ++i)
  48. {
  49. Stream<<std::setw(2)<<std::setfill('0')<<(unsigned int)A[width-i-1];
  50. }
  51. return Stream.str();
  52. }
  53. inline uint160 uint160S(const char *str)
  54. {
  55. uint160 rv;
  56. rv.SetHex(str);
  57. return rv;
  58. }
  59. inline uint160 uint160S(const std::string& str)
  60. {
  61. uint160 rv;
  62. rv.SetHex(str);
  63. return rv;
  64. }
  65. BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
  66. {
  67. BOOST_CHECK(1 == 0+1);
  68. // constructor uint256(vector<char>):
  69. BOOST_CHECK(R1L.ToString() == ArrayToString(R1Array,32));
  70. BOOST_CHECK(R1S.ToString() == ArrayToString(R1Array,20));
  71. BOOST_CHECK(R2L.ToString() == ArrayToString(R2Array,32));
  72. BOOST_CHECK(R2S.ToString() == ArrayToString(R2Array,20));
  73. BOOST_CHECK(ZeroL.ToString() == ArrayToString(ZeroArray,32));
  74. BOOST_CHECK(ZeroS.ToString() == ArrayToString(ZeroArray,20));
  75. BOOST_CHECK(OneL.ToString() == ArrayToString(OneArray,32));
  76. BOOST_CHECK(OneS.ToString() == ArrayToString(OneArray,20));
  77. BOOST_CHECK(MaxL.ToString() == ArrayToString(MaxArray,32));
  78. BOOST_CHECK(MaxS.ToString() == ArrayToString(MaxArray,20));
  79. BOOST_CHECK(OneL.ToString() != ArrayToString(ZeroArray,32));
  80. BOOST_CHECK(OneS.ToString() != ArrayToString(ZeroArray,20));
  81. // == and !=
  82. BOOST_CHECK(R1L != R2L && R1S != R2S);
  83. BOOST_CHECK(ZeroL != OneL && ZeroS != OneS);
  84. BOOST_CHECK(OneL != ZeroL && OneS != ZeroS);
  85. BOOST_CHECK(MaxL != ZeroL && MaxS != ZeroS);
  86. // String Constructor and Copy Constructor
  87. BOOST_CHECK(uint256S("0x"+R1L.ToString()) == R1L);
  88. BOOST_CHECK(uint256S("0x"+R2L.ToString()) == R2L);
  89. BOOST_CHECK(uint256S("0x"+ZeroL.ToString()) == ZeroL);
  90. BOOST_CHECK(uint256S("0x"+OneL.ToString()) == OneL);
  91. BOOST_CHECK(uint256S("0x"+MaxL.ToString()) == MaxL);
  92. BOOST_CHECK(uint256S(R1L.ToString()) == R1L);
  93. BOOST_CHECK(uint256S(" 0x"+R1L.ToString()+" ") == R1L);
  94. BOOST_CHECK(uint256S("") == ZeroL);
  95. BOOST_CHECK(R1L == uint256S(R1ArrayHex));
  96. BOOST_CHECK(uint256(R1L) == R1L);
  97. BOOST_CHECK(uint256(ZeroL) == ZeroL);
  98. BOOST_CHECK(uint256(OneL) == OneL);
  99. BOOST_CHECK(uint160S("0x"+R1S.ToString()) == R1S);
  100. BOOST_CHECK(uint160S("0x"+R2S.ToString()) == R2S);
  101. BOOST_CHECK(uint160S("0x"+ZeroS.ToString()) == ZeroS);
  102. BOOST_CHECK(uint160S("0x"+OneS.ToString()) == OneS);
  103. BOOST_CHECK(uint160S("0x"+MaxS.ToString()) == MaxS);
  104. BOOST_CHECK(uint160S(R1S.ToString()) == R1S);
  105. BOOST_CHECK(uint160S(" 0x"+R1S.ToString()+" ") == R1S);
  106. BOOST_CHECK(uint160S("") == ZeroS);
  107. BOOST_CHECK(R1S == uint160S(R1ArrayHex));
  108. BOOST_CHECK(uint160(R1S) == R1S);
  109. BOOST_CHECK(uint160(ZeroS) == ZeroS);
  110. BOOST_CHECK(uint160(OneS) == OneS);
  111. }
  112. BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
  113. {
  114. uint256 LastL;
  115. for (int i = 255; i >= 0; --i) {
  116. uint256 TmpL;
  117. *(TmpL.begin() + (i>>3)) |= 1<<(7-(i&7));
  118. BOOST_CHECK( LastL < TmpL );
  119. LastL = TmpL;
  120. }
  121. BOOST_CHECK( ZeroL < R1L );
  122. BOOST_CHECK( R2L < R1L );
  123. BOOST_CHECK( ZeroL < OneL );
  124. BOOST_CHECK( OneL < MaxL );
  125. BOOST_CHECK( R1L < MaxL );
  126. BOOST_CHECK( R2L < MaxL );
  127. uint160 LastS;
  128. for (int i = 159; i >= 0; --i) {
  129. uint160 TmpS;
  130. *(TmpS.begin() + (i>>3)) |= 1<<(7-(i&7));
  131. BOOST_CHECK( LastS < TmpS );
  132. LastS = TmpS;
  133. }
  134. BOOST_CHECK( ZeroS < R1S );
  135. BOOST_CHECK( R2S < R1S );
  136. BOOST_CHECK( ZeroS < OneS );
  137. BOOST_CHECK( OneS < MaxS );
  138. BOOST_CHECK( R1S < MaxS );
  139. BOOST_CHECK( R2S < MaxS );
  140. }
  141. BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 GetSerializeSize, Serialize, Unserialize
  142. {
  143. BOOST_CHECK(R1L.GetHex() == R1L.ToString());
  144. BOOST_CHECK(R2L.GetHex() == R2L.ToString());
  145. BOOST_CHECK(OneL.GetHex() == OneL.ToString());
  146. BOOST_CHECK(MaxL.GetHex() == MaxL.ToString());
  147. uint256 TmpL(R1L);
  148. BOOST_CHECK(TmpL == R1L);
  149. TmpL.SetHex(R2L.ToString()); BOOST_CHECK(TmpL == R2L);
  150. TmpL.SetHex(ZeroL.ToString()); BOOST_CHECK(TmpL == uint256());
  151. TmpL.SetHex(R1L.ToString());
  152. BOOST_CHECK(memcmp(R1L.begin(), R1Array, 32)==0);
  153. BOOST_CHECK(memcmp(TmpL.begin(), R1Array, 32)==0);
  154. BOOST_CHECK(memcmp(R2L.begin(), R2Array, 32)==0);
  155. BOOST_CHECK(memcmp(ZeroL.begin(), ZeroArray, 32)==0);
  156. BOOST_CHECK(memcmp(OneL.begin(), OneArray, 32)==0);
  157. BOOST_CHECK(R1L.size() == sizeof(R1L));
  158. BOOST_CHECK(sizeof(R1L) == 32);
  159. BOOST_CHECK(R1L.size() == 32);
  160. BOOST_CHECK(R2L.size() == 32);
  161. BOOST_CHECK(ZeroL.size() == 32);
  162. BOOST_CHECK(MaxL.size() == 32);
  163. BOOST_CHECK(R1L.begin() + 32 == R1L.end());
  164. BOOST_CHECK(R2L.begin() + 32 == R2L.end());
  165. BOOST_CHECK(OneL.begin() + 32 == OneL.end());
  166. BOOST_CHECK(MaxL.begin() + 32 == MaxL.end());
  167. BOOST_CHECK(TmpL.begin() + 32 == TmpL.end());
  168. BOOST_CHECK(R1L.GetSerializeSize(0,PROTOCOL_VERSION) == 32);
  169. BOOST_CHECK(ZeroL.GetSerializeSize(0,PROTOCOL_VERSION) == 32);
  170. std::stringstream ss;
  171. R1L.Serialize(ss,0,PROTOCOL_VERSION);
  172. BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+32));
  173. TmpL.Unserialize(ss,0,PROTOCOL_VERSION);
  174. BOOST_CHECK(R1L == TmpL);
  175. ss.str("");
  176. ZeroL.Serialize(ss,0,PROTOCOL_VERSION);
  177. BOOST_CHECK(ss.str() == std::string(ZeroArray,ZeroArray+32));
  178. TmpL.Unserialize(ss,0,PROTOCOL_VERSION);
  179. BOOST_CHECK(ZeroL == TmpL);
  180. ss.str("");
  181. MaxL.Serialize(ss,0,PROTOCOL_VERSION);
  182. BOOST_CHECK(ss.str() == std::string(MaxArray,MaxArray+32));
  183. TmpL.Unserialize(ss,0,PROTOCOL_VERSION);
  184. BOOST_CHECK(MaxL == TmpL);
  185. ss.str("");
  186. BOOST_CHECK(R1S.GetHex() == R1S.ToString());
  187. BOOST_CHECK(R2S.GetHex() == R2S.ToString());
  188. BOOST_CHECK(OneS.GetHex() == OneS.ToString());
  189. BOOST_CHECK(MaxS.GetHex() == MaxS.ToString());
  190. uint160 TmpS(R1S);
  191. BOOST_CHECK(TmpS == R1S);
  192. TmpS.SetHex(R2S.ToString()); BOOST_CHECK(TmpS == R2S);
  193. TmpS.SetHex(ZeroS.ToString()); BOOST_CHECK(TmpS == uint160());
  194. TmpS.SetHex(R1S.ToString());
  195. BOOST_CHECK(memcmp(R1S.begin(), R1Array, 20)==0);
  196. BOOST_CHECK(memcmp(TmpS.begin(), R1Array, 20)==0);
  197. BOOST_CHECK(memcmp(R2S.begin(), R2Array, 20)==0);
  198. BOOST_CHECK(memcmp(ZeroS.begin(), ZeroArray, 20)==0);
  199. BOOST_CHECK(memcmp(OneS.begin(), OneArray, 20)==0);
  200. BOOST_CHECK(R1S.size() == sizeof(R1S));
  201. BOOST_CHECK(sizeof(R1S) == 20);
  202. BOOST_CHECK(R1S.size() == 20);
  203. BOOST_CHECK(R2S.size() == 20);
  204. BOOST_CHECK(ZeroS.size() == 20);
  205. BOOST_CHECK(MaxS.size() == 20);
  206. BOOST_CHECK(R1S.begin() + 20 == R1S.end());
  207. BOOST_CHECK(R2S.begin() + 20 == R2S.end());
  208. BOOST_CHECK(OneS.begin() + 20 == OneS.end());
  209. BOOST_CHECK(MaxS.begin() + 20 == MaxS.end());
  210. BOOST_CHECK(TmpS.begin() + 20 == TmpS.end());
  211. BOOST_CHECK(R1S.GetSerializeSize(0,PROTOCOL_VERSION) == 20);
  212. BOOST_CHECK(ZeroS.GetSerializeSize(0,PROTOCOL_VERSION) == 20);
  213. R1S.Serialize(ss,0,PROTOCOL_VERSION);
  214. BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+20));
  215. TmpS.Unserialize(ss,0,PROTOCOL_VERSION);
  216. BOOST_CHECK(R1S == TmpS);
  217. ss.str("");
  218. ZeroS.Serialize(ss,0,PROTOCOL_VERSION);
  219. BOOST_CHECK(ss.str() == std::string(ZeroArray,ZeroArray+20));
  220. TmpS.Unserialize(ss,0,PROTOCOL_VERSION);
  221. BOOST_CHECK(ZeroS == TmpS);
  222. ss.str("");
  223. MaxS.Serialize(ss,0,PROTOCOL_VERSION);
  224. BOOST_CHECK(ss.str() == std::string(MaxArray,MaxArray+20));
  225. TmpS.Unserialize(ss,0,PROTOCOL_VERSION);
  226. BOOST_CHECK(MaxS == TmpS);
  227. ss.str("");
  228. }
  229. BOOST_AUTO_TEST_CASE( conversion )
  230. {
  231. BOOST_CHECK(ArithToUint256(UintToArith256(ZeroL)) == ZeroL);
  232. BOOST_CHECK(ArithToUint256(UintToArith256(OneL)) == OneL);
  233. BOOST_CHECK(ArithToUint256(UintToArith256(R1L)) == R1L);
  234. BOOST_CHECK(ArithToUint256(UintToArith256(R2L)) == R2L);
  235. BOOST_CHECK(UintToArith256(ZeroL) == 0);
  236. BOOST_CHECK(UintToArith256(OneL) == 1);
  237. BOOST_CHECK(ArithToUint256(0) == ZeroL);
  238. BOOST_CHECK(ArithToUint256(1) == OneL);
  239. BOOST_CHECK(arith_uint256(R1L.GetHex()) == UintToArith256(R1L));
  240. BOOST_CHECK(arith_uint256(R2L.GetHex()) == UintToArith256(R2L));
  241. BOOST_CHECK(R1L.GetHex() == UintToArith256(R1L).GetHex());
  242. BOOST_CHECK(R2L.GetHex() == UintToArith256(R2L).GetHex());
  243. }
  244. BOOST_AUTO_TEST_SUITE_END()