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) 2016-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 <crypto/siphash.h>7#include <uint256.h>9#include <cassert>10#include <span>12CSipHasher::CSipHasher(uint64_t k0, uint64_t k1) : m_state{k0, k1} {}14CSipHasher& CSipHasher::Write(uint64_t data)15{16assert(m_count % 8 == 0);17m_state.Compress2(data);18m_count += 8;19return *this;20}22CSipHasher& CSipHasher::Write(std::span<const unsigned char> data)23{24SipHashState state{m_state.Copy()};25uint64_t t{m_tmp};26uint8_t c{m_count};28while (data.size() > 0) {29t |= uint64_t{data.front()} << (8 * (c % 8));30c++;31if ((c & 7) == 0) {32state.Compress2(t);33t = 0;34}35data = data.subspan(1);36}38m_state = state;39m_count = c;40m_tmp = t;42return *this;43}45uint64_t CSipHasher::Finalize() const46{47return m_state.Copy()48.Compress2(m_tmp | (uint64_t{m_count} << 56))49.Finalize4();50}52SipHasher13UJ& SipHasher13UJ::Write(uint64_t data) noexcept53{54m_state.Compress1(data);55return *this;56}58SipHasher13UJ& SipHasher13UJ::WriteJumbo(const uint256& hash) noexcept59{60m_state.Compress1Jumbo(hash);61return *this;62}64uint64_t SipHasher13UJ::Finalize() const noexcept65{66return m_state.Copy().Finalize3U();67}69uint64_t PresaltedSipHasher::operator()(const uint256& val) const noexcept70{71return m_state.Copy()72.Compress2(val.GetUint64(0))73.Compress2(val.GetUint64(1))74.Compress2(val.GetUint64(2))75.Compress2(val.GetUint64(3))76.Compress2(uint64_t{32} << 56)77.Finalize4();78}80uint64_t PresaltedSipHasher::operator()(const uint256& val, uint32_t extra) const noexcept81{82return m_state.Copy()83.Compress2(val.GetUint64(0))84.Compress2(val.GetUint64(1))85.Compress2(val.GetUint64(2))86.Compress2(val.GetUint64(3))87.Compress2((uint64_t{36} << 56) | extra)88.Finalize4();89}