chacha20.cpp
1 // Copyright (c) 2019-present 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> 8 #include <crypto/chacha20poly1305.h> 9 #include <span.h> 10 #include <util/byte_units.h> 11 12 #include <cstddef> 13 #include <cstdint> 14 #include <vector> 15 16 /* Number of bytes to process per iteration */ 17 static const uint64_t BUFFER_SIZE_TINY = 64; 18 static const uint64_t BUFFER_SIZE_SMALL = 256; 19 static const uint64_t BUFFER_SIZE_LARGE{1_MiB}; 20 21 static void CHACHA20(benchmark::Bench& bench, size_t buffersize) 22 { 23 std::vector<std::byte> key(32, {}); 24 ChaCha20 ctx(key); 25 ctx.Seek({0, 0}, 0); 26 std::vector<std::byte> in(buffersize, {}); 27 std::vector<std::byte> out(buffersize, {}); 28 bench.batch(in.size()).unit("byte").run([&] { 29 ctx.Crypt(in, out); 30 }); 31 } 32 33 static void FSCHACHA20POLY1305(benchmark::Bench& bench, size_t buffersize) 34 { 35 std::vector<std::byte> key(32); 36 FSChaCha20Poly1305 ctx(key, 224); 37 std::vector<std::byte> in(buffersize); 38 std::vector<std::byte> aad; 39 std::vector<std::byte> out(buffersize + FSChaCha20Poly1305::EXPANSION); 40 bench.batch(in.size()).unit("byte").run([&] { 41 ctx.Encrypt(in, aad, out); 42 }); 43 } 44 45 static void CHACHA20_64BYTES(benchmark::Bench& bench) 46 { 47 CHACHA20(bench, BUFFER_SIZE_TINY); 48 } 49 50 static void CHACHA20_256BYTES(benchmark::Bench& bench) 51 { 52 CHACHA20(bench, BUFFER_SIZE_SMALL); 53 } 54 55 static void CHACHA20_1MB(benchmark::Bench& bench) 56 { 57 CHACHA20(bench, BUFFER_SIZE_LARGE); 58 } 59 60 static void FSCHACHA20POLY1305_64BYTES(benchmark::Bench& bench) 61 { 62 FSCHACHA20POLY1305(bench, BUFFER_SIZE_TINY); 63 } 64 65 static void FSCHACHA20POLY1305_256BYTES(benchmark::Bench& bench) 66 { 67 FSCHACHA20POLY1305(bench, BUFFER_SIZE_SMALL); 68 } 69 70 static void FSCHACHA20POLY1305_1MB(benchmark::Bench& bench) 71 { 72 FSCHACHA20POLY1305(bench, BUFFER_SIZE_LARGE); 73 } 74 75 BENCHMARK(CHACHA20_64BYTES); 76 BENCHMARK(CHACHA20_256BYTES); 77 BENCHMARK(CHACHA20_1MB); 78 BENCHMARK(FSCHACHA20POLY1305_64BYTES); 79 BENCHMARK(FSCHACHA20POLY1305_256BYTES); 80 BENCHMARK(FSCHACHA20POLY1305_1MB);