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
6#include <bench/bench.h>
7#include <crypto/chacha20.h>
9#include <span.h>
10#include <util/byte_units.h>
11
12#include <cstddef>
13#include <cstdint>
14#include <vector>
15
16/* Number of bytes to process per iteration */
17static const uint64_t BUFFER_SIZE_TINY = 64;
18static const uint64_t BUFFER_SIZE_SMALL = 256;
19static const uint64_t BUFFER_SIZE_LARGE{1_MiB};
20
21static void CHACHA20(benchmark::Bench& bench, size_t buffersize)
22{
23 std::vector<std::byte> key(32, {});
24 ChaCha20 ctx(key);
25 ctx.Seek({0, 0}, 0);
26 std::vector<std::byte> in(buffersize, {});
27 std::vector<std::byte> out(buffersize, {});
28 bench.batch(in.size()).unit("byte").run([&] {
29 ctx.Crypt(in, out);
30 });
31}
32
33static void FSCHACHA20POLY1305(benchmark::Bench& bench, size_t buffersize)
34{
35 std::vector<std::byte> key(32);
36 FSChaCha20Poly1305 ctx(key, 224);
37 std::vector<std::byte> in(buffersize);
38 std::vector<std::byte> aad;
39 std::vector<std::byte> out(buffersize + FSChaCha20Poly1305::EXPANSION);
40 bench.batch(in.size()).unit("byte").run([&] {
41 ctx.Encrypt(in, aad, out);
42 });
43}
44
46{
48}
49
51{
53}
54
55static void CHACHA20_1MB(benchmark::Bench& bench)
56{
58}
59
61{
63}
64
66{
68}
69
71{
73}
74
static const uint64_t BUFFER_SIZE_LARGE
Definition: chacha20.cpp:19
BENCHMARK(CHACHA20_64BYTES)
static void FSCHACHA20POLY1305(benchmark::Bench &bench, size_t buffersize)
Definition: chacha20.cpp:33
static void CHACHA20_256BYTES(benchmark::Bench &bench)
Definition: chacha20.cpp:50
static void CHACHA20(benchmark::Bench &bench, size_t buffersize)
Definition: chacha20.cpp:21
static const uint64_t BUFFER_SIZE_TINY
Definition: chacha20.cpp:17
static void CHACHA20_1MB(benchmark::Bench &bench)
Definition: chacha20.cpp:55
static void CHACHA20_64BYTES(benchmark::Bench &bench)
Definition: chacha20.cpp:45
static void FSCHACHA20POLY1305_64BYTES(benchmark::Bench &bench)
Definition: chacha20.cpp:60
static const uint64_t BUFFER_SIZE_SMALL
Definition: chacha20.cpp:18
static void FSCHACHA20POLY1305_1MB(benchmark::Bench &bench)
Definition: chacha20.cpp:70
static void FSCHACHA20POLY1305_256BYTES(benchmark::Bench &bench)
Definition: chacha20.cpp:65
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:633
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1292
Bench & batch(T b) noexcept
Sets the batch size.
Definition: nanobench.h:1316
Bench & unit(char const *unit)
Sets the operation unit.