diff --git a/src/Makefile.bench.include b/src/Makefile.bench.include index 849c7f965..499b4d386 100644 --- a/src/Makefile.bench.include +++ b/src/Makefile.bench.include @@ -1,83 +1,84 @@ # Copyright (c) 2015-2016 The Bitcoin Core developers # Distributed under the MIT software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. bin_PROGRAMS += bench/bench_bitcoin BENCH_SRCDIR = bench BENCH_BINARY = bench/bench_bitcoin$(EXEEXT) RAW_BENCH_FILES = \ bench/data/block413567.raw GENERATED_BENCH_FILES = $(RAW_BENCH_FILES:.raw=.raw.h) bench_bench_bitcoin_SOURCES = \ $(RAW_BENCH_FILES) \ bench/bench_bitcoin.cpp \ bench/bench.cpp \ bench/bench.h \ bench/cashaddr.cpp \ bench/checkblock.cpp \ bench/checkqueue.cpp \ bench/Examples.cpp \ bench/rollingbloom.cpp \ + bench/crypto_aes.cpp \ bench/crypto_hash.cpp \ bench/ccoins_caching.cpp \ bench/gcs_filter.cpp \ bench/merkle_root.cpp \ bench/mempool_eviction.cpp \ bench/base58.cpp \ bench/lockedpool.cpp \ bench/perf.cpp \ bench/perf.h \ bench/prevector.cpp nodist_bench_bench_bitcoin_SOURCES = $(GENERATED_BENCH_FILES) bench_bench_bitcoin_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(EVENT_CLFAGS) $(EVENT_PTHREADS_CFLAGS) -I$(builddir)/bench/ bench_bench_bitcoin_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) bench_bench_bitcoin_LDADD = \ $(LIBBITCOIN_SERVER) \ $(LIBBITCOIN_WALLET) \ $(LIBBITCOIN_COMMON) \ $(LIBBITCOIN_UTIL) \ $(LIBBITCOIN_CONSENSUS) \ $(LIBBITCOIN_CRYPTO) \ $(LIBLEVELDB) \ $(LIBLEVELDB_SSE42) \ $(LIBMEMENV) \ $(LIBSECP256K1) \ $(LIBUNIVALUE) if ENABLE_ZMQ bench_bench_bitcoin_LDADD += $(LIBBITCOIN_ZMQ) $(ZMQ_LIBS) endif if ENABLE_WALLET bench_bench_bitcoin_SOURCES += bench/coin_selection.cpp endif bench_bench_bitcoin_LDADD += $(BOOST_LIBS) $(BDB_LIBS) $(SSL_LIBS) $(CRYPTO_LIBS) $(MINIUPNPC_LIBS) $(EVENT_PTHREADS_LIBS) $(EVENT_LIBS) bench_bench_bitcoin_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS) CLEAN_BITCOIN_BENCH = bench/*.gcda bench/*.gcno $(GENERATED_BENCH_FILES) CLEANFILES += $(CLEAN_BITCOIN_BENCH) bench/checkblock.cpp: bench/data/block413567.raw.h bitcoin_bench: $(BENCH_BINARY) bench: $(BENCH_BINARY) FORCE $(BENCH_BINARY) bitcoin_bench_clean : FORCE rm -f $(CLEAN_BITCOIN_BENCH) $(bench_bench_bitcoin_OBJECTS) $(BENCH_BINARY) %.raw.h: %.raw @$(MKDIR_P) $(@D) @{ \ echo "static unsigned const char $(*F)[] = {" && \ $(HEXDUMP) -v -e '8/1 "0x%02x, "' -e '"\n"' $< | $(SED) -e 's/0x ,//g' && \ echo "};"; \ } > "$@.new" && mv -f "$@.new" "$@" @echo "Generated $@" diff --git a/src/bench/CMakeLists.txt b/src/bench/CMakeLists.txt index 2b360be91..9610a684b 100644 --- a/src/bench/CMakeLists.txt +++ b/src/bench/CMakeLists.txt @@ -1,36 +1,37 @@ # Copyright (c) 2018 The Bitcoin developers project(bitcoin-bench) add_executable(bitcoin-bench EXCLUDE_FROM_ALL base58.cpp bench.cpp bench_bitcoin.cpp cashaddr.cpp ccoins_caching.cpp # checkblock.cpp TODO Fix including bench/data/*.raw files checkqueue.cpp + crypto_aes.cpp crypto_hash.cpp Examples.cpp gcs_filter.cpp lockedpool.cpp mempool_eviction.cpp merkle_root.cpp perf.cpp prevector.cpp rollingbloom.cpp ) target_link_libraries(bitcoin-bench common bitcoinconsensus server) if(BUILD_BITCOIN_WALLET) target_sources(bitcoin-bench PRIVATE coin_selection.cpp) endif() add_custom_target(bench-bitcoin COMMAND ./bitcoin-bench DEPENDS bitcoin-bench ) diff --git a/src/bench/crypto_aes.cpp b/src/bench/crypto_aes.cpp new file mode 100644 index 000000000..8ed4fe516 --- /dev/null +++ b/src/bench/crypto_aes.cpp @@ -0,0 +1,162 @@ +// Copyright (c) 2019 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include +#include +#include +#include + +#define BENCH_AES128_ITERATION 800000 +#define BENCH_AES128CBC_ITERATION 200000 +#define BENCH_AES256_ITERATION 640000 +#define BENCH_AES256CBC_ITERATION 160000 + +static void AES128_Encrypt(benchmark::State &state) { + const std::vector key(AES128_KEYSIZE, 0); + const std::vector plaintext(16, 0); + std::vector cyphertext(16, 0); + + while (state.KeepRunning()) { + AES128Encrypt(key.data()).Encrypt(cyphertext.data(), plaintext.data()); + } +} + +static void AES128_Decrypt(benchmark::State &state) { + const std::vector key(AES128_KEYSIZE, 0); + const std::vector cyphertext(16, 0); + std::vector plaintext(16, 0); + + while (state.KeepRunning()) { + AES128Decrypt(key.data()).Decrypt(plaintext.data(), cyphertext.data()); + } +} + +static void AES256_Encrypt(benchmark::State &state) { + const std::vector key(AES256_KEYSIZE, 0); + const std::vector plaintext(16, 0); + std::vector cyphertext(16, 0); + + while (state.KeepRunning()) { + AES128Encrypt(key.data()).Encrypt(cyphertext.data(), plaintext.data()); + } +} + +static void AES256_Decrypt(benchmark::State &state) { + const std::vector key(AES256_KEYSIZE, 0); + const std::vector cyphertext(16, 0); + std::vector plaintext(16, 0); + + while (state.KeepRunning()) { + AES128Decrypt(key.data()).Decrypt(plaintext.data(), cyphertext.data()); + } +} + +static void AES128CBC_EncryptNoPad(benchmark::State &state) { + const std::vector key(AES128_KEYSIZE, 0); + const std::vector iv(AES_BLOCKSIZE, 0); + const std::vector plaintext(128, 0); + std::vector cyphertext(128, 0); + + while (state.KeepRunning()) { + AES128CBCEncrypt(key.data(), iv.data(), false) + .Encrypt(plaintext.data(), plaintext.size(), cyphertext.data()); + } +} + +static void AES128CBC_DecryptNoPad(benchmark::State &state) { + const std::vector key(AES128_KEYSIZE, 0); + const std::vector iv(AES_BLOCKSIZE, 0); + const std::vector cyphertext(128, 0); + std::vector plaintext(128, 0); + + while (state.KeepRunning()) { + AES128CBCDecrypt(key.data(), iv.data(), false) + .Decrypt(cyphertext.data(), cyphertext.size(), plaintext.data()); + } +} + +static void AES128CBC_EncryptWithPad(benchmark::State &state) { + const std::vector key(AES128_KEYSIZE, 0); + const std::vector iv(AES_BLOCKSIZE, 0); + const std::vector plaintext(128, 0); + std::vector cyphertext(128 + AES_BLOCKSIZE, 0); + + while (state.KeepRunning()) { + AES128CBCEncrypt(key.data(), iv.data(), true) + .Encrypt(plaintext.data(), plaintext.size(), cyphertext.data()); + } +} + +static void AES128CBC_DecryptWithPad(benchmark::State &state) { + const std::vector key(AES128_KEYSIZE, 0); + const std::vector iv(AES_BLOCKSIZE, 0); + const std::vector cyphertext(128, 0); + std::vector plaintext(128 + AES_BLOCKSIZE, 0); + + while (state.KeepRunning()) { + AES128CBCDecrypt(key.data(), iv.data(), true) + .Decrypt(cyphertext.data(), cyphertext.size(), plaintext.data()); + } +} + +static void AES256CBC_EncryptNoPad(benchmark::State &state) { + const std::vector key(AES256_KEYSIZE, 0); + const std::vector iv(AES_BLOCKSIZE, 0); + const std::vector plaintext(128, 0); + std::vector cyphertext(128, 0); + + while (state.KeepRunning()) { + AES256CBCEncrypt(key.data(), iv.data(), false) + .Encrypt(plaintext.data(), plaintext.size(), cyphertext.data()); + } +} + +static void AES256CBC_DecryptNoPad(benchmark::State &state) { + const std::vector key(AES256_KEYSIZE, 0); + const std::vector iv(AES_BLOCKSIZE, 0); + const std::vector cyphertext(128, 0); + std::vector plaintext(128, 0); + + while (state.KeepRunning()) { + AES256CBCDecrypt(key.data(), iv.data(), false) + .Decrypt(cyphertext.data(), cyphertext.size(), plaintext.data()); + } +} + +static void AES256CBC_EncryptWithPad(benchmark::State &state) { + const std::vector key(AES256_KEYSIZE, 0); + const std::vector iv(AES_BLOCKSIZE, 0); + const std::vector plaintext(128, 0); + std::vector cyphertext(128 + AES_BLOCKSIZE, 0); + + while (state.KeepRunning()) { + AES256CBCEncrypt(key.data(), iv.data(), true) + .Encrypt(plaintext.data(), plaintext.size(), cyphertext.data()); + } +} + +static void AES256CBC_DecryptWithPad(benchmark::State &state) { + const std::vector key(AES256_KEYSIZE, 0); + const std::vector iv(AES_BLOCKSIZE, 0); + const std::vector cyphertext(128, 0); + std::vector plaintext(128 + AES_BLOCKSIZE, 0); + + while (state.KeepRunning()) { + AES256CBCDecrypt(key.data(), iv.data(), true) + .Decrypt(cyphertext.data(), cyphertext.size(), plaintext.data()); + } +} + +BENCHMARK(AES128_Encrypt, BENCH_AES128_ITERATION); +BENCHMARK(AES128_Decrypt, BENCH_AES128_ITERATION); +BENCHMARK(AES256_Encrypt, BENCH_AES256_ITERATION); +BENCHMARK(AES256_Decrypt, BENCH_AES256_ITERATION); +BENCHMARK(AES128CBC_EncryptNoPad, BENCH_AES128CBC_ITERATION); +BENCHMARK(AES128CBC_DecryptNoPad, BENCH_AES128CBC_ITERATION); +BENCHMARK(AES128CBC_EncryptWithPad, BENCH_AES128CBC_ITERATION); +BENCHMARK(AES128CBC_DecryptWithPad, BENCH_AES128CBC_ITERATION); +BENCHMARK(AES256CBC_EncryptNoPad, BENCH_AES256CBC_ITERATION); +BENCHMARK(AES256CBC_DecryptNoPad, BENCH_AES256CBC_ITERATION); +BENCHMARK(AES256CBC_EncryptWithPad, BENCH_AES256CBC_ITERATION); +BENCHMARK(AES256CBC_DecryptWithPad, BENCH_AES256CBC_ITERATION);