Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2011-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <arith_uint256.h>6#include <test/util/common.h>7#include <uint256.h>9#include <boost/test/unit_test.hpp>11#include <cmath>12#include <cstdint>13#include <iomanip>14#include <limits>15#include <sstream>16#include <string>17#include <vector>19BOOST_AUTO_TEST_SUITE(arith_uint256_tests)21/// Convert vector to arith_uint256, via uint256 blob22static inline arith_uint256 arith_uint256V(const std::vector<unsigned char>& vch)23{24return UintToArith256(uint256(vch));25}26const 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";29const char R1ArrayHex[] = "7D1DE5EAF9B156D53208F033B5AA8122D2d2355d5e12292b121156cfdb4a529c";30const double R1Ldouble = 0.4887374590559308955; // R1L equals roughly R1Ldouble * 2^25631const arith_uint256 R1L = arith_uint256V(std::vector<unsigned char>(R1Array,R1Array+32));32const uint64_t R1LLow64 = 0x121156cfdb4a529cULL;34const 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";37const arith_uint256 R2L = arith_uint256V(std::vector<unsigned char>(R2Array,R2Array+32));39const 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";42const arith_uint256 ZeroL = arith_uint256V(std::vector<unsigned char>(ZeroArray,ZeroArray+32));44const 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";47const arith_uint256 OneL = arith_uint256V(std::vector<unsigned char>(OneArray,OneArray+32));49const 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";52const arith_uint256 MaxL = arith_uint256V(std::vector<unsigned char>(MaxArray,MaxArray+32));54const arith_uint256 HalfL = (OneL << 255);55static std::string ArrayToString(const unsigned char A[], unsigned int width)56{57std::stringstream Stream;58Stream << std::hex;59for (unsigned int i = 0; i < width; ++i)60{61Stream<<std::setw(2)<<std::setfill('0')<<(unsigned int)A[width-i-1];62}63return Stream.str();64}66BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality67{68BOOST_CHECK(1 == 0+1);69// constructor arith_uint256(vector<char>):70BOOST_CHECK(R1L.ToString() == ArrayToString(R1Array,32));71BOOST_CHECK(R2L.ToString() == ArrayToString(R2Array,32));72BOOST_CHECK(ZeroL.ToString() == ArrayToString(ZeroArray,32));73BOOST_CHECK(OneL.ToString() == ArrayToString(OneArray,32));74BOOST_CHECK(MaxL.ToString() == ArrayToString(MaxArray,32));75BOOST_CHECK(OneL.ToString() != ArrayToString(ZeroArray,32));77// == and !=78BOOST_CHECK(R1L != R2L);79BOOST_CHECK(ZeroL != OneL);80BOOST_CHECK(OneL != ZeroL);81BOOST_CHECK(MaxL != ZeroL);82BOOST_CHECK(~MaxL == ZeroL);83BOOST_CHECK( ((R1L ^ R2L) ^ R1L) == R2L);85uint64_t Tmp64 = 0xc4dab720d9c7acaaULL;86for (unsigned int i = 0; i < 256; ++i)87{88BOOST_CHECK(ZeroL != (OneL << i));89BOOST_CHECK((OneL << i) != ZeroL);90BOOST_CHECK(R1L != (R1L ^ (OneL << i)));91BOOST_CHECK(((arith_uint256(Tmp64) ^ (OneL << i) ) != Tmp64 ));92}93BOOST_CHECK(ZeroL == (OneL << 256));95// Construct from hex string96BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(R1L.ToString()).value()), R1L);97BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(R2L.ToString()).value()), R2L);98BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(ZeroL.ToString()).value()), ZeroL);99BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(OneL.ToString()).value()), OneL);100BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(MaxL.ToString()).value()), MaxL);101BOOST_CHECK_EQUAL(UintToArith256(uint256::FromHex(R1ArrayHex).value()), R1L);103// Copy constructor104BOOST_CHECK(arith_uint256(R1L) == R1L);105BOOST_CHECK((arith_uint256(R1L^R2L)^R2L) == R1L);106BOOST_CHECK(arith_uint256(ZeroL) == ZeroL);107BOOST_CHECK(arith_uint256(OneL) == OneL);109// uint64_t constructor110BOOST_CHECK_EQUAL(R1L & arith_uint256{0xffffffffffffffff}, arith_uint256{R1LLow64});111BOOST_CHECK_EQUAL(ZeroL, arith_uint256{0});112BOOST_CHECK_EQUAL(OneL, arith_uint256{1});113BOOST_CHECK_EQUAL(arith_uint256{0xffffffffffffffff}, arith_uint256{0xffffffffffffffffULL});115// Assignment (from base_uint)116arith_uint256 tmpL = ~ZeroL; BOOST_CHECK(tmpL == ~ZeroL);117tmpL = ~OneL; BOOST_CHECK(tmpL == ~OneL);118tmpL = ~R1L; BOOST_CHECK(tmpL == ~R1L);119tmpL = ~R2L; BOOST_CHECK(tmpL == ~R2L);120tmpL = ~MaxL; BOOST_CHECK(tmpL == ~MaxL);121}123static void shiftArrayRight(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift)124{125for (unsigned int T=0; T < arrayLength; ++T)126{127unsigned int F = (T+bitsToShift/8);128if (F < arrayLength)129to[T] = uint8_t(from[F] >> (bitsToShift % 8));130else131to[T] = 0;132if (F + 1 < arrayLength)133to[T] |= uint8_t(from[(F + 1)] << (8 - bitsToShift % 8));134}135}137static void shiftArrayLeft(unsigned char* to, const unsigned char* from, unsigned int arrayLength, unsigned int bitsToShift)138{139for (unsigned int T=0; T < arrayLength; ++T)140{141if (T >= bitsToShift/8)142{143unsigned int F = T-bitsToShift/8;144to[T] = uint8_t(from[F] << (bitsToShift % 8));145if (T >= bitsToShift/8+1)146to[T] |= uint8_t(from[F - 1] >> (8 - bitsToShift % 8));147}148else {149to[T] = 0;150}151}152}154BOOST_AUTO_TEST_CASE( shifts ) { // "<<" ">>" "<<=" ">>="155unsigned char TmpArray[32];156arith_uint256 TmpL;157for (unsigned int i = 0; i < 256; ++i)158{159shiftArrayLeft(TmpArray, OneArray, 32, i);160BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (OneL << i));161TmpL = OneL; TmpL <<= i;162BOOST_CHECK(TmpL == (OneL << i));163BOOST_CHECK((HalfL >> (255-i)) == (OneL << i));164TmpL = HalfL; TmpL >>= (255-i);165BOOST_CHECK(TmpL == (OneL << i));167shiftArrayLeft(TmpArray, R1Array, 32, i);168BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L << i));169TmpL = R1L; TmpL <<= i;170BOOST_CHECK(TmpL == (R1L << i));172shiftArrayRight(TmpArray, R1Array, 32, i);173BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (R1L >> i));174TmpL = R1L; TmpL >>= i;175BOOST_CHECK(TmpL == (R1L >> i));177shiftArrayLeft(TmpArray, MaxArray, 32, i);178BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL << i));179TmpL = MaxL; TmpL <<= i;180BOOST_CHECK(TmpL == (MaxL << i));182shiftArrayRight(TmpArray, MaxArray, 32, i);183BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (MaxL >> i));184TmpL = MaxL; TmpL >>= i;185BOOST_CHECK(TmpL == (MaxL >> i));186}187arith_uint256 c1L = arith_uint256(0x0123456789abcdefULL);188arith_uint256 c2L = c1L << 128;189for (unsigned int i = 0; i < 128; ++i) {190BOOST_CHECK((c1L << i) == (c2L >> (128-i)));191}192for (unsigned int i = 128; i < 256; ++i) {193BOOST_CHECK((c1L << i) == (c2L << (i-128)));194}195}197BOOST_AUTO_TEST_CASE( unaryOperators ) // ! ~ -198{199BOOST_CHECK(~ZeroL == MaxL);201unsigned char TmpArray[32];202for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = uint8_t(~R1Array[i]); }203BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (~R1L));205BOOST_CHECK(-ZeroL == ZeroL);206BOOST_CHECK(-R1L == (~R1L)+1);207for (unsigned int i = 0; i < 256; ++i)208BOOST_CHECK(-(OneL<<i) == (MaxL << i));209}212// Check if doing _A_ _OP_ _B_ results in the same as applying _OP_ onto each213// element of Aarray and Barray, and then converting the result into an arith_uint256.214#define CHECKBITWISEOPERATOR(_A_,_B_,_OP_) \215for (unsigned int i = 0; i < 32; ++i) { TmpArray[i] = uint8_t(_A_##Array[i] _OP_ _B_##Array[i]); } \216BOOST_CHECK(arith_uint256V(std::vector<unsigned char>(TmpArray,TmpArray+32)) == (_A_##L _OP_ _B_##L));218#define CHECKASSIGNMENTOPERATOR(_A_,_B_,_OP_) \219TmpL = _A_##L; TmpL _OP_##= _B_##L; BOOST_CHECK(TmpL == (_A_##L _OP_ _B_##L));221BOOST_AUTO_TEST_CASE( bitwiseOperators )222{223unsigned char TmpArray[32];225CHECKBITWISEOPERATOR(R1,R2,|)226CHECKBITWISEOPERATOR(R1,R2,^)227CHECKBITWISEOPERATOR(R1,R2,&)228CHECKBITWISEOPERATOR(R1,Zero,|)229CHECKBITWISEOPERATOR(R1,Zero,^)230CHECKBITWISEOPERATOR(R1,Zero,&)231CHECKBITWISEOPERATOR(R1,Max,|)232CHECKBITWISEOPERATOR(R1,Max,^)233CHECKBITWISEOPERATOR(R1,Max,&)234CHECKBITWISEOPERATOR(Zero,R1,|)235CHECKBITWISEOPERATOR(Zero,R1,^)236CHECKBITWISEOPERATOR(Zero,R1,&)237CHECKBITWISEOPERATOR(Max,R1,|)238CHECKBITWISEOPERATOR(Max,R1,^)239CHECKBITWISEOPERATOR(Max,R1,&)241arith_uint256 TmpL;242CHECKASSIGNMENTOPERATOR(R1,R2,|)243CHECKASSIGNMENTOPERATOR(R1,R2,^)244CHECKASSIGNMENTOPERATOR(R1,R2,&)245CHECKASSIGNMENTOPERATOR(R1,Zero,|)246CHECKASSIGNMENTOPERATOR(R1,Zero,^)247CHECKASSIGNMENTOPERATOR(R1,Zero,&)248CHECKASSIGNMENTOPERATOR(R1,Max,|)249CHECKASSIGNMENTOPERATOR(R1,Max,^)250CHECKASSIGNMENTOPERATOR(R1,Max,&)
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.