Bitcoin ABC  0.24.7
P2P Digital Currency
chacha_poly_aead.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019 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 <iostream>
6 
7 #include <bench/bench.h>
9 #include <crypto/poly1305.h> // for the POLY1305_TAGLEN constant
10 #include <hash.h>
11 
12 #include <cassert>
13 #include <limits>
14 
15 /* Number of bytes to process per iteration */
16 static constexpr uint64_t BUFFER_SIZE_TINY = 64;
17 static constexpr uint64_t BUFFER_SIZE_SMALL = 256;
18 static constexpr uint64_t BUFFER_SIZE_LARGE = 1024 * 1024;
19 
20 static const uint8_t k1[32] = {0};
21 static const uint8_t k2[32] = {0};
22 
23 static ChaCha20Poly1305AEAD aead(k1, 32, k2, 32);
24 
25 static void CHACHA20_POLY1305_AEAD(benchmark::Bench &bench, size_t buffersize,
26  bool include_decryption) {
27  std::vector<uint8_t> in(
29  std::vector<uint8_t> out(
31  uint64_t seqnr_payload = 0;
32  uint64_t seqnr_aad = 0;
33  int aad_pos = 0;
34  uint32_t len = 0;
35  bench.batch(buffersize).unit("byte").run([&] {
36  // encrypt or decrypt the buffer with a static key
37  assert(aead.Crypt(seqnr_payload, seqnr_aad, aad_pos, out.data(),
38  out.size(), in.data(), buffersize, true));
39 
40  if (include_decryption) {
41  // if we decrypt, include the GetLength
42  assert(aead.GetLength(&len, seqnr_aad, aad_pos, in.data()));
43  assert(aead.Crypt(seqnr_payload, seqnr_aad, aad_pos, out.data(),
44  out.size(), in.data(), buffersize, true));
45  }
46 
47  // increase main sequence number
48  seqnr_payload++;
49  // increase aad position (position in AAD keystream)
52  aad_pos = 0;
53  seqnr_aad++;
54  }
55  if (seqnr_payload + 1 == std::numeric_limits<uint64_t>::max()) {
56  // reuse of nonce+key is okay while benchmarking.
57  seqnr_payload = 0;
58  seqnr_aad = 0;
59  aad_pos = 0;
60  }
61  });
62 }
63 
64 static void
67 }
68 
69 static void
72 }
73 
76 }
77 
78 static void
81 }
82 
83 static void
86 }
87 
88 static void
91 }
92 
93 // Add Hash() (dbl-sha256) bench for comparison
94 
95 static void HASH(benchmark::Bench &bench, size_t buffersize) {
96  uint8_t hash[CHash256::OUTPUT_SIZE];
97  std::vector<uint8_t> in(buffersize, 0);
98  bench.batch(in.size()).unit("byte").run(
99  [&] { CHash256().Write(in).Finalize(hash); });
100 }
101 
102 static void HASH_64BYTES(benchmark::Bench &bench) {
103  HASH(bench, BUFFER_SIZE_TINY);
104 }
105 
106 static void HASH_256BYTES(benchmark::Bench &bench) {
107  HASH(bench, BUFFER_SIZE_SMALL);
108 }
109 
110 static void HASH_1MB(benchmark::Bench &bench) {
111  HASH(bench, BUFFER_SIZE_LARGE);
112 }
113 
chacha_poly_aead.h
ankerl::nanobench::Bench::batch
ANKERL_NANOBENCH(NODISCARD) std Bench & batch(T b) noexcept
Sets the batch size.
k1
static const uint8_t k1[32]
Definition: chacha_poly_aead.cpp:20
k2
static const uint8_t k2[32]
Definition: chacha_poly_aead.cpp:21
BENCHMARK
BENCHMARK(CHACHA20_POLY1305_AEAD_64BYTES_ONLY_ENCRYPT)
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:583
CHash256::Finalize
void Finalize(Span< uint8_t > output)
Definition: hash.h:29
HASH_256BYTES
static void HASH_256BYTES(benchmark::Bench &bench)
Definition: chacha_poly_aead.cpp:106
CHACHA20_ROUND_OUTPUT
static constexpr int CHACHA20_ROUND_OUTPUT
Definition: chacha_poly_aead.h:14
CHACHA20_POLY1305_AEAD
static void CHACHA20_POLY1305_AEAD(benchmark::Bench &bench, size_t buffersize, bool include_decryption)
Definition: chacha_poly_aead.cpp:25
CHACHA20_POLY1305_AEAD_64BYTES_ONLY_ENCRYPT
static void CHACHA20_POLY1305_AEAD_64BYTES_ONLY_ENCRYPT(benchmark::Bench &bench)
Definition: chacha_poly_aead.cpp:65
poly1305.h
CHash256::OUTPUT_SIZE
static const size_t OUTPUT_SIZE
Definition: hash.h:27
BUFFER_SIZE_SMALL
static constexpr uint64_t BUFFER_SIZE_SMALL
Definition: chacha_poly_aead.cpp:17
ChaCha20Poly1305AEAD
Definition: chacha_poly_aead.h:117
ankerl::nanobench::Bench::run
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1134
ChaCha20Poly1305AEAD::Crypt
bool Crypt(uint64_t seqnr_payload, uint64_t seqnr_aad, int aad_pos, uint8_t *dest, size_t dest_len, const uint8_t *src, size_t src_len, bool is_encrypt)
Encrypts/decrypts a packet.
Definition: chacha_poly_aead.cpp:43
ankerl::nanobench::Bench::unit
Bench & unit(char const *unit)
Sets the operation unit.
CHACHA20_POLY1305_AEAD_1MB_ONLY_ENCRYPT
static void CHACHA20_POLY1305_AEAD_1MB_ONLY_ENCRYPT(benchmark::Bench &bench)
Definition: chacha_poly_aead.cpp:74
CHACHA20_POLY1305_AEAD_64BYTES_ENCRYPT_DECRYPT
static void CHACHA20_POLY1305_AEAD_64BYTES_ENCRYPT_DECRYPT(benchmark::Bench &bench)
Definition: chacha_poly_aead.cpp:79
aead
static ChaCha20Poly1305AEAD aead(k1, 32, k2, 32)
bench.h
POLY1305_TAGLEN
#define POLY1305_TAGLEN
Definition: poly1305.h:12
ChaCha20Poly1305AEAD::GetLength
bool GetLength(uint32_t *len24_out, uint64_t seqnr_aad, int aad_pos, const uint8_t *ciphertext)
decrypts the 3 bytes AAD data and decodes it into a uint32_t field
Definition: chacha_poly_aead.cpp:121
CHACHA20_POLY1305_AEAD_256BYTES_ONLY_ENCRYPT
static void CHACHA20_POLY1305_AEAD_256BYTES_ONLY_ENCRYPT(benchmark::Bench &bench)
Definition: chacha_poly_aead.cpp:70
HASH_64BYTES
static void HASH_64BYTES(benchmark::Bench &bench)
Definition: chacha_poly_aead.cpp:102
BUFFER_SIZE_TINY
static constexpr uint64_t BUFFER_SIZE_TINY
Definition: chacha_poly_aead.cpp:16
HASH_1MB
static void HASH_1MB(benchmark::Bench &bench)
Definition: chacha_poly_aead.cpp:110
HASH
static void HASH(benchmark::Bench &bench, size_t buffersize)
Definition: chacha_poly_aead.cpp:95
CHash256
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
Definition: hash.h:22
CHACHA20_POLY1305_AEAD_1MB_ENCRYPT_DECRYPT
static void CHACHA20_POLY1305_AEAD_1MB_ENCRYPT_DECRYPT(benchmark::Bench &bench)
Definition: chacha_poly_aead.cpp:89
CHACHA20_POLY1305_AEAD_256BYTES_ENCRYPT_DECRYPT
static void CHACHA20_POLY1305_AEAD_256BYTES_ENCRYPT_DECRYPT(benchmark::Bench &bench)
Definition: chacha_poly_aead.cpp:84
CHash256::Write
CHash256 & Write(Span< const uint8_t > input)
Definition: hash.h:36
BUFFER_SIZE_LARGE
static constexpr uint64_t BUFFER_SIZE_LARGE
Definition: chacha_poly_aead.cpp:18
CHACHA20_POLY1305_AEAD_AAD_LEN
static constexpr int CHACHA20_POLY1305_AEAD_AAD_LEN
Definition: chacha_poly_aead.h:13