Skip to content

Commit ebb3588

Browse files
Merge #1796: bench: fail early if user inputs invalid value for SECP256K1_BENCH_ITERS
c09215f bench: fail early if user inputs invalid value for SECP256K1_BENCH_ITERS (kevkevinpal) Pull request description: ### Description Motivated by #1793 (comment) In this change, the `get_iters` function was updated to print an error message and then return 0. In the functions that use `get_iters` they print the help text and then EXIT_FAILURE ### Before ``` secp256k1 $ SECP256K1_BENCH_ITERS=abc ./build/bin/bench Benchmark , Min(us) , Avg(us) , Max(us) Floating point exception (core dumped) ``` ### After ``` secp256k1 $ SECP256K1_BENCH_ITERS=abc ./build/bin/bench Invalid value for SECP256K1_BENCH_ITERS must be a positive integer: abc Benchmarks the following algorithms: - ECDSA signing/verification - ECDH key exchange (optional module) - Schnorr signatures (optional module) - ElligatorSwift (optional module) The default number of iterations for each benchmark is 20000. This can be customized using the SECP256K1_BENCH_ITERS environment variable. Usage: ./bench [args] By default, all benchmarks will be run. args: help : display this help and exit ecdsa : all ECDSA algorithms--sign, verify, recovery (if enabled) ecdsa_sign : ECDSA siging algorithm ecdsa_verify : ECDSA verification algorithm ec : all EC public key algorithms (keygen) ec_keygen : EC public key generation ecdh : ECDH key exchange algorithm schnorrsig : all Schnorr signature algorithms (sign, verify) schnorrsig_sign : Schnorr sigining algorithm schnorrsig_verify : Schnorr verification algorithm ellswift : all ElligatorSwift benchmarks (encode, decode, keygen, ecdh) ellswift_encode : ElligatorSwift encoding ellswift_decode : ElligatorSwift decoding ellswift_keygen : ElligatorSwift key generation ellswift_ecdh : ECDH on ElligatorSwift keys ``` ACKs for top commit: hebasto: re-ACK c09215f. real-or-random: utACK c09215f Tree-SHA512: 356df69e356db0b201339d40a6ffbcf29e4b7cc1e6aa82c00e1e7a2a7d11c47dd9c51baabcc63cabcff2ab42e2746a3cab659205f871a85122edda4a599d56c8
2 parents 471e3a1 + c09215f commit ebb3588

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

src/bench.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,6 @@ int main(int argc, char** argv) {
177177
bench_data data;
178178

179179
int d = argc == 1;
180-
int default_iters = 20000;
181-
int iters = get_iters(default_iters);
182180

183181
/* Check for invalid user arguments */
184182
char* valid_args[] = {"ecdsa", "verify", "ecdsa_verify", "sign", "ecdsa_sign", "ecdh", "recover",
@@ -188,6 +186,13 @@ int main(int argc, char** argv) {
188186
size_t valid_args_size = sizeof(valid_args)/sizeof(valid_args[0]);
189187
int invalid_args = have_invalid_args(argc, argv, valid_args, valid_args_size);
190188

189+
int default_iters = 20000;
190+
int iters = get_iters(default_iters);
191+
if (iters == 0) {
192+
help(default_iters);
193+
return EXIT_FAILURE;
194+
}
195+
191196
if (argc > 1) {
192197
if (have_flag(argc, argv, "-h")
193198
|| have_flag(argc, argv, "--help")

src/bench.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,13 @@ static int have_invalid_args(int argc, char** argv, char** valid_args, size_t n)
150150
static int get_iters(int default_iters) {
151151
char* env = getenv("SECP256K1_BENCH_ITERS");
152152
if (env) {
153-
return strtol(env, NULL, 0);
153+
char* endptr;
154+
long int iters = strtol(env, &endptr, 0);
155+
if (*endptr != '\0' || iters <= 0) {
156+
printf("Error: Value of SECP256K1_BENCH_ITERS is not a positive integer: %s\n\n", env);
157+
return 0;
158+
}
159+
return iters;
154160
} else {
155161
return default_iters;
156162
}

src/bench_ecmult.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@ int main(int argc, char **argv) {
313313

314314
int default_iters = 10000;
315315
int iters = get_iters(default_iters);
316+
if (iters == 0) {
317+
help(argv, default_iters);
318+
return EXIT_FAILURE;
319+
}
316320

317321
data.ecmult_multi = secp256k1_ecmult_multi_var;
318322

src/bench_internal.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,13 @@ static void bench_context(void* arg, int iters) {
385385

386386
int main(int argc, char **argv) {
387387
bench_inv data;
388+
int d = argc == 1; /* default */
388389
int default_iters = 20000;
389390
int iters = get_iters(default_iters);
390-
int d = argc == 1; /* default */
391+
if (iters == 0) {
392+
help(default_iters);
393+
return EXIT_FAILURE;
394+
}
391395

392396
if (argc > 1) {
393397
if (have_flag(argc, argv, "-h")

0 commit comments

Comments
 (0)