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