Bitcoin ABC  0.24.11
P2P Digital Currency
crypto_aes.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019 The Bitcoin 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/aes.h>
7 #include <util/time.h>
8 #include <validation.h>
9 
10 static void AES128_Encrypt(benchmark::Bench &bench) {
11  const std::vector<uint8_t> key(AES128_KEYSIZE, 0);
12  const std::vector<uint8_t> plaintext(16, 0);
13  std::vector<uint8_t> cyphertext(16, 0);
14 
15  bench.batch(plaintext.size()).unit("byte").run([&] {
16  AES128Encrypt(key.data()).Encrypt(cyphertext.data(), plaintext.data());
17  });
18 }
19 
20 static void AES128_Decrypt(benchmark::Bench &bench) {
21  const std::vector<uint8_t> key(AES128_KEYSIZE, 0);
22  const std::vector<uint8_t> cyphertext(16, 0);
23  std::vector<uint8_t> plaintext(16, 0);
24 
25  bench.batch(cyphertext.size()).unit("byte").run([&] {
26  AES128Decrypt(key.data()).Decrypt(plaintext.data(), cyphertext.data());
27  });
28 }
29 
30 static void AES256_Encrypt(benchmark::Bench &bench) {
31  const std::vector<uint8_t> key(AES256_KEYSIZE, 0);
32  const std::vector<uint8_t> plaintext(16, 0);
33  std::vector<uint8_t> cyphertext(16, 0);
34 
35  bench.batch(plaintext.size()).unit("byte").run([&] {
36  AES256Encrypt(key.data()).Encrypt(cyphertext.data(), plaintext.data());
37  });
38 }
39 
40 static void AES256_Decrypt(benchmark::Bench &bench) {
41  const std::vector<uint8_t> key(AES256_KEYSIZE, 0);
42  const std::vector<uint8_t> cyphertext(16, 0);
43  std::vector<uint8_t> plaintext(16, 0);
44 
45  bench.batch(cyphertext.size()).unit("byte").run([&] {
46  AES256Decrypt(key.data()).Decrypt(plaintext.data(), cyphertext.data());
47  });
48 }
49 
51  const std::vector<uint8_t> key(AES128_KEYSIZE, 0);
52  const std::vector<uint8_t> iv(AES_BLOCKSIZE, 0);
53  const std::vector<uint8_t> plaintext(128, 0);
54  std::vector<uint8_t> cyphertext(128, 0);
55 
56  bench.batch(plaintext.size()).unit("byte").run([&] {
57  AES128CBCEncrypt(key.data(), iv.data(), false)
58  .Encrypt(plaintext.data(), plaintext.size(), cyphertext.data());
59  });
60 }
61 
63  const std::vector<uint8_t> key(AES128_KEYSIZE, 0);
64  const std::vector<uint8_t> iv(AES_BLOCKSIZE, 0);
65  const std::vector<uint8_t> cyphertext(128, 0);
66  std::vector<uint8_t> plaintext(128, 0);
67 
68  bench.batch(cyphertext.size()).unit("byte").run([&] {
69  AES128CBCDecrypt(key.data(), iv.data(), false)
70  .Decrypt(cyphertext.data(), cyphertext.size(), plaintext.data());
71  });
72 }
73 
75  const std::vector<uint8_t> key(AES128_KEYSIZE, 0);
76  const std::vector<uint8_t> iv(AES_BLOCKSIZE, 0);
77  const std::vector<uint8_t> plaintext(128, 0);
78  std::vector<uint8_t> cyphertext(128 + AES_BLOCKSIZE, 0);
79 
80  bench.batch(plaintext.size()).unit("byte").run([&] {
81  AES128CBCEncrypt(key.data(), iv.data(), true)
82  .Encrypt(plaintext.data(), plaintext.size(), cyphertext.data());
83  });
84 }
85 
87  const std::vector<uint8_t> key(AES128_KEYSIZE, 0);
88  const std::vector<uint8_t> iv(AES_BLOCKSIZE, 0);
89  const std::vector<uint8_t> cyphertext(128, 0);
90  std::vector<uint8_t> plaintext(128 + AES_BLOCKSIZE, 0);
91 
92  bench.batch(cyphertext.size()).unit("byte").run([&] {
93  AES128CBCDecrypt(key.data(), iv.data(), true)
94  .Decrypt(cyphertext.data(), cyphertext.size(), plaintext.data());
95  });
96 }
97 
99  const std::vector<uint8_t> key(AES256_KEYSIZE, 0);
100  const std::vector<uint8_t> iv(AES_BLOCKSIZE, 0);
101  const std::vector<uint8_t> plaintext(128, 0);
102  std::vector<uint8_t> cyphertext(128, 0);
103 
104  bench.batch(plaintext.size()).unit("byte").run([&] {
105  AES256CBCEncrypt(key.data(), iv.data(), false)
106  .Encrypt(plaintext.data(), plaintext.size(), cyphertext.data());
107  });
108 }
109 
111  const std::vector<uint8_t> key(AES256_KEYSIZE, 0);
112  const std::vector<uint8_t> iv(AES_BLOCKSIZE, 0);
113  const std::vector<uint8_t> cyphertext(128, 0);
114  std::vector<uint8_t> plaintext(128, 0);
115 
116  bench.batch(cyphertext.size()).unit("byte").run([&] {
117  AES256CBCDecrypt(key.data(), iv.data(), false)
118  .Decrypt(cyphertext.data(), cyphertext.size(), plaintext.data());
119  });
120 }
121 
123  const std::vector<uint8_t> key(AES256_KEYSIZE, 0);
124  const std::vector<uint8_t> iv(AES_BLOCKSIZE, 0);
125  const std::vector<uint8_t> plaintext(128, 0);
126  std::vector<uint8_t> cyphertext(128 + AES_BLOCKSIZE, 0);
127 
128  bench.batch(plaintext.size()).unit("byte").run([&] {
129  AES256CBCEncrypt(key.data(), iv.data(), true)
130  .Encrypt(plaintext.data(), plaintext.size(), cyphertext.data());
131  });
132 }
133 
135  const std::vector<uint8_t> key(AES256_KEYSIZE, 0);
136  const std::vector<uint8_t> iv(AES_BLOCKSIZE, 0);
137  const std::vector<uint8_t> cyphertext(128, 0);
138  std::vector<uint8_t> plaintext(128 + AES_BLOCKSIZE, 0);
139 
140  bench.batch(cyphertext.size()).unit("byte").run([&] {
141  AES256CBCDecrypt(key.data(), iv.data(), true)
142  .Decrypt(cyphertext.data(), cyphertext.size(), plaintext.data());
143  });
144 }
145 
ankerl::nanobench::Bench::batch
ANKERL_NANOBENCH(NODISCARD) std Bench & batch(T b) noexcept
Sets the batch size.
AES128CBCDecrypt
Definition: aes.h:101
AES128_Decrypt
static void AES128_Decrypt(benchmark::Bench &bench)
Definition: crypto_aes.cpp:20
AES256Decrypt::Decrypt
void Decrypt(uint8_t plaintext[16], const uint8_t ciphertext[16]) const
Definition: aes.cpp:60
AES128CBCEncrypt
Definition: aes.h:88
AES128_Encrypt
static void AES128_Encrypt(benchmark::Bench &bench)
Definition: crypto_aes.cpp:10
AES256_Decrypt
static void AES256_Decrypt(benchmark::Bench &bench)
Definition: crypto_aes.cpp:40
AES128Encrypt::Encrypt
void Encrypt(uint8_t ciphertext[16], const uint8_t plaintext[16]) const
Definition: aes.cpp:21
ankerl::nanobench::Bench
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:583
AES256Encrypt::Encrypt
void Encrypt(uint8_t ciphertext[16], const uint8_t plaintext[16]) const
Definition: aes.cpp:47
AES128CBC_EncryptWithPad
static void AES128CBC_EncryptWithPad(benchmark::Bench &bench)
Definition: crypto_aes.cpp:74
AES256_KEYSIZE
static const int AES256_KEYSIZE
Definition: aes.h:16
AES128CBC_DecryptNoPad
static void AES128CBC_DecryptNoPad(benchmark::Bench &bench)
Definition: crypto_aes.cpp:62
AES128Decrypt
A decryption class for AES-128.
Definition: aes.h:30
aes.h
AES256CBCDecrypt
Definition: aes.h:75
AES256CBC_EncryptWithPad
static void AES256CBC_EncryptWithPad(benchmark::Bench &bench)
Definition: crypto_aes.cpp:122
AES256CBCEncrypt
Definition: aes.h:62
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
ankerl::nanobench::Bench::unit
Bench & unit(char const *unit)
Sets the operation unit.
time.h
AES256CBCEncrypt::Encrypt
int Encrypt(const uint8_t *data, int size, uint8_t *out) const
Definition: aes.cpp:158
AES256CBC_DecryptNoPad
static void AES256CBC_DecryptNoPad(benchmark::Bench &bench)
Definition: crypto_aes.cpp:110
AES256Encrypt
An encryption class for AES-256.
Definition: aes.h:41
AES256CBCDecrypt::Decrypt
int Decrypt(const uint8_t *data, int size, uint8_t *out) const
Definition: aes.cpp:174
bench.h
AES_BLOCKSIZE
static const int AES_BLOCKSIZE
Definition: aes.h:14
AES256CBC_EncryptNoPad
static void AES256CBC_EncryptNoPad(benchmark::Bench &bench)
Definition: crypto_aes.cpp:98
AES128CBCEncrypt::Encrypt
int Encrypt(const uint8_t *data, int size, uint8_t *out) const
Definition: aes.cpp:194
AES128Decrypt::Decrypt
void Decrypt(uint8_t plaintext[16], const uint8_t ciphertext[16]) const
Definition: aes.cpp:34
AES128CBC_EncryptNoPad
static void AES128CBC_EncryptNoPad(benchmark::Bench &bench)
Definition: crypto_aes.cpp:50
AES128CBC_DecryptWithPad
static void AES128CBC_DecryptWithPad(benchmark::Bench &bench)
Definition: crypto_aes.cpp:86
AES256_Encrypt
static void AES256_Encrypt(benchmark::Bench &bench)
Definition: crypto_aes.cpp:30
AES128_KEYSIZE
static const int AES128_KEYSIZE
Definition: aes.h:15
BENCHMARK
BENCHMARK(AES128_Encrypt)
AES128CBCDecrypt::Decrypt
int Decrypt(const uint8_t *data, int size, uint8_t *out) const
Definition: aes.cpp:210
AES256CBC_DecryptWithPad
static void AES256CBC_DecryptWithPad(benchmark::Bench &bench)
Definition: crypto_aes.cpp:134
AES128Encrypt
An encryption class for AES-128.
Definition: aes.h:19
AES256Decrypt
A decryption class for AES-256.
Definition: aes.h:52