From 2a7be678a660d58742b1e767444c0fe75fa22592 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Sun, 17 Oct 2021 12:08:06 -0400 Subject: [PATCH] Combine bench_sign and bench_verify into single bench --- .gitignore | 4 +-- Makefile.am | 8 ++--- ci/cirrus.sh | 5 ++- src/{bench_verify.c => bench.c} | 45 +++++++++++++++++++++++++ src/bench_sign.c | 60 --------------------------------- 5 files changed, 51 insertions(+), 71 deletions(-) rename src/{bench_verify.c => bench.c} (67%) delete mode 100644 src/bench_sign.c diff --git a/.gitignore b/.gitignore index aa7009af1..2ed952579 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,7 @@ -bench_inv +bench bench_ecdh bench_ecmult bench_schnorrsig -bench_sign -bench_verify bench_recover bench_internal tests diff --git a/Makefile.am b/Makefile.am index a04ad05e8..aa8014f5b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -81,11 +81,9 @@ endif noinst_PROGRAMS = if USE_BENCHMARK -noinst_PROGRAMS += bench_verify bench_sign bench_internal bench_ecmult -bench_verify_SOURCES = src/bench_verify.c -bench_verify_LDADD = libsecp256k1.la $(SECP_LIBS) $(SECP_TEST_LIBS) $(COMMON_LIB) -bench_sign_SOURCES = src/bench_sign.c -bench_sign_LDADD = libsecp256k1.la $(SECP_LIBS) $(SECP_TEST_LIBS) $(COMMON_LIB) +noinst_PROGRAMS += bench bench_internal bench_ecmult +bench_SOURCES = src/bench.c +bench_LDADD = libsecp256k1.la $(SECP_LIBS) $(SECP_TEST_LIBS) $(COMMON_LIB) bench_internal_SOURCES = src/bench_internal.c bench_internal_LDADD = $(SECP_LIBS) $(COMMON_LIB) bench_internal_CPPFLAGS = $(SECP_INCLUDES) diff --git a/ci/cirrus.sh b/ci/cirrus.sh index 27db1e677..a87494314 100755 --- a/ci/cirrus.sh +++ b/ci/cirrus.sh @@ -26,7 +26,7 @@ make # Print information about binaries so that we can see that the architecture is correct file *tests* || true -file bench_* || true +file bench* || true file .libs/* || true # This tells `make check` to wrap test invocations. @@ -49,8 +49,7 @@ then { $EXEC ./bench_ecmult $EXEC ./bench_internal - $EXEC ./bench_sign - $EXEC ./bench_verify + $EXEC ./bench } >> bench.log 2>&1 if [ "$RECOVERY" = "yes" ] then diff --git a/src/bench_verify.c b/src/bench.c similarity index 67% rename from src/bench_verify.c rename to src/bench.c index d62e34b77..704c7c09c 100644 --- a/src/bench_verify.c +++ b/src/bench.c @@ -40,6 +40,42 @@ static void bench_verify(void* arg, int iters) { } } +typedef struct { + secp256k1_context* ctx; + unsigned char msg[32]; + unsigned char key[32]; +} bench_sign_data; + +static void bench_sign_setup(void* arg) { + int i; + bench_sign_data *data = (bench_sign_data*)arg; + + for (i = 0; i < 32; i++) { + data->msg[i] = i + 1; + } + for (i = 0; i < 32; i++) { + data->key[i] = i + 65; + } +} + +static void bench_sign_run(void* arg, int iters) { + int i; + bench_sign_data *data = (bench_sign_data*)arg; + + unsigned char sig[74]; + for (i = 0; i < iters; i++) { + size_t siglen = 74; + int j; + secp256k1_ecdsa_signature signature; + CHECK(secp256k1_ecdsa_sign(data->ctx, &signature, data->msg, data->key, NULL, NULL)); + CHECK(secp256k1_ecdsa_signature_serialize_der(data->ctx, sig, &siglen, &signature)); + for (j = 0; j < 32; j++) { + data->msg[j] = sig[j]; + data->key[j] = sig[j + 32]; + } + } +} + int main(void) { int i; secp256k1_pubkey pubkey; @@ -48,6 +84,7 @@ int main(void) { int iters = get_iters(20000); + /* ECDSA verification benchmark */ data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY); for (i = 0; i < 32; i++) { @@ -67,5 +104,13 @@ int main(void) { run_benchmark("ecdsa_verify", bench_verify, NULL, NULL, &data, 10, iters); secp256k1_context_destroy(data.ctx); + + /* ECDSA signing benchmark */ + data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN); + + run_benchmark("ecdsa_sign", bench_sign_run, bench_sign_setup, NULL, &data, 10, iters); + + secp256k1_context_destroy(data.ctx); + return 0; } diff --git a/src/bench_sign.c b/src/bench_sign.c deleted file mode 100644 index c44f7b10b..000000000 --- a/src/bench_sign.c +++ /dev/null @@ -1,60 +0,0 @@ -/*********************************************************************** - * Copyright (c) 2014 Pieter Wuille * - * Distributed under the MIT software license, see the accompanying * - * file COPYING or https://www.opensource.org/licenses/mit-license.php.* - ***********************************************************************/ - -#include "../include/secp256k1.h" -#include "util.h" -#include "bench.h" - -typedef struct { - secp256k1_context* ctx; - unsigned char msg[32]; - unsigned char key[32]; -} bench_sign_data; - -static void bench_sign_setup(void* arg) { - int i; - bench_sign_data *data = (bench_sign_data*)arg; - - for (i = 0; i < 32; i++) { - data->msg[i] = i + 1; - } - for (i = 0; i < 32; i++) { - data->key[i] = i + 65; - } -} - -static void bench_sign_run(void* arg, int iters) { - int i; - bench_sign_data *data = (bench_sign_data*)arg; - - unsigned char sig[74]; - for (i = 0; i < iters; i++) { - size_t siglen = 74; - int j; - secp256k1_ecdsa_signature signature; - CHECK(secp256k1_ecdsa_sign(data->ctx, &signature, data->msg, data->key, NULL, NULL)); - CHECK(secp256k1_ecdsa_signature_serialize_der(data->ctx, sig, &siglen, &signature)); - for (j = 0; j < 32; j++) { - data->msg[j] = sig[j]; - data->key[j] = sig[j + 32]; - } - } -} - -int main(void) { - bench_sign_data data; - - int iters = get_iters(20000); - - data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN); - - print_output_table_header_row(); - - run_benchmark("ecdsa_sign", bench_sign_run, bench_sign_setup, NULL, &data, 10, iters); - - secp256k1_context_destroy(data.ctx); - return 0; -}