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

    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
    
  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:
                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:
            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:

    $ SECP256K1_BENCH_ITERS=-1 ./build/bin/bench
    Benchmark                     ,    Min(us)    ,    Avg(us)    ,    Max(us)    
    
    ecdsa_verify                  ,     0.0       ,    -0.300     ,    -1.00   
    ecdsa_sign                    ,     0.0       ,    -0.100     ,    -1.00   
    ec_keygen                     ,     0.0       ,     0.0       ,     0.0    
    ecdh                          ,     0.0       ,     0.0       ,     0.0    
    ecdsa_recover                 ,     0.0       ,    -0.100     ,    -1.00   
    schnorrsig_sign               ,     0.0       ,     0.0       ,     0.0    
    schnorrsig_verify             ,     0.0       ,     0.0       ,     0.0    
    ellswift_encode               ,     0.0       ,    -0.200     ,    -1.00   
    ellswift_decode               ,     0.0       ,     0.0       ,     0.0    
    ellswift_keygen               ,     0.0       ,    -0.100     ,    -1.00   
    ellswift_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-04-18 17:15 UTC

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