Skip to content

Commit dcbe84b

Browse files
committed
bench: add --help option to bench.
The following functions were created: 1. bench.c: help() - prints the help to the command-line 2. bench.h: have_invalid_args() - takes a list of arguments that the user is allowed to enter on the command-line - returns 1 if the user entered an invalid argument - returns 0 if all the user entered arguments are valid
1 parent 515a5db commit dcbe84b

File tree

3 files changed

+119
-3
lines changed

3 files changed

+119
-3
lines changed

src/bench.c

+91-1
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

+27-1
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

+1-1
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)