Skip to content

Commit

Permalink
Merge bitcoin-core/secp256k1#991: Merge all "external" benchmarks int…
Browse files Browse the repository at this point in the history
…o a single bench binary

af6abcb Make bench support selecting which benchmarks to run (Pieter Wuille)
9f56bdf Merge bench_schnorrsig into bench (Pieter Wuille)
3208557 Merge bench_recover into bench (Pieter Wuille)
855e18d Merge bench_ecdh into bench (Pieter Wuille)
2a7be67 Combine bench_sign and bench_verify into single bench (Pieter Wuille)

Pull request description:

  This combines `bench_verify`, `bench_sign`, `bench_ecdh`, `bench_recovery`, and `bench_schnorrsig` into a single `bench` binary.

  I don't think there is a good reason to have this many binaries, and it complicates build config and CI.

ACKs for top commit:
  real-or-random:
    ACK af6abcb diff looks good, command line options work, valgrind is happy
  siv2r:
    tACK af6abcb, the command-line options work as expected on my ubuntu machine. The diff looks good.

Tree-SHA512: 39c099b508c70136eaac8a429591b4250a8c22e423fa922d83928ea9273d8f2c1136317427563b28f249c02cf83d1c73ea787c6d26aa88545236241641965705
  • Loading branch information
real-or-random committed Nov 8, 2021
2 parents 8fa4120 + af6abcb commit 515a5db
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 137 deletions.
7 changes: 1 addition & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
bench_inv
bench_ecdh
bench
bench_ecmult
bench_schnorrsig
bench_sign
bench_verify
bench_recover
bench_internal
tests
exhaustive_tests
Expand Down
8 changes: 3 additions & 5 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 2 additions & 15 deletions ci/cirrus.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -49,21 +49,8 @@ then
{
$EXEC ./bench_ecmult
$EXEC ./bench_internal
$EXEC ./bench_sign
$EXEC ./bench_verify
$EXEC ./bench
} >> bench.log 2>&1
if [ "$RECOVERY" = "yes" ]
then
$EXEC ./bench_recover >> bench.log 2>&1
fi
if [ "$ECDH" = "yes" ]
then
$EXEC ./bench_ecdh >> bench.log 2>&1
fi
if [ "$SCHNORRSIG" = "yes" ]
then
$EXEC ./bench_schnorrsig >> bench.log 2>&1
fi
fi
if [ "$CTIMETEST" = "yes" ]
then
Expand Down
77 changes: 75 additions & 2 deletions src/bench_verify.c → src/bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,64 @@ static void bench_verify(void* arg, int iters) {
}
}

int main(void) {
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];
}
}
}

#ifdef ENABLE_MODULE_ECDH
# include "modules/ecdh/bench_impl.h"
#endif

#ifdef ENABLE_MODULE_RECOVERY
# include "modules/recovery/bench_impl.h"
#endif

#ifdef ENABLE_MODULE_SCHNORRSIG
# include "modules/schnorrsig/bench_impl.h"
#endif

int main(int argc, char** argv) {
int i;
secp256k1_pubkey pubkey;
secp256k1_ecdsa_signature sig;
bench_verify_data data;

int d = argc == 1;
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++) {
Expand All @@ -64,8 +114,31 @@ int main(void) {
CHECK(secp256k1_ec_pubkey_serialize(data.ctx, data.pubkey, &data.pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED) == 1);

print_output_table_header_row();
run_benchmark("ecdsa_verify", bench_verify, NULL, NULL, &data, 10, iters);
if (d || have_flag(argc, argv, "ecdsa") || have_flag(argc, argv, "verify") || have_flag(argc, argv, "ecdsa_verify")) 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);

if (d || have_flag(argc, argv, "ecdsa") || have_flag(argc, argv, "sign") || have_flag(argc, argv, "ecdsa_sign")) run_benchmark("ecdsa_sign", bench_sign_run, bench_sign_setup, NULL, &data, 10, iters);

secp256k1_context_destroy(data.ctx);

#ifdef ENABLE_MODULE_ECDH
/* ECDH benchmarks */
run_ecdh_bench(iters, argc, argv);
#endif

#ifdef ENABLE_MODULE_RECOVERY
/* ECDSA recovery benchmarks */
run_recovery_bench(iters, argc, argv);
#endif

#ifdef ENABLE_MODULE_SCHNORRSIG
/* Schnorr signature benchmarks */
run_schnorrsig_bench(iters, argc, argv);
#endif

return 0;
}
60 changes: 0 additions & 60 deletions src/bench_sign.c

This file was deleted.

6 changes: 1 addition & 5 deletions src/modules/ecdh/Makefile.am.include
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
include_HEADERS += include/secp256k1_ecdh.h
noinst_HEADERS += src/modules/ecdh/main_impl.h
noinst_HEADERS += src/modules/ecdh/tests_impl.h
if USE_BENCHMARK
noinst_PROGRAMS += bench_ecdh
bench_ecdh_SOURCES = src/bench_ecdh.c
bench_ecdh_LDADD = libsecp256k1.la $(SECP_LIBS) $(COMMON_LIB)
endif
noinst_HEADERS += src/modules/ecdh/bench_impl.h
18 changes: 7 additions & 11 deletions src/bench_ecdh.c → src/modules/ecdh/bench_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
***********************************************************************/

#include <string.h>
#ifndef SECP256K1_MODULE_ECDH_BENCH_H
#define SECP256K1_MODULE_ECDH_BENCH_H

#include "../include/secp256k1.h"
#include "../include/secp256k1_ecdh.h"
#include "util.h"
#include "bench.h"

typedef struct {
secp256k1_context *ctx;
Expand Down Expand Up @@ -44,18 +42,16 @@ static void bench_ecdh(void* arg, int iters) {
}
}

int main(void) {
void run_ecdh_bench(int iters, int argc, char** argv) {
bench_ecdh_data data;

int iters = get_iters(20000);
int d = argc == 1;

/* create a context with no capabilities */
data.ctx = secp256k1_context_create(SECP256K1_FLAGS_TYPE_CONTEXT);

print_output_table_header_row();

run_benchmark("ecdh", bench_ecdh, bench_ecdh_setup, NULL, &data, 10, iters);
if (d || have_flag(argc, argv, "ecdh")) run_benchmark("ecdh", bench_ecdh, bench_ecdh_setup, NULL, &data, 10, iters);

secp256k1_context_destroy(data.ctx);
return 0;
}

#endif /* SECP256K1_MODULE_ECDH_BENCH_H */
6 changes: 1 addition & 5 deletions src/modules/recovery/Makefile.am.include
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@ include_HEADERS += include/secp256k1_recovery.h
noinst_HEADERS += src/modules/recovery/main_impl.h
noinst_HEADERS += src/modules/recovery/tests_impl.h
noinst_HEADERS += src/modules/recovery/tests_exhaustive_impl.h
if USE_BENCHMARK
noinst_PROGRAMS += bench_recover
bench_recover_SOURCES = src/bench_recover.c
bench_recover_LDADD = libsecp256k1.la $(SECP_LIBS) $(COMMON_LIB)
endif
noinst_HEADERS += src/modules/recovery/bench_impl.h
18 changes: 8 additions & 10 deletions src/bench_recover.c → src/modules/recovery/bench_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
***********************************************************************/

#include "../include/secp256k1.h"
#ifndef SECP256K1_MODULE_RECOVERY_BENCH_H
#define SECP256K1_MODULE_RECOVERY_BENCH_H

#include "../include/secp256k1_recovery.h"
#include "util.h"
#include "bench.h"

typedef struct {
secp256k1_context *ctx;
Expand Down Expand Up @@ -48,17 +48,15 @@ void bench_recover_setup(void* arg) {
}
}

int main(void) {
void run_recovery_bench(int iters, int argc, char** argv) {
bench_recover_data data;

int iters = get_iters(20000);
int d = argc == 1;

data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);

print_output_table_header_row();

run_benchmark("ecdsa_recover", bench_recover, bench_recover_setup, NULL, &data, 10, iters);
if (d || have_flag(argc, argv, "ecdsa") || have_flag(argc, argv, "recover") || have_flag(argc, argv, "ecdsa_recover")) run_benchmark("ecdsa_recover", bench_recover, bench_recover_setup, NULL, &data, 10, iters);

secp256k1_context_destroy(data.ctx);
return 0;
}

#endif /* SECP256K1_MODULE_RECOVERY_BENCH_H */
6 changes: 1 addition & 5 deletions src/modules/schnorrsig/Makefile.am.include
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@ include_HEADERS += include/secp256k1_schnorrsig.h
noinst_HEADERS += src/modules/schnorrsig/main_impl.h
noinst_HEADERS += src/modules/schnorrsig/tests_impl.h
noinst_HEADERS += src/modules/schnorrsig/tests_exhaustive_impl.h
if USE_BENCHMARK
noinst_PROGRAMS += bench_schnorrsig
bench_schnorrsig_SOURCES = src/bench_schnorrsig.c
bench_schnorrsig_LDADD = libsecp256k1.la $(SECP_LIBS) $(COMMON_LIB)
endif
noinst_HEADERS += src/modules/schnorrsig/bench_impl.h
22 changes: 9 additions & 13 deletions src/bench_schnorrsig.c → src/modules/schnorrsig/bench_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
***********************************************************************/

#include <string.h>
#include <stdlib.h>
#ifndef SECP256K1_MODULE_SCHNORRSIG_BENCH_H
#define SECP256K1_MODULE_SCHNORRSIG_BENCH_H


#include "../include/secp256k1.h"
#include "../include/secp256k1_schnorrsig.h"
#include "util.h"
#include "bench.h"
#include "../../../include/secp256k1_schnorrsig.h"

#define MSGLEN 32

Expand Down Expand Up @@ -49,10 +45,10 @@ void bench_schnorrsig_verify(void* arg, int iters) {
}
}

int main(void) {
void run_schnorrsig_bench(int iters, int argc, char** argv) {
int i;
bench_schnorrsig_data data;
int iters = get_iters(10000);
int d = argc == 1;

data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN);
data.keypairs = (const secp256k1_keypair **)malloc(iters * sizeof(secp256k1_keypair *));
Expand Down Expand Up @@ -86,9 +82,8 @@ int main(void) {
CHECK(secp256k1_xonly_pubkey_serialize(data.ctx, pk_char, &pk) == 1);
}

print_output_table_header_row();
run_benchmark("schnorrsig_sign", bench_schnorrsig_sign, NULL, NULL, (void *) &data, 10, iters);
run_benchmark("schnorrsig_verify", bench_schnorrsig_verify, NULL, NULL, (void *) &data, 10, iters);
if (d || have_flag(argc, argv, "schnorrsig") || have_flag(argc, argv, "sign") || have_flag(argc, argv, "schnorrsig_sign")) run_benchmark("schnorrsig_sign", bench_schnorrsig_sign, NULL, NULL, (void *) &data, 10, iters);
if (d || have_flag(argc, argv, "schnorrsig") || have_flag(argc, argv, "verify") || have_flag(argc, argv, "schnorrsig_verify")) run_benchmark("schnorrsig_verify", bench_schnorrsig_verify, NULL, NULL, (void *) &data, 10, iters);

for (i = 0; i < iters; i++) {
free((void *)data.keypairs[i]);
Expand All @@ -102,5 +97,6 @@ int main(void) {
free(data.sigs);

secp256k1_context_destroy(data.ctx);
return 0;
}

#endif

0 comments on commit 515a5db

Please sign in to comment.