6 #include <boost/test/unit_test.hpp>
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";
34 const uint64_t
R1LLow64 = 0x121156cfdb4a529cULL;
37 "\x70\x32\x1d\x7c\x47\xa5\x6b\x40\x26\x7e\x0a\xc3\xa6\x9c\xb6\xbf"
38 "\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7";
42 const char R1LplusR2L[] =
"549FB09FEA236A1EA3E31D4D58F1B1369288D204211CA751527CFC175767850C";
45 "\x00\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";
51 "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
52 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
57 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
58 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
66 std::stringstream Stream;
68 for (
unsigned int i = 0; i < width; ++i)
70 Stream<<std::setw(2)<<std::setfill(
'0')<<(
unsigned int)A[width-i-1];
101 uint64_t Tmp64 = 0xc4dab720d9c7acaaULL;
102 for (
unsigned int i = 0; i < 256; ++i)
111 for (
unsigned int i = 0; i < 160; ++i)
179 void shiftArrayRight(
unsigned char* to,
const unsigned char* from,
unsigned int arrayLength,
unsigned int bitsToShift)
181 for (
unsigned int T=0;
T < arrayLength; ++
T)
183 unsigned int F = (
T+bitsToShift/8);
185 to[
T] = from[F] >> (bitsToShift%8);
188 if (F + 1 < arrayLength)
189 to[
T] |= from[(F+1)] << (8-bitsToShift%8);
193 void shiftArrayLeft(
unsigned char* to,
const unsigned char* from,
unsigned int arrayLength,
unsigned int bitsToShift)
195 for (
unsigned int T=0;
T < arrayLength; ++
T)
197 if (
T >= bitsToShift/8)
199 unsigned int F =
T-bitsToShift/8;
200 to[
T] = from[F] << (bitsToShift%8);
201 if (
T >= bitsToShift/8+1)
202 to[
T] |= from[F-1] >> (8-bitsToShift%8);
211 unsigned char TmpArray[32];
213 for (
unsigned int i = 0; i < 256; ++i)
217 TmpL =
OneL; TmpL <<= i;
220 TmpL =
HalfL; TmpL >>= (255-i);
225 TmpL =
R1L; TmpL <<= i;
230 TmpL =
R1L; TmpL >>= i;
235 TmpL =
MaxL; TmpL <<= i;
240 TmpL =
MaxL; TmpL >>= i;
245 for (
unsigned int i = 0; i < 128; ++i) {
248 for (
unsigned int i = 128; i < 256; ++i) {
253 for (
unsigned int i = 0; i < 160; ++i)
257 TmpS =
OneS; TmpS <<= i;
260 TmpS =
HalfS; TmpS >>= (159-i);
265 TmpS =
R1S; TmpS <<= i;
270 TmpS =
R1S; TmpS >>= i;
275 TmpS =
MaxS; TmpS <<= i;
280 TmpS =
MaxS; TmpS >>= i;
285 for (
unsigned int i = 0; i < 80; ++i) {
288 for (
unsigned int i = 80; i < 160; ++i) {
297 for (
unsigned int i = 0; i < 256; ++i)
299 for (
unsigned int i = 0; i < 160; ++i)
307 unsigned char TmpArray[32];
308 for (
unsigned int i = 0; i < 32; ++i) { TmpArray[i] = ~
R1Array[i]; }
315 for (
unsigned int i = 0; i < 256; ++i)
317 for (
unsigned int i = 0; i < 160; ++i)
324 #define CHECKBITWISEOPERATOR(_A_,_B_,_OP_) \
325 for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = _A_##Array[i] _OP_ _B_##Array[i]; } \
326 BOOST_CHECK(arith_uint256(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (_A_##L _OP_ _B_##L)); \
327 for (unsigned int i = 0; i < 20; ++i) { TmpArray[i] = _A_##Array[i] _OP_ _B_##Array[i]; } \
328 BOOST_CHECK(arith_uint160(std::vector<unsigned char>(TmpArray,TmpArray+20)) == (_A_##S _OP_ _B_##S));
330 #define CHECKASSIGNMENTOPERATOR(_A_,_B_,_OP_) \
331 TmpL = _A_##L; TmpL _OP_##= _B_##L; BOOST_CHECK(TmpL == (_A_##L _OP_ _B_##L)); \
332 TmpS = _A_##S; TmpS _OP_##= _B_##S; BOOST_CHECK(TmpS == (_A_##S _OP_ _B_##S));
336 unsigned char TmpArray[32];
372 uint64_t Tmp64 = 0xe1db685c9a0b47a2ULL;
386 for (
unsigned int i = 0; i < 256; ++i) {
389 BOOST_CHECK( TmpL >= 0 && TmpL > 0 && 0 < TmpL && 0 <= TmpL);
396 for (
unsigned int i = 0; i < 160; ++i) {
399 BOOST_CHECK( TmpS >= 0 && TmpS > 0 && 0 < TmpS && 0 <= TmpS);
417 for (
unsigned int i = 1; i < 256; ++i) {
422 TmpL = (
MaxL>>i); TmpL += 1;
429 TmpL =
arith_uint256(0xbedc77e27940a7ULL); TmpL += 0xee8d836fce66fbULL;
431 TmpL -= 0xee8d836fce66fbULL;
BOOST_CHECK(TmpL == 0xbedc77e27940a7ULL);
438 for (
unsigned int i = 1; i < 256; ++i) {
441 TmpL = (
HalfL >> (i-1));
444 TmpL = (
HalfL >> (i-1));
459 for (
unsigned int i = 1; i < 160; ++i) {
464 TmpS = (
MaxS>>i); TmpS += 1;
471 TmpS =
arith_uint160(0xbedc77e27940a7ULL); TmpS += 0xee8d836fce66fbULL;
473 TmpS -= 0xee8d836fce66fbULL;
BOOST_CHECK(TmpS == 0xbedc77e27940a7ULL);
480 for (
unsigned int i = 1; i < 160; ++i) {
483 TmpS = (
HalfS >> (i-1));
486 TmpS = (
HalfS >> (i-1));
496 BOOST_CHECK((
R1L *
R1L).ToString() ==
"62a38c0486f01e45879d7910a7761bf30d5237e9873f9bff3642a732c4d84f10");
497 BOOST_CHECK((
R1L *
R2L).ToString() ==
"de37805e9986996cfba76ff6ba51c008df851987d9dd323f0e5de07760529c40");
502 BOOST_CHECK((
R2L *
R2L).ToString() ==
"ac8c010096767d3cae5005dec28bb2b45a1d85ab7996ccd3e102a650f74ff100");
507 BOOST_CHECK((
R1S *
R1S).ToString() ==
"a7761bf30d5237e9873f9bff3642a732c4d84f10");
508 BOOST_CHECK((
R1S *
R2S).ToString() ==
"ba51c008df851987d9dd323f0e5de07760529c40");
513 BOOST_CHECK((
R2S *
R2S).ToString() ==
"c28bb2b45a1d85ab7996ccd3e102a650f74ff100");
523 BOOST_CHECK((
R1L * 3).ToString() ==
"7759b1c0ed14047f961ad09b20ff83687876a0181a367b813634046f91def7d4");
524 BOOST_CHECK((
R2L * 0x87654321UL).ToString() ==
"23f7816e30c4ae2017257b7a0fa64d60402f5234d46e746b61c960d09a26d070");
527 BOOST_CHECK((
R1S * 7).ToString() ==
"f7a987f3c3bf758d927f202d7e795faeff084244");
528 BOOST_CHECK((
R2S * 0xFFFFFFFFUL).ToString() ==
"1c6f6c930353e17f7d6127213bb18d2883e2cd90");
535 BOOST_CHECK((
R1L / D1L).ToString() ==
"00000000000000000b8ac01106981635d9ed112290f8895545a7654dde28fb3a");
536 BOOST_CHECK((
R1L / D2L).ToString() ==
"000000000873ce8efec5b67150bad3aa8c5fcb70e947586153bf2cec7c37c57a");
541 BOOST_CHECK((
R2L / D1L).ToString() ==
"000000000000000013e1665895a1cc981de6d93670105a6b3ec3b73141b3a3c5");
542 BOOST_CHECK((
R2L / D2L).ToString() ==
"000000000e8f0abe753bb0afe2e9437ee85d280be60882cf0bd1aaf7fa3cc2c4");
550 BOOST_CHECK((
R1S / D1S).ToString() ==
"0000000000000000000000000db9af3beade6c02");
551 BOOST_CHECK((
R1S / D2S).ToString() ==
"000098dfb6cc40ca592bf74366794f298ada205c");
556 BOOST_CHECK((
R2S / D1S).ToString() ==
"0000000000000000000000000c5608e781182047");
557 BOOST_CHECK((
R2S / D2S).ToString() ==
"00008966751b7187c3c67c1fda5cea7db2c1c069");
567 return fabs(d1-d2) <= 4*fabs(d1)*std::numeric_limits<double>::epsilon();
667 for (
unsigned int i = 0; i < 255; ++i)
674 for (
int i = 256; i > 53; --i)
676 for (
int i = 160; i > 53; --i)
678 uint64_t R1L64part = (
R1L>>192).GetLow64();
679 uint64_t R1S64part = (
R1S>>96).GetLow64();
680 for (
int i = 53; i > 0; --i)
682 BOOST_CHECK((
R1L>>(256-i)).getdouble() == (
double)(R1L64part >> (64-i)));
683 BOOST_CHECK((
R1S>>(160-i)).getdouble() == (
double)(R1S64part >> (64-i)));
698 num.
SetCompact(0x00123456, &fNegative, &fOverflow);
704 num.
SetCompact(0x01003456, &fNegative, &fOverflow);
710 num.
SetCompact(0x02000056, &fNegative, &fOverflow);
716 num.
SetCompact(0x03000000, &fNegative, &fOverflow);
722 num.
SetCompact(0x04000000, &fNegative, &fOverflow);
728 num.
SetCompact(0x00923456, &fNegative, &fOverflow);
734 num.
SetCompact(0x01803456, &fNegative, &fOverflow);
740 num.
SetCompact(0x02800056, &fNegative, &fOverflow);
746 num.
SetCompact(0x03800000, &fNegative, &fOverflow);
752 num.
SetCompact(0x04800000, &fNegative, &fOverflow);
758 num.
SetCompact(0x01123456, &fNegative, &fOverflow);
768 num.
SetCompact(0x01fedcba, &fNegative, &fOverflow);
774 num.
SetCompact(0x02123456, &fNegative, &fOverflow);
780 num.
SetCompact(0x03123456, &fNegative, &fOverflow);
786 num.
SetCompact(0x04123456, &fNegative, &fOverflow);
792 num.
SetCompact(0x04923456, &fNegative, &fOverflow);
798 num.
SetCompact(0x05009234, &fNegative, &fOverflow);
804 num.
SetCompact(0x20123456, &fNegative, &fOverflow);
810 num.
SetCompact(0xff123456, &fNegative, &fOverflow);
836 unsigned char TmpArray[32];
arith_uint256 UintToArith256(const uint256 &a)
std::string ArrayToString(const unsigned char A[], unsigned int width)
bool almostEqual(double d1, double d2)
#define CHECKBITWISEOPERATOR(_A_, _B_, _OP_)
#define CHECKASSIGNMENTOPERATOR(_A_, _B_, _OP_)
const arith_uint160 ZeroS
const unsigned char ZeroArray[]
const unsigned char R1Array[]
BOOST_AUTO_TEST_CASE(basics)
const arith_uint256 HalfL
const unsigned char OneArray[]
void shiftArrayRight(unsigned char *to, const unsigned char *from, unsigned int arrayLength, unsigned int bitsToShift)
arith_uint256 arith_uint256V(const std::vector< unsigned char > &vch)
Convert vector to arith_uint256, via uint256 blob.
const unsigned char R2Array[]
const unsigned char MaxArray[]
void shiftArrayLeft(unsigned char *to, const unsigned char *from, unsigned int arrayLength, unsigned int bitsToShift)
const arith_uint256 ZeroL
const arith_uint160 HalfS
160-bit unsigned big integer.
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
void SetHex(const char *psz)
std::string GetHex() const
BOOST_AUTO_TEST_SUITE_END()
#define T(expected, seed, data)
#define BOOST_CHECK_THROW(stmt, excMatch)
#define BOOST_FIXTURE_TEST_SUITE(a, b)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
unsigned int GetSerializeSize(const std::array< T, N > &item)
array