Bitcoin Core 31.99.0
P2P Digital Currency
chacha20.cpp
Go to the documentation of this file.
1// Copyright (c) 2019-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <bench/bench.h>
6#include <crypto/chacha20.h>
8// IWYU incorrectly suggests removing this header.
9// See https://github.com/include-what-you-use/include-what-you-use/issues/2014.
10#include <util/byte_units.h> // IWYU pragma: keep
11
12#include <cstddef>
13#include <cstdint>
14#include <span>
15#include <vector>
16
17/* Number of bytes to process per iteration */
18static const uint64_t BUFFER_SIZE_TINY = 64;
19static const uint64_t BUFFER_SIZE_SMALL = 256;
20static const uint64_t BUFFER_SIZE_LARGE{1_MiB};
21
22static void CHACHA20(benchmark::Bench& bench, size_t buffersize)
23{
24 std::vector<std::byte> key(32, {});
25 ChaCha20 ctx(key);
26 ctx.Seek({0, 0}, 0);
27 std::vector<std::byte> in(buffersize, {});
28 std::vector<std::byte> out(buffersize, {});
29 bench.batch(in.size()).unit("byte").run([&] {
30 ctx.Crypt(in, out);
31 });
32}
33
34static void FSCHACHA20POLY1305(benchmark::Bench& bench, size_t buffersize)
35{
36 std::vector<std::byte> key(32);
37 FSChaCha20Poly1305 ctx(key, 224);
38 std::vector<std::byte> in(buffersize);
39 std::vector<std::byte> aad;
40 std::vector<std::byte> out(buffersize + FSChaCha20Poly1305::EXPANSION);
41 bench.batch(in.size()).unit("byte").run([&] {
42 ctx.Encrypt(in, aad, out);
43 });
44}
45
47{
49}
50
52{
54}
55
56static void CHACHA20_1MB(benchmark::Bench& bench)
57{
59}
60
62{
64}
65
67{
69}
70
72{
74}
75
static const uint64_t BUFFER_SIZE_LARGE
Definition: chacha20.cpp:20
BENCHMARK(CHACHA20_64BYTES)
static void FSCHACHA20POLY1305(benchmark::Bench &bench, size_t buffersize)
Definition: chacha20.cpp:34
static void CHACHA20_256BYTES(benchmark::Bench &bench)
Definition: chacha20.cpp:51
static void CHACHA20(benchmark::Bench &bench, size_t buffersize)
Definition: chacha20.cpp:22
static const uint64_t BUFFER_SIZE_TINY
Definition: chacha20.cpp:18
static void CHACHA20_1MB(benchmark::Bench &bench)
Definition: chacha20.cpp:56
static void CHACHA20_64BYTES(benchmark::Bench &bench)
Definition: chacha20.cpp:46
static void FSCHACHA20POLY1305_64BYTES(benchmark::Bench &bench)
Definition: chacha20.cpp:61
static const uint64_t BUFFER_SIZE_SMALL
Definition: chacha20.cpp:19
static void FSCHACHA20POLY1305_1MB(benchmark::Bench &bench)
Definition: chacha20.cpp:71
static void FSCHACHA20POLY1305_256BYTES(benchmark::Bench &bench)
Definition: chacha20.cpp:66
Unrestricted ChaCha20 cipher.
Definition: chacha20.h:76
Forward-secure wrapper around AEADChaCha20Poly1305.
void Encrypt(std::span< const std::byte > plain, std::span< const std::byte > aad, std::span< std::byte > cipher) noexcept
Encrypt a message with a specified aad.
static constexpr auto EXPANSION
Expansion when encrypting.
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:649
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1308
Bench & batch(T b) noexcept
Sets the batch size.
Definition: nanobench.h:1332
Bench & unit(char const *unit)
Sets the operation unit.