bench: fail early if user inputs invalid value for SECP256K1_BENCH_ITERS #1796

pull kevkevinpal wants to merge 1 commits into bitcoin-core:master from kevkevinpal:failEarlyOnInvalidIters changing 4 files +23 −4
  1. kevkevinpal commented at 6:15 pm on January 8, 2026: contributor

    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

    0secp256k1 $ SECP256K1_BENCH_ITERS=abc ./build/bin/bench
    1
    2Benchmark                     ,    Min(us)    ,    Avg(us)    ,    Max(us)
    3
    4Floating point exception (core dumped)
    

    After

     0secp256k1 $ SECP256K1_BENCH_ITERS=abc ./build/bin/bench
     1
     2Invalid value for SECP256K1_BENCH_ITERS must be a positive integer: abc
     3
     4Benchmarks the following algorithms:
     5    - ECDSA signing/verification
     6    - ECDH key exchange (optional module)
     7    - Schnorr signatures (optional module)
     8    - ElligatorSwift (optional module)
     9
    10The default number of iterations for each benchmark is 20000. This can be
    11customized using the SECP256K1_BENCH_ITERS environment variable.
    12
    13Usage: ./bench [args]
    14By default, all benchmarks will be run.
    15args:
    16    help              : display this help and exit
    17    ecdsa             : all ECDSA algorithms--sign, verify, recovery (if enabled)
    18    ecdsa_sign        : ECDSA siging algorithm
    19    ecdsa_verify      : ECDSA verification algorithm
    20    ec                : all EC public key algorithms (keygen)
    21    ec_keygen         : EC public key generation
    22    ecdh              : ECDH key exchange algorithm
    23    schnorrsig        : all Schnorr signature algorithms (sign, verify)
    24    schnorrsig_sign   : Schnorr sigining algorithm
    25    schnorrsig_verify : Schnorr verification algorithm
    26    ellswift          : all ElligatorSwift benchmarks (encode, decode, keygen, ecdh)
    27    ellswift_encode   : ElligatorSwift encoding
    28    ellswift_decode   : ElligatorSwift decoding
    29    ellswift_keygen   : ElligatorSwift key generation
    30    ellswift_ecdh     : ECDH on ElligatorSwift keys
    
  2. kevkevinpal force-pushed on Jan 8, 2026
  3. kevkevinpal force-pushed on Jan 8, 2026
  4. real-or-random added the label tweak/refactor on Jan 8, 2026
  5. in src/bench.h:156 in 331bb48bed
    149@@ -150,7 +150,13 @@ static int have_invalid_args(int argc, char** argv, char** valid_args, size_t n)
    150 static int get_iters(int default_iters) {
    151     char* env = getenv("SECP256K1_BENCH_ITERS");
    152     if (env) {
    153-        return strtol(env, NULL, 0);
    154+        char* endptr;
    155+        long int iters = strtol(env, &endptr, 0);
    156+        if (*endptr != '\0' || iters == 0) {
    157+            printf("Invalid value for SECP256K1_BENCH_ITERS must be a positive integer: %s\n\n", env);
    


    real-or-random commented at 8:30 am on January 9, 2026:
    0            printf("Error: Value of SECP256K1_BENCH_ITERS is not a positive integer: %s\n\n", env);
    

    nit:
    I think the wording “invalid value …. must be a positive integer” is a bit confusing.


    kevkevinpal commented at 2:20 pm on January 9, 2026:
    Thanks for the review l, I took your suggestion and fixed it in a02ea3566c28e1e919ca8e037c42dad4ec223138
  6. in src/bench.h:155 in 331bb48bed
    149@@ -150,7 +150,13 @@ static int have_invalid_args(int argc, char** argv, char** valid_args, size_t n)
    150 static int get_iters(int default_iters) {
    151     char* env = getenv("SECP256K1_BENCH_ITERS");
    152     if (env) {
    153-        return strtol(env, NULL, 0);
    154+        char* endptr;
    155+        long int iters = strtol(env, &endptr, 0);
    156+        if (*endptr != '\0' || iters == 0) {
    


    real-or-random commented at 8:32 am on January 9, 2026:
    0        if (*endptr != '\0' || iters <= 0) {
    

    kevkevinpal commented at 2:19 pm on January 9, 2026:
    Thanks for the review! I fixed this in a02ea3566c28e1e919ca8e037c42dad4ec223138

    hebasto commented at 4:55 pm on January 9, 2026:

    Right. Otherwise, negative values are accepted:

     0$ SECP256K1_BENCH_ITERS=-1 ./build/bin/bench
     1Benchmark                     ,    Min(us)    ,    Avg(us)    ,    Max(us)    
     2
     3ecdsa_verify                  ,     0.0       ,    -0.300     ,    -1.00   
     4ecdsa_sign                    ,     0.0       ,    -0.100     ,    -1.00   
     5ec_keygen                     ,     0.0       ,     0.0       ,     0.0    
     6ecdh                          ,     0.0       ,     0.0       ,     0.0    
     7ecdsa_recover                 ,     0.0       ,    -0.100     ,    -1.00   
     8schnorrsig_sign               ,     0.0       ,     0.0       ,     0.0    
     9schnorrsig_verify             ,     0.0       ,     0.0       ,     0.0    
    10ellswift_encode               ,     0.0       ,    -0.200     ,    -1.00   
    11ellswift_decode               ,     0.0       ,     0.0       ,     0.0    
    12ellswift_keygen               ,     0.0       ,    -0.100     ,    -1.00   
    13ellswift_ecdh                 ,     0.0       ,    -0.100     ,    -1.00
    
  7. real-or-random commented at 8:32 am on January 9, 2026: contributor
    ACK mod nits
  8. kevkevinpal force-pushed on Jan 9, 2026
  9. hebasto approved
  10. hebasto commented at 4:56 pm on January 9, 2026: member
    ACK a02ea3566c28e1e919ca8e037c42dad4ec223138, I have reviewed the code and it looks OK. Tested on Ubuntu 25.10.
  11. real-or-random commented at 1:34 pm on January 13, 2026: contributor
    Needs rebase (on your own stuff) :)
  12. kevkevinpal force-pushed on Jan 23, 2026
  13. kevkevinpal commented at 1:05 pm on January 23, 2026: contributor

    Needs rebase (on your own stuff) :)

    Sorry was traveling for a bit, rebased to c09215f

  14. bench: fail early if user inputs invalid value for SECP256K1_BENCH_ITERS
    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
    c09215f7af
  15. kevkevinpal force-pushed on Jan 23, 2026
  16. hebasto approved
  17. hebasto commented at 1:20 pm on January 23, 2026: member
    re-ACK c09215f7af9db3e34427ff9a5633e7978acabca1.
  18. real-or-random approved
  19. real-or-random commented at 1:29 pm on January 23, 2026: contributor
    utACK c09215f7af9db3e34427ff9a5633e7978acabca1
  20. real-or-random merged this on Jan 23, 2026
  21. real-or-random closed this on Jan 23, 2026


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin-core/secp256k1. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-01-27 09:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me