1
1
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 } ;
6
3
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 ;
11
5
#[ allow( unused_imports, clippy:: single_component_path_imports) ]
12
6
use openvm_keccak256:: keccak256;
13
7
// export native keccak
@@ -26,8 +20,7 @@ pub fn main() {
26
20
}
27
21
}
28
22
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 > {
31
24
// parse signature
32
25
let mut sig = Signature :: from_slice ( sig. as_slice ( ) ) ?;
33
26
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> {
36
29
}
37
30
let recid = RecoveryId :: from_byte ( recid) . expect ( "recovery ID is valid" ) ;
38
31
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
+
48
39
// truncate to 20 bytes
49
40
hash[ ..12 ] . fill ( 0 ) ;
50
41
Ok ( B256 :: from ( hash) )
0 commit comments