Commit Graph

471 Commits

Author SHA1 Message Date
mllwchrry
65093e1444 surjection: prevent s-value reuse for different proof inputs
The s-values produced by secp256k1_surjection_genrand previously
depended only on their indices and the difference between
input_blinding_key and output_blinding_key. Calls with the same
difference therefore reused s-values even when their proof inputs
differed.

For proofs with the same used-input selection and honest input index,
the same generated s-value was used as the signing nonce. Reusing this
nonce across different proof messages allowed recovery of the
blinding-key difference. The remaining repeated s-values also revealed
the honest input index.

This commit affects proof generation only; verification is unchanged.
2026-08-13 17:35:31 +03:00
mllwchrry
217fe59bbf tests: register run_util_tests and run_ec_commit, remove dead print_vector 2026-07-03 15:17:36 +03:00
mllwchrry
b1f9e6e360 Merge branch 'master' into sync-ebf59432 2026-07-02 18:43:56 +03:00
Sebastian Falbesoner
d7125e517d test: musig: fix dead "aggnonce encodes two points at infinity" check 2026-06-10 00:21:24 +02:00
Sebastian Falbesoner
9e017e5062 refactor: rename _ecmult_gen -> _ecmult_gen_gej for consistency
Now that we have a function `_ecmult_gen_ge`, it makes sense to rename
the existing function `_ecmult_gen` to `_ecmult_gen_gej` for
consistency, to signal that the result is a Jacobian group element.

This diff was created by applying
```
$ sed -i s/secp256k1_ecmult_gen\(/secp256k1_ecmult_gen_gej\(/g $(git ls-files)
```
2026-06-07 20:21:18 +02:00
Sebastian Falbesoner
a3296d5e23 refactor: introduce _ecmult_gen_ge helper (preventing accidental gej leaks)
Scalar multiplication with the generator point frequently involves a
conversion to affine coordinates and clearing out the temporary Jacobian
group element object after to avoid leaking secret key material, i.e.
executing the following three steps:
    - secp256k1_ecmult_gen(ctx, &rj, ...)
    - secp256k1_ge_set_gej(&r, &rj)
    - secp256k1_gej_clear(&rj)

This commit introduces a corresponding helper to deduplicate code
and mitigate the risk that last step is forgotten (which can easily
happen and is not detected by tests).

The idea came up during a conversation with furszy, see
https://github.com/bitcoin-core/secp256k1/pull/1765#issuecomment-4482838033
2026-06-07 20:21:18 +02:00
merge-script
c63062380f Merge bitcoin-core/secp256k1#1852: Add exhaustive test for ECDH module
5698e66c64 Add exhaustive test for ECDH module (Sebastian Falbesoner)

Pull request description:

  This PR adds an exhaustive test for the ECDH module, looping over all key combinations and verifying the commutativity property (ECDH(i\*G, j) == ECDH(j\*G, i)) and checking against a recalculated ECDH result (by manually invoking the default ECDH hash function on the precalculated group element `group[i * j]`'s coordinates). The existing test coverage is already solid (including Wycheproof test vectors), but I figured it likely wouldn't hurt to add this as well.

ACKs for top commit:
  sipa:
    ACK 5698e66c64
  real-or-random:
    utACK 5698e66c64

Tree-SHA512: e80b8508ee61e3bf5230951393a08c8937f19d2d16220ff2b02fe69b039195a1545809d3ea420dfed1f192c3711f403c63e86c3af2bb2fc4ab1254388ba50287
2026-06-07 13:38:34 +02:00
merge-script
95b983597a Merge BlockstreamResearch/secp256k1-zkp#361: ecdsa_adaptor: make DLEQ nonce generation pluggable
b36c4ab717 ecdsa_adaptor: use context hash functions in nonce generation (DarkWindman)

Pull request description:

  Addresses #359.

ACKs for top commit:
  real-or-random:
    utACK b36c4ab717

Tree-SHA512: 0262dfcc2ec2e3d91e37c85bcf1243099aa11d9f4bff115400ce81d36a223818e09c02c41c0602fcbeeaa1f129c4f936ef643afb7149d1230cec2ea57f5b3894
2026-06-03 13:40:27 +02:00
DarkWindman
b36c4ab717 ecdsa_adaptor: use context hash functions in nonce generation 2026-06-03 13:07:37 +03:00
Sebastian Falbesoner
5698e66c64 Add exhaustive test for ECDH module 2026-06-02 14:25:34 +02:00
merge-script
cfb3312645 Merge BlockstreamResearch/secp256k1-zkp#364: Upstream PRs 1854
af1fdd1215 tests: compare full MuSig aggregate nonce (w0xlt)

Pull request description:

  This PR has been created by a GitHub Actions workflow without human involvement.

  [bitcoin-core/secp256k1#1854]: tests: compare full MuSig aggregate nonce

  Tips:
   * Use `git show --remerge-diff <pr-branch>` to show the conflict resolution in the merge commit.
   * Use `git read-tree --reset -u <pr-branch>` to replay these resolutions during the conflict resolution stage when recreating the PR branch locally.
     Be aware that this may discard your index as well as the uncommitted changes and untracked files in your worktree.

ACKs for top commit:
  mllwchrry:
    ACK 8363a2d

Tree-SHA512: 55406814de269c612159922ae521dd874887107ee9bd26cff81e2e2cca28ade74a0609e5c48e25a7d9913698899da146aef8b3eebf96e5ad70933a884d75a655
2026-06-01 13:11:35 +03:00
w0xlt
af1fdd1215 tests: compare full MuSig aggregate nonce 2026-05-12 15:26:12 -07:00
merge-script
45f6f0f158 Merge BlockstreamResearch/secp256k1-zkp#363: Upstream PRs 1846, 1848, 1849
8479eafa57 musig: always clear out secret key in `secp256k1_musig_nonce_gen_counter` (Sebastian Falbesoner)
3cca6451a2 ci: Bump GCC snapshot major version to 17 (Hennadii Stepanov)
285cb788e9 ci: Replace `ilammy/msvc-dev-cmd` with manual MSVC setup (Hennadii Stepanov)

Pull request description:

  This PR has been created by a GitHub Actions workflow without human involvement.

  [bitcoin-core/secp256k1#1846]: ci: Replace `ilammy/msvc-dev-cmd` with manual MSVC setup
  [bitcoin-core/secp256k1#1848]: ci: Bump GCC snapshot major version to 17
  [bitcoin-core/secp256k1#1849]: musig: always clear out secret key in `secp256k1_musig_nonce_gen_counter`

  Tips:
   * Use `git show --remerge-diff <pr-branch>` to show the conflict resolution in the merge commit.
   * Use `git read-tree --reset -u <pr-branch>` to replay these resolutions during the conflict resolution stage when recreating the PR branch locally.
     Be aware that this may discard your index as well as the uncommitted changes and untracked files in your worktree.

ACKs for top commit:
  mllwchrry:
    ACK b11340b. This sync PR can be merged as-is.
  DarkWindman:
    ACK b11340b3ce

Tree-SHA512: 4b311e9bfa21f00b4780202c08af9d00380f5b3df40704641764d706cfc3408615b6206c7c82ca915b56c27a354bdf24680674269a28eefe7d4e93adc06cbaa5
2026-05-05 16:23:21 +03:00
Sebastian Falbesoner
8479eafa57 musig: always clear out secret key in secp256k1_musig_nonce_gen_counter
Even though `secp256k1_musig_nonce_gen_internal` can currently only fail
if the API is misused (invalid `keypair` or `keyagg_cache` parameters),
clear out the buffer holding secret key data as well in this case to
follow best practices.

The issue was found and reported by l0rinc using GPT 5.5 (Thanks!).
2026-04-28 23:22:29 +02:00
DarkWindman
baac08d207 modules, tests: Port bitcoin-core/secp256k1#1777 and bitcoin-core/secp256k1#1824 to zkp-specific code 2026-04-20 15:39:54 +03:00
DarkWindman
f7e7e6bb15 Merge branch 'master' into sync-7262adb4 2026-04-01 17:29:57 +03:00
mllwchrry
b84635ed3b tests: Fix C89 function pointer initialization in ellswift tests 2026-03-20 16:45:28 +02:00
merge-script
b0ddc0357e Merge BlockstreamResearch/secp256k1-zkp#338: ci: enable surjectionproof in CI module-enabled configurations
4359f050cc surjection: Remove test that reads out of bounds (Tim Ruffing)
78999f3a9a surjection: Fix leading whitespace (Tim Ruffing)
229e1f127a surjection: Fix read of uninitialized value in tests (Tim Ruffing)
c0a26a9c1b ci: enable surjectionproof module in CI configs (copilot-swe-agent[bot])

Pull request description:

  The CI workflow enabled the ZKP experimental module set but omitted surjectionproof, so that module was never exercised in those jobs. This change wires surjectionproof into the same CI paths as the other enabled ZKP modules.

  - **CI module plumbing (`ci/ci.sh`)**
    - Added `SURJECTIONPROOF` to the exported/printed CI env set.
    - Passed `SURJECTIONPROOF` into configure via `--enable-module-surjectionproof`.

  - **Workflow env defaults and matrices (`.github/workflows/ci.yml`)**
    - Added a global default: `SURJECTIONPROOF: 'no'`.
    - Added `SURJECTIONPROOF: 'yes'` in every job/matrix env block that already enables the ZKP module group (`RANGEPROOF`, `WHITELIST`, `GENERATOR`, etc.).

  - **Representative wiring change**
    ```sh
    --enable-module-rangeproof="$RANGEPROOF" \
    --enable-module-surjectionproof="$SURJECTIONPROOF" \
    --enable-module-whitelist="$WHITELIST"
    ```

  <!-- START COPILOT ORIGINAL PROMPT -->

  <details>

  <summary>Original prompt</summary>

  >
  > ----
  >
  > *This section details on the original issue you should resolve*
  >
  > <issue_title>ci: surjectionproof module is never enabled</issue_title>
  > <issue_description>There's not even a SURJECTIONPROOF env variable in the GitHub Actions workflow... This should be enabled everywhere where the other ZKP modules are enabled.
  >
  > It may be better to enable all modules by default, but that's a change that could potentially benefit even upstream, so maybe let's first just fix this by enabling surjectionproof.</issue_description>
  >
  > <agent_instructions>Only add enabling of the surjectionproof module (no enabling of all modules by default)</agent_instructions>
  >
  > ## Comments on the Issue (you are @copilot in this section)
  >
  > <comments>
  > </comments>
  >

  </details>

  <!-- START COPILOT CODING AGENT SUFFIX -->

  - Fixes BlockstreamResearch/secp256k1-zkp#334

ACKs for top commit:
  mllwchrry:
    ACK 4359f05

Tree-SHA512: 13d776a76f73db6e74ca253099583da3d2600f424ea1f6bcbc9cd12544721477225a4190ffd34f4f1a77ab3b1d1e21d1f6d83409aa969bafe66a7978bef1b2d2
2026-03-06 12:12:06 +01:00
Tim Ruffing
4359f050cc surjection: Remove test that reads out of bounds 2026-03-05 21:27:19 +01:00
Tim Ruffing
78999f3a9a surjection: Fix leading whitespace 2026-03-05 21:12:46 +01:00
Tim Ruffing
229e1f127a surjection: Fix read of uninitialized value in tests 2026-03-05 21:12:40 +01:00
Tim Ruffing
ed985641f4 ecdsa_adaptor: Make arg order in dleq_{prove,verify} consistent 2026-03-05 13:28:23 +01:00
Tim Ruffing
a7d0f246d7 ecdsa_adaptor: Simplify code 2026-03-05 13:25:12 +01:00
Tim Ruffing
41a8a2a65b ecdsa_adaptor: Clarify identifiers 2026-03-05 13:25:12 +01:00
Tim Ruffing
7f1c5390c2 ecdsa_adaptor: Make files more self-contained 2026-03-05 10:09:45 +01:00
Tim Ruffing
dd8db2ea2b ecdsa_adaptor: Run tests with default and overflowing nonce function 2026-03-05 09:38:10 +01:00
Tim Ruffing
a4af91d5b9 ecdsa_adaptor: Add test case for R1==infinity in DLEQ proof 2026-03-05 09:33:53 +01:00
merge-script
5a67b63ecd Merge BlockstreamResearch/secp256k1-zkp#339: ecdsa_adaptor: Fix secp256k1_dleq_verify abort on infinity points (#335)
799a27c813 ecdsa_adaptor: Check for infinity in secp256k1_dleq_verify (mllwchrry)

Pull request description:

  Address #335.

ACKs for top commit:
  real-or-random:
    ACK 799a27c813

Tree-SHA512: 9634ccd864cabbe3188e19a551f18cb21cd634955276e9a12fd92977b71ecde4bec1159e9f7baf6157c5b4c6b956ed938600f85f70379603704ec738035eb4c7
2026-03-05 09:02:31 +01:00
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
mllwchrry
799a27c813 ecdsa_adaptor: Check for infinity in secp256k1_dleq_verify 2026-03-03 17:33:57 +02: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
mllwchrry
48cbd78dfc modules: Port bitcoin-core/secp256k1#1825 to zkp-specific code 2026-03-03 15:42:49 +02: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
mllwchrry
126501f58b modules: Port bitcoin-core/secp256k1#1815 to zkp-specific code 2026-03-03 15:16:04 +02:00
mllwchrry
3b2ceb3e7a Merge commits '14e56970 1605b02f cd49c57e 453949ab 57315a69 97de5120 c5da3bde 99ab4a10 d071aa56 1d146ac3 322d0a43 c7a7f732 ac561601 dfe042fe 3019186a 95e68158 10f546a2 c0a2aba0 ' into temp-merge-1811 2026-03-03 14:45:28 +02:00
mllwchrry
fe48cc9fa5 generator: Port bitcoin-core/secp256k1#1764 to zkp-specific code 2026-03-03 13:08:07 +02:00
mllwchrry
d111d31293 generator: Port bitcoin-core/secp256k1#1779 to zkp-specific code 2026-03-03 13:08:07 +02:00
mllwchrry
2542b43451 modules: Port bitcoin-core/secp256k1#1774 to zkp-specific code 2026-03-03 13:07:59 +02:00
mllwchrry
07d4de6433 Merge commits '115b135f c8206b1c b6c2a3cd e7f7083b be5e4f02 5c751833 540fec8a aa2a39c1 8d445730 f9a944ff 2d9137ce 4721e077 471e3a13 ebb35882 1a53f496 c7a52400 ' into temp-merge-1809 2026-03-02 15:56:43 +02:00
mllwchrry
3f7a9429ad ecdsa_adaptor: batch affine conversion in dleq_pair and dleq_prove 2026-03-02 12:41:51 +02:00
mllwchrry
d380549e38 ecdsa_adaptor: optimize encrypt with batch affine conversion 2026-03-02 12:33:12 +02: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
DarkWindman
7111d365fb modules, tests: Port bitcoin-core/secp256k1#1734 to zkp-specific code 2026-02-27 14:47:59 +02:00
DarkWindman
7699fe9aa6 modules: Port bitcoin-core/secp256k1#1735 to zkp-specific code 2026-02-27 14:47:59 +02:00
DarkWindman
f1e52fac20 Merge commits '88be4e8d b4756543 10dab907 58178851 de6af6ae baa26542 2b7337f6 a44a3393 f44c1ebd d543c0d9 43e7b115 7a2fff85 ' into temp-merge-1758 2026-02-27 14:47:34 +02: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
DarkWindman
01b1b916eb modules: Port bitcoin-core/secp256k1#1725 to zkp-specific code 2026-02-25 13:16:24 +02:00
DarkWindman
38284aa008 Merge commits '2c076d90 20e3b447 74b8068c e523e4f9 d5997141 d2dcf520 f36afb8b 8113671f d93380fb 03fb60ad 4985ac0f 36e76952 ' into temp-merge-1738 2026-02-25 10:44:34 +02:00
mllwchrry
9dcd857d54 Merge commits '29e73f4b 89096c23 c4987790 ad60ef7e 943479a7 cbbbf3bd 73a69595 7c338042 5e74086d 6037833c 020ee604 a660a497 b9313c6e ' into temp-merge-1708 2026-02-24 13:31:44 +02:00