Inside Bitcoin
Opening the selected file…
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#ifndef BITCOIN_ARITH_UINT256_H7#define BITCOIN_ARITH_UINT256_H9#include <compare>10#include <cstdint>11#include <cstring>12#include <limits>13#include <stdexcept>14#include <string>16class uint256;18class uint_error : public std::runtime_error {19public:20explicit uint_error(const std::string& str) : std::runtime_error(str) {}21};23/** Template base class for unsigned big integers. */24template <unsigned int BITS>25class base_uint26{27protected:28static_assert(BITS / 32 > 0 && BITS % 32 == 0, "Template parameter BITS must be a positive multiple of 32.");29static constexpr int WIDTH = BITS / 32;30/** Big integer represented with 32-bit digits, least-significant first. */31uint32_t pn[WIDTH];33public:34constexpr base_uint()35{36for (int i = 0; i < WIDTH; i++)37pn[i] = 0;38}40base_uint(const base_uint& b) = default;41base_uint& operator=(const base_uint& b) = default;43constexpr base_uint(uint64_t b)44{45pn[0] = (unsigned int)b;46pn[1] = (unsigned int)(b >> 32);47for (int i = 2; i < WIDTH; i++)48pn[i] = 0;49}51base_uint operator~() const52{53base_uint ret;54for (int i = 0; i < WIDTH; i++)55ret.pn[i] = ~pn[i];56return ret;57}59base_uint operator-() const60{61base_uint ret;62for (int i = 0; i < WIDTH; i++)63ret.pn[i] = ~pn[i];64++ret;65return ret;66}68double getdouble() const;70base_uint& operator=(uint64_t b)71{72pn[0] = (unsigned int)b;73pn[1] = (unsigned int)(b >> 32);74for (int i = 2; i < WIDTH; i++)75pn[i] = 0;76return *this;77}79base_uint& operator^=(const base_uint& b)80{81for (int i = 0; i < WIDTH; i++)82pn[i] ^= b.pn[i];83return *this;84}86base_uint& operator&=(const base_uint& b)87{88for (int i = 0; i < WIDTH; i++)89pn[i] &= b.pn[i];90return *this;91}93base_uint& operator|=(const base_uint& b)94{95for (int i = 0; i < WIDTH; i++)96pn[i] |= b.pn[i];97return *this;98}100base_uint& operator^=(uint64_t b)101{102pn[0] ^= (unsigned int)b;103pn[1] ^= (unsigned int)(b >> 32);104return *this;105}107base_uint& operator|=(uint64_t b)108{109pn[0] |= (unsigned int)b;110pn[1] |= (unsigned int)(b >> 32);111return *this;112}114base_uint& operator<<=(unsigned int shift);115base_uint& operator>>=(unsigned int shift);117base_uint& operator+=(const base_uint& b)118{119uint64_t carry = 0;120for (int i = 0; i < WIDTH; i++)121{122uint64_t n = carry + pn[i] + b.pn[i];123pn[i] = n & 0xffffffff;124carry = n >> 32;125}126return *this;127}129base_uint& operator-=(const base_uint& b)130{131*this += -b;132return *this;133}135base_uint& operator+=(uint64_t b64)136{137base_uint b;138b = b64;139*this += b;140return *this;141}143base_uint& operator-=(uint64_t b64)144{145base_uint b;146b = b64;147*this += -b;148return *this;149}151base_uint& operator*=(uint32_t b32);152base_uint& operator*=(const base_uint& b);153base_uint& operator/=(const base_uint& b);155base_uint& operator++()156{157// prefix operator158int i = 0;159while (i < WIDTH && ++pn[i] == 0)160i++;161return *this;162}164base_uint operator++(int)165{166// postfix operator167const base_uint ret = *this;168++(*this);169return ret;170}172base_uint& operator--()173{174// prefix operator175int i = 0;176while (i < WIDTH && --pn[i] == std::numeric_limits<uint32_t>::max())177i++;178return *this;179}181base_uint operator--(int)182{183// postfix operator184const base_uint ret = *this;185--(*this);186return ret;187}189/** Numeric ordering (unlike \ref base_blob::Compare) */190int CompareTo(const base_uint& b) const;191bool EqualTo(uint64_t b) const;193friend inline base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; }194friend inline base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; }195friend inline base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; }196friend inline base_uint operator/(const base_uint& a, const base_uint& b) { return base_uint(a) /= b; }197friend inline base_uint operator|(const base_uint& a, const base_uint& b) { return base_uint(a) |= b; }198friend inline base_uint operator&(const base_uint& a, const base_uint& b) { return base_uint(a) &= b; }199friend inline base_uint operator^(const base_uint& a, const base_uint& b) { return base_uint(a) ^= b; }200friend inline base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; }201friend inline base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }202friend inline base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }203friend inline bool operator==(const base_uint& a, const base_uint& b) { return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0; }204friend inline std::strong_ordering operator<=>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <=> 0; }205friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); }207/** Hex encoding of the number (with the most significant digits first). */208std::string GetHex() const;209std::string ToString() const;211unsigned int size() const212{213return sizeof(pn);214}216/**217* Returns the position of the highest bit set plus one, or zero if the218* value is zero.219*/220unsigned int bits() const;222uint64_t GetLow64() const223{224static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / 32). BITS is a template parameter.");225return pn[0] | (uint64_t)pn[1] << 32;226}227};229/** 256-bit unsigned big integer. */230class arith_uint256 : public base_uint<256>231{232public:233constexpr arith_uint256() = default;234constexpr arith_uint256(const base_uint& b) : base_uint(b) {}235constexpr arith_uint256(uint64_t b) : base_uint(b) {}237/**238* The "compact" format is a representation of a whole239* number N using an unsigned 32bit number similar to a240* floating point format.241* The most significant 8 bits are the unsigned exponent of base 256.242* This exponent can be thought of as "number of bytes of N".243* The lower 23 bits are the mantissa.244* Bit number 24 (0x800000) represents the sign of N.245* N = (-1^sign) * mantissa * 256^(exponent-3)246*247* Satoshi's original implementation used BN_bn2mpi() and BN_mpi2bn().248* MPI uses the most significant bit of the first byte as sign.249* Thus 0x1234560000 is compact (0x05123456)250* and 0xc0de000000 is compact (0x0600c0de)