-
Notifications
You must be signed in to change notification settings - Fork 616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: integrate rust-secp256k1 #1915
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2324c9c
chore: integrate rust-secp256k1
stevencartavia 265b3bd
parity version
stevencartavia 6027fc3
fmt
stevencartavia 5f3311b
dep
stevencartavia ec268ac
restructure,cleanup and order of activation
rakita c95c51a
rm comments
rakita 9afbfbd
Merge remote-tracking branch 'origin/main' into secpt256
rakita File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
#[cfg(feature = "secp256k1")] | ||
pub mod bitcoin_secp256k1; | ||
pub mod k256; | ||
#[cfg(feature = "libsecp256k1")] | ||
pub mod parity_libsecp256k1; | ||
|
||
use crate::{ | ||
utilities::right_pad, PrecompileError, PrecompileOutput, PrecompileResult, | ||
PrecompileWithAddress, | ||
|
@@ -7,65 +13,6 @@ use primitives::{alloy_primitives::B512, Bytes, B256}; | |
pub const ECRECOVER: PrecompileWithAddress = | ||
PrecompileWithAddress(crate::u64_to_address(1), ec_recover_run); | ||
|
||
pub use self::secp256k1::ecrecover; | ||
|
||
#[cfg(not(feature = "secp256k1"))] | ||
#[allow(clippy::module_inception)] | ||
mod secp256k1 { | ||
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) | ||
} | ||
} | ||
|
||
#[cfg(feature = "secp256k1")] | ||
#[allow(clippy::module_inception)] | ||
mod secp256k1 { | ||
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) | ||
} | ||
} | ||
|
||
pub fn ec_recover_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { | ||
const ECRECOVER_BASE: u64 = 3_000; | ||
|
||
|
@@ -84,8 +31,16 @@ pub fn ec_recover_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { | |
let recid = input[63] - 27; | ||
let sig = <&B512>::try_from(&input[64..128]).unwrap(); | ||
|
||
let out = secp256k1::ecrecover(sig, recid, msg) | ||
.map(|o| o.to_vec().into()) | ||
.unwrap_or_default(); | ||
cfg_if::cfg_if! { | ||
if #[cfg(feature = "secp256k1")] { | ||
let res = bitcoin_secp256k1::ecrecover(sig, recid, msg); | ||
} else if #[cfg(feature = "libsecp256k1")] { | ||
let res = parity_libsecp256k1::ecrecover(sig, recid, msg); | ||
} else { | ||
let res = k256::ecrecover(sig, recid, msg); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is were switch is happening, priority is secp256k1 (bitcoin lib), parity (if enabled) and by default k256. |
||
}; | ||
|
||
let out = res.map(|o| o.to_vec().into()).unwrap_or_default(); | ||
Ok(PrecompileOutput::new(ECRECOVER_BASE, out)) | ||
} |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved it to a separate file so it is easier to navigate. and all of them can be enabled in same time