From 6f7b07686241fb32857a6123a2c307bcb7c2e1f0 Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Sat, 6 Jun 2020 09:36:54 -0400 Subject: Minor cleanups to timing_test --- src/cli/timing_tests.cpp | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) (limited to 'src/cli') diff --git a/src/cli/timing_tests.cpp b/src/cli/timing_tests.cpp index 01dd61596..2a07fe164 100644 --- a/src/cli/timing_tests.cpp +++ b/src/cli/timing_tests.cpp @@ -80,18 +80,19 @@ class Timing_Test size_t warmup_runs, size_t measurement_runs); - virtual std::vector prepare_input(std::string input) + virtual std::vector prepare_input(const std::string& input) { return Botan::hex_decode(input); } - virtual ticks measure_critical_function(std::vector input) = 0; + virtual ticks measure_critical_function(const std::vector& input) = 0; protected: static ticks get_ticks() { // Returns CPU counter or best approximation (monotonic clock of some kind) - return Botan::OS::get_high_resolution_clock(); + //return Botan::OS::get_high_resolution_clock(); + return Botan::OS::get_system_timestamp_ns(); } Botan::RandomNumberGenerator& timing_test_rng() @@ -114,14 +115,14 @@ class Bleichenbacker_Timing_Test final : public Timing_Test , m_enc(m_pubkey, timing_test_rng(), "Raw") , m_dec(m_privkey, timing_test_rng(), "PKCS1v15") {} - std::vector prepare_input(std::string input) override + std::vector prepare_input(const std::string& input) override { const std::vector input_vector = Botan::hex_decode(input); const std::vector encrypted = m_enc.encrypt(input_vector, timing_test_rng()); return encrypted; } - ticks measure_critical_function(std::vector input) override + ticks measure_critical_function(const std::vector& input) override { const ticks start = get_ticks(); m_dec.decrypt_or_random(input.data(), m_ctext_length, m_expected_content_size, timing_test_rng()); @@ -158,14 +159,14 @@ class Manger_Timing_Test final : public Timing_Test , m_enc(m_pubkey, timing_test_rng(), m_encrypt_padding) , m_dec(m_privkey, timing_test_rng(), m_decrypt_padding) {} - std::vector prepare_input(std::string input) override + std::vector prepare_input(const std::string& input) override { const std::vector input_vector = Botan::hex_decode(input); const std::vector encrypted = m_enc.encrypt(input_vector, timing_test_rng()); return encrypted; } - ticks measure_critical_function(std::vector input) override + ticks measure_critical_function(const std::vector& input) override { ticks start = get_ticks(); try @@ -207,8 +208,8 @@ class Lucky13_Timing_Test final : public Timing_Test Botan::MessageAuthenticationCode::create_or_throw("HMAC(" + m_mac_algo + ")"), 16, m_mac_keylen, Botan::TLS::Protocol_Version::TLS_V11, false) {} - std::vector prepare_input(std::string input) override; - ticks measure_critical_function(std::vector input) override; + std::vector prepare_input(const std::string& input) override; + ticks measure_critical_function(const std::vector& input) override; private: const std::string m_mac_algo; @@ -216,7 +217,7 @@ class Lucky13_Timing_Test final : public Timing_Test Botan::TLS::TLS_CBC_HMAC_AEAD_Decryption m_dec; }; -std::vector Lucky13_Timing_Test::prepare_input(std::string input) +std::vector Lucky13_Timing_Test::prepare_input(const std::string& input) { const std::vector input_vector = Botan::hex_decode(input); const std::vector key(16); @@ -231,7 +232,7 @@ std::vector Lucky13_Timing_Test::prepare_input(std::string input) return unlock(buf); } -ticks Lucky13_Timing_Test::measure_critical_function(std::vector input) +ticks Lucky13_Timing_Test::measure_critical_function(const std::vector& input) { Botan::secure_vector data(input.begin(), input.end()); Botan::secure_vector aad(13); @@ -263,7 +264,7 @@ class ECDSA_Timing_Test final : public Timing_Test public: ECDSA_Timing_Test(std::string ecgroup); - ticks measure_critical_function(std::vector input) override; + ticks measure_critical_function(const std::vector& input) override; private: const Botan::EC_Group m_group; @@ -278,7 +279,7 @@ ECDSA_Timing_Test::ECDSA_Timing_Test(std::string ecgroup) , m_x(m_privkey.private_value()) {} -ticks ECDSA_Timing_Test::measure_critical_function(std::vector input) +ticks ECDSA_Timing_Test::measure_critical_function(const std::vector& input) { const Botan::BigInt k(input.data(), input.size()); const Botan::BigInt msg(5); // fixed message to minimize noise @@ -310,14 +311,14 @@ class ECC_Mul_Timing_Test final : public Timing_Test m_group(ecgroup) {} - ticks measure_critical_function(std::vector input) override; + ticks measure_critical_function(const std::vector& input) override; private: const Botan::EC_Group m_group; std::vector m_ws; }; -ticks ECC_Mul_Timing_Test::measure_critical_function(std::vector input) +ticks ECC_Mul_Timing_Test::measure_critical_function(const std::vector& input) { const Botan::BigInt k(input.data(), input.size()); @@ -341,12 +342,12 @@ class Powmod_Timing_Test final : public Timing_Test { } - ticks measure_critical_function(std::vector input) override; + ticks measure_critical_function(const std::vector& input) override; private: Botan::DL_Group m_group; }; -ticks Powmod_Timing_Test::measure_critical_function(std::vector input) +ticks Powmod_Timing_Test::measure_critical_function(const std::vector& input) { const Botan::BigInt x(input.data(), input.size()); const size_t max_x_bits = m_group.p_bits(); @@ -372,13 +373,13 @@ class Invmod_Timing_Test final : public Timing_Test m_p = Botan::random_prime(timing_test_rng(), p_bits); } - ticks measure_critical_function(std::vector input) override; + ticks measure_critical_function(const std::vector& input) override; private: Botan::BigInt m_p; }; -ticks Invmod_Timing_Test::measure_critical_function(std::vector input) +ticks Invmod_Timing_Test::measure_critical_function(const std::vector& input) { const Botan::BigInt k(input.data(), input.size()); @@ -417,10 +418,10 @@ std::vector> Timing_Test::execute_evaluation( } size_t total_runs = 0; + std::vector results(inputs.size()); + while(total_runs < (warmup_runs + measurement_runs)) { - std::vector results(inputs.size()); - for(size_t i = 0; i != inputs.size(); ++i) { results[i] = measure_critical_function(inputs[i]); @@ -428,7 +429,7 @@ std::vector> Timing_Test::execute_evaluation( total_runs++; - if(total_runs >= warmup_runs) + if(total_runs > warmup_runs) { for(size_t i = 0; i != results.size(); ++i) { @@ -445,7 +446,7 @@ class Timing_Test_Command final : public Command public: Timing_Test_Command() : Command("timing_test test_type --test-data-file= --test-data-dir=src/tests/data/timing " - "--warmup-runs=1000 --measurement-runs=10000") {} + "--warmup-runs=5000 --measurement-runs=50000") {} std::string group() const override { -- cgit v1.2.3