Bitcoin Core  29.1.0
P2P Digital Currency
arith_uint256_tests.cpp
Go to the documentation of this file.
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>
7 #include <uint256.h>
8 
9 #include <boost/test/unit_test.hpp>
10 
11 #include <cmath>
12 #include <cstdint>
13 #include <iomanip>
14 #include <limits>
15 #include <sstream>
16 #include <string>
17 #include <vector>
18 
19 BOOST_AUTO_TEST_SUITE(arith_uint256_tests)
20 
21 static inline arith_uint256 arith_uint256V(const std::vector<unsigned char>& vch)
23 {
24  return UintToArith256(uint256(vch));
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 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 const arith_uint256 R2L = arith_uint256V(std::vector<unsigned char>(R2Array,R2Array+32));
38 
39 const unsigned char ZeroArray[] =
40  "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
41  "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
42 const arith_uint256 ZeroL = arith_uint256V(std::vector<unsigned char>(ZeroArray,ZeroArray+32));
43 
44 const unsigned char OneArray[] =
45  "\x01\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 OneL = arith_uint256V(std::vector<unsigned char>(OneArray,OneArray+32));
48 
49 const unsigned char MaxArray[] =
50  "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
51  "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
52 const arith_uint256 MaxL = arith_uint256V(std::vector<unsigned char>(MaxArray,MaxArray+32));
53 
54 const arith_uint256 HalfL = (OneL << 255);
55 static std::string ArrayToString(const unsigned char A[], unsigned int width)
56 {
57  std::stringstream Stream;
58  Stream << std::hex;
59  for (unsigned int i = 0; i < width; ++i)
60  {
61  Stream<<std::setw(2)<<std::setfill('0')<<(unsigned int)A[width-i-1];
62  }
63  return Stream.str();
64 }
65 
66 BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
67 {
68  BOOST_CHECK(1 == 0+1);
69  // constructor arith_uint256(vector<char>):
76 
77  // == and !=
78  BOOST_CHECK(R1L != R2L);
82  BOOST_CHECK(~MaxL == ZeroL);
83  BOOST_CHECK( ((R1L ^ R2L) ^ R1L) == R2L);
84 
85  uint64_t Tmp64 = 0xc4dab720d9c7acaaULL;
86  for (unsigned int i = 0; i < 256; ++i)
87  {
88  BOOST_CHECK(ZeroL != (OneL << i));
89  BOOST_CHECK((OneL << i) != ZeroL);
90  BOOST_CHECK(R1L != (R1L ^ (OneL << i)));
91  BOOST_CHECK(((arith_uint256(Tmp64) ^ (OneL << i) ) != Tmp64 ));
92  }
93  BOOST_CHECK(ZeroL == (OneL << 256));
94 
95  // Construct from hex string
102 
103  // Copy constructor
108 
109  // uint64_t constructor
110  BOOST_CHECK_EQUAL(R1L & arith_uint256{0xffffffffffffffff}, arith_uint256{R1LLow64});
113  BOOST_CHECK_EQUAL(arith_uint256{0xffffffffffffffff}, arith_uint256{0xffffffffffffffffULL});
114 
115  // Assignment (from base_uint)
116  arith_uint256 tmpL = ~ZeroL; BOOST_CHECK(tmpL == ~ZeroL);
117  tmpL = ~OneL; BOOST_CHECK(tmpL == ~OneL);
118  tmpL = ~R1L; BOOST_CHECK(tmpL == ~R1L);
119  tmpL = ~R2L; BOOST_CHECK(tmpL == ~R2L);
120  tmpL = ~MaxL; BOOST_CHECK(tmpL == ~MaxL);
121 }
122 
123 static void shiftArrayRight(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift)
124 {
125  for (unsigned int T=0; T < arrayLength; ++T)
126  {
127  unsigned int F = (T+bitsToShift/8);
128  if (F < arrayLength)
129  to[T] = uint8_t(from[F] >> (bitsToShift % 8));
130  else
131  to[T] = 0;
132  if (F + 1 < arrayLength)
133  to[T] |= uint8_t(from[(F + 1)] << (8 - bitsToShift % 8));
134  }
135 }
136 
137 static void shiftArrayLeft(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift)
138 {
139  for (unsigned int T=0; T < arrayLength; ++T)
140  {
141  if (T >= bitsToShift/8)
142  {
143  unsigned int F = T-bitsToShift/8;
144  to[T] = uint8_t(from[F] << (bitsToShift % 8));
145  if (T >= bitsToShift/8+1)
146  to[T] |= uint8_t(from[F - 1] >> (8 - bitsToShift % 8));
147  }
148  else {
149  to[T] = 0;
150  }
151  }
152 }
153 
154 BOOST_AUTO_TEST_CASE( shifts ) { // "<<" ">>" "<<=" ">>="
155  unsigned char TmpArray[32];
156  arith_uint256 TmpL;
157  for (unsigned int i = 0; i < 256; ++i)
158  {
159  shiftArrayLeft(TmpArray, OneArray, 32, i);
160  BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (OneL << i));
161  TmpL = OneL; TmpL <<= i;
162  BOOST_CHECK(TmpL == (OneL << i));
163  BOOST_CHECK((HalfL >> (255-i)) == (OneL << i));
164  TmpL = HalfL; TmpL >>= (255-i);
165  BOOST_CHECK(TmpL == (OneL << i));
166 
167  shiftArrayLeft(TmpArray, R1Array, 32, i);
168  BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L << i));
169  TmpL = R1L; TmpL <<= i;
170  BOOST_CHECK(TmpL == (R1L << i));
171 
172  shiftArrayRight(TmpArray, R1Array, 32, i);
173  BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L >> i));
174  TmpL = R1L; TmpL >>= i;
175  BOOST_CHECK(TmpL == (R1L >> i));
176 
177  shiftArrayLeft(TmpArray, MaxArray, 32, i);
178  BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL << i));
179  TmpL = MaxL; TmpL <<= i;
180  BOOST_CHECK(TmpL == (MaxL << i));
181 
182  shiftArrayRight(TmpArray, MaxArray, 32, i);
183  BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL >> i));
184  TmpL = MaxL; TmpL >>= i;
185  BOOST_CHECK(TmpL == (MaxL >> i));
186  }
187  arith_uint256 c1L = arith_uint256(0x0123456789abcdefULL);
188  arith_uint256 c2L = c1L << 128;
189  for (unsigned int i = 0; i < 128; ++i) {
190  BOOST_CHECK((c1L << i) == (c2L >> (128-i)));
191  }
192  for (unsigned int i = 128; i < 256; ++i) {
193  BOOST_CHECK((c1L << i) == (c2L << (i-128)));
194  }
195 }
196 
197 BOOST_AUTO_TEST_CASE( unaryOperators ) // ! ~ -
198 {
199  BOOST_CHECK(~ZeroL == MaxL);
200 
201  unsigned char TmpArray[32];
202  for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = uint8_t(~R1Array[i]); }
203  BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (~R1L));
204 
205  BOOST_CHECK(-ZeroL == ZeroL);
206  BOOST_CHECK(-R1L == (~R1L)+1);
207  for (unsigned int i = 0; i < 256; ++i)
208  BOOST_CHECK(-(OneL<<i) == (MaxL << i));
209 }
210 
211 
212 // Check if doing _A_ _OP_ _B_ results in the same as applying _OP_ onto each
213 // element of Aarray and Barray, and then converting the result into an arith_uint256.
214 #define CHECKBITWISEOPERATOR(_A_,_B_,_OP_) \
215  for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = uint8_t(_A_##Array[i] _OP_ _B_##Array[i]); } \
216  BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (_A_##L _OP_ _B_##L));
217 
218 #define CHECKASSIGNMENTOPERATOR(_A_,_B_,_OP_) \
219  TmpL = _A_##L; TmpL _OP_##= _B_##L; BOOST_CHECK(TmpL == (_A_##L _OP_ _B_##L));
220 
221 BOOST_AUTO_TEST_CASE( bitwiseOperators )
222 {
223  unsigned char TmpArray[32];
224 
225  CHECKBITWISEOPERATOR(R1,R2,|)
226  CHECKBITWISEOPERATOR(R1,R2,^)
227  CHECKBITWISEOPERATOR(R1,R2,&)
228  CHECKBITWISEOPERATOR(R1,Zero,|)
229  CHECKBITWISEOPERATOR(R1,Zero,^)
230  CHECKBITWISEOPERATOR(R1,Zero,&)
231  CHECKBITWISEOPERATOR(R1,Max,|)
232  CHECKBITWISEOPERATOR(R1,Max,^)
233  CHECKBITWISEOPERATOR(R1,Max,&)
234  CHECKBITWISEOPERATOR(Zero,R1,|)
235  CHECKBITWISEOPERATOR(Zero,R1,^)
236  CHECKBITWISEOPERATOR(Zero,R1,&)
237  CHECKBITWISEOPERATOR(Max,R1,|)
238  CHECKBITWISEOPERATOR(Max,R1,^)
239  CHECKBITWISEOPERATOR(Max,R1,&)
240 
241  arith_uint256 TmpL;
242  CHECKASSIGNMENTOPERATOR(R1,R2,|)
243  CHECKASSIGNMENTOPERATOR(R1,R2,^)
244  CHECKASSIGNMENTOPERATOR(R1,R2,&)
245  CHECKASSIGNMENTOPERATOR(R1,Zero,|)
246  CHECKASSIGNMENTOPERATOR(R1,Zero,^)
247  CHECKASSIGNMENTOPERATOR(R1,Zero,&)
248  CHECKASSIGNMENTOPERATOR(R1,Max,|)
249  CHECKASSIGNMENTOPERATOR(R1,Max,^)
250  CHECKASSIGNMENTOPERATOR(R1,Max,&)
251  CHECKASSIGNMENTOPERATOR(Zero,R1,|)
252  CHECKASSIGNMENTOPERATOR(Zero,R1,^)
253  CHECKASSIGNMENTOPERATOR(Zero,R1,&)
254  CHECKASSIGNMENTOPERATOR(Max,R1,|)
255  CHECKASSIGNMENTOPERATOR(Max,R1,^)
256  CHECKASSIGNMENTOPERATOR(Max,R1,&)
257 
258  uint64_t Tmp64 = 0xe1db685c9a0b47a2ULL;
259  TmpL = R1L; TmpL |= Tmp64; BOOST_CHECK(TmpL == (R1L | arith_uint256(Tmp64)));
260  TmpL = R1L; TmpL |= 0; BOOST_CHECK(TmpL == R1L);
261  TmpL ^= 0; BOOST_CHECK(TmpL == R1L);
262  TmpL ^= Tmp64; BOOST_CHECK(TmpL == (R1L ^ arith_uint256(Tmp64)));
263 }
264 
265 BOOST_AUTO_TEST_CASE( comparison ) // <= >= < >
266 {
267  arith_uint256 TmpL;
268  for (unsigned int i = 0; i < 256; ++i) {
269  TmpL= OneL<< i;
270  BOOST_CHECK( TmpL >= ZeroL && TmpL > ZeroL && ZeroL < TmpL && ZeroL <= TmpL);
271  BOOST_CHECK( TmpL >= 0 && TmpL > 0 && 0 < TmpL && 0 <= TmpL);
272  TmpL |= R1L;
273  BOOST_CHECK( TmpL >= R1L ); BOOST_CHECK( (TmpL == R1L) != (TmpL > R1L)); BOOST_CHECK( (TmpL == R1L) || !( TmpL <= R1L));
274  BOOST_CHECK( R1L <= TmpL ); BOOST_CHECK( (R1L == TmpL) != (R1L < TmpL)); BOOST_CHECK( (TmpL == R1L) || !( R1L >= TmpL));
275  BOOST_CHECK(! (TmpL < R1L)); BOOST_CHECK(! (R1L > TmpL));
276  }
277 
278  BOOST_CHECK_LT(ZeroL,
279  OneL);
280 }
281 
283 {
284  arith_uint256 TmpL = 0;
285  BOOST_CHECK_EQUAL(R1L + R2L, UintToArith256(uint256{"549fb09fea236a1ea3e31d4d58f1b1369288d204211ca751527cfc175767850c"}));
286  TmpL += R1L;
287  BOOST_CHECK(TmpL == R1L);
288  TmpL += R2L;
289  BOOST_CHECK(TmpL == R1L + R2L);
292  for (unsigned int i = 1; i < 256; ++i) {
293  BOOST_CHECK( (MaxL >> i) + OneL == (HalfL >> (i-1)) );
294  BOOST_CHECK( OneL + (MaxL >> i) == (HalfL >> (i-1)) );
295  TmpL = (MaxL>>i); TmpL += OneL;
296  BOOST_CHECK( TmpL == (HalfL >> (i-1)) );
297  TmpL = (MaxL>>i); TmpL += 1;
298  BOOST_CHECK( TmpL == (HalfL >> (i-1)) );
299  TmpL = (MaxL>>i);
300  BOOST_CHECK( TmpL++ == (MaxL>>i) );
301  BOOST_CHECK( TmpL == (HalfL >> (i-1)));
302  }
303  BOOST_CHECK(arith_uint256(0xbedc77e27940a7ULL) + 0xee8d836fce66fbULL == arith_uint256(0xbedc77e27940a7ULL + 0xee8d836fce66fbULL));
304  TmpL = arith_uint256(0xbedc77e27940a7ULL); TmpL += 0xee8d836fce66fbULL;
305  BOOST_CHECK(TmpL == arith_uint256(0xbedc77e27940a7ULL+0xee8d836fce66fbULL));
306  TmpL -= 0xee8d836fce66fbULL; BOOST_CHECK(TmpL == 0xbedc77e27940a7ULL);
307  TmpL = R1L;
308  BOOST_CHECK(++TmpL == R1L+1);
309 
310  BOOST_CHECK(R1L -(-R2L) == R1L+R2L);
311  BOOST_CHECK(R1L -(-OneL) == R1L+OneL);
312  BOOST_CHECK(R1L - OneL == R1L+(-OneL));
313  for (unsigned int i = 1; i < 256; ++i) {
314  BOOST_CHECK((MaxL>>i) - (-OneL) == (HalfL >> (i-1)));
315  BOOST_CHECK((HalfL >> (i-1)) - OneL == (MaxL>>i));
316  TmpL = (HalfL >> (i-1));
317  BOOST_CHECK(TmpL-- == (HalfL >> (i-1)));
318  BOOST_CHECK(TmpL == (MaxL >> i));
319  TmpL = (HalfL >> (i-1));
320  BOOST_CHECK(--TmpL == (MaxL >> i));
321  }
322  TmpL = R1L;
323  BOOST_CHECK(--TmpL == R1L-1);
324 }
325 
327 {
328  BOOST_CHECK((R1L * R1L).ToString() == "62a38c0486f01e45879d7910a7761bf30d5237e9873f9bff3642a732c4d84f10");
329  BOOST_CHECK((R1L * R2L).ToString() == "de37805e9986996cfba76ff6ba51c008df851987d9dd323f0e5de07760529c40");
330  BOOST_CHECK((R1L * ZeroL) == ZeroL);
331  BOOST_CHECK((R1L * OneL) == R1L);
332  BOOST_CHECK((R1L * MaxL) == -R1L);
333  BOOST_CHECK((R2L * R1L) == (R1L * R2L));
334  BOOST_CHECK((R2L * R2L).ToString() == "ac8c010096767d3cae5005dec28bb2b45a1d85ab7996ccd3e102a650f74ff100");
335  BOOST_CHECK((R2L * ZeroL) == ZeroL);
336  BOOST_CHECK((R2L * OneL) == R2L);
337  BOOST_CHECK((R2L * MaxL) == -R2L);
338 
339  BOOST_CHECK(MaxL * MaxL == OneL);
340 
341  BOOST_CHECK((R1L * 0) == 0);
342  BOOST_CHECK((R1L * 1) == R1L);
343  BOOST_CHECK((R1L * 3).ToString() == "7759b1c0ed14047f961ad09b20ff83687876a0181a367b813634046f91def7d4");
344  BOOST_CHECK((R2L * 0x87654321UL).ToString() == "23f7816e30c4ae2017257b7a0fa64d60402f5234d46e746b61c960d09a26d070");
345 }
346 
348 {
349  arith_uint256 D1L{UintToArith256(uint256{"00000000000000000000000000000000000000000000000ad7133ac1977fa2b7"})};
350  arith_uint256 D2L{UintToArith256(uint256{"0000000000000000000000000000000000000000000000000000000ecd751716"})};
351  BOOST_CHECK((R1L / D1L).ToString() == "00000000000000000b8ac01106981635d9ed112290f8895545a7654dde28fb3a");
352  BOOST_CHECK((R1L / D2L).ToString() == "000000000873ce8efec5b67150bad3aa8c5fcb70e947586153bf2cec7c37c57a");
353  BOOST_CHECK(R1L / OneL == R1L);
354  BOOST_CHECK(R1L / MaxL == ZeroL);
355  BOOST_CHECK(MaxL / R1L == 2);
357  BOOST_CHECK((R2L / D1L).ToString() == "000000000000000013e1665895a1cc981de6d93670105a6b3ec3b73141b3a3c5");
358  BOOST_CHECK((R2L / D2L).ToString() == "000000000e8f0abe753bb0afe2e9437ee85d280be60882cf0bd1aaf7fa3cc2c4");
359  BOOST_CHECK(R2L / OneL == R2L);
360  BOOST_CHECK(R2L / MaxL == ZeroL);
361  BOOST_CHECK(MaxL / R2L == 1);
363 }
364 
365 
366 static bool almostEqual(double d1, double d2)
367 {
368  return fabs(d1-d2) <= 4*fabs(d1)*std::numeric_limits<double>::epsilon();
369 }
370 
371 BOOST_AUTO_TEST_CASE(methods) // GetHex operator= size() GetLow64 GetSerializeSize, Serialize, Unserialize
372 {
377  arith_uint256 TmpL(R1L);
378  BOOST_CHECK(TmpL == R1L);
379  TmpL = R2L;
380  BOOST_CHECK(TmpL == R2L);
381  TmpL = ZeroL;
382  BOOST_CHECK(TmpL == 0);
383  TmpL = HalfL;
384  BOOST_CHECK(TmpL == HalfL);
385 
386  TmpL = R1L;
387  BOOST_CHECK(R1L.size() == 32);
388  BOOST_CHECK(R2L.size() == 32);
389  BOOST_CHECK(ZeroL.size() == 32);
390  BOOST_CHECK(MaxL.size() == 32);
392  BOOST_CHECK(HalfL.GetLow64() ==0x0000000000000000ULL);
393  BOOST_CHECK(OneL.GetLow64() ==0x0000000000000001ULL);
394 
395  for (unsigned int i = 0; i < 255; ++i)
396  {
397  BOOST_CHECK((OneL << i).getdouble() == ldexp(1.0,i));
398  }
399  BOOST_CHECK(ZeroL.getdouble() == 0.0);
400  for (int i = 256; i > 53; --i)
401  BOOST_CHECK(almostEqual((R1L>>(256-i)).getdouble(), ldexp(R1Ldouble,i)));
402  uint64_t R1L64part = (R1L>>192).GetLow64();
403  for (int i = 53; i > 0; --i) // doubles can store all integers in {0,...,2^54-1} exactly
404  {
405  BOOST_CHECK((R1L>>(256-i)).getdouble() == (double)(R1L64part >> (64-i)));
406  }
407 }
408 
409 BOOST_AUTO_TEST_CASE(bignum_SetCompact)
410 {
411  arith_uint256 num;
412  bool fNegative;
413  bool fOverflow;
414  num.SetCompact(0, &fNegative, &fOverflow);
415  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
416  BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
417  BOOST_CHECK_EQUAL(fNegative, false);
418  BOOST_CHECK_EQUAL(fOverflow, false);
419 
420  num.SetCompact(0x00123456, &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 
426  num.SetCompact(0x01003456, &fNegative, &fOverflow);
427  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
428  BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
429  BOOST_CHECK_EQUAL(fNegative, false);
430  BOOST_CHECK_EQUAL(fOverflow, false);
431 
432  num.SetCompact(0x02000056, &fNegative, &fOverflow);
433  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
434  BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
435  BOOST_CHECK_EQUAL(fNegative, false);
436  BOOST_CHECK_EQUAL(fOverflow, false);
437 
438  num.SetCompact(0x03000000, &fNegative, &fOverflow);
439  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
440  BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
441  BOOST_CHECK_EQUAL(fNegative, false);
442  BOOST_CHECK_EQUAL(fOverflow, false);
443 
444  num.SetCompact(0x04000000, &fNegative, &fOverflow);
445  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
446  BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
447  BOOST_CHECK_EQUAL(fNegative, false);
448  BOOST_CHECK_EQUAL(fOverflow, false);
449 
450  num.SetCompact(0x00923456, &fNegative, &fOverflow);
451  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
452  BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
453  BOOST_CHECK_EQUAL(fNegative, false);
454  BOOST_CHECK_EQUAL(fOverflow, false);
455 
456  num.SetCompact(0x01803456, &fNegative, &fOverflow);
457  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
458  BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
459  BOOST_CHECK_EQUAL(fNegative, false);
460  BOOST_CHECK_EQUAL(fOverflow, false);
461 
462  num.SetCompact(0x02800056, &fNegative, &fOverflow);
463  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
464  BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
465  BOOST_CHECK_EQUAL(fNegative, false);
466  BOOST_CHECK_EQUAL(fOverflow, false);
467 
468  num.SetCompact(0x03800000, &fNegative, &fOverflow);
469  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
470  BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
471  BOOST_CHECK_EQUAL(fNegative, false);
472  BOOST_CHECK_EQUAL(fOverflow, false);
473 
474  num.SetCompact(0x04800000, &fNegative, &fOverflow);
475  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000000");
476  BOOST_CHECK_EQUAL(num.GetCompact(), 0U);
477  BOOST_CHECK_EQUAL(fNegative, false);
478  BOOST_CHECK_EQUAL(fOverflow, false);
479 
480  num.SetCompact(0x01123456, &fNegative, &fOverflow);
481  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000000012");
482  BOOST_CHECK_EQUAL(num.GetCompact(), 0x01120000U);
483  BOOST_CHECK_EQUAL(fNegative, false);
484  BOOST_CHECK_EQUAL(fOverflow, false);
485 
486  // Make sure that we don't generate compacts with the 0x00800000 bit set
487  num = 0x80;
488  BOOST_CHECK_EQUAL(num.GetCompact(), 0x02008000U);
489 
490  num.SetCompact(0x01fedcba, &fNegative, &fOverflow);
491  BOOST_CHECK_EQUAL(num.GetHex(), "000000000000000000000000000000000000000000000000000000000000007e");
492  BOOST_CHECK_EQUAL(num.GetCompact(true), 0x01fe0000U);
493  BOOST_CHECK_EQUAL(fNegative, true);
494  BOOST_CHECK_EQUAL(fOverflow, false);
495 
496  num.SetCompact(0x02123456, &fNegative, &fOverflow);
497  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000001234");
498  BOOST_CHECK_EQUAL(num.GetCompact(), 0x02123400U);
499  BOOST_CHECK_EQUAL(fNegative, false);
500  BOOST_CHECK_EQUAL(fOverflow, false);
501 
502  num.SetCompact(0x03123456, &fNegative, &fOverflow);
503  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000000123456");
504  BOOST_CHECK_EQUAL(num.GetCompact(), 0x03123456U);
505  BOOST_CHECK_EQUAL(fNegative, false);
506  BOOST_CHECK_EQUAL(fOverflow, false);
507 
508  num.SetCompact(0x04123456, &fNegative, &fOverflow);
509  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000012345600");
510  BOOST_CHECK_EQUAL(num.GetCompact(), 0x04123456U);
511  BOOST_CHECK_EQUAL(fNegative, false);
512  BOOST_CHECK_EQUAL(fOverflow, false);
513 
514  num.SetCompact(0x04923456, &fNegative, &fOverflow);
515  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000012345600");
516  BOOST_CHECK_EQUAL(num.GetCompact(true), 0x04923456U);
517  BOOST_CHECK_EQUAL(fNegative, true);
518  BOOST_CHECK_EQUAL(fOverflow, false);
519 
520  num.SetCompact(0x05009234, &fNegative, &fOverflow);
521  BOOST_CHECK_EQUAL(num.GetHex(), "0000000000000000000000000000000000000000000000000000000092340000");
522  BOOST_CHECK_EQUAL(num.GetCompact(), 0x05009234U);
523  BOOST_CHECK_EQUAL(fNegative, false);
524  BOOST_CHECK_EQUAL(fOverflow, false);
525 
526  num.SetCompact(0x20123456, &fNegative, &fOverflow);
527  BOOST_CHECK_EQUAL(num.GetHex(), "1234560000000000000000000000000000000000000000000000000000000000");
528  BOOST_CHECK_EQUAL(num.GetCompact(), 0x20123456U);
529  BOOST_CHECK_EQUAL(fNegative, false);
530  BOOST_CHECK_EQUAL(fOverflow, false);
531 
532  num.SetCompact(0xff123456, &fNegative, &fOverflow);
533  BOOST_CHECK_EQUAL(fNegative, false);
534  BOOST_CHECK_EQUAL(fOverflow, true);
535 }
536 
537 
538 BOOST_AUTO_TEST_CASE( getmaxcoverage ) // some more tests just to get 100% coverage
539 {
540  // ~R1L give a base_uint<256>
541  BOOST_CHECK((~~R1L >> 10) == (R1L >> 10));
542  BOOST_CHECK((~~R1L << 10) == (R1L << 10));
543  BOOST_CHECK(!(~~R1L < R1L));
544  BOOST_CHECK(~~R1L <= R1L);
545  BOOST_CHECK(!(~~R1L > R1L));
546  BOOST_CHECK(~~R1L >= R1L);
547  BOOST_CHECK(!(R1L < ~~R1L));
548  BOOST_CHECK(R1L <= ~~R1L);
549  BOOST_CHECK(!(R1L > ~~R1L));
550  BOOST_CHECK(R1L >= ~~R1L);
551 
552  BOOST_CHECK(~~R1L + R2L == R1L + ~~R2L);
553  BOOST_CHECK(~~R1L - R2L == R1L - ~~R2L);
554  BOOST_CHECK(~R1L != R1L); BOOST_CHECK(R1L != ~R1L);
555  unsigned char TmpArray[32];
556  CHECKBITWISEOPERATOR(~R1,R2,|)
557  CHECKBITWISEOPERATOR(~R1,R2,^)
558  CHECKBITWISEOPERATOR(~R1,R2,&)
559  CHECKBITWISEOPERATOR(R1,~R2,|)
560  CHECKBITWISEOPERATOR(R1,~R2,^)
561  CHECKBITWISEOPERATOR(R1,~R2,&)
562 }
563 
565 {
566  for (const arith_uint256& arith : {ZeroL, OneL, R1L, R2L}) {
567  const auto u256{uint256::FromHex(arith.GetHex()).value()};
569  BOOST_CHECK_EQUAL(UintToArith256(u256), arith);
570  BOOST_CHECK_EQUAL(u256, ArithToUint256(arith));
571  BOOST_CHECK_EQUAL(ArithToUint256(arith).GetHex(), UintToArith256(u256).GetHex());
572  }
573 
574  for (uint8_t num : {0, 1, 0xff}) {
578  }
579 }
580 
581 BOOST_AUTO_TEST_CASE(operator_with_self)
582 {
583  /* Clang 16 and earlier detects v -= v and v /= v as self-assignments
584  to 0 and 1 respectively.
585  See: https://github.com/llvm/llvm-project/issues/42469
586  and the fix in commit c5302325b2a62d77cf13dd16cd5c19141862fed0 .
587 
588  This makes some sense for arithmetic classes, but could be considered a bug
589  elsewhere. Disable the warning here so that the code can be tested, but the
590  warning should remain on as there will likely always be a better way to
591  express this.
592  */
593 #if defined(__clang__)
594 #pragma clang diagnostic push
595 #pragma clang diagnostic ignored "-Wself-assign-overloaded"
596 #endif
597  arith_uint256 v{2};
598  v *= v;
600  v /= v;
602  v += v;
604  v -= v;
606 #if defined(__clang__)
607 #pragma clang diagnostic pop
608 #endif
609 }
610 
std::string ToString() const
#define BOOST_CHECK_THROW(stmt, excMatch)
Definition: object.cpp:19
BOOST_AUTO_TEST_CASE(basics)
#define CHECKBITWISEOPERATOR(_A_, _B_, _OP_)
const char R1ArrayHex[]
const arith_uint256 R1L
static void shiftArrayLeft(unsigned char *to, const unsigned char *from, unsigned int arrayLength, unsigned int bitsToShift)
uint32_t GetCompact(bool fNegative=false) const
const arith_uint256 ZeroL
arith_uint256 UintToArith256(const uint256 &a)
const uint64_t R1LLow64
const arith_uint256 HalfL
const unsigned char R2Array[]
const unsigned char OneArray[]
BOOST_AUTO_TEST_SUITE_END()
static std::optional< uint256 > FromHex(std::string_view str)
Definition: uint256.h:203
const unsigned char R1Array[]
const arith_uint256 MaxL
#define CHECKASSIGNMENTOPERATOR(_A_, _B_, _OP_)
static arith_uint256 arith_uint256V(const std::vector< unsigned char > &vch)
Convert vector to arith_uint256, via uint256 blob.
256-bit unsigned big integer.
const unsigned char MaxArray[]
const arith_uint256 R2L
const double R1Ldouble
256-bit opaque blob.
Definition: uint256.h:201
uint256 ArithToUint256(const arith_uint256 &a)
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
uint64_t GetLow64() const
const unsigned char ZeroArray[]
arith_uint256 & SetCompact(uint32_t nCompact, bool *pfNegative=nullptr, bool *pfOverflow=nullptr)
The "compact" format is a representation of a whole number N using an unsigned 32bit number similar t...
const arith_uint256 OneL
std::string GetHex() const
Hex encoding of the number (with the most significant digits first).
static bool almostEqual(double d1, double d2)
static void shiftArrayRight(unsigned char *to, const unsigned char *from, unsigned int arrayLength, unsigned int bitsToShift)
static std::string ArrayToString(const unsigned char A[], unsigned int width)
unsigned int size() const
double getdouble() const
#define T(expected, seed, data)
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:233
#define BOOST_CHECK(expr)
Definition: object.cpp:17