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) 2019-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/chacha20.h>7#include <crypto/chacha20poly1305.h>8#include <util/byte_units.h>10#include <cstddef>11#include <cstdint>12#include <span>13#include <vector>15/* Number of bytes to process per iteration */16static const uint64_t BUFFER_SIZE_TINY = 64;17static const uint64_t BUFFER_SIZE_SMALL = 256;18static const uint64_t BUFFER_SIZE_LARGE{1_MiB};20static void CHACHA20(benchmark::Bench& bench, size_t buffersize)21{22std::vector<std::byte> key(32, {});23ChaCha20 ctx(key);24ctx.Seek({0, 0}, 0);25std::vector<std::byte> in(buffersize, {});26std::vector<std::byte> out(buffersize, {});27bench.batch(in.size()).unit("byte").run([&] {28ctx.Crypt(in, out);29});30}32static void FSCHACHA20POLY1305(benchmark::Bench& bench, size_t buffersize)33{34std::vector<std::byte> key(32);35FSChaCha20Poly1305 ctx(key, 224);36std::vector<std::byte> in(buffersize);37std::vector<std::byte> aad;38std::vector<std::byte> out(buffersize + FSChaCha20Poly1305::EXPANSION);39bench.batch(in.size()).unit("byte").run([&] {40ctx.Encrypt(in, aad, out);41});42}44static void CHACHA20_64BYTES(benchmark::Bench& bench)45{46CHACHA20(bench, BUFFER_SIZE_TINY);47}49static void CHACHA20_256BYTES(benchmark::Bench& bench)50{51CHACHA20(bench, BUFFER_SIZE_SMALL);52}54static void CHACHA20_1MB(benchmark::Bench& bench)55{56CHACHA20(bench, BUFFER_SIZE_LARGE);57}59static void FSCHACHA20POLY1305_64BYTES(benchmark::Bench& bench)60{61FSCHACHA20POLY1305(bench, BUFFER_SIZE_TINY);62}64static void FSCHACHA20POLY1305_256BYTES(benchmark::Bench& bench)65{66FSCHACHA20POLY1305(bench, BUFFER_SIZE_SMALL);67}69static void FSCHACHA20POLY1305_1MB(benchmark::Bench& bench)70{71FSCHACHA20POLY1305(bench, BUFFER_SIZE_LARGE);72}74BENCHMARK(CHACHA20_64BYTES);75BENCHMARK(CHACHA20_256BYTES);76BENCHMARK(CHACHA20_1MB);77BENCHMARK(FSCHACHA20POLY1305_64BYTES);78BENCHMARK(FSCHACHA20POLY1305_256BYTES);79BENCHMARK(FSCHACHA20POLY1305_1MB);
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.