Commit Graph

1401 Commits

Author SHA1 Message Date
merge-script
1aafe15139 Merge bitcoin-core/secp256k1#1777: Make SHA256 compression runtime pluggable
4d92a083bc sha256: speed up writes using multi-block compression (furszy)
0753f8b909 Add API to override SHA256 compression at runtime (furszy)
fdb6a91a5e Introduce hash context to support pluggable SHA256 compression (furszy)

Pull request description:

  Tackling the long-standing request #702.

  Right now we ship our own SHA256 implementation, a standard baseline version that does not take advantage of any hardware-optimized instruction, and it cannot be accessed by the embedding application - it is for internal usage only.

  This means embedding applications often have to implement or include a different version for their use cases, wasting space on constrained environments, and in performance-sensitive setups it forces them to use a slower path than what the platform provides. Many projects already rely on tuned SHA-NI / ARMv8 / or other hardware-optimized code, so always using the baseline implementation we ship within the library is not ideal.

  These changes allow users to supply their own SHA256 compression function at runtime, while preserving the existing default behavior for everyone else. This is primarily intended for environments where the available SHA256 implementation is detected dynamically and recompiling the library with a different implementation is not feasible (equivalent build-time functionality will come in a follow-up PR).

  It introduces a new API:

  ```C89
  secp256k1_context_set_sha256_transform_callback(ctx, fn_transform)
  ```

  This function installs the optimized SHA256 compression into the `secp256k1_context`, which is then used by all internal computations. Important: The provided function is verified to be output-equivalent to the original one.

  As a quick example, using this functionality in Bitcoin-Core will be very straightforward: f68bef06d9

ACKs for top commit:
  real-or-random:
    ACK 4d92a083bc
  w0xlt:
    ACK 4d92a083bc
  theStack:
    ACK 4d92a083bc

Tree-SHA512: 058e2e82071f1ca77254b684458292c621e60d65bbcc5500574429717e7db75bc9f3221129fafd11eb5d33e666a5efec5e9844460d3b194ef3b6b16f2df28fb9
2026-03-04 08:43:07 +01:00
merge-script
b9cb1cbfd7 Merge bitcoin-core/secp256k1#1824: util: introduce and use ARRAY_SIZE macro
921b9711ea util: introduce and use `ARRAY_SIZE` macro (Sebastian Falbesoner)

Pull request description:

  This PR is another tiny improvement found while working on #1765, with the goal to avoid code repetition.

  The `ARRAY_SIZE` macro definition is pretty wide-spread in C projects and e.g. matches the one [used in the Linux Kernel](9702969978/include/linux/array_size.h (L11))  (without the additional check to reject pointers, as we would need GNU C for that, see e.g. https://stackoverflow.com/a/19455169; not sure if a useful counterpart exists that only relies on C89). Replacement instances were identified via `$ git grep sizeof.*/.*sizeof`.

ACKs for top commit:
  w0xlt:
    ACK 921b9711ea
  real-or-random:
    utACK 921b9711ea

Tree-SHA512: 44b6bf0132cf00fade526a3fc04e03dc896d04874123614c032206b61f97c81f94d139b6cc0c108eceaa699251580c19420d230b3150607303ca2cb7ab9a0bcb
2026-03-03 15:31:46 +01:00
furszy
4d92a083bc sha256: speed up writes using multi-block compression
Multiple 64-byte blocks can now be compressed directly
from the input buffer, without copying them into the
internal buffer.
2026-03-03 10:35:53 -03:00
furszy
0753f8b909 Add API to override SHA256 compression at runtime
This introduces `secp256k1_context_set_sha256_compression()`,
which allows users to provide their own SHA256 block-compression
function at runtime.

This is useful in setups where the fastest implementation can only
be determined dynamically based on the available CPU features, and
rebuilding the library is not possible.

The callback is installed on the `secp256k1_context` and is then used
by all operations that compute SHA256 hashes. As part of the setup,
the library performs sanity checks to ensure that the supplied
function is equivalent to the default transform.

Passing NULL to the callback setter restores the built-in
implementation.
2026-03-03 10:35:53 -03:00
furszy
fdb6a91a5e Introduce hash context to support pluggable SHA256 compression
This is purely a mechanical change with no behavior change.

It introduces a secp256k1_hash_ctx struct inside secp256k1_context
and propagates it to all SHA256-related operations.

This sets up the ability to provide a hardware-optimized SHA256
compression function at runtime in a follow-up commit.
2026-03-03 10:25:50 -03:00
merge-script
c0a2aba088 Merge bitcoin-core/secp256k1#1811: bench: Update help functions in bench and bench_internal
c49c9be504 bench: Update help functions in bench and bench_internal (kevkevinpal)

Pull request description:

  ### Motivation
  This change is motivated by https://github.com/bitcoin-core/secp256k1/pull/1793#pullrequestreview-3644885897

  > While aligning implementation across all benchmarks, argv could be passed to the help() in bench.c and bench_internal.c.

  ### Description

  In the `bench` and `bench_internal` `help` functions `argv` was not being passed. In this change, we pass in argv and use it in the help text.

ACKs for top commit:
  real-or-random:
    ACK c49c9be504

Tree-SHA512: 77184db4bf5c16827f19d888af73939f4139cc2e84ae5256d995cf61f606d5865928480fc009a0185e1a6843f3c38dd1b858d1316e524c9b165459c7367f2318
2026-03-03 09:13:57 +01:00
Tim Ruffing
8d0eda07e9 testrand: Remove testrand_finish
This removes printing of the "random run = " at the end of the tests. I
haven't seen a single case where this proved to be useful. And as of
48789dafc2, this is anyway printed only at
the end of the exhaustive tests and not the normal tests, so the
probability that this will be useful in the future is very low.
2026-03-02 15:06:39 +01:00
merge-script
95e6815843 Merge bitcoin-core/secp256k1#1825: hash: remove redundant secp256k1_sha256_initialize in tagged hash midstate functions
f48b1bfa5d hash: add midstate initializer and use it for tagged hashes (w0xlt)

Pull request description:

  Each tagged hash midstate function (e.g., `secp256k1_schnorrsig_sha256_tagged`) calls `secp256k1_sha256_initialize` before immediately overwriting every field it sets: `s[0]` through `s[7]` and `bytes`. The `buf[64]` member does not need initialization either, because `bytes` is set to 64, which means the buffer position (`bytes & 0x3F`) (`= bytes % 64`) is 0, so buf is always written before being read.

  Remove the 11 redundant `secp256k1_sha256_initialize` calls across the `schnorrsig`, `ellswift`, and `musig` modules.

ACKs for top commit:
  real-or-random:
    utACK f48b1bfa5d
  theStack:
    Code-review ACK f48b1bfa5d

Tree-SHA512: 769beb96f3921cc3c180ed0d17484ffa0dc78041c889a8e56603679d8eaca5fe13e63759ada78f83d8e0ff7aae392e6bcbc1a9fe8b959105ea4a3d8ef51abf15
2026-02-27 21:10:43 +01:00
w0xlt
f48b1bfa5d hash: add midstate initializer and use it for tagged hashes
Introduce secp256k1_sha256_initialize_midstate() in the hash layer and use it at all tagged-hash midstate call sites across schnorrsig, musig, and ellswift.

Document the byte-counter contract at the declaration site in hash.h and add run_sha256_initialize_midstate_tests() to directly verify helper behavior against initialize_tagged.

Also switch the helper to take const uint32_t state[8] to reduce argument-order risk at call sites.
2026-02-25 15:37:43 -08:00
merge-script
ac561601b8 Merge bitcoin-core/secp256k1#1760: cmake: Add dynamic test discovery to improve parallelism
8354618e02 cmake: Set `LABELS` property for tests (Hennadii Stepanov)
29f26ec3cf cmake: Integrate DiscoverTests and normalize test names (Hennadii Stepanov)
f95b263f23 cmake: Add DiscoverTests module (Hennadii Stepanov)
4ac651144b cmake, refactor: Deduplicate test-related code (Hennadii Stepanov)

Pull request description:

  This PR implements the idea suggested in https://github.com/bitcoin-core/secp256k1/pull/1734#pullrequestreview-3284918572 and is based on the work from https://github.com/bitcoin/bitcoin/pull/33483.

  Here is an example of the `ctest` output:
  ```
  $ ctest --test-dir build -j $(nproc)
  Test project /home/hebasto/dev/secp256k1/secp256k1/build
          Start   1: secp256k1.noverify_tests.selftest_tests
          Start   2: secp256k1.noverify_tests.all_proper_context_tests
          Start   3: secp256k1.noverify_tests.all_static_context_tests
          Start   4: secp256k1.noverify_tests.deprecated_context_flags_test
  <snip>
  193/196 Test  #31: secp256k1.noverify_tests.ecmult_constants .........................   Passed    5.32 sec
  194/196 Test #184: secp256k1.tests.ellswift_xdh_correctness_tests ....................   Passed    5.62 sec
  195/196 Test #191: secp256k1.exhaustive_tests ........................................   Passed    6.97 sec
  196/196 Test #126: secp256k1.tests.ecmult_constants ..................................   Passed    9.60 sec

  100% tests passed, 0 tests failed out of 196

  Label Time Summary:
  secp256k1_example           =   0.02 sec*proc (5 tests)
  secp256k1_exhaustive        =   6.97 sec*proc (1 test)
  secp256k1_noverify_tests    =  23.77 sec*proc (95 tests)
  secp256k1_tests             =  43.67 sec*proc (95 tests)

  Total Test time (real) =  10.21 sec
  ```

  For comparison, here is the output for the master branch on the same machine:
  ```
  $ ctest --test-dir build -j $(nproc)
  Test project /home/hebasto/dev/secp256k1/secp256k1/build
      Start 1: secp256k1_noverify_tests
      Start 2: secp256k1_tests
      Start 3: secp256k1_exhaustive_tests
      Start 4: secp256k1_ecdsa_example
      Start 5: secp256k1_ecdh_example
      Start 6: secp256k1_schnorr_example
      Start 7: secp256k1_ellswift_example
      Start 8: secp256k1_musig_example
  1/8 Test #4: secp256k1_ecdsa_example ..........   Passed    0.00 sec
  2/8 Test #5: secp256k1_ecdh_example ...........   Passed    0.00 sec
  3/8 Test #6: secp256k1_schnorr_example ........   Passed    0.00 sec
  4/8 Test #7: secp256k1_ellswift_example .......   Passed    0.00 sec
  5/8 Test #8: secp256k1_musig_example ..........   Passed    0.00 sec
  6/8 Test #3: secp256k1_exhaustive_tests .......   Passed    6.26 sec
  7/8 Test #1: secp256k1_noverify_tests .........   Passed   14.31 sec
  8/8 Test #2: secp256k1_tests ..................   Passed   31.65 sec

  100% tests passed, 0 tests failed out of 8

  Total Test time (real) =  31.65 sec
  ```

  ---

  **New Feature:** As the number of tests has grown, the _labels_ have been introduced to simplify test management. Now, one can run:
  ```
  $ ctest --test-dir build -j $(nproc) -L example
  Test project /home/hebasto/dev/secp256k1/secp256k1/build
      Start 192: secp256k1.example.ecdsa
      Start 193: secp256k1.example.ecdh
      Start 194: secp256k1.example.schnorr
      Start 195: secp256k1.example.ellswift
      Start 196: secp256k1.example.musig
  1/5 Test #192: secp256k1.example.ecdsa ..........   Passed    0.00 sec
  2/5 Test #193: secp256k1.example.ecdh ...........   Passed    0.00 sec
  3/5 Test #194: secp256k1.example.schnorr ........   Passed    0.00 sec
  4/5 Test #195: secp256k1.example.ellswift .......   Passed    0.00 sec
  5/5 Test #196: secp256k1.example.musig ..........   Passed    0.00 sec

  100% tests passed, 0 tests failed out of 5

  Label Time Summary:
  secp256k1_example    =   0.01 sec*proc (5 tests)

  Total Test time (real) =   0.01 sec
  ```
  or
  ```
  $ ctest --test-dir build -j $(nproc) -LE tests
  Test project /home/hebasto/dev/secp256k1/secp256k1/build
      Start 192: secp256k1.example.ecdsa
      Start 193: secp256k1.example.ecdh
      Start 194: secp256k1.example.schnorr
      Start 195: secp256k1.example.ellswift
      Start 196: secp256k1.example.musig
      Start 191: secp256k1.exhaustive_tests
  1/6 Test #192: secp256k1.example.ecdsa ..........   Passed    0.00 sec
  2/6 Test #193: secp256k1.example.ecdh ...........   Passed    0.00 sec
  3/6 Test #194: secp256k1.example.schnorr ........   Passed    0.00 sec
  4/6 Test #195: secp256k1.example.ellswift .......   Passed    0.00 sec
  5/6 Test #196: secp256k1.example.musig ..........   Passed    0.00 sec
  6/6 Test #191: secp256k1.exhaustive_tests .......   Passed    6.19 sec

  100% tests passed, 0 tests failed out of 6

  Label Time Summary:
  secp256k1_example       =   0.01 sec*proc (5 tests)
  secp256k1_exhaustive    =   6.19 sec*proc (1 test)

  Total Test time (real) =   6.20 sec
  ```

ACKs for top commit:
  purpleKarrot:
    ACK 8354618e02
  furszy:
    Tested ACK 8354618

Tree-SHA512: 8c506ab08491aba4836b3058a8a09c929c6dd097c11e4e6f4deb20cf602285e73c3fd8a2c2040f7e92a058c7f8fc09752fa9de2ce80f7673adbdd505237ed262
2026-02-19 15:02:44 +01:00
Sebastian Falbesoner
921b9711ea util: introduce and use ARRAY_SIZE macro
The macro definition matches the one used in Linux, see e.g.
9702969978/include/linux/array_size.h (L11)
(without the additional check rejecting pointers, as we would need
 GNU C for that, see e.g. https://stackoverflow.com/a/19455169)
2026-02-17 00:21:58 +01:00
gzJx0DuTRHytnHe7P5RmMbPf3wKy2BztweVGXTf
b99a94c382 Add tests for bad scalar inputs in ellswift XDH 2026-02-16 15:49:39 +01:00
gzJx0DuTRHytnHe7P5RmMbPf3wKy2BztweVGXTf
307b49f1b9 ellswift: fix overflow flag handling in secp256k1_ellswift_xdh
The secp256k1_ellswift_xdh function uses overflow = secp256k1_scalar_is_zero(&s) which overwrites the overflow flag from the preceding secp256k1_scalar_set_b32 call. This means secret keys >= the curve order are silently accepted (reduced mod n) instead of being rejected.

The fix changes = to |=, matching the correct pattern already used in secp256k1_ecdh (main_impl.h, line 51).

The ECDH module's test suite explicitly tests overflow rejection (passes secp256k1_group_order_bytes as a key and checks the function returns 0). The ellswift test suite has no corresponding test, which is why this went undetected.
2026-02-16 14:39:05 +01:00
kevkevinpal
c49c9be504 bench: Update help functions in bench and bench_internal
In the bench and bench_internal help functions argv was not being
passed, in this change we pass in argv[0] and use it in the help text.

Additionally instead of passing all of argv in bench_ecmult we now
just pass argv[0] and is used as the executable_path variable.
2026-02-09 19:17:14 -05:00
merge-script
1d146ac3ed Merge bitcoin-core/secp256k1#1819: tests: Improve secp256k1_scalar_check_overflow tests (Issue #1812)
f47bbc07f0 test: add unit tests for secp256k1_scalar_check_overflow (Rohit Yadav)

Pull request description:

  This Pull Request improves the tests for `secp256k1_scalar_check_overflow` as requested in #1812.

  ### Changes:
  - Removed the redundant "all ones" check from `run_scalar_tests`.
  - Added a new dedicated test function `test_scalar_check_overflow`.
  - Added static checks for edge cases: `0`, `N-1`, `N`, `N+1`, and `MAX`.
  - Added random input tests that verify `check_overflow` against a manual byte comparison.

  Fixes #1812.

ACKs for top commit:
  theStack:
    re-ACK f47bbc07f0
  real-or-random:
    utACK f47bbc07f0

Tree-SHA512: dad3aa31ecf3f296843c907ac3d9aa5a9b9cb839b36aa3b59e49c853c60c58291412e70dff37dc15f8e14023a8f1e1aba87395065607612d5f6cfa92e14e73b5
2026-02-04 20:24:44 +01:00
Rohit Yadav
f47bbc07f0 test: add unit tests for secp256k1_scalar_check_overflow 2026-02-05 00:00:32 +05:30
merge-script
d071aa56d5 Merge bitcoin-core/secp256k1#1815: refactor: remove unnecessary malloc result casts
97b3c47849 refactor: remove unnecessary `malloc` result casts (Sebastian Falbesoner)

Pull request description:

  While working on benchmark code for #1765, I noticed that in some instances we explicitly cast `malloc` results in the codebase. It seems that there is no good reason to do this in C, and it's even considered bad practice, see e.g. https://stackoverflow.com/a/605858.

  This commit touches mostly test code, the only two functions used in production are `secp256k1_context_{create,clone}`. Instances were found manually via `$ git grep "malloc("`.

ACKs for top commit:
  real-or-random:
    Weak Concept ACK && Code Review ACK 97b3c47849
  w0xlt:
    ACK 97b3c47849

Tree-SHA512: 74aa9f47eb52b7f2a6fcb69deb6aef0c0daa136c5deedfba1228218ef178c722212d8e9936fd2946d2035df932637ca4df49c98ddde488c6b009a74c4d5df316
2026-02-04 08:44:43 +01:00
merge-script
97de5120cf Merge bitcoin-core/secp256k1#1804: test: show both CMake and Autotools usage for ctime_tests
1bc74a22f8 test: show both Autotools and CMake usage for ctime_tests (8144225309)

Pull request description:

  When building with CMake and running `ctime_tests` outside valgrind, users see:

  ```
  Usage: libtool --mode=execute valgrind ./ctime_tests
  ```

  CMake users don't have libtool. Show both commands.

  ### Before
  ```
  $ ./build/bin/ctime_tests
  This test can only usefully be run inside valgrind because it was not compiled under msan.
  Usage: libtool --mode=execute valgrind ./ctime_tests
  ```

  ### After
  ```
  $ ./build/bin/ctime_tests
  This test can only usefully be run inside valgrind because it was not compiled under msan.
  Usage: valgrind ./ctime_tests (or with Autotools: libtool --mode=execute valgrind ./ctime_tests)
  ```

  Fixes #1697

ACKs for top commit:
  real-or-random:
    utACK 1bc74a22f8

Tree-SHA512: d35c332c75fe3df66928cb8b137e11995c67a57744985a50a539d1d9f24cf39ee46f17c6f6a501664a62f67e11b7bb041ba0e1eed6632bf7dccdb57a2c88f9bc
2026-02-03 12:57:57 +01:00
Sebastian Falbesoner
97b3c47849 refactor: remove unnecessary malloc result casts
It seems that there is no good reason to do this and it's even
considered bad practice, see e.g. https://stackoverflow.com/a/605858

This commit touches mostly test code, the only two functions used
in production are `secp256k1_context_{create,clone}`.

Instances were found manually via `$ git grep "malloc("`
2026-02-02 18:41:29 +01:00
Hennadii Stepanov
fb229e7602 build: Add -Wtrailing-whitespace=any compiler flag 2026-02-02 13:01:24 +00:00
Hennadii Stepanov
13e3bee504 refactor: Remove trailing whitespace 2026-02-02 13:01:18 +00:00
merge-script
1605b02f75 Merge bitcoin-core/secp256k1#1775: Add CMake build directory patterns to .gitignore
748c0fdd67 Add CMake build directory patterns to `.gitignore` (Hennadii Stepanov)
7eb86bdb01 autotools: Rename `build-aux` to `autotools-aux` (Hennadii Stepanov)

Pull request description:

  Whenever I work on changes that require comparison, such as benchmarking, I end up with two or more build directories that provide different binary variants simultaneously. Adding these build directories to `.gitignore` makes the workflow a bit easier.

  Additionally, a trivial refactoring is included to reduce the code.

ACKs for top commit:
  real-or-random:
    utACK 748c0fdd67
  furszy:
    ACK 748c0fdd67

Tree-SHA512: 948917dcdc2ec6d5a2227f35ef9208fdbc62c56047db1c60b39f6da632642847aefa18f136986f9f15f08e0b2385964afe9a311346b728536323c54b4f0e3f04
2026-01-28 08:27:56 +01:00
merge-script
14e56970cb Merge bitcoin-core/secp256k1#1794: ecmult: Use size_t for array indices
47eb70959a ecmult: Use size_t for array indices in _odd_multiplies_table (Tim Ruffing)
bb1d199de5 ecmult: Use size_t for array indices into tables (Tim Ruffing)

Pull request description:

  I don't think the current code is incorrect, but using `size_t` improves readability because the type makes it clear that we're dealing with array indices.

  Also, making the result of the `ECMULT_TABLE_SIZE` macro (hopefully) a `size_t` fixes a compiler warning on MSVC, see #1791.

ACKs for top commit:
  hebasto:
    re-ACK 47eb70959a.
  jonasnick:
    ACK 47eb70959a
  theStack:
    ACK 47eb70959a

Tree-SHA512: e484fd610d50e972021c0184a683993364290eb58e09b65f9521b4507ec8d0639b402c67002005630b389bc863a7aa05b75f7224524dbcbafbfa5f9a4812b4a5
2026-01-27 09:50:16 +01:00
kevkevinpal
c09215f7af 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
2026-01-23 08:07:22 -05:00
8144225309
1bc74a22f8 test: show both Autotools and CMake usage for ctime_tests
The existing message only shows the libtool command, which is
specific to Autotools builds.

Fixes #1697
2026-01-22 10:28:30 -05:00
Hennadii Stepanov
8354618e02 cmake: Set LABELS property for tests 2026-01-20 16:53:29 +00:00
Hennadii Stepanov
29f26ec3cf cmake: Integrate DiscoverTests and normalize test names
Updates the build system to use the new DiscoverTests module.
This also standardizes test names to use dot-separated parts for
consistency.
2026-01-20 16:53:17 +00:00
Hennadii Stepanov
4ac651144b cmake, refactor: Deduplicate test-related code
Co-authored-by: furszy <matiasfurszyfer@protonmail.com>
2026-01-13 16:18:48 +00:00
Jonas Nick
4721e077b4 Merge bitcoin-core/secp256k1#1793: doc/bench: added help text for SECP256K1_BENCH_ITERS env var for bench_ecmult
bd5ced1fe1 doc/bench: added help text for SECP256K1_BENCH_ITERS env var for bench_ecmult (kevkevinpal)

Pull request description:

ACKs for top commit:
  real-or-random:
    utACK bd5ced1fe1
  hebasto:
    ACK bd5ced1fe1, I have reviewed the code and it looks OK. Tested on Ubuntu 25.10.
  jonasnick:
    ACK bd5ced1fe1

Tree-SHA512: 7cfc1a8915717bdfe2901f20f578e23368ece9937a40f36805a0a5b741f97a0502a085c973f6912b96c2bca921ef1654908cfe2c90c0601a7ffa92de4415dc62
2026-01-11 20:45:00 +00:00
kevkevinpal
bd5ced1fe1 doc/bench: added help text for SECP256K1_BENCH_ITERS env var for bench_ecmult
In addition a print message saying some tests were skipped was added
2026-01-07 13:02:06 -05:00
Tim Ruffing
47eb70959a ecmult: Use size_t for array indices in _odd_multiplies_table 2026-01-07 11:58:12 +01:00
Tim Ruffing
bb1d199de5 ecmult: Use size_t for array indices into tables 2026-01-07 11:58:12 +01:00
merge-script
2d9137ce9d Merge bitcoin-core/secp256k1#1764: group: Avoid using infinity field directly in other modules
2f73e5281d group: Avoid using infinity field directly in other modules (Tim Ruffing)

Pull request description:

  Minor refactoring to make the abstraction cleaner

ACKs for top commit:
  hebasto:
    ACK 2f73e5281d, I have reviewed the code and it looks OK.
  theStack:
    ACK 2f73e5281d

Tree-SHA512: eae5ad1ce81f491adb48ab1cbf04211f8d43e41255abcacc958fa3dcb1de5021707d56ed1b009a6f3f6c45cd8f20c1f2677891690a3c0a467fc7e064af2512a8
2026-01-06 10:12:53 +01:00
merge-script
8d445730ec Merge bitcoin-core/secp256k1#1783: Add VERIFY_CHECKs and documentation that flags must be 0 or 1
ae00c552df Add VERIFY_CHECKs that flags are 0 or 1 (John Moffett)

Pull request description:

  Flags for constant-time masking rely on the values being exactly `0` or `1` rather than `0` or true (any nonzero). One function, `secp256k1_fe_cmov` [documents](e7f7083b53/src/field.h (L315)) and [`VERIFY_CHECK`s](e7f7083b53/src/field_impl.h (L365)) this, but most don't.

  This updates the documentation and adds `VERIFY_CHECK`s enforcing `flag == 0 || flag == 1` for:

  `secp256k1_fe_storage_cmov`
  `secp256k1_gej_cmov`
  `secp256k1_ge_storage_cmov`
  `secp256k1_scalar_cadd_bit`
  `secp256k1_scalar_cond_negate`
  `secp256k1_scalar_cmov`
  `secp256k1_int_cmov`

ACKs for top commit:
  furszy:
    ACK ae00c55
  hebasto:
    re-ACK ae00c552df.

Tree-SHA512: c9d358929d39d93b0aea602d318429f7e82af96bf601f048a1cdeb0621b8adc6d1204648d352aa2060cb0f63db6dcf0da863854375ed313cea44dfad61c19a18
2025-12-15 20:43:58 +01:00
merge-script
aa2a39c1a7 Merge bitcoin-core/secp256k1#1778: doc/bench: Added cmake build options to bench error messages
3b5b03f301 doc/bench: Added cmake build options to bench error messages (kevkevinpal)

Pull request description:

  ## Motivation
  I wanted to try and run the benchmarking scripts and I noticed the recovery benchmark in `bench.c`. I wanted to run but I was using `cmake` and the error message telling me to use `./configure -enable-module-recovery` wasn't sufficient.

  I figure rather than forcing users to look into the `CMakeLists.txt` file or anywhere else we should add this to the output

  ## Solution
  I appended to the message to include the `-DSECP256K1_ENABLE_MODULE_...=ON` in the message.

ACKs for top commit:
  real-or-random:
    utACK 3b5b03f301
  hebasto:
    ACK 3b5b03f301, I have reviewed the code and it looks OK.

Tree-SHA512: 3a6c966b65ab3f0d6dda81e5dd95529087db3f2901f2686af68c16079ec5b323568f3c9acb155d92a6d50b4102faf0844d0d87216df001adbf69cab4ce86dabc
2025-12-15 20:42:15 +01:00
furszy
d822b29021 test: split monolithic ellswift test into independent cases
No behavior changes.

Refactors the previously monolithic ElligatorSwift test into isolated,
independent test cases. Doing so allows the test suite to execute
these cases in parallel rather than sequentially.

Overall, seen 35-40% tests time reduction locally.

This is quite useful for the Debug build with no optimizations,
which is noticeably slow.

#### Local Debug-build Results (7 jobs):

- master: 138.0 seconds.
- this PR: 89.3 seconds.
   (~1.55× speedup, ~35% reduction)

#### Local Release-build Results (7 jobs):

- master: 9.5 seconds.
- this PR: 5.9 seconds.
   (~1.61× speedup, ~38% reduction)
2025-12-15 09:28:28 -05:00
John Moffett
ae00c552df Add VERIFY_CHECKs that flags are 0 or 1
Flags for constant-time masking rely
on the values being exactly 0 or 1 rather
than 0 or true. Add VERIFY_CHECKs to enforce
in VERIFY builds as a preventative
measure and add documentation where relevant.
2025-12-15 09:07:42 -05:00
merge-script
5c75183344 Merge bitcoin-core/secp256k1#1784: refactor: remove ret from secp256k1_ec_pubkey_serialize
3daab83a60 refactor: remove ret from secp256k1_ec_pubkey_serialize (kevkevinpal)

Pull request description:

  This is a follow-up to https://github.com/bitcoin-core/secp256k1/pull/1774#discussion_r2539737079

  It is pretty straightforward to remove `ret` and to just return either `0` or `1`

ACKs for top commit:
  real-or-random:
    utACK 3daab83a60
  theStack:
    ACK 3daab83a60

Tree-SHA512: ce598d917455a2d25297436bf2b900a9e88a638617cb79ca22e467135035c334b6815911fe4429ff44dbd877e6d10a346d0b37f2e5a7459e5b35854023832d27
2025-12-10 16:13:24 +01:00
kevkevinpal
3daab83a60 refactor: remove ret from secp256k1_ec_pubkey_serialize 2025-12-09 16:08:35 -05:00
Sebastian Falbesoner
8bcda186d2 test: Add non-NULL checks for "pointer of array" API functions 2025-12-09 01:38:48 +01:00
Sebastian Falbesoner
5a08c1bcdc Add ARG_CHECKs to ensure "array of pointers" elements are non-NULL 2025-12-06 01:13:58 +01:00
kevkevinpal
3b5b03f301 doc/bench: Added cmake build options to bench error messages 2025-12-05 09:25:23 -05:00
merge-script
e7f7083b53 Merge bitcoin-core/secp256k1#1774: refactor: split up internal pubkey serialization function into compressed/uncompressed variants
f5e815f430 remove secp256k1_eckey_pubkey_serialize function (Sebastian Falbesoner)
0d3659c547 use new `_eckey_pubkey_serialize{33,65}` functions in modules (ellswift,musig) (Sebastian Falbesoner)
adb76f82ea use new `_eckey_pubkey_serialize{33,65}` functions in public API (Sebastian Falbesoner)
fc7458ca3e introduce `secp256k1_eckey_pubkey_serialize{33,65}` functions (Sebastian Falbesoner)

Pull request description:

  This PR splits up the pubkey serialization function `secp256k1_eckey_pubkey_serialize` into two variants for the compressed (33 bytes) and uncompressed (65 bytes) public key output format each, where only non-infinity group elements as input are allowed. The motivation is to simplify call-sites significantly, as they currently need to introduce two variables and a VERIFY_CHECKs on the return value and the in/out size parameter within a pre-processor block, typically leading to 8 lines of code. By using the new functions, the code is reduced to a single line of code that just calls the function (see #1773). This is helpful for already existing modules on master (ellswift, musig) and upcoming ones (silentpayments, see #1765).

  One drawback is that the public API function `secp256k1_ec_pubkey_serialize` is now slightly more complex (we now call one of two functions instead of a single one, depending on whether the compressed flag is set or not), but that should hopefully not be a problem.

  The commits are intentionally kept small to ease review, happy to squash them if that is preferred.

  (Kudos to w0xlt for the initial idea (https://github.com/bitcoin-core/secp256k1/pull/1765#pullrequestreview-3462461331) and to real-or-random for the suggestion to split the already existing function (https://github.com/bitcoin-core/secp256k1/issues/1773#issuecomment-3540461718).)

ACKs for top commit:
  real-or-random:
    utACK f5e815f430
  w0xlt:
    ACK f5e815f430

Tree-SHA512: da576bbeae477f31ba76c0001f8df08b51fe5e31d67b422a238348ead3341bf37f0c1509ad9d0a93b63e6d61c152707c85beabd02f4eac3b3bdcff129e0ea750
2025-11-27 17:27:08 +01:00
Hennadii Stepanov
7eb86bdb01 autotools: Rename build-aux to autotools-aux
This change improves separation from CMake build directories, which
typically use the "build" prefix.

Additionally, corresponding `.gitignore` entries have been refactored.
2025-11-20 12:40:50 +00:00
merge-script
b6c2a3cd77 Merge bitcoin-core/secp256k1#1761: ecmult_multi: reduce strauss memory usage by 30%
26166c4f5f ecmult_multi: reduce strauss memory usage by 30% (Jonas Nick)

Pull request description:

  This is a draft because I'm not sure about the cleanest way to implement it.

ACKs for top commit:
  real-or-random:
    ACK 26166c4f5f benchmarks show no significant difference (only tried low point counts)
  siv2r:
    tACK 26166c4
  hebasto:
    ACK 26166c4f5f, I have reviewed the code and it looks OK.

Tree-SHA512: f289daee0b0b51451331eefdd99200a78bd83539365d38465c038dc0e6ad940daf821119f7161b08a2390cf046e3859a8f950f2fe881a427aba16353031def7d
2025-11-18 09:36:40 +01:00
Sebastian Falbesoner
f5e815f430 remove secp256k1_eckey_pubkey_serialize function 2025-11-17 18:35:32 +01:00
Sebastian Falbesoner
0d3659c547 use new _eckey_pubkey_serialize{33,65} functions in modules (ellswift,musig) 2025-11-17 18:13:56 +01:00
Sebastian Falbesoner
adb76f82ea use new _eckey_pubkey_serialize{33,65} functions in public API 2025-11-17 17:19:57 +01:00
Sebastian Falbesoner
fc7458ca3e introduce secp256k1_eckey_pubkey_serialize{33,65} functions 2025-11-17 17:12:20 +01:00
Tim Ruffing
2f73e5281d group: Avoid using infinity field directly in other modules 2025-11-06 17:51:08 +01:00