9#include <boost/test/unit_test.hpp>
19BOOST_AUTO_TEST_SUITE(arith_uint256_tests)
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";
29const char R1ArrayHex[] =
"7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c";
32const uint64_t
R1LLow64 = 0x121156cfdb4a529cULL;
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";
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";
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";
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";
55static std::string
ArrayToString(
const unsigned char A[],
unsigned int width)
57 std::stringstream Stream;
59 for (
unsigned int i = 0; i < width; ++i)
61 Stream<<std::setw(2)<<std::setfill(
'0')<<(
unsigned int)A[width-i-1];
85 uint64_t Tmp64 = 0xc4dab720d9c7acaaULL;
86 for (
unsigned int i = 0; i < 256; ++i)
123static void shiftArrayRight(
unsigned char* to,
const unsigned char* from,
unsigned int arrayLength,
unsigned int bitsToShift)
125 for (
unsigned int T=0;
T < arrayLength; ++
T)
127 unsigned int F = (
T+bitsToShift/8);
129 to[
T] = uint8_t(from[F] >> (bitsToShift % 8));
132 if (F + 1 < arrayLength)
133 to[
T] |= uint8_t(from[(F + 1)] << (8 - bitsToShift % 8));
137static void shiftArrayLeft(
unsigned char* to,
const unsigned char* from,
unsigned int arrayLength,
unsigned int bitsToShift)
139 for (
unsigned int T=0;
T < arrayLength; ++
T)
141 if (
T >= bitsToShift/8)
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));
155 unsigned char TmpArray[32];
157 for (
unsigned int i = 0; i < 256; ++i)
161 TmpL =
OneL; TmpL <<= i;
164 TmpL =
HalfL; TmpL >>= (255-i);
169 TmpL =
R1L; TmpL <<= i;
174 TmpL =
R1L; TmpL >>= i;
179 TmpL =
MaxL; TmpL <<= i;
184 TmpL =
MaxL; TmpL >>= i;
189 for (
unsigned int i = 0; i < 128; ++i) {
192 for (
unsigned int i = 128; i < 256; ++i) {
201 unsigned char TmpArray[32];
202 for (
unsigned int i = 0; i < 32; ++i) { TmpArray[i] = uint8_t(~
R1Array[i]); }
207 for (
unsigned int i = 0; i < 256; ++i)
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));
218#define CHECKASSIGNMENTOPERATOR(_A_,_B_,_OP_) \
219 TmpL = _A_##L; TmpL _OP_##= _B_##L; BOOST_CHECK(TmpL == (_A_##L _OP_ _B_##L));
223 unsigned char TmpArray[32];
258 uint64_t Tmp64 = 0xe1db685c9a0b47a2ULL;
268 for (
unsigned int i = 0; i < 256; ++i) {
271 BOOST_CHECK( TmpL >= 0 && TmpL > 0 && 0 < TmpL && 0 <= TmpL);
278 BOOST_CHECK_LT(
ZeroL,
292 for (
unsigned int i = 1; i < 256; ++i) {
297 TmpL = (
MaxL>>i); TmpL += 1;
304 TmpL =
arith_uint256(0xbedc77e27940a7ULL); TmpL += 0xee8d836fce66fbULL;
306 TmpL -= 0xee8d836fce66fbULL;
BOOST_CHECK(TmpL == 0xbedc77e27940a7ULL);
313 for (
unsigned int i = 1; i < 256; ++i) {
316 TmpL = (
HalfL >> (i-1));
319 TmpL = (
HalfL >> (i-1));
344 BOOST_CHECK((
R2L * 0x87654321UL).
ToString() ==
"23f7816e30c4ae2017257b7a0fa64d60402f5234d46e746b61c960d09a26d070");
351 BOOST_CHECK((
R1L / D1L).
ToString() ==
"00000000000000000b8ac01106981635d9ed112290f8895545a7654dde28fb3a");
352 BOOST_CHECK((
R1L / D2L).
ToString() ==
"000000000873ce8efec5b67150bad3aa8c5fcb70e947586153bf2cec7c37c57a");
357 BOOST_CHECK((
R2L / D1L).
ToString() ==
"000000000000000013e1665895a1cc981de6d93670105a6b3ec3b73141b3a3c5");
358 BOOST_CHECK((
R2L / D2L).
ToString() ==
"000000000e8f0abe753bb0afe2e9437ee85d280be60882cf0bd1aaf7fa3cc2c4");
368 return fabs(d1-d2) <= 4*fabs(d1)*std::numeric_limits<double>::epsilon();
395 for (
unsigned int i = 0; i < 255; ++i)
400 for (
int i = 256; i > 53; --i)
402 uint64_t R1L64part = (
R1L>>192).GetLow64();
403 for (
int i = 53; i > 0; --i)
405 BOOST_CHECK((
R1L>>(256-i)).getdouble() == (
double)(R1L64part >> (64-i)));
420 num.
SetCompact(0x00123456, &fNegative, &fOverflow);
426 num.
SetCompact(0x01003456, &fNegative, &fOverflow);
432 num.
SetCompact(0x02000056, &fNegative, &fOverflow);
438 num.
SetCompact(0x03000000, &fNegative, &fOverflow);
444 num.
SetCompact(0x04000000, &fNegative, &fOverflow);
450 num.
SetCompact(0x00923456, &fNegative, &fOverflow);
456 num.
SetCompact(0x01803456, &fNegative, &fOverflow);
462 num.
SetCompact(0x02800056, &fNegative, &fOverflow);
468 num.
SetCompact(0x03800000, &fNegative, &fOverflow);
474 num.
SetCompact(0x04800000, &fNegative, &fOverflow);
480 num.
SetCompact(0x01123456, &fNegative, &fOverflow);
490 num.
SetCompact(0x01fedcba, &fNegative, &fOverflow);
496 num.
SetCompact(0x02123456, &fNegative, &fOverflow);
502 num.
SetCompact(0x03123456, &fNegative, &fOverflow);
508 num.
SetCompact(0x04123456, &fNegative, &fOverflow);
514 num.
SetCompact(0x04923456, &fNegative, &fOverflow);
520 num.
SetCompact(0x05009234, &fNegative, &fOverflow);
526 num.
SetCompact(0x20123456, &fNegative, &fOverflow);
532 num.
SetCompact(0xff123456, &fNegative, &fOverflow);
555 unsigned char TmpArray[32];
574 for (uint8_t num : {0, 1, 0xff}) {
593#if defined(__clang__)
594#pragma clang diagnostic push
595#pragma clang diagnostic ignored "-Wself-assign-overloaded"
606#if defined(__clang__)
607#pragma clang diagnostic pop
arith_uint256 UintToArith256(const uint256 &a)
uint256 ArithToUint256(const arith_uint256 &a)
static void shiftArrayLeft(unsigned char *to, const unsigned char *from, unsigned int arrayLength, unsigned int bitsToShift)
static void shiftArrayRight(unsigned char *to, const unsigned char *from, unsigned int arrayLength, unsigned int bitsToShift)
#define CHECKBITWISEOPERATOR(_A_, _B_, _OP_)
#define CHECKASSIGNMENTOPERATOR(_A_, _B_, _OP_)
const unsigned char ZeroArray[]
const unsigned char R1Array[]
BOOST_AUTO_TEST_CASE(basics)
const arith_uint256 HalfL
const unsigned char OneArray[]
static bool almostEqual(double d1, double d2)
const unsigned char R2Array[]
static std::string ArrayToString(const unsigned char A[], unsigned int width)
static arith_uint256 arith_uint256V(const std::vector< unsigned char > &vch)
Convert vector to arith_uint256, via uint256 blob.
const unsigned char MaxArray[]
const arith_uint256 ZeroL
256-bit unsigned big integer.
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...
uint32_t GetCompact(bool fNegative=false) const
unsigned int size() const
std::string ToString() const
uint64_t GetLow64() const
std::string GetHex() const
Hex encoding of the number (with the most significant digits first).
static std::optional< uint256 > FromHex(std::string_view str)
BOOST_AUTO_TEST_SUITE_END()
#define T(expected, seed, data)
std::string ToString(const T &t)
Locale-independent version of std::to_string.
#define BOOST_CHECK_THROW(stmt, excMatch)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)