Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
arith_uint256_tests.cpp File Reference
#include <arith_uint256.h>
#include <test/util/setup_common.h>
#include <uint256.h>
#include <boost/test/unit_test.hpp>
#include <cmath>
#include <cstdint>
#include <iomanip>
#include <limits>
#include <sstream>
#include <string>
#include <vector>
Include dependency graph for arith_uint256_tests.cpp:

Go to the source code of this file.

Macros

#define CHECKBITWISEOPERATOR(_A_, _B_, _OP_)
 
#define CHECKASSIGNMENTOPERATOR(_A_, _B_, _OP_)
 

Functions

static arith_uint256 arith_uint256V (const std::vector< unsigned char > &vch)
 Convert vector to arith_uint256, via uint256 blob.
 
static arith_uint256 arith_uint256S (std::string_view str)
 
static std::string ArrayToString (const unsigned char A[], unsigned int width)
 
 BOOST_CHECK (R1L.ToString()==ArrayToString(R1Array, 32))
 
 BOOST_CHECK (R2L.ToString()==ArrayToString(R2Array, 32))
 
 BOOST_CHECK (ZeroL.ToString()==ArrayToString(ZeroArray, 32))
 
 BOOST_CHECK (OneL.ToString()==ArrayToString(OneArray, 32))
 
 BOOST_CHECK (MaxL.ToString()==ArrayToString(MaxArray, 32))
 
 BOOST_CHECK (OneL.ToString() !=ArrayToString(ZeroArray, 32))
 
 BOOST_CHECK (R1L !=R2L)
 
 BOOST_CHECK (ZeroL !=OneL)
 
 BOOST_CHECK (OneL !=ZeroL)
 
 BOOST_CHECK (MaxL !=ZeroL)
 
 BOOST_CHECK (~MaxL==ZeroL)
 
 BOOST_CHECK (((R1L ^ R2L) ^ R1L)==R2L)
 
 for (unsigned int i=0;i< 256;++i) == (MaxL << i))
 
 BOOST_CHECK (ZeroL==(OneL<< 256))
 
 BOOST_CHECK (arith_uint256S("0x"+R1L.ToString())==R1L)
 
 BOOST_CHECK (arith_uint256S("0x"+R2L.ToString())==R2L)
 
 BOOST_CHECK (arith_uint256S("0x"+ZeroL.ToString())==ZeroL)
 
 BOOST_CHECK (arith_uint256S("0x"+OneL.ToString())==OneL)
 
 BOOST_CHECK (arith_uint256S("0x"+MaxL.ToString())==MaxL)
 
 BOOST_CHECK (arith_uint256S(R1L.ToString())==R1L)
 
 BOOST_CHECK (arith_uint256S(" 0x"+R1L.ToString()+" ")==R1L)
 
 BOOST_CHECK (arith_uint256S("")==ZeroL)
 
 BOOST_CHECK (arith_uint256S("1")==OneL)
 
 BOOST_CHECK (R1L==arith_uint256S(R1ArrayHex))
 
 BOOST_CHECK (arith_uint256(R1L)==R1L)
 
 BOOST_CHECK ((arith_uint256(R1L^R2L)^R2L)==R1L)
 
 BOOST_CHECK (arith_uint256(ZeroL)==ZeroL)
 
 BOOST_CHECK (arith_uint256(OneL)==OneL)
 
 BOOST_CHECK ((R1L &arith_uint256S("0xffffffffffffffff"))==arith_uint256(R1LLow64))
 
 BOOST_CHECK (OneL==arith_uint256(1))
 
 BOOST_CHECK (arith_uint256S("0xffffffffffffffff")==arith_uint256(0xffffffffffffffffULL))
 
 BOOST_CHECK (tmpL==~ZeroL)
 
static void shiftArrayRight (unsigned char *to, const unsigned char *from, unsigned int arrayLength, unsigned int bitsToShift)
 
static void shiftArrayLeft (unsigned char *to, const unsigned char *from, unsigned int arrayLength, unsigned int bitsToShift)
 
 BOOST_AUTO_TEST_CASE (shifts)
 
 BOOST_CHECK (arith_uint256V(std::vector< unsigned char >(TmpArray, TmpArray+32))==(~R1L))
 
 BOOST_CHECK (-ZeroL==ZeroL)
 
 BOOST_AUTO_TEST_CASE (bitwiseOperators)
 
 BOOST_CHECK_LT (ZeroL, OneL)
 
 BOOST_CHECK_LT (arith_uint256S("0000000000000000000000000000000000000000000000000000000000000001"), arith_uint256S("1000000000000000000000000000000000000000000000000000000000000000"))
 
 BOOST_AUTO_TEST_CASE (plusMinus)
 
 BOOST_AUTO_TEST_CASE (multiply)
 
 BOOST_AUTO_TEST_CASE (divide)
 
static bool almostEqual (double d1, double d2)
 
 BOOST_CHECK (TmpL==R1L)
 
 BOOST_CHECK (HalfL.GetLow64()==0x0000000000000000ULL)
 
 for (int i=256;i > 53;--i) BOOST_CHECK(almostEqual((R1L > >(256-i)).getdouble()
 
 ldexp (R1Ldouble, i)))
 
 for (int i=53;i > 0;--i)
 
 BOOST_AUTO_TEST_CASE (bignum_SetCompact)
 
 BOOST_CHECK ((~~R1L<< 10)==(R1L<< 10))
 
 BOOST_CHECK (!(~~R1L< R1L))
 
 BOOST_CHECK (~~R1L<=R1L)
 
 BOOST_CHECK (!(~~R1L > R1L))
 
 BOOST_CHECK (~~R1L >=R1L)
 
 BOOST_CHECK (!(R1L< ~~R1L))
 
 BOOST_CHECK (R1L<=~~R1L)
 
 BOOST_CHECK (!(R1L > ~~R1L))
 
 BOOST_CHECK (R1L >=~~R1L)
 
 BOOST_CHECK (~~R1L+R2L==R1L+~~R2L)
 
 BOOST_CHECK (~~R1L - R2L==R1L - ~~R2L)
 
 BOOST_CHECK (~R1L !=R1L)
 

Variables

const unsigned char R1Array []
 
const char R1ArrayHex [] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c"
 
const double R1Ldouble = 0.4887374590559308955
 
const arith_uint256 R1L = arith_uint256V(std::vector<unsigned char>(R1Array,R1Array+32))
 
const uint64_t R1LLow64 = 0x121156cfdb4a529cULL
 
const unsigned char R2Array []
 
const arith_uint256 R2L = arith_uint256V(std::vector<unsigned char>(R2Array,R2Array+32))
 
const char R1LplusR2L [] = "549FB09FEA236A1EA3E31D4D58F1B1369288D204211CA751527CFC175767850C"
 
const unsigned char ZeroArray []
 
const arith_uint256 ZeroL = arith_uint256V(std::vector<unsigned char>(ZeroArray,ZeroArray+32))
 
const unsigned char OneArray []
 
const arith_uint256 OneL = arith_uint256V(std::vector<unsigned char>(OneArray,OneArray+32))
 
const unsigned char MaxArray []
 
const arith_uint256 MaxL = arith_uint256V(std::vector<unsigned char>(MaxArray,MaxArray+32))
 
const arith_uint256 HalfL = (OneL << 255)
 
uint64_t Tmp64 = 0xc4dab720d9c7acaaULL
 
arith_uint256 tmpL = ~ZeroL
 
unsigned char TmpArray [32]
 
arith_uint256 TmpL (R1L) = R2L
 
uint64_t R1L64part = (R1L>>192).GetLow64()
 

Macro Definition Documentation

◆ CHECKASSIGNMENTOPERATOR

#define CHECKASSIGNMENTOPERATOR ( _A_,
_B_,
_OP_ )
Value:
TmpL = _A_##L; TmpL _OP_##= _B_##L; BOOST_CHECK(TmpL == (_A_##L _OP_ _B_##L));
arith_uint256 TmpL(R1L)
#define BOOST_CHECK(expr)
Definition object.cpp:17

Definition at line 225 of file arith_uint256_tests.cpp.

◆ CHECKBITWISEOPERATOR

#define CHECKBITWISEOPERATOR ( _A_,
_B_,
_OP_ )
Value:
for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = uint8_t(_A_##Array[i] _OP_ _B_##Array[i]); } \
BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (_A_##L _OP_ _B_##L));
static arith_uint256 arith_uint256V(const std::vector< unsigned char > &vch)
Convert vector to arith_uint256, via uint256 blob.
unsigned char TmpArray[32]

Definition at line 221 of file arith_uint256_tests.cpp.

Function Documentation

◆ almostEqual()

static bool almostEqual ( double d1,
double d2 )
static

Definition at line 376 of file arith_uint256_tests.cpp.

◆ arith_uint256S()

static arith_uint256 arith_uint256S ( std::string_view str)
inlinestatic

Definition at line 27 of file arith_uint256_tests.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ arith_uint256V()

static arith_uint256 arith_uint256V ( const std::vector< unsigned char > & vch)
inlinestatic

Convert vector to arith_uint256, via uint256 blob.

Definition at line 22 of file arith_uint256_tests.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ArrayToString()

static std::string ArrayToString ( const unsigned char A[],
unsigned int width )
static

Definition at line 60 of file arith_uint256_tests.cpp.

◆ BOOST_AUTO_TEST_CASE() [1/6]

BOOST_AUTO_TEST_CASE ( bignum_SetCompact )

Definition at line 419 of file arith_uint256_tests.cpp.

Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [2/6]

BOOST_AUTO_TEST_CASE ( bitwiseOperators )

Definition at line 228 of file arith_uint256_tests.cpp.

◆ BOOST_AUTO_TEST_CASE() [3/6]

BOOST_AUTO_TEST_CASE ( divide )

Definition at line 357 of file arith_uint256_tests.cpp.

Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [4/6]

BOOST_AUTO_TEST_CASE ( multiply )

Definition at line 336 of file arith_uint256_tests.cpp.

◆ BOOST_AUTO_TEST_CASE() [5/6]

BOOST_AUTO_TEST_CASE ( plusMinus )

Definition at line 292 of file arith_uint256_tests.cpp.

Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [6/6]

BOOST_AUTO_TEST_CASE ( shifts )

Definition at line 161 of file arith_uint256_tests.cpp.

Here is the call graph for this function:

◆ BOOST_CHECK() [1/47]

BOOST_CHECK ( ! R1L > ~~R1L)

◆ BOOST_CHECK() [2/47]

BOOST_CHECK ( ! R1L< ~~R1L)

◆ BOOST_CHECK() [3/47]

BOOST_CHECK ( ! ~~R1L > R1L)

◆ BOOST_CHECK() [4/47]

BOOST_CHECK ( ! ~~R1L< R1L)

◆ BOOST_CHECK() [5/47]

BOOST_CHECK ( ((R1L ^ R2L) ^ R1L) = =R2L)

◆ BOOST_CHECK() [6/47]

BOOST_CHECK ( (arith_uint256(R1L^R2L)^R2L) = =R1L)

◆ BOOST_CHECK() [7/47]

BOOST_CHECK ( (R1L &arith_uint256S("0xffffffffffffffff")) = =arith_uint256(R1LLow64))

◆ BOOST_CHECK() [8/47]

BOOST_CHECK ( (~~R1L<< 10) = =(R1L<< 10))

◆ BOOST_CHECK() [9/47]

BOOST_CHECK ( - ZeroL = =ZeroL)

◆ BOOST_CHECK() [10/47]

BOOST_CHECK ( arith_uint256(OneL) = =OneL)

◆ BOOST_CHECK() [11/47]

BOOST_CHECK ( arith_uint256(R1L) = =R1L)

◆ BOOST_CHECK() [12/47]

BOOST_CHECK ( arith_uint256(ZeroL) = =ZeroL)

◆ BOOST_CHECK() [13/47]

BOOST_CHECK ( arith_uint256S(" 0x"+R1L.ToString()+" ") = =R1L)

◆ BOOST_CHECK() [14/47]

BOOST_CHECK ( arith_uint256S("") = =ZeroL)

◆ BOOST_CHECK() [15/47]

BOOST_CHECK ( arith_uint256S("0x"+MaxL.ToString()) = =MaxL)

◆ BOOST_CHECK() [16/47]

BOOST_CHECK ( arith_uint256S("0x"+OneL.ToString()) = =OneL)

◆ BOOST_CHECK() [17/47]

BOOST_CHECK ( arith_uint256S("0x"+R1L.ToString()) = =R1L)

◆ BOOST_CHECK() [18/47]

BOOST_CHECK ( arith_uint256S("0x"+R2L.ToString()) = =R2L)

◆ BOOST_CHECK() [19/47]

BOOST_CHECK ( arith_uint256S("0x"+ZeroL.ToString()) = =ZeroL)

◆ BOOST_CHECK() [20/47]

BOOST_CHECK ( arith_uint256S("0xffffffffffffffff") = =arith_uint256(0xffffffffffffffffULL))

◆ BOOST_CHECK() [21/47]

BOOST_CHECK ( arith_uint256S("1") = =OneL)

◆ BOOST_CHECK() [22/47]

BOOST_CHECK ( arith_uint256S(R1L.ToString()) = =R1L)

◆ BOOST_CHECK() [23/47]

BOOST_CHECK ( arith_uint256V(std::vector< unsigned char >(TmpArray, TmpArray+32)) = =(~R1L))

◆ BOOST_CHECK() [24/47]

BOOST_CHECK ( HalfL. GetLow64() = =0x0000000000000000ULL)

◆ BOOST_CHECK() [25/47]

BOOST_CHECK ( MaxL ! = ZeroL)

◆ BOOST_CHECK() [26/47]

BOOST_CHECK ( MaxL. ToString() = =ArrayToString(MaxArray, 32))

◆ BOOST_CHECK() [27/47]

BOOST_CHECK ( OneL ! = ZeroL)

◆ BOOST_CHECK() [28/47]

BOOST_CHECK ( OneL.ToString() ! = ArrayToString(ZeroArray, 32))

◆ BOOST_CHECK() [29/47]

BOOST_CHECK ( OneL. ToString() = =ArrayToString(OneArray, 32))

◆ BOOST_CHECK() [30/47]

BOOST_CHECK ( OneL = =arith_uint256(1))

◆ BOOST_CHECK() [31/47]

BOOST_CHECK ( R1L ! = R2L)

◆ BOOST_CHECK() [32/47]

BOOST_CHECK ( R1L >=~~ R1L)

◆ BOOST_CHECK() [33/47]

BOOST_CHECK ( R1L. ToString() = =ArrayToString(R1Array, 32))

◆ BOOST_CHECK() [34/47]

BOOST_CHECK ( R1L<=~~ R1L)

◆ BOOST_CHECK() [35/47]

BOOST_CHECK ( R1L = =arith_uint256S(R1ArrayHex))

◆ BOOST_CHECK() [36/47]

BOOST_CHECK ( R2L. ToString() = =ArrayToString(R2Array, 32))
Initial value:
{
const arith_uint256 R1L
std::string ToString() const
std::string GetHex() const
Hex encoding of the number (with the most significant digits first).

◆ BOOST_CHECK() [37/47]

BOOST_CHECK ( TmpL = =R1L)

◆ BOOST_CHECK() [38/47]

BOOST_CHECK ( tmpL = =~ZeroL)

◆ BOOST_CHECK() [39/47]

BOOST_CHECK ( ZeroL ! = OneL)

◆ BOOST_CHECK() [40/47]

BOOST_CHECK ( ZeroL. ToString() = =ArrayToString(ZeroArray, 32))

◆ BOOST_CHECK() [41/47]

BOOST_CHECK ( ZeroL = =(OneL<< 256))

◆ BOOST_CHECK() [42/47]

BOOST_CHECK ( ~ MaxL = =ZeroL)

◆ BOOST_CHECK() [43/47]

BOOST_CHECK ( ~R1L ! = R1L)

◆ BOOST_CHECK() [44/47]

BOOST_CHECK ( ~~R1L - R2L = =R1L - ~~R2L)

◆ BOOST_CHECK() [45/47]

BOOST_CHECK ( ~~R1L >= R1L)

◆ BOOST_CHECK() [46/47]

BOOST_CHECK ( ~~R1L+ R2L = =R1L+~~R2L)

◆ BOOST_CHECK() [47/47]

BOOST_CHECK ( ~~R1L<= R1L)

◆ BOOST_CHECK_LT() [1/2]

BOOST_CHECK_LT ( arith_uint256S("0000000000000000000000000000000000000000000000000000000000000001") ,
arith_uint256S("1000000000000000000000000000000000000000000000000000000000000000")  )

◆ BOOST_CHECK_LT() [2/2]

BOOST_CHECK_LT ( ZeroL ,
OneL  )
Here is the caller graph for this function:

◆ for() [1/3]

for ( int i = 256; i,
53;-- i )

◆ for() [2/3]

for ( int i = 53; i,
0;-- i )

Definition at line 413 of file arith_uint256_tests.cpp.

◆ for() [3/3]

for ( ) == (MaxL << i))

Definition at line 91 of file arith_uint256_tests.cpp.

Here is the caller graph for this function:

◆ ldexp()

ldexp ( R1Ldouble ,
i  )
Here is the caller graph for this function:

◆ shiftArrayLeft()

static void shiftArrayLeft ( unsigned char * to,
const unsigned char * from,
unsigned int arrayLength,
unsigned int bitsToShift )
static

Definition at line 144 of file arith_uint256_tests.cpp.

Here is the caller graph for this function:

◆ shiftArrayRight()

static void shiftArrayRight ( unsigned char * to,
const unsigned char * from,
unsigned int arrayLength,
unsigned int bitsToShift )
static

Definition at line 130 of file arith_uint256_tests.cpp.

Here is the caller graph for this function:

Variable Documentation

◆ HalfL

const arith_uint256 HalfL = (OneL << 255)

Definition at line 59 of file arith_uint256_tests.cpp.

◆ MaxArray

const unsigned char MaxArray[]
Initial value:
=
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"

Definition at line 54 of file arith_uint256_tests.cpp.

◆ MaxL

const arith_uint256 MaxL = arith_uint256V(std::vector<unsigned char>(MaxArray,MaxArray+32))

Definition at line 57 of file arith_uint256_tests.cpp.

◆ OneArray

const unsigned char OneArray[]
Initial value:
=
"\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"

Definition at line 49 of file arith_uint256_tests.cpp.

◆ OneL

const arith_uint256 OneL = arith_uint256V(std::vector<unsigned char>(OneArray,OneArray+32))

Definition at line 52 of file arith_uint256_tests.cpp.

◆ R1Array

const unsigned char R1Array[]
Initial value:
=
"\x9c\x52\x4a\xdb\xcf\x56\x11\x12\x2b\x29\x12\x5e\x5d\x35\xd2\xd2"
"\x22\x81\xaa\xb5\x33\xf0\x08\x32\xd5\x56\xb1\xf9\xea\xe5\x1d\x7d"

Definition at line 29 of file arith_uint256_tests.cpp.

◆ R1ArrayHex

const char R1ArrayHex[] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c"

Definition at line 32 of file arith_uint256_tests.cpp.

◆ R1L

BOOST_CHECK_LT * R1L = arith_uint256V(std::vector<unsigned char>(R1Array,R1Array+32))

Definition at line 34 of file arith_uint256_tests.cpp.

◆ R1L64part

uint64_t R1L64part = (R1L>>192).GetLow64()

Definition at line 412 of file arith_uint256_tests.cpp.

◆ R1Ldouble

const double R1Ldouble = 0.4887374590559308955

Definition at line 33 of file arith_uint256_tests.cpp.

◆ R1LLow64

const uint64_t R1LLow64 = 0x121156cfdb4a529cULL

Definition at line 35 of file arith_uint256_tests.cpp.

◆ R1LplusR2L

const char R1LplusR2L[] = "549FB09FEA236A1EA3E31D4D58F1B1369288D204211CA751527CFC175767850C"

Definition at line 42 of file arith_uint256_tests.cpp.

◆ R2Array

const unsigned char R2Array[]
Initial value:
=
"\x70\x32\x1d\x7c\x47\xa5\x6b\x40\x26\x7e\x0a\xc3\xa6\x9c\xb6\xbf"
"\x13\x30\x47\xa3\x19\x2d\xda\x71\x49\x13\x72\xf0\xb4\xca\x81\xd7"

Definition at line 37 of file arith_uint256_tests.cpp.

◆ R2L

BOOST_CHECK_LT * R2L = arith_uint256V(std::vector<unsigned char>(R2Array,R2Array+32))

Definition at line 40 of file arith_uint256_tests.cpp.

◆ Tmp64

uint64_t Tmp64 = 0xc4dab720d9c7acaaULL

Definition at line 90 of file arith_uint256_tests.cpp.

◆ TmpArray

unsigned char TmpArray
Initial value:
{
const arith_uint256 MaxL
const arith_uint256 ZeroL

Definition at line 208 of file arith_uint256_tests.cpp.

◆ TmpL

TmpL ( R1L ) = R2L

Definition at line 389 of file arith_uint256_tests.cpp.

◆ tmpL

tmpL = ~ZeroL

Definition at line 123 of file arith_uint256_tests.cpp.

◆ ZeroArray

const unsigned char ZeroArray[]
Initial value:
=
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"

Definition at line 44 of file arith_uint256_tests.cpp.

◆ ZeroL

const arith_uint256 ZeroL = arith_uint256V(std::vector<unsigned char>(ZeroArray,ZeroArray+32))

Definition at line 47 of file arith_uint256_tests.cpp.