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) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#include <arith_uint256.h>8#include <crypto/common.h>9#include <uint256.h>10#include <util/overflow.h>12#include <cassert>14template <unsigned int BITS>15base_uint<BITS>& base_uint<BITS>::operator<<=(unsigned int shift)16{17base_uint<BITS> a(*this);18for (int i = 0; i < WIDTH; i++)19pn[i] = 0;20int k = shift / 32;21shift = shift % 32;22for (int i = 0; i < WIDTH; i++) {23if (i + k + 1 < WIDTH && shift != 0)24pn[i + k + 1] |= (a.pn[i] >> (32 - shift));25if (i + k < WIDTH)26pn[i + k] |= (a.pn[i] << shift);27}28return *this;29}31template <unsigned int BITS>32base_uint<BITS>& base_uint<BITS>::operator>>=(unsigned int shift)33{34base_uint<BITS> a(*this);35for (int i = 0; i < WIDTH; i++)36pn[i] = 0;37int k = shift / 32;38shift = shift % 32;39for (int i = 0; i < WIDTH; i++) {40if (i - k - 1 >= 0 && shift != 0)41pn[i - k - 1] |= (a.pn[i] << (32 - shift));42if (i - k >= 0)43pn[i - k] |= (a.pn[i] >> shift);44}45return *this;46}48template <unsigned int BITS>49base_uint<BITS>& base_uint<BITS>::operator*=(uint32_t b32)50{51uint64_t carry = 0;52for (int i = 0; i < WIDTH; i++) {53uint64_t n = carry + (uint64_t)b32 * pn[i];54pn[i] = n & 0xffffffff;55carry = n >> 32;56}57return *this;58}60template <unsigned int BITS>61base_uint<BITS>& base_uint<BITS>::operator*=(const base_uint& b)62{63base_uint<BITS> a;64for (int j = 0; j < WIDTH; j++) {65uint64_t carry = 0;66for (int i = 0; i + j < WIDTH; i++) {67uint64_t n = carry + a.pn[i + j] + (uint64_t)pn[j] * b.pn[i];68a.pn[i + j] = n & 0xffffffff;69carry = n >> 32;70}71}72*this = a;73return *this;74}76template <unsigned int BITS>77base_uint<BITS>& base_uint<BITS>::operator/=(const base_uint& b)78{79base_uint<BITS> div = b; // make a copy, so we can shift.80base_uint<BITS> num = *this; // make a copy, so we can subtract.81*this = 0; // the quotient.82int num_bits = num.bits();83int div_bits = div.bits();84if (div_bits == 0)85throw uint_error("Division by zero");86if (div_bits > num_bits) // the result is certainly 0.87return *this;88int shift = num_bits - div_bits;89div <<= shift; // shift so that div and num align.90while (shift >= 0) {91if (num >= div) {92num -= div;93pn[shift / 32] |= (1U << (shift & 31)); // set a bit of the result.94}95div >>= 1; // shift back.96shift--;97}98// num now contains the remainder of the division.99return *this;100}102template <unsigned int BITS>103int base_uint<BITS>::CompareTo(const base_uint<BITS>& b) const104{105for (int i = WIDTH - 1; i >= 0; i--) {106if (pn[i] < b.pn[i])107return -1;108if (pn[i] > b.pn[i])109return 1;110}111return 0;112}114template <unsigned int BITS>115bool base_uint<BITS>::EqualTo(uint64_t b) const116{117for (int i = WIDTH - 1; i >= 2; i--) {118if (pn[i])119return false;120}121if (pn[1] != (b >> 32))122return false;123if (pn[0] != (b & 0xfffffffful))124return false;125return true;126}128template <unsigned int BITS>129double base_uint<BITS>::getdouble() const130{131double ret = 0.0;132double fact = 1.0;133for (int i = 0; i < WIDTH; i++) {134ret += fact * pn[i];135fact *= 4294967296.0;136}137return ret;138}140template <unsigned int BITS>141std::string base_uint<BITS>::GetHex() const142{143base_blob<BITS> b;144for (int x = 0; x < this->WIDTH; ++x) {145WriteLE32(b.begin() + x*4, this->pn[x]);146}147return b.GetHex();148}150template <unsigned int BITS>151std::string base_uint<BITS>::ToString() const152{153return GetHex();154}156template <unsigned int BITS>157unsigned int base_uint<BITS>::bits() const158{159for (int pos = WIDTH - 1; pos >= 0; pos--) {160if (pn[pos]) {161for (int nbits = 31; nbits > 0; nbits--) {162if (pn[pos] & 1U << nbits)163return 32 * pos + nbits + 1;164}165return 32 * pos + 1;166}167}168return 0;169}171// Explicit instantiations for base_uint<256>172template class base_uint<256>;174// This implementation directly uses shifts instead of going175// through an intermediate MPI representation.176arith_uint256& arith_uint256::SetCompact(uint32_t nCompact, bool* pfNegative, bool* pfOverflow)177{178int nSize = nCompact >> 24;179uint32_t nWord = nCompact & 0x007fffff;180if (nSize <= 3) {181nWord >>= 8 * (3 - nSize);182*this = nWord;183} else {184*this = nWord;185*this <<= 8 * (nSize - 3);186}187if (pfNegative)188*pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0;189if (pfOverflow)190*pfOverflow = nWord != 0 && ((nSize > 34) ||191(nWord > 0xff && nSize > 33) ||192(nWord > 0xffff && nSize > 32));193return *this;194}196uint32_t arith_uint256::GetCompact(bool fNegative) const197{198int nSize = CeilDiv(bits(), 8u);199uint32_t nCompact = 0;200if (nSize <= 3) {201nCompact = GetLow64() << 8 * (3 - nSize);202} else {203arith_uint256 bn = *this >> 8 * (nSize - 3);204nCompact = bn.GetLow64();205}206// The 0x00800000 bit denotes the sign.207// Thus, if it is already set, divide the mantissa by 256 and increase the exponent.208if (nCompact & 0x00800000) {209nCompact >>= 8;210nSize++;211}212assert((nCompact & ~0x007fffffU) == 0);213assert(nSize < 256);214nCompact |= nSize << 24;215nCompact |= (fNegative && (nCompact & 0x007fffff) ? 0x00800000 : 0);216return nCompact;217}219uint256 ArithToUint256(const arith_uint256 &a)220{221uint256 b;222for(int x=0; x<a.WIDTH; ++x)223WriteLE32(b.begin() + x*4, a.pn[x]);224return b;225}226arith_uint256 UintToArith256(const uint256 &a)227{228arith_uint256 b;229for(int x=0; x<b.WIDTH; ++x)230b.pn[x] = ReadLE32(a.begin() + x*4);231return b;232}234// Explicit instantiations for base_uint<6144> (used in test/fuzz/muhash.cpp).235template base_uint<6144>& base_uint<6144>::operator*=(const base_uint<6144>& b);236template base_uint<6144>& base_uint<6144>::operator/=(const base_uint<6144>& b);
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.