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