Skip to content

Commit f99b6f4

Browse files
Add ecdsa module to openvm_k256
1 parent a550714 commit f99b6f4

File tree

5 files changed

+29
-6
lines changed

5 files changed

+29
-6
lines changed

benchmarks/guest/ecrecover/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ version = "0.0.0"
55
edition = "2021"
66

77
[dependencies]
8-
k256 = { version = "0.13.3", default-features = false, features = ["ecdsa"] }
98
ecdsa = { version = "0.16.9", default-features = false }
109
openvm = { path = "../../../crates/toolchain/openvm", features = ["std"] }
1110
openvm-platform = { path = "../../../crates/toolchain/platform", default-features = false }
@@ -14,7 +13,7 @@ openvm-ecc-guest = { path = "../../../extensions/ecc/guest", default-features =
1413
"k256",
1514
] }
1615
openvm-keccak256 = { path = "../../../guest-libs/keccak256/guest", default-features = false }
17-
openvm-k256 = { path = "../../../guest-libs/k256/guest", default-features = false }
16+
k256 = { path = "../../../guest-libs/k256/guest", default-features = false, features = ["ecdsa"], package = "openvm-k256" }
1817
# We do not patch revm-precompile so that the benchmark only depends on this repo.
1918
revm-precompile = { version = "14.0.0", default-features = false }
2019
alloy-primitives = { version = "0.8.10", default-features = false, features = [

benchmarks/guest/ecrecover/src/main.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use alloy_primitives::{Bytes, B256, B512};
22
// Be careful not to import k256::ecdsa::{Signature, VerifyingKey}
33
// because those are type aliases that their (non-zkvm) implementations
4-
use ecdsa::{Signature, VerifyingKey};
5-
use k256::ecdsa::{Error, RecoveryId};
4+
use k256::{
5+
ecdsa::{Error, RecoveryId, Signature, VerifyingKey},
6+
Secp256k1Point,
7+
};
68
use openvm::io::read_vec;
7-
use openvm_k256::{Secp256k1, Secp256k1Point};
89
#[allow(unused_imports, clippy::single_component_path_imports)]
910
use openvm_keccak256::keccak256;
1011
// export native keccak
@@ -32,7 +33,7 @@ fn ecrecover(sig: &B512, mut recid: u8, msg: &B256) -> Result<B256, Error> {
3233
}
3334
let recid = RecoveryId::from_byte(recid).expect("recovery ID is valid");
3435

35-
let recovered_key = VerifyingKey::<Secp256k1>::recover_from_prehash(&msg[..], &sig, recid)?;
36+
let recovered_key = VerifyingKey::recover_from_prehash(&msg[..], &sig, recid)?;
3637
let mut hash = keccak256(
3738
&recovered_key
3839
.to_encoded_point(/* compress = */ false)

guest-libs/k256/guest/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@ ecdsa = { workspace = true }
2121
serde = { workspace = true }
2222
hex-literal = { workspace = true }
2323
ff = { workspace = true }
24+
k256 = { workspace = true }
2425

2526
[target.'cfg(not(target_os = "zkvm"))'.dependencies]
2627
num-bigint = { workspace = true }
2728

29+
[features]
30+
default = ["ecdsa"]
31+
ecdsa = ["k256/ecdsa", "ecdsa-core"]
32+
ecdsa-core = ["k256/ecdsa-core"]
33+
2834
[package.metadata.cargo-shear]
2935
ignored = ["openvm", "num-bigint", "serde"]

guest-libs/k256/guest/src/ecdsa.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// re-export types that are visible in the k256 crate for API compatibility
2+
3+
pub use k256::ecdsa::{Error, RecoveryId};
4+
5+
/// ECDSA/secp256k1 signature (fixed-size)
6+
pub type Signature = ecdsa::Signature<crate::Secp256k1>;
7+
8+
/// ECDSA/secp256k1 signing key
9+
#[cfg(feature = "ecdsa")]
10+
pub type SigningKey = ecdsa::SigningKey<crate::Secp256k1>;
11+
12+
/// ECDSA/secp256k1 verification key (i.e. public key)
13+
#[cfg(feature = "ecdsa")]
14+
pub type VerifyingKey = ecdsa::VerifyingKey<crate::Secp256k1>;

guest-libs/k256/guest/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ mod internal;
1313
mod point;
1414
mod scalar;
1515

16+
#[cfg(feature = "ecdsa-core")]
17+
pub mod ecdsa;
18+
1619
// Needs to be public so that the `sw_init` macro can access it
1720
pub use internal::{
1821
Secp256k1Point, Secp256k1Point as AffinePoint, Secp256k1Point as ProjectivePoint,

0 commit comments

Comments
 (0)