Skip to content

Commit 13fecc2

Browse files
Update ecrecover benchmark to use RustCrypto's ecdsa traits
1 parent 3020527 commit 13fecc2

File tree

2 files changed

+11
-19
lines changed

2 files changed

+11
-19
lines changed

benchmarks/guest/ecrecover/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ openvm-ecc-guest = { path = "../../../extensions/ecc/guest", default-features =
1313
"k256",
1414
] }
1515
openvm-keccak256 = { path = "../../../guest-libs/keccak256/guest", default-features = false }
16+
openvm-k256 = { path = "../../../guest-libs/k256/guest", default-features = false }
1617
# We do not patch revm-precompile so that the benchmark only depends on this repo.
1718
revm-precompile = { version = "14.0.0", default-features = false }
1819
alloy-primitives = { version = "0.8.10", default-features = false, features = [

benchmarks/guest/ecrecover/src/main.rs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
use alloy_primitives::{Bytes, B256, B512};
2-
use k256::{
3-
ecdsa::{Error, RecoveryId, Signature},
4-
Secp256k1,
5-
};
2+
use k256::ecdsa::{Error, RecoveryId, Signature, VerifyingKey};
63
use openvm::io::read_vec;
7-
#[allow(unused_imports)]
8-
use openvm_ecc_guest::{
9-
algebra::IntMod, ecdsa::VerifyingKey, k256::Secp256k1Point, weierstrass::WeierstrassPoint,
10-
};
4+
use openvm_k256::Secp256k1Point;
115
#[allow(unused_imports, clippy::single_component_path_imports)]
126
use openvm_keccak256::keccak256;
137
// export native keccak
@@ -26,8 +20,7 @@ pub fn main() {
2620
}
2721
}
2822

29-
// OpenVM version of ecrecover precompile.
30-
pub fn ecrecover(sig: &B512, mut recid: u8, msg: &B256) -> Result<B256, Error> {
23+
fn ecrecover(sig: &B512, mut recid: u8, msg: &B256) -> Result<B256, Error> {
3124
// parse signature
3225
let mut sig = Signature::from_slice(sig.as_slice())?;
3326
if let Some(sig_normalized) = sig.normalize_s() {
@@ -36,15 +29,13 @@ pub fn ecrecover(sig: &B512, mut recid: u8, msg: &B256) -> Result<B256, Error> {
3629
}
3730
let recid = RecoveryId::from_byte(recid).expect("recovery ID is valid");
3831

39-
// annoying: Signature::to_bytes copies from slice
40-
let recovered_key =
41-
VerifyingKey::<Secp256k1>::recover_from_prehash_noverify(&msg[..], &sig.to_bytes(), recid)?;
42-
let public_key = recovered_key.as_affine();
43-
let mut encoded = [0u8; 64];
44-
encoded[..32].copy_from_slice(&public_key.x().to_be_bytes());
45-
encoded[32..].copy_from_slice(&public_key.y().to_be_bytes());
46-
// hash it
47-
let mut hash = keccak256(&encoded);
32+
let recovered_key = VerifyingKey::recover_from_prehash(&msg[..], &sig, recid)?;
33+
let mut hash = keccak256(
34+
&recovered_key
35+
.to_encoded_point(/* compress = */ false)
36+
.as_bytes()[1..],
37+
);
38+
4839
// truncate to 20 bytes
4940
hash[..12].fill(0);
5041
Ok(B256::from(hash))

0 commit comments

Comments
 (0)