Skip to content

Commit 515a5db

Browse files
Merge bitcoin-core/secp256k1#991: Merge all "external" benchmarks into 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
2 parents 8fa4120 + af6abcb commit 515a5db

11 files changed

+108
-137
lines changed

.gitignore

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
bench_inv
2-
bench_ecdh
1+
bench
32
bench_ecmult
4-
bench_schnorrsig
5-
bench_sign
6-
bench_verify
7-
bench_recover
83
bench_internal
94
tests
105
exhaustive_tests

Makefile.am

+3-5
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,9 @@ endif
8181

8282
noinst_PROGRAMS =
8383
if USE_BENCHMARK
84-
noinst_PROGRAMS += bench_verify bench_sign bench_internal bench_ecmult
85-
bench_verify_SOURCES = src/bench_verify.c
86-
bench_verify_LDADD = libsecp256k1.la $(SECP_LIBS) $(SECP_TEST_LIBS) $(COMMON_LIB)
87-
bench_sign_SOURCES = src/bench_sign.c
88-
bench_sign_LDADD = libsecp256k1.la $(SECP_LIBS) $(SECP_TEST_LIBS) $(COMMON_LIB)
84+
noinst_PROGRAMS += bench bench_internal bench_ecmult
85+
bench_SOURCES = src/bench.c
86+
bench_LDADD = libsecp256k1.la $(SECP_LIBS) $(SECP_TEST_LIBS) $(COMMON_LIB)
8987
bench_internal_SOURCES = src/bench_internal.c
9088
bench_internal_LDADD = $(SECP_LIBS) $(COMMON_LIB)
9189
bench_internal_CPPFLAGS = $(SECP_INCLUDES)

ci/cirrus.sh

+2-15
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ make
2626

2727
# Print information about binaries so that we can see that the architecture is correct
2828
file *tests* || true
29-
file bench_* || true
29+
file bench* || true
3030
file .libs/* || true
3131

3232
# This tells `make check` to wrap test invocations.
@@ -49,21 +49,8 @@ then
4949
{
5050
$EXEC ./bench_ecmult
5151
$EXEC ./bench_internal
52-
$EXEC ./bench_sign
53-
$EXEC ./bench_verify
52+
$EXEC ./bench
5453
} >> bench.log 2>&1
55-
if [ "$RECOVERY" = "yes" ]
56-
then
57-
$EXEC ./bench_recover >> bench.log 2>&1
58-
fi
59-
if [ "$ECDH" = "yes" ]
60-
then
61-
$EXEC ./bench_ecdh >> bench.log 2>&1
62-
fi
63-
if [ "$SCHNORRSIG" = "yes" ]
64-
then
65-
$EXEC ./bench_schnorrsig >> bench.log 2>&1
66-
fi
6754
fi
6855
if [ "$CTIMETEST" = "yes" ]
6956
then

src/bench_verify.c src/bench.c

+75-2
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,64 @@ static void bench_verify(void* arg, int iters) {
4040
}
4141
}
4242

43-
int main(void) {
43+
typedef struct {
44+
secp256k1_context* ctx;
45+
unsigned char msg[32];
46+
unsigned char key[32];
47+
} bench_sign_data;
48+
49+
static void bench_sign_setup(void* arg) {
50+
int i;
51+
bench_sign_data *data = (bench_sign_data*)arg;
52+
53+
for (i = 0; i < 32; i++) {
54+
data->msg[i] = i + 1;
55+
}
56+
for (i = 0; i < 32; i++) {
57+
data->key[i] = i + 65;
58+
}
59+
}
60+
61+
static void bench_sign_run(void* arg, int iters) {
62+
int i;
63+
bench_sign_data *data = (bench_sign_data*)arg;
64+
65+
unsigned char sig[74];
66+
for (i = 0; i < iters; i++) {
67+
size_t siglen = 74;
68+
int j;
69+
secp256k1_ecdsa_signature signature;
70+
CHECK(secp256k1_ecdsa_sign(data->ctx, &signature, data->msg, data->key, NULL, NULL));
71+
CHECK(secp256k1_ecdsa_signature_serialize_der(data->ctx, sig, &siglen, &signature));
72+
for (j = 0; j < 32; j++) {
73+
data->msg[j] = sig[j];
74+
data->key[j] = sig[j + 32];
75+
}
76+
}
77+
}
78+
79+
#ifdef ENABLE_MODULE_ECDH
80+
# include "modules/ecdh/bench_impl.h"
81+
#endif
82+
83+
#ifdef ENABLE_MODULE_RECOVERY
84+
# include "modules/recovery/bench_impl.h"
85+
#endif
86+
87+
#ifdef ENABLE_MODULE_SCHNORRSIG
88+
# include "modules/schnorrsig/bench_impl.h"
89+
#endif
90+
91+
int main(int argc, char** argv) {
4492
int i;
4593
secp256k1_pubkey pubkey;
4694
secp256k1_ecdsa_signature sig;
4795
bench_verify_data data;
4896

97+
int d = argc == 1;
4998
int iters = get_iters(20000);
5099

100+
/* ECDSA verification benchmark */
51101
data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
52102

53103
for (i = 0; i < 32; i++) {
@@ -64,8 +114,31 @@ int main(void) {
64114
CHECK(secp256k1_ec_pubkey_serialize(data.ctx, data.pubkey, &data.pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
65115

66116
print_output_table_header_row();
67-
run_benchmark("ecdsa_verify", bench_verify, NULL, NULL, &data, 10, iters);
117+
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);
118+
119+
secp256k1_context_destroy(data.ctx);
120+
121+
/* ECDSA signing benchmark */
122+
data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
123+
124+
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);
68125

69126
secp256k1_context_destroy(data.ctx);
127+
128+
#ifdef ENABLE_MODULE_ECDH
129+
/* ECDH benchmarks */
130+
run_ecdh_bench(iters, argc, argv);
131+
#endif
132+
133+
#ifdef ENABLE_MODULE_RECOVERY
134+
/* ECDSA recovery benchmarks */
135+
run_recovery_bench(iters, argc, argv);
136+
#endif
137+
138+
#ifdef ENABLE_MODULE_SCHNORRSIG
139+
/* Schnorr signature benchmarks */
140+
run_schnorrsig_bench(iters, argc, argv);
141+
#endif
142+
70143
return 0;
71144
}

src/bench_sign.c

-60
This file was deleted.

src/modules/ecdh/Makefile.am.include

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
include_HEADERS += include/secp256k1_ecdh.h
22
noinst_HEADERS += src/modules/ecdh/main_impl.h
33
noinst_HEADERS += src/modules/ecdh/tests_impl.h
4-
if USE_BENCHMARK
5-
noinst_PROGRAMS += bench_ecdh
6-
bench_ecdh_SOURCES = src/bench_ecdh.c
7-
bench_ecdh_LDADD = libsecp256k1.la $(SECP_LIBS) $(COMMON_LIB)
8-
endif
4+
noinst_HEADERS += src/modules/ecdh/bench_impl.h

src/bench_ecdh.c src/modules/ecdh/bench_impl.h

+7-11
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
55
***********************************************************************/
66

7-
#include <string.h>
7+
#ifndef SECP256K1_MODULE_ECDH_BENCH_H
8+
#define SECP256K1_MODULE_ECDH_BENCH_H
89

9-
#include "../include/secp256k1.h"
1010
#include "../include/secp256k1_ecdh.h"
11-
#include "util.h"
12-
#include "bench.h"
1311

1412
typedef struct {
1513
secp256k1_context *ctx;
@@ -44,18 +42,16 @@ static void bench_ecdh(void* arg, int iters) {
4442
}
4543
}
4644

47-
int main(void) {
45+
void run_ecdh_bench(int iters, int argc, char** argv) {
4846
bench_ecdh_data data;
49-
50-
int iters = get_iters(20000);
47+
int d = argc == 1;
5148

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

55-
print_output_table_header_row();
56-
57-
run_benchmark("ecdh", bench_ecdh, bench_ecdh_setup, NULL, &data, 10, iters);
52+
if (d || have_flag(argc, argv, "ecdh")) run_benchmark("ecdh", bench_ecdh, bench_ecdh_setup, NULL, &data, 10, iters);
5853

5954
secp256k1_context_destroy(data.ctx);
60-
return 0;
6155
}
56+
57+
#endif /* SECP256K1_MODULE_ECDH_BENCH_H */

src/modules/recovery/Makefile.am.include

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,4 @@ include_HEADERS += include/secp256k1_recovery.h
22
noinst_HEADERS += src/modules/recovery/main_impl.h
33
noinst_HEADERS += src/modules/recovery/tests_impl.h
44
noinst_HEADERS += src/modules/recovery/tests_exhaustive_impl.h
5-
if USE_BENCHMARK
6-
noinst_PROGRAMS += bench_recover
7-
bench_recover_SOURCES = src/bench_recover.c
8-
bench_recover_LDADD = libsecp256k1.la $(SECP_LIBS) $(COMMON_LIB)
9-
endif
5+
noinst_HEADERS += src/modules/recovery/bench_impl.h

src/bench_recover.c src/modules/recovery/bench_impl.h

+8-10
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
55
***********************************************************************/
66

7-
#include "../include/secp256k1.h"
7+
#ifndef SECP256K1_MODULE_RECOVERY_BENCH_H
8+
#define SECP256K1_MODULE_RECOVERY_BENCH_H
9+
810
#include "../include/secp256k1_recovery.h"
9-
#include "util.h"
10-
#include "bench.h"
1111

1212
typedef struct {
1313
secp256k1_context *ctx;
@@ -48,17 +48,15 @@ void bench_recover_setup(void* arg) {
4848
}
4949
}
5050

51-
int main(void) {
51+
void run_recovery_bench(int iters, int argc, char** argv) {
5252
bench_recover_data data;
53-
54-
int iters = get_iters(20000);
53+
int d = argc == 1;
5554

5655
data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
5756

58-
print_output_table_header_row();
59-
60-
run_benchmark("ecdsa_recover", bench_recover, bench_recover_setup, NULL, &data, 10, iters);
57+
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);
6158

6259
secp256k1_context_destroy(data.ctx);
63-
return 0;
6460
}
61+
62+
#endif /* SECP256K1_MODULE_RECOVERY_BENCH_H */

src/modules/schnorrsig/Makefile.am.include

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,4 @@ include_HEADERS += include/secp256k1_schnorrsig.h
22
noinst_HEADERS += src/modules/schnorrsig/main_impl.h
33
noinst_HEADERS += src/modules/schnorrsig/tests_impl.h
44
noinst_HEADERS += src/modules/schnorrsig/tests_exhaustive_impl.h
5-
if USE_BENCHMARK
6-
noinst_PROGRAMS += bench_schnorrsig
7-
bench_schnorrsig_SOURCES = src/bench_schnorrsig.c
8-
bench_schnorrsig_LDADD = libsecp256k1.la $(SECP_LIBS) $(COMMON_LIB)
9-
endif
5+
noinst_HEADERS += src/modules/schnorrsig/bench_impl.h

src/bench_schnorrsig.c src/modules/schnorrsig/bench_impl.h

+9-13
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@
44
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
55
***********************************************************************/
66

7-
#include <string.h>
8-
#include <stdlib.h>
7+
#ifndef SECP256K1_MODULE_SCHNORRSIG_BENCH_H
8+
#define SECP256K1_MODULE_SCHNORRSIG_BENCH_H
99

10-
11-
#include "../include/secp256k1.h"
12-
#include "../include/secp256k1_schnorrsig.h"
13-
#include "util.h"
14-
#include "bench.h"
10+
#include "../../../include/secp256k1_schnorrsig.h"
1511

1612
#define MSGLEN 32
1713

@@ -49,10 +45,10 @@ void bench_schnorrsig_verify(void* arg, int iters) {
4945
}
5046
}
5147

52-
int main(void) {
48+
void run_schnorrsig_bench(int iters, int argc, char** argv) {
5349
int i;
5450
bench_schnorrsig_data data;
55-
int iters = get_iters(10000);
51+
int d = argc == 1;
5652

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

89-
print_output_table_header_row();
90-
run_benchmark("schnorrsig_sign", bench_schnorrsig_sign, NULL, NULL, (void *) &data, 10, iters);
91-
run_benchmark("schnorrsig_verify", bench_schnorrsig_verify, NULL, NULL, (void *) &data, 10, iters);
85+
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);
86+
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);
9287

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

10499
secp256k1_context_destroy(data.ctx);
105-
return 0;
106100
}
101+
102+
#endif

0 commit comments

Comments
 (0)