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) 2013-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 <hash.h>6#include <span.h>7#include <crypto/common.h>8#include <crypto/hmac_sha512.h>10#include <bit>11#include <string>13unsigned int MurmurHash3(unsigned int nHashSeed, std::span<const unsigned char> vDataToHash)14{15// The following is MurmurHash3 (x86_32), see https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp16uint32_t h1 = nHashSeed;17const uint32_t c1 = 0xcc9e2d51;18const uint32_t c2 = 0x1b873593;20const int nblocks = vDataToHash.size() / 4;22//----------23// body24const uint8_t* blocks = vDataToHash.data();26for (int i = 0; i < nblocks; ++i) {27uint32_t k1 = ReadLE32(blocks + i*4);29k1 *= c1;30k1 = std::rotl(k1, 15);31k1 *= c2;33h1 ^= k1;34h1 = std::rotl(h1, 13);35h1 = h1 * 5 + 0xe6546b64;36}38//----------39// tail40const uint8_t* tail = vDataToHash.data() + nblocks * 4;42uint32_t k1 = 0;44switch (vDataToHash.size() & 3) {45case 3:46k1 ^= tail[2] << 16;47[[fallthrough]];48case 2:49k1 ^= tail[1] << 8;50[[fallthrough]];51case 1:52k1 ^= tail[0];53k1 *= c1;54k1 = std::rotl(k1, 15);55k1 *= c2;56h1 ^= k1;57}59//----------60// finalization61h1 ^= vDataToHash.size();62h1 ^= h1 >> 16;63h1 *= 0x85ebca6b;64h1 ^= h1 >> 13;65h1 *= 0xc2b2ae35;66h1 ^= h1 >> 16;68return h1;69}71void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])72{73unsigned char num[4];74WriteBE32(num, nChild);75CHMAC_SHA512(chainCode.begin(), chainCode.size()).Write(&header, 1).Write(data, 32).Write(num, 4).Finalize(output);76}78uint256 SHA256Uint256(const uint256& input)79{80uint256 result;81CSHA256().Write(input.begin(), 32).Finalize(result.begin());82return result;83}85HashWriter TaggedHash(const std::string& tag)86{87HashWriter writer{};88uint256 taghash;89CSHA256().Write((const unsigned char*)tag.data(), tag.size()).Finalize(taghash.begin());90writer << taghash << taghash;91return writer;92}
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.