-
Notifications
You must be signed in to change notification settings - Fork 619
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: integrate rust-secp256k1 (#1915)
* chore: integrate rust-secp256k1 * parity version * fmt * dep * restructure,cleanup and order of activation * rm comments --------- Co-authored-by: rakita <[email protected]>
- Loading branch information
1 parent
60ce865
commit d2a3b5b
Showing
6 changed files
with
180 additions
and
71 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use primitives::{alloy_primitives::B512, keccak256, B256}; | ||
use secp256k1::{ | ||
ecdsa::{RecoverableSignature, RecoveryId}, | ||
Message, SECP256K1, | ||
}; | ||
|
||
// Silence the unused crate dependency warning. | ||
use k256 as _; | ||
|
||
pub fn ecrecover(sig: &B512, recid: u8, msg: &B256) -> Result<B256, secp256k1::Error> { | ||
let recid = RecoveryId::from_i32(recid as i32).expect("recovery ID is valid"); | ||
let sig = RecoverableSignature::from_compact(sig.as_slice(), recid)?; | ||
|
||
let msg = Message::from_digest(msg.0); | ||
let public = SECP256K1.recover_ecdsa(&msg, &sig)?; | ||
|
||
let mut hash = keccak256(&public.serialize_uncompressed()[1..]); | ||
hash[..12].fill(0); | ||
Ok(hash) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use k256::ecdsa::{Error, RecoveryId, Signature, VerifyingKey}; | ||
use primitives::{alloy_primitives::B512, keccak256, B256}; | ||
|
||
pub fn ecrecover(sig: &B512, mut recid: u8, msg: &B256) -> Result<B256, Error> { | ||
// parse signature | ||
let mut sig = Signature::from_slice(sig.as_slice())?; | ||
|
||
// normalize signature and flip recovery id if needed. | ||
if let Some(sig_normalized) = sig.normalize_s() { | ||
sig = sig_normalized; | ||
recid ^= 1; | ||
} | ||
let recid = RecoveryId::from_byte(recid).expect("recovery ID is valid"); | ||
|
||
// recover key | ||
let recovered_key = VerifyingKey::recover_from_prehash(&msg[..], &sig, recid)?; | ||
// hash it | ||
let mut hash = keccak256( | ||
&recovered_key | ||
.to_encoded_point(/* compress = */ false) | ||
.as_bytes()[1..], | ||
); | ||
|
||
// truncate to 20 bytes | ||
hash[..12].fill(0); | ||
Ok(hash) | ||
} |
Oops, something went wrong.