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