|
| 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 | +#include <assert.h> |
| 6 | +#include <bench/bench.h> |
| 7 | +#include <crypto/bip324_suite.h> |
| 8 | +#include <crypto/rfc8439.h> |
| 9 | +#include <hash.h> |
| 10 | + |
| 11 | +#include <array> |
| 12 | +#include <cstddef> |
| 13 | +#include <vector> |
| 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 std::vector<std::byte> zero_vec(BIP324_KEY_LEN, std::byte{0x00}); |
| 21 | + |
| 22 | +static void BIP324_CIPHER_SUITE(benchmark::Bench& bench, size_t contents_len, bool include_decryption) |
| 23 | +{ |
| 24 | + BIP324Key zero_arr; |
| 25 | + std::memcpy(zero_arr.data(), zero_vec.data(), BIP324_KEY_LEN); |
| 26 | + BIP324CipherSuite enc{zero_arr, zero_arr}; |
| 27 | + BIP324CipherSuite dec{zero_arr, zero_arr}; |
| 28 | + |
| 29 | + auto packet_len = BIP324_LENGTH_FIELD_LEN + BIP324_HEADER_LEN + contents_len + RFC8439_EXPANSION; |
| 30 | + |
| 31 | + std::vector<std::byte> in(contents_len, std::byte{0x00}); |
| 32 | + std::vector<std::byte> out(packet_len, std::byte{0x00}); |
| 33 | + |
| 34 | + BIP324HeaderFlags flags{BIP324_NONE}; |
| 35 | + |
| 36 | + bench.batch(contents_len).unit("byte").run([&] { |
| 37 | + // encrypt or decrypt the buffer with a static key |
| 38 | + const bool crypt_ok_1 = enc.Crypt(in, out, flags, true); |
| 39 | + assert(crypt_ok_1); |
| 40 | + |
| 41 | + if (include_decryption) { |
| 42 | + // if we decrypt, we need to decrypt the length first |
| 43 | + std::array<std::byte, BIP324_LENGTH_FIELD_LEN> encrypted_pkt_len; |
| 44 | + std::memcpy(encrypted_pkt_len.data(), out.data(), BIP324_LENGTH_FIELD_LEN); |
| 45 | + (void)dec.DecryptLength(encrypted_pkt_len); |
| 46 | + const bool crypt_ok_2 = dec.Crypt({out.data() + BIP324_LENGTH_FIELD_LEN, out.size() - BIP324_LENGTH_FIELD_LEN}, in, flags, false); |
| 47 | + assert(crypt_ok_2); |
| 48 | + } |
| 49 | + }); |
| 50 | +} |
| 51 | + |
| 52 | +static void BIP324_CIPHER_SUITE_64BYTES_ONLY_ENCRYPT(benchmark::Bench& bench) |
| 53 | +{ |
| 54 | + BIP324_CIPHER_SUITE(bench, BUFFER_SIZE_TINY, false); |
| 55 | +} |
| 56 | + |
| 57 | +static void BIP324_CIPHER_SUITE_256BYTES_ONLY_ENCRYPT(benchmark::Bench& bench) |
| 58 | +{ |
| 59 | + BIP324_CIPHER_SUITE(bench, BUFFER_SIZE_SMALL, false); |
| 60 | +} |
| 61 | + |
| 62 | +static void BIP324_CIPHER_SUITE_1MB_ONLY_ENCRYPT(benchmark::Bench& bench) |
| 63 | +{ |
| 64 | + BIP324_CIPHER_SUITE(bench, BUFFER_SIZE_LARGE, false); |
| 65 | +} |
| 66 | + |
| 67 | +static void BIP324_CIPHER_SUITE_64BYTES_ENCRYPT_DECRYPT(benchmark::Bench& bench) |
| 68 | +{ |
| 69 | + BIP324_CIPHER_SUITE(bench, BUFFER_SIZE_TINY, true); |
| 70 | +} |
| 71 | + |
| 72 | +static void BIP324_CIPHER_SUITE_256BYTES_ENCRYPT_DECRYPT(benchmark::Bench& bench) |
| 73 | +{ |
| 74 | + BIP324_CIPHER_SUITE(bench, BUFFER_SIZE_SMALL, true); |
| 75 | +} |
| 76 | + |
| 77 | +static void BIP324_CIPHER_SUITE_1MB_ENCRYPT_DECRYPT(benchmark::Bench& bench) |
| 78 | +{ |
| 79 | + BIP324_CIPHER_SUITE(bench, BUFFER_SIZE_LARGE, true); |
| 80 | +} |
| 81 | + |
| 82 | +// Add Hash() (dbl-sha256) bench for comparison |
| 83 | + |
| 84 | +static void HASH(benchmark::Bench& bench, size_t buffersize) |
| 85 | +{ |
| 86 | + uint8_t hash[CHash256::OUTPUT_SIZE]; |
| 87 | + std::vector<uint8_t> in(buffersize, 0); |
| 88 | + bench.batch(in.size()).unit("byte").run([&] { |
| 89 | + CHash256().Write(in).Finalize(hash); |
| 90 | + }); |
| 91 | +} |
| 92 | + |
| 93 | +static void HASH_64BYTES(benchmark::Bench& bench) |
| 94 | +{ |
| 95 | + HASH(bench, BUFFER_SIZE_TINY); |
| 96 | +} |
| 97 | + |
| 98 | +static void HASH_256BYTES(benchmark::Bench& bench) |
| 99 | +{ |
| 100 | + HASH(bench, BUFFER_SIZE_SMALL); |
| 101 | +} |
| 102 | + |
| 103 | +static void HASH_1MB(benchmark::Bench& bench) |
| 104 | +{ |
| 105 | + HASH(bench, BUFFER_SIZE_LARGE); |
| 106 | +} |
| 107 | + |
| 108 | +BENCHMARK(BIP324_CIPHER_SUITE_64BYTES_ONLY_ENCRYPT, benchmark::PriorityLevel::HIGH); |
| 109 | +BENCHMARK(BIP324_CIPHER_SUITE_256BYTES_ONLY_ENCRYPT, benchmark::PriorityLevel::HIGH); |
| 110 | +BENCHMARK(BIP324_CIPHER_SUITE_1MB_ONLY_ENCRYPT, benchmark::PriorityLevel::HIGH); |
| 111 | +BENCHMARK(BIP324_CIPHER_SUITE_64BYTES_ENCRYPT_DECRYPT, benchmark::PriorityLevel::HIGH); |
| 112 | +BENCHMARK(BIP324_CIPHER_SUITE_256BYTES_ENCRYPT_DECRYPT, benchmark::PriorityLevel::HIGH); |
| 113 | +BENCHMARK(BIP324_CIPHER_SUITE_1MB_ENCRYPT_DECRYPT, benchmark::PriorityLevel::HIGH); |
| 114 | +BENCHMARK(HASH_64BYTES, benchmark::PriorityLevel::HIGH); |
| 115 | +BENCHMARK(HASH_256BYTES, benchmark::PriorityLevel::HIGH); |
| 116 | +BENCHMARK(HASH_1MB, benchmark::PriorityLevel::HIGH); |
0 commit comments