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) 2017-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#ifndef BITCOIN_CRYPTO_MUHASH_H6#define BITCOIN_CRYPTO_MUHASH_H8#include <serialize.h>10#include <cstddef>11#include <cstdint>12#include <span>14class uint256;16class Num307217{18private:19void FullReduce();20bool IsOverflow() const;21Num3072 GetInverse() const;23public:24static constexpr size_t BYTE_SIZE = 384;26#ifdef __SIZEOF_INT128__27typedef unsigned __int128 double_limb_t;28typedef signed __int128 signed_double_limb_t;29typedef uint64_t limb_t;30typedef int64_t signed_limb_t;31static constexpr int LIMBS = 48;32static constexpr int SIGNED_LIMBS = 50;33static constexpr int LIMB_SIZE = 64;34static constexpr int SIGNED_LIMB_SIZE = 62;35#else36typedef uint64_t double_limb_t;37typedef int64_t signed_double_limb_t;38typedef uint32_t limb_t;39typedef int32_t signed_limb_t;40static constexpr int LIMBS = 96;41static constexpr int SIGNED_LIMBS = 103;42static constexpr int LIMB_SIZE = 32;43static constexpr int SIGNED_LIMB_SIZE = 30;44#endif45limb_t limbs[LIMBS];47// Sanity check for Num3072 constants48static_assert(LIMB_SIZE * LIMBS == 3072, "Num3072 isn't 3072 bits");49static_assert(sizeof(double_limb_t) == sizeof(limb_t) * 2, "bad size for double_limb_t");50static_assert(sizeof(limb_t) * 8 == LIMB_SIZE, "LIMB_SIZE is incorrect");51static_assert(SIGNED_LIMB_SIZE * SIGNED_LIMBS > 3072, "SIGNED_LIMBS * SIGNED_LIMB_SIZE is too small");52static_assert(3072 / SIGNED_LIMB_SIZE == SIGNED_LIMBS - 1, "Bit 3072 must land in top signed limb");54// Hard coded values in MuHash3072 constructor and Finalize55static_assert(sizeof(limb_t) == 4 || sizeof(limb_t) == 8, "bad size for limb_t");57void Multiply(const Num3072& a);58void Divide(const Num3072& a);59void SetToOne();60void ToBytes(unsigned char (&out)[BYTE_SIZE]);62Num3072() { this->SetToOne(); };63Num3072(const unsigned char (&data)[BYTE_SIZE]);65SERIALIZE_METHODS(Num3072, obj)66{67for (auto& limb : obj.limbs) {68READWRITE(limb);69}70}71};73/** A class representing MuHash sets74*75* MuHash is a hashing algorithm that supports adding set elements in any76* order but also deleting in any order. As a result, it can maintain a77* running sum for a set of data as a whole, and add/remove when data78* is added to or removed from it. A downside of MuHash is that computing79* an inverse is relatively expensive. This is solved by representing80* the running value as a fraction, and multiplying added elements into81* the numerator and removed elements into the denominator. Only when the82* final hash is desired, a single modular inverse and multiplication is83* needed to combine the two. The combination is also run on serialization84* to allow for space-efficient storage on disk.85*86* As the update operations are also associative, H(a)+H(b)+H(c)+H(d) can87* in fact be computed as (H(a)+H(b)) + (H(c)+H(d)). This implies that88* all of this is perfectly parallellizable: each thread can process an89* arbitrary subset of the update operations, allowing them to be90* efficiently combined later.91*92* MuHash does not support checking if an element is already part of the93* set. That is why this class does not enforce the use of a set as the94* data it represents because there is no efficient way to do so.95* It is possible to add elements more than once and also to remove96* elements that have not been added before. However, this implementation97* is intended to represent a set of elements.98*99* See also https://cseweb.ucsd.edu/~mihir/papers/inchash.pdf and100* https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2017-May/014337.html.101*/102class MuHash3072103{104private:105Num3072 m_numerator;106Num3072 m_denominator;108Num3072 ToNum3072(std::span<const unsigned char> in);110public:111/* The empty set. */112MuHash3072() noexcept = default;114/* A singleton with variable sized data in it. */115explicit MuHash3072(std::span<const unsigned char> in) noexcept;117/* Insert a single piece of data into the set. */118MuHash3072& Insert(std::span<const unsigned char> in) noexcept;120/* Remove a single piece of data from the set. */121MuHash3072& Remove(std::span<const unsigned char> in) noexcept;123/* Multiply (resulting in a hash for the union of the sets) */124MuHash3072& operator*=(const MuHash3072& mul) noexcept;126/* Divide (resulting in a hash for the difference of the sets) */127MuHash3072& operator/=(const MuHash3072& div) noexcept;129/* Finalize into a 32-byte hash. Does not change this object's value. */130void Finalize(uint256& out) noexcept;132SERIALIZE_METHODS(MuHash3072, obj)133{134READWRITE(obj.m_numerator);135READWRITE(obj.m_denominator);136}137};139#endif // BITCOIN_CRYPTO_MUHASH_H