revm_precompile/secp256k1/
bitcoin_secp256k1.rs1use primitives::{alloy_primitives::B512, keccak256, B256};
2use secp256k1::{
3 ecdsa::{RecoverableSignature, RecoveryId},
4 Message, SECP256K1,
5};
6
7use k256 as _;
9
10pub fn ecrecover(sig: &B512, recid: u8, msg: &B256) -> Result<B256, secp256k1::Error> {
11 let recid = RecoveryId::try_from(recid as i32).expect("recovery ID is valid");
12 let sig = RecoverableSignature::from_compact(sig.as_slice(), recid)?;
13
14 let msg = Message::from_digest(msg.0);
15 let public = SECP256K1.recover_ecdsa(&msg, &sig)?;
16
17 let mut hash = keccak256(&public.serialize_uncompressed()[1..]);
18 hash[..12].fill(0);
19 Ok(hash)
20}