Skip to content

Commit 6ad908a

Browse files
Merge bitcoin-core/secp256k1#1008: bench.c: add --help option and ci: move env variables
592661c ci: move test environment variable declaration to .cirrus.yml (siv2r) dcbe84b bench: add --help option to bench. (siv2r) Pull request description: Fixes bitcoin#1005 **Changes:** - added `--help` option to `bench.c` - `help()` function prints the help to command-line - `have_invalid_args()` checks if the user has entered an invalid argument - moved `secp256k1_bench_iters` and `secp256k1_test_iters` environment variables declaration to `.cirrus.yml` ACKs for top commit: sipa: utACK 592661c real-or-random: ACK 592661c Tree-SHA512: ebc6a2e6e47b529212efa1c9b75cc79649fca7f42aa75ce46502db24ac94f46b6cef59c828d13265d1fa69187a81c140d1951e7daeb7c8e008a6c1ad75259741
2 parents 4900227 + 592661c commit 6ad908a

File tree

5 files changed

+128
-16
lines changed

5 files changed

+128
-16
lines changed

.cirrus.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ env:
1919
RECOVERY: no
2020
SCHNORRSIG: no
2121
### test options
22-
TEST_ITERS:
22+
SECP256K1_TEST_ITERS:
2323
BENCH: yes
24-
BENCH_ITERS: 2
24+
SECP256K1_BENCH_ITERS: 2
2525
CTIMETEST: yes
2626

2727
cat_logs_snippet: &CAT_LOGS
@@ -171,7 +171,7 @@ task:
171171
memory: 1G
172172
env:
173173
WRAPPER_CMD: qemu-s390x
174-
TEST_ITERS: 16
174+
SECP256K1_TEST_ITERS: 16
175175
HOST: s390x-linux-gnu
176176
WITH_VALGRIND: no
177177
ECDH: yes
@@ -194,7 +194,7 @@ task:
194194
memory: 1G
195195
env:
196196
WRAPPER_CMD: qemu-arm
197-
TEST_ITERS: 16
197+
SECP256K1_TEST_ITERS: 16
198198
HOST: arm-linux-gnueabihf
199199
WITH_VALGRIND: no
200200
ECDH: yes
@@ -218,7 +218,7 @@ task:
218218
memory: 1G
219219
env:
220220
WRAPPER_CMD: qemu-aarch64
221-
TEST_ITERS: 16
221+
SECP256K1_TEST_ITERS: 16
222222
HOST: aarch64-linux-gnu
223223
WITH_VALGRIND: no
224224
ECDH: yes
@@ -239,7 +239,7 @@ task:
239239
memory: 1G
240240
env:
241241
WRAPPER_CMD: qemu-ppc64le
242-
TEST_ITERS: 16
242+
SECP256K1_TEST_ITERS: 16
243243
HOST: powerpc64le-linux-gnu
244244
WITH_VALGRIND: no
245245
ECDH: yes
@@ -260,7 +260,7 @@ task:
260260
memory: 1G
261261
env:
262262
WRAPPER_CMD: wine64-stable
263-
TEST_ITERS: 16
263+
SECP256K1_TEST_ITERS: 16
264264
HOST: x86_64-w64-mingw32
265265
WITH_VALGRIND: no
266266
ECDH: yes
@@ -290,15 +290,15 @@ task:
290290
env:
291291
# The `--error-exitcode` is required to make the test fail if valgrind found errors, otherwise it'll return 0 (https://www.valgrind.org/docs/manual/manual-core.html)
292292
WRAPPER_CMD: "valgrind --error-exitcode=42"
293-
TEST_ITERS: 16
293+
SECP256K1_TEST_ITERS: 16
294294
- name: "UBSan, ASan, LSan"
295295
env:
296296
CFLAGS: "-fsanitize=undefined,address -g"
297297
CFLAGS_FOR_BUILD: "-fsanitize=undefined,address -g"
298298
UBSAN_OPTIONS: "print_stacktrace=1:halt_on_error=1"
299299
ASAN_OPTIONS: "strict_string_checks=1:detect_stack_use_after_return=1:detect_leaks=1"
300300
LSAN_OPTIONS: "use_unaligned=1"
301-
TEST_ITERS: 32
301+
SECP256K1_TEST_ITERS: 32
302302
# Try to cover many configurations with just a tiny matrix.
303303
matrix:
304304
- env:

ci/cirrus.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ file .libs/* || true
3232
# This tells `make check` to wrap test invocations.
3333
export LOG_COMPILER="$WRAPPER_CMD"
3434

35-
# This limits the iterations in the tests and benchmarks.
36-
export SECP256K1_TEST_ITERS="$TEST_ITERS"
37-
export SECP256K1_BENCH_ITERS="$BENCH_ITERS"
38-
3935
make "$BUILD"
4036

4137
if [ "$BENCH" = "yes" ]

src/bench.c

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,51 @@
1111
#include "util.h"
1212
#include "bench.h"
1313

14+
void help(int default_iters) {
15+
printf("Benchmarks the following algorithms:\n");
16+
printf(" - ECDSA signing/verification\n");
17+
18+
#ifdef ENABLE_MODULE_ECDH
19+
printf(" - ECDH key exchange (optional module)\n");
20+
#endif
21+
22+
#ifdef ENABLE_MODULE_RECOVERY
23+
printf(" - Public key recovery (optional module)\n");
24+
#endif
25+
26+
#ifdef ENABLE_MODULE_SCHNORRSIG
27+
printf(" - Schnorr signatures (optional module)\n");
28+
#endif
29+
30+
printf("\n");
31+
printf("The default number of iterations for each benchmark is %d. This can be\n", default_iters);
32+
printf("customized using the SECP256K1_BENCH_ITERS environment variable.\n");
33+
printf("\n");
34+
printf("Usage: ./bench [args]\n");
35+
printf("By default, all benchmarks will be run.\n");
36+
printf("args:\n");
37+
printf(" help : display this help and exit\n");
38+
printf(" ecdsa : all ECDSA algorithms--sign, verify, recovery (if enabled)\n");
39+
printf(" ecdsa_sign : ECDSA siging algorithm\n");
40+
printf(" ecdsa_verify : ECDSA verification algorithm\n");
41+
42+
#ifdef ENABLE_MODULE_RECOVERY
43+
printf(" ecdsa_recover : ECDSA public key recovery algorithm\n");
44+
#endif
45+
46+
#ifdef ENABLE_MODULE_ECDH
47+
printf(" ecdh : ECDH key exchange algorithm\n");
48+
#endif
49+
50+
#ifdef ENABLE_MODULE_SCHNORRSIG
51+
printf(" schnorrsig : all Schnorr signature algorithms (sign, verify)\n");
52+
printf(" schnorrsig_sign : Schnorr sigining algorithm\n");
53+
printf(" schnorrsig_verify : Schnorr verification algorithm\n");
54+
#endif
55+
56+
printf("\n");
57+
}
58+
1459
typedef struct {
1560
secp256k1_context *ctx;
1661
unsigned char msg[32];
@@ -95,7 +140,52 @@ int main(int argc, char** argv) {
95140
bench_verify_data data;
96141

97142
int d = argc == 1;
98-
int iters = get_iters(20000);
143+
int default_iters = 20000;
144+
int iters = get_iters(default_iters);
145+
146+
/* Check for invalid user arguments */
147+
char* valid_args[] = {"ecdsa", "verify", "ecdsa_verify", "sign", "ecdsa_sign", "ecdh", "recover",
148+
"ecdsa_recover", "schnorrsig", "schnorrsig_verify", "schnorrsig_sign"};
149+
size_t valid_args_size = sizeof(valid_args)/sizeof(valid_args[0]);
150+
int invalid_args = have_invalid_args(argc, argv, valid_args, valid_args_size);
151+
152+
if (argc > 1) {
153+
if (have_flag(argc, argv, "-h")
154+
|| have_flag(argc, argv, "--help")
155+
|| have_flag(argc, argv, "help")) {
156+
help(default_iters);
157+
return 0;
158+
} else if (invalid_args) {
159+
fprintf(stderr, "./bench: unrecognized argument.\n\n");
160+
help(default_iters);
161+
return 1;
162+
}
163+
}
164+
165+
/* Check if the user tries to benchmark optional module without building it */
166+
#ifndef ENABLE_MODULE_ECDH
167+
if (have_flag(argc, argv, "ecdh")) {
168+
fprintf(stderr, "./bench: ECDH module not enabled.\n");
169+
fprintf(stderr, "Use ./configure --enable-module-ecdh.\n\n");
170+
return 1;
171+
}
172+
#endif
173+
174+
#ifndef ENABLE_MODULE_RECOVERY
175+
if (have_flag(argc, argv, "recover") || have_flag(argc, argv, "ecdsa_recover")) {
176+
fprintf(stderr, "./bench: Public key recovery module not enabled.\n");
177+
fprintf(stderr, "Use ./configure --enable-module-recovery.\n\n");
178+
return 1;
179+
}
180+
#endif
181+
182+
#ifndef ENABLE_MODULE_SCHNORRSIG
183+
if (have_flag(argc, argv, "schnorrsig") || have_flag(argc, argv, "schnorrsig_sign") || have_flag(argc, argv, "schnorrsig_verify")) {
184+
fprintf(stderr, "./bench: Schnorr signatures module not enabled.\n");
185+
fprintf(stderr, "Use ./configure --enable-module-schnorrsig.\n\n");
186+
return 1;
187+
}
188+
#endif
99189

100190
/* ECDSA verification benchmark */
101191
data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);

src/bench.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void run_benchmark(char *name, void (*benchmark)(void*, int), void (*setup)(void
116116
int have_flag(int argc, char** argv, char *flag) {
117117
char** argm = argv + argc;
118118
argv++;
119-
while (argv != NULL && argv != argm) {
119+
while (argv != argm) {
120120
if (strcmp(*argv, flag) == 0) {
121121
return 1;
122122
}
@@ -125,6 +125,32 @@ int have_flag(int argc, char** argv, char *flag) {
125125
return 0;
126126
}
127127

128+
/* takes an array containing the arguments that the user is allowed to enter on the command-line
129+
returns:
130+
- 1 if the user entered an invalid argument
131+
- 0 if all the user entered arguments are valid */
132+
int have_invalid_args(int argc, char** argv, char** valid_args, size_t n) {
133+
size_t i;
134+
int found_valid;
135+
char** argm = argv + argc;
136+
argv++;
137+
138+
while (argv != argm) {
139+
found_valid = 0;
140+
for (i = 0; i < n; i++) {
141+
if (strcmp(*argv, valid_args[i]) == 0) {
142+
found_valid = 1; /* user entered a valid arg from the list */
143+
break;
144+
}
145+
}
146+
if (found_valid == 0) {
147+
return 1; /* invalid arg found */
148+
}
149+
argv++;
150+
}
151+
return 0;
152+
}
153+
128154
int get_iters(int default_iters) {
129155
char* env = getenv("SECP256K1_BENCH_ITERS");
130156
if (env) {

src/bench_ecmult.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ int main(int argc, char **argv) {
292292
|| have_flag(argc, argv, "--help")
293293
|| have_flag(argc, argv, "help")) {
294294
help(argv);
295-
return 1;
295+
return 0;
296296
} else if(have_flag(argc, argv, "pippenger_wnaf")) {
297297
printf("Using pippenger_wnaf:\n");
298298
data.ecmult_multi = secp256k1_ecmult_pippenger_batch_single;

0 commit comments

Comments
 (0)