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

  22. fanquake referenced this in commit c4c4e04ca1 on Jan 26, 2026
  23. fanquake referenced this in commit 2fccbea3c8 on Jan 27, 2026
  24. fjahr referenced this in commit 182197f98a on Jan 29, 2026
  25. fjahr referenced this in commit ea9a84ab3c on Jan 31, 2026
  26. fjahr referenced this in commit 23c2527f12 on Feb 8, 2026
  27. Sjors referenced this in commit d5660d3a13 on Feb 16, 2026
  28. mllwchrry referenced this in commit db9696b1ab on Feb 26, 2026
  29. github-actions[bot] referenced this in commit c3f80fff5f on Mar 1, 2026
  30. github-actions[bot] referenced this in commit 4aeff8400e on Mar 1, 2026
  31. github-actions[bot] referenced this in commit 5f15eb0c55 on Mar 1, 2026
  32. 0x000000000019d6689c085ae165831e934ff76 referenced this in commit d54574beca on Mar 2, 2026
  33. csjones referenced this in commit fb3e16af04 on Mar 2, 2026
  34. mllwchrry referenced this in commit dc2a10425f on Mar 2, 2026
  35. mllwchrry referenced this in commit dc0bda5731 on Mar 3, 2026
  36. real-or-random referenced this in commit 459eab20f2 on Mar 3, 2026
  37. vmta referenced this in commit 1ddc2f947f on Apr 26, 2026
  38. vmta referenced this in commit 56c40fe100 on Apr 27, 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-05-19 06:52 UTC