Skip to content

feat: add optional secp256k1 backend for ECDSA operations - #1104

Merged
DaniPopes merged 2 commits into
alloy-rs:mainfrom
decofe:feat/secp256k1-backend
May 14, 2026
Merged

DaniPopes merged 2 commits into
alloy-rs:mainfrom
decofe:feat/secp256k1-backend

Conversation

@decofe

@decofe decofe commented May 14, 2026

Copy link
Copy Markdown
Contributor

Summary

Add a secp256k1 feature (disabled by default) to alloy-primitives and alloy-core that uses the C libsecp256k1 bindings (secp256k1 v0.31) as an alternative to the pure-Rust k256 crate for elliptic curve operations.

When both k256 and secp256k1 features are enabled, secp256k1 is preferred for address recovery operations.

Changes

Feature wiring

  • Workspace dep: secp256k1 = { version = "0.31", default-features = false }
  • alloy-primitives: optional dep with alloc + recovery features; global-context enabled under std
  • alloy-core: forwards secp256k1 feature to alloy-primitives

Backend-neutral recovery

recover_address_from_msg and recover_address_from_prehash now compile under #[cfg(any(feature = "k256", feature = "secp256k1"))] and use cfg_if! to select the preferred backend:

  1. secp256k1 (if enabled) — uses Secp256k1::recover_ecdsa + RecoverableSignature
  2. k256 (fallback) — uses VerifyingKey::recover_from_prehash

New secp256k1-specific API surface

  • Signature::to_secp256k1() / to_secp256k1_recoverable()
  • Signature::from_secp256k1_signature_and_parity()
  • Signature::recover_secp256k1_from_prehash()
  • Address::from_secp256k1_public_key() / from_secp256k1_secret_key()
  • From<(RecoverableSignature, RecoveryId)> for Signature
  • TryFrom<Signature> for secp256k1::ecdsa::Signature
  • SignatureError::Secp256k1 variant

Preserved

All existing k256-gated API is untouched — no breaking changes.

Testing

Tested with all feature combinations:

  • --features secp256k1 ✅
  • --features k256 ✅
  • --features k256,secp256k1 ✅
  • --no-default-features --features secp256k1 ✅

Recovery tests (can_recover_tx_sender_not_normalized, recover_web3_signature) broadened to run under both backends.

@decofe
decofe requested review from DaniPopes and mattsse as code owners May 14, 2026 00:41
decofe pushed a commit to decofe/eips that referenced this pull request May 14, 2026
Add an optional `secp256k1` feature to `alloy-eip7702` that uses
alloy-primitives' secp256k1 backend for ECDSA public key recovery.

The feature is disabled by default. When both `secp256k1` and `k256`
features are enabled, `secp256k1` is preferred (handled by
alloy-primitives internally via `cfg_if!`).

This avoids importing k256 or secp256k1 directly for recovery —
the backend selection is fully delegated to alloy-primitives'
`Signature::recover_address_from_prehash`.

Changes:
- Add `secp256k1` feature to eip7702 that forwards to
  `alloy-primitives/secp256k1`
- Unify recovery impl under `#[cfg(any(feature = "k256", feature = "secp256k1"))]`
  instead of separate impl blocks
- Patch alloy-primitives to decofe/core#feat/secp256k1-backend
  (alloy-rs/core#1104) until merged and released

Depends on: alloy-rs/core#1104

Amp-Thread-ID: https://ampcode.com/threads/T-019e23bb-1e32-718f-8ec4-5f3b7bef33e2
decofe pushed a commit to decofe/eips that referenced this pull request May 14, 2026
Add an optional `secp256k1` feature to `alloy-eip7702` that uses
alloy-primitives' secp256k1 backend for ECDSA public key recovery.

The feature is disabled by default. When both `secp256k1` and `k256`
features are enabled, `secp256k1` is preferred (handled by
alloy-primitives internally via `cfg_if!`).

This avoids importing k256 or secp256k1 directly for recovery —
the backend selection is fully delegated to alloy-primitives'
`Signature::recover_address_from_prehash`.

Changes:
- Add `secp256k1` feature to eip7702 that forwards to
  `alloy-primitives/secp256k1`
- Unify recovery impl under `#[cfg(any(feature = "k256", feature = "secp256k1"))]`
  instead of separate impl blocks
- Patch alloy-primitives to decofe/core#feat/secp256k1-backend
  (alloy-rs/core#1104) until merged and released

Depends on: alloy-rs/core#1104

Amp-Thread-ID: https://ampcode.com/threads/T-019e23bb-1e32-718f-8ec4-5f3b7bef33e2
decofe pushed a commit to decofe/eips that referenced this pull request May 14, 2026
Add an optional `secp256k1` feature to `alloy-eip7702` that uses
alloy-primitives' secp256k1 backend for ECDSA public key recovery.

The feature is disabled by default. When both `secp256k1` and `k256`
features are enabled, `secp256k1` is preferred (handled by
alloy-primitives internally via `cfg_if!`).

This avoids importing k256 or secp256k1 directly for recovery —
the backend selection is fully delegated to alloy-primitives'
`Signature::recover_address_from_prehash`.

Changes:
- Add `secp256k1` feature to eip7702 that forwards to
  `alloy-primitives/secp256k1`
- Unify recovery impl under `#[cfg(any(feature = "k256", feature = "secp256k1"))]`
  instead of separate impl blocks
- Patch alloy-primitives to decofe/core#feat/secp256k1-backend
  (alloy-rs/core#1104) until merged and released

Depends on: alloy-rs/core#1104

Amp-Thread-ID: https://ampcode.com/threads/T-019e23bb-1e32-718f-8ec4-5f3b7bef33e2
@github-project-automation github-project-automation Bot moved this to Reviewed in Alloy May 14, 2026
Comment thread crates/primitives/src/signature/sig.rs Resolved
Add an optional `secp256k1` feature (disabled by default) to
`alloy-primitives` and `alloy-core` that uses the `secp256k1` C
library bindings as an alternative to the pure-Rust `k256` crate for
secp256k1 curve operations.

When both `k256` and `secp256k1` features are enabled, the
`secp256k1` backend is preferred for address recovery operations.

Changes:
- Add `secp256k1 = "0.31"` workspace dependency with `recovery` feature
- Add `secp256k1` optional dep and feature to alloy-primitives
- Forward `secp256k1` feature through alloy-core
- Add `From<secp256k1::ecdsa::RecoverableSignature>` for Signature
- Add `to_secp256k1()`, `secp256k1_recid()` methods on Signature
- Add `recover_from_prehash_secp256k1()` and `recover_from_msg_secp256k1()`
- Use secp256k1 backend in `recover_address_from_prehash` when enabled
- Add `Address::from_secp256k1_public_key()` and
  `Address::from_secp256k1_secret_key()`
- Add `SignatureError::Secp256k1` variant

Amp-Thread-ID: https://ampcode.com/threads/T-019e23c4-b4d3-772c-81d0-b858df960e84
@decofe
decofe force-pushed the feat/secp256k1-backend branch from 2ec50b3 to 21e2a77 Compare May 14, 2026 13:46
- Add alloc feature to secp256k1 workspace dep so context creation
  works in no_std environments
- Fix formatting of matches! expression
- Add CC0-1.0 license exceptions for secp256k1, secp256k1-sys,
  bitcoin_hashes, and bitcoin-io in deny.toml

Amp-Thread-ID: https://ampcode.com/threads/T-019e23c4-b4d3-772c-81d0-b858df960e84
@DaniPopes
DaniPopes enabled auto-merge (squash) May 14, 2026 14:07
@DaniPopes
DaniPopes merged commit 45dfd95 into alloy-rs:main May 14, 2026
60 of 62 checks passed
@github-project-automation github-project-automation Bot moved this from Reviewed to Done in Alloy May 14, 2026
decofe pushed a commit to decofe/eips that referenced this pull request May 14, 2026
DaniPopes pushed a commit to alloy-rs/eips that referenced this pull request May 14, 2026
* feat(eip7702): add `secp256k1` feature for ECDSA recovery

Add an optional `secp256k1` feature to `alloy-eip7702` that uses
alloy-primitives' secp256k1 backend for ECDSA public key recovery.

The feature is disabled by default. When both `secp256k1` and `k256`
features are enabled, `secp256k1` is preferred (handled by
alloy-primitives internally via `cfg_if!`).

This avoids importing k256 or secp256k1 directly for recovery —
the backend selection is fully delegated to alloy-primitives'
`Signature::recover_address_from_prehash`.

Changes:
- Add `secp256k1` feature to eip7702 that forwards to
  `alloy-primitives/secp256k1`
- Unify recovery impl under `#[cfg(any(feature = "k256", feature = "secp256k1"))]`
  instead of separate impl blocks
- Patch alloy-primitives to decofe/core#feat/secp256k1-backend
  (alloy-rs/core#1104) until merged and released

Depends on: alloy-rs/core#1104

Amp-Thread-ID: https://ampcode.com/threads/T-019e23bb-1e32-718f-8ec4-5f3b7bef33e2

* chore: bump alloy-primitives to 1.6.0 and remove patch

alloy-core 1.6.0 includes native secp256k1 backend support
(alloy-rs/core#1104), so the temporary git patch is no longer needed.

Amp-Thread-ID: https://ampcode.com/threads/T-019e23bb-1e32-718f-8ec4-5f3b7bef33e2

* chore: revert temporary deny.toml changes and fix trailing whitespace

Amp-Thread-ID: https://ampcode.com/threads/T-019e23bb-1e32-718f-8ec4-5f3b7bef33e2

---------

Co-authored-by: Centaur AI <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants