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