Bitcoin Core  27.99.0
P2P Digital Currency
chacha20.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2022 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 
10 /* Number of bytes to process per iteration */
11 static const uint64_t BUFFER_SIZE_TINY = 64;
12 static const uint64_t BUFFER_SIZE_SMALL = 256;
13 static const uint64_t BUFFER_SIZE_LARGE = 1024*1024;
14 
15 static void CHACHA20(benchmark::Bench& bench, size_t buffersize)
16 {
17  std::vector<std::byte> key(32, {});
18  ChaCha20 ctx(key);
19  ctx.Seek({0, 0}, 0);
20  std::vector<std::byte> in(buffersize, {});
21  std::vector<std::byte> out(buffersize, {});
22  bench.batch(in.size()).unit("byte").run([&] {
23  ctx.Crypt(in, out);
24  });
25 }
26 
27 static void FSCHACHA20POLY1305(benchmark::Bench& bench, size_t buffersize)
28 {
29  std::vector<std::byte> key(32);
30  FSChaCha20Poly1305 ctx(key, 224);
31  std::vector<std::byte> in(buffersize);
32  std::vector<std::byte> aad;
33  std::vector<std::byte> out(buffersize + FSChaCha20Poly1305::EXPANSION);
34  bench.batch(in.size()).unit("byte").run([&] {
35  ctx.Encrypt(in, aad, out);
36  });
37 }
38 
40 {
41  CHACHA20(bench, BUFFER_SIZE_TINY);
42 }
43 
45 {
47 }
48 
49 static void CHACHA20_1MB(benchmark::Bench& bench)
50 {
52 }
53 
55 {
57 }
58 
60 {
62 }
63 
65 {
67 }
68 
static const uint64_t BUFFER_SIZE_LARGE
Definition: chacha20.cpp:13
BENCHMARK(CHACHA20_64BYTES, benchmark::PriorityLevel::HIGH)
static void FSCHACHA20POLY1305(benchmark::Bench &bench, size_t buffersize)
Definition: chacha20.cpp:27
static void CHACHA20_256BYTES(benchmark::Bench &bench)
Definition: chacha20.cpp:44
static void CHACHA20(benchmark::Bench &bench, size_t buffersize)
Definition: chacha20.cpp:15
static const uint64_t BUFFER_SIZE_TINY
Definition: chacha20.cpp:11
static void CHACHA20_1MB(benchmark::Bench &bench)
Definition: chacha20.cpp:49
static void CHACHA20_64BYTES(benchmark::Bench &bench)
Definition: chacha20.cpp:39
static void FSCHACHA20POLY1305_64BYTES(benchmark::Bench &bench)
Definition: chacha20.cpp:54
static const uint64_t BUFFER_SIZE_SMALL
Definition: chacha20.cpp:12
static void FSCHACHA20POLY1305_1MB(benchmark::Bench &bench)
Definition: chacha20.cpp:64
static void FSCHACHA20POLY1305_256BYTES(benchmark::Bench &bench)
Definition: chacha20.cpp:59
Unrestricted ChaCha20 cipher.
Definition: chacha20.h:78
Forward-secure wrapper around AEADChaCha20Poly1305.
static constexpr auto EXPANSION
Expansion when encrypting.
void Encrypt(Span< const std::byte > plain, Span< const std::byte > aad, Span< std::byte > cipher) noexcept
Encrypt a message with a specified aad.
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:627
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1234
Bench & batch(T b) noexcept
Sets the batch size.
Definition: nanobench.h:1258
Bench & unit(char const *unit)
Sets the operation unit.
@ HIGH
Definition: bench.h:47