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) 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 <bench/bench.h>6#include <crypto/muhash.h>7#include <crypto/ripemd160.h>8#include <crypto/sha1.h>9#include <crypto/sha256.h>10#include <crypto/sha3.h>11#include <crypto/sha512.h>12#include <crypto/siphash.h>13#include <random.h>14#include <tinyformat.h>15#include <uint256.h>17#include <cstdint>18#include <span>19#include <string>20#include <vector>22/* Number of bytes to hash per iteration */23static const uint64_t BUFFER_SIZE = 1000*1000;25static void BenchRIPEMD160(benchmark::Bench& bench)26{27uint8_t hash[CRIPEMD160::OUTPUT_SIZE];28std::vector<uint8_t> in(BUFFER_SIZE,0);29bench.batch(in.size()).unit("byte").run([&] {30CRIPEMD160().Write(in.data(), in.size()).Finalize(hash);31});32}34static void SHA1(benchmark::Bench& bench)35{36uint8_t hash[CSHA1::OUTPUT_SIZE];37std::vector<uint8_t> in(BUFFER_SIZE,0);38bench.batch(in.size()).unit("byte").run([&] {39CSHA1().Write(in.data(), in.size()).Finalize(hash);40});41}43static void SHA256_STANDARD(benchmark::Bench& bench)44{45bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::STANDARD)));46uint8_t hash[CSHA256::OUTPUT_SIZE];47std::vector<uint8_t> in(BUFFER_SIZE,0);48bench.batch(in.size()).unit("byte").run([&] {49CSHA256().Write(in.data(), in.size()).Finalize(hash);50});51SHA256AutoDetect();52}54static void SHA256_SSE4(benchmark::Bench& bench)55{56bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4)));57uint8_t hash[CSHA256::OUTPUT_SIZE];58std::vector<uint8_t> in(BUFFER_SIZE,0);59bench.batch(in.size()).unit("byte").run([&] {60CSHA256().Write(in.data(), in.size()).Finalize(hash);61});62SHA256AutoDetect();63}65static void SHA256_AVX2(benchmark::Bench& bench)66{67bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4_AND_AVX2)));68uint8_t hash[CSHA256::OUTPUT_SIZE];69std::vector<uint8_t> in(BUFFER_SIZE,0);70bench.batch(in.size()).unit("byte").run([&] {71CSHA256().Write(in.data(), in.size()).Finalize(hash);72});73SHA256AutoDetect();74}76static void SHA256_SHANI(benchmark::Bench& bench)77{78bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4_AND_SHANI)));79uint8_t hash[CSHA256::OUTPUT_SIZE];80std::vector<uint8_t> in(BUFFER_SIZE,0);81bench.batch(in.size()).unit("byte").run([&] {82CSHA256().Write(in.data(), in.size()).Finalize(hash);83});84SHA256AutoDetect();85}87static void SHA3_256_1M(benchmark::Bench& bench)88{89uint8_t hash[SHA3_256::OUTPUT_SIZE];90std::vector<uint8_t> in(BUFFER_SIZE,0);91bench.batch(in.size()).unit("byte").run([&] {92SHA3_256().Write(in).Finalize(hash);93});94}96static void SHA256_32b_STANDARD(benchmark::Bench& bench)97{98bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::STANDARD)));99std::vector<uint8_t> in(32,0);100bench.batch(in.size()).unit("byte").run([&] {101CSHA256()102.Write(in.data(), in.size())103.Finalize(in.data());104});105SHA256AutoDetect();106}108static void SHA256_32b_SSE4(benchmark::Bench& bench)109{110bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4)));111std::vector<uint8_t> in(32,0);112bench.batch(in.size()).unit("byte").run([&] {113CSHA256()114.Write(in.data(), in.size())115.Finalize(in.data());116});117SHA256AutoDetect();118}120static void SHA256_32b_AVX2(benchmark::Bench& bench)121{122bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4_AND_AVX2)));123std::vector<uint8_t> in(32,0);124bench.batch(in.size()).unit("byte").run([&] {125CSHA256()126.Write(in.data(), in.size())127.Finalize(in.data());128});129SHA256AutoDetect();130}132static void SHA256_32b_SHANI(benchmark::Bench& bench)133{134bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4_AND_SHANI)));135std::vector<uint8_t> in(32,0);136bench.batch(in.size()).unit("byte").run([&] {137CSHA256()138.Write(in.data(), in.size())139.Finalize(in.data());140});141SHA256AutoDetect();142}144static void SHA256D64_1024_STANDARD(benchmark::Bench& bench)145{146bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::STANDARD)));147std::vector<uint8_t> in(64 * 1024, 0);148bench.batch(in.size()).unit("byte").run([&] {149SHA256D64(in.data(), in.data(), 1024);150});151SHA256AutoDetect();152}154static void SHA256D64_1024_SSE4(benchmark::Bench& bench)155{156bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4)));157std::vector<uint8_t> in(64 * 1024, 0);158bench.batch(in.size()).unit("byte").run([&] {159SHA256D64(in.data(), in.data(), 1024);160});161SHA256AutoDetect();162}164static void SHA256D64_1024_AVX2(benchmark::Bench& bench)165{166bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4_AND_AVX2)));167std::vector<uint8_t> in(64 * 1024, 0);168bench.batch(in.size()).unit("byte").run([&] {169SHA256D64(in.data(), in.data(), 1024);170});171SHA256AutoDetect();172}174static void SHA256D64_1024_SHANI(benchmark::Bench& bench)175{176bench.name(strprintf("%s using the '%s' SHA256 implementation", __func__, SHA256AutoDetect(sha256_implementation::USE_SSE4_AND_SHANI)));177std::vector<uint8_t> in(64 * 1024, 0);178bench.batch(in.size()).unit("byte").run([&] {179SHA256D64(in.data(), in.data(), 1024);180});181SHA256AutoDetect();182}184static void SHA512(benchmark::Bench& bench)185{186uint8_t hash[CSHA512::OUTPUT_SIZE];187std::vector<uint8_t> in(BUFFER_SIZE,0);188bench.batch(in.size()).unit("byte").run([&] {189CSHA512().Write(in.data(), in.size()).Finalize(hash);190});191}193static void SipHash24_32b(benchmark::Bench& bench)194{195FastRandomContext rng{/*fDeterministic=*/true};196PresaltedSipHasher presalted_sip_hasher{rng.rand64(), rng.rand64()};197auto val{rng.rand256()};198auto i{0U};199bench.run([&] {200ankerl::nanobench::doNotOptimizeAway(presalted_sip_hasher(val));201++i;202val.data()[i % uint256::size()] ^= i & 0xFF;203});204}206static void SipHash24_36b(benchmark::Bench& bench)207{208FastRandomContext rng{/*fDeterministic=*/true};209PresaltedSipHasher presalted_sip_hasher{rng.rand64(), rng.rand64()};210auto val{rng.rand256()};211uint32_t extra{rng.rand32()};212auto i{0U};213bench.run([&] {214ankerl::nanobench::doNotOptimizeAway(presalted_sip_hasher(val, extra));215++i;216val.data()[i % uint256::size()] ^= i & 0xFF;217extra += i;218});219}221static void SipHash13UJ_32b(benchmark::Bench& bench)222{223FastRandomContext rng{/*fDeterministic=*/true};224SipHasher13UJ sip_hasher{rng.rand64(), rng.rand64()};225auto val{rng.rand256()};226auto i{0U};227bench.run([&] {228ankerl::nanobench::doNotOptimizeAway(sip_hasher.Hash(val));229++i;230val.data()[i % uint256::size()] ^= i & 0xFF;231});232}234static void SipHash13UJ_36b(benchmark::Bench& bench)235{236FastRandomContext rng{/*fDeterministic=*/true};237SipHasher13UJ sip_hasher{rng.rand64(), rng.rand64()};238auto val{rng.rand256()};239uint32_t extra{rng.rand32()};240auto i{0U};241bench.run([&] {242ankerl::nanobench::doNotOptimizeAway(sip_hasher.Hash(val, uint64_t{extra}));243++i;244val.data()[i % uint256::size()] ^= i & 0xFF;245extra += i;246});247}249static void MuHash(benchmark::Bench& bench)250{
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.