Skip to content
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: move ecrecover implementation to aurora-engine-sdk #996

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 52 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ missing_panics_doc = "allow"
module_name_repetitions = "allow"
unreadable_literal = "allow"
similar_names = "allow"
too_long_first_doc_paragraph = "allow"

[workspace]
resolver = "2"
Expand Down
3 changes: 1 addition & 2 deletions engine-precompiles/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ bn.workspace = true
ethabi.workspace = true
evm.workspace = true
hex.workspace = true
libsecp256k1 = { workspace = true, features = ["static-context", "hmac"] }
num.workspace = true
ripemd.workspace = true
sha2.workspace = true
Expand All @@ -33,7 +32,7 @@ workspace = true

[features]
default = ["std"]
std = ["aurora-engine-types/std", "aurora-engine-sdk/std", "bn/std", "evm/std", "libsecp256k1/std", "ripemd/std", "sha2/std", "sha3/std", "ethabi/std"]
std = ["aurora-engine-types/std", "aurora-engine-sdk/std", "bn/std", "evm/std", "ripemd/std", "sha2/std", "sha3/std", "ethabi/std"]
contract = ["aurora-engine-sdk/contract"]
log = []
error_refund = []
Expand Down
55 changes: 1 addition & 54 deletions engine-precompiles/src/secp256k1.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::prelude::types::{make_address, Address, EthGas};
use crate::prelude::{sdk, vec::Vec, Borrowed, H256};
use crate::{EvmPrecompileResult, Precompile, PrecompileOutput};
#[cfg(not(feature = "contract"))]
use aurora_engine_types::ToString;
use evm::{Context, ExitError};

mod costs {
Expand All @@ -25,38 +23,7 @@ pub fn ecrecover(
hash: H256,
signature: &[u8; consts::SIGNATURE_LENGTH],
) -> Result<Address, ExitError> {
#[cfg(feature = "contract")]
return sdk::ecrecover(hash, signature).map_err(|e| ExitError::Other(Borrowed(e.as_str())));

#[cfg(not(feature = "contract"))]
internal_impl(hash, signature)
}

#[cfg(not(feature = "contract"))]
fn internal_impl(hash: H256, signature: &[u8]) -> Result<Address, ExitError> {
use aurora_engine_types::Cow::Owned;
use sha3::Digest;

let hash = libsecp256k1::Message::parse_slice(hash.as_bytes())
.map_err(|e| ExitError::Other(Owned(e.to_string())))?;
let v = signature[64];
let signature = libsecp256k1::Signature::parse_standard_slice(&signature[0..64])
.map_err(|_| ExitError::Other(Borrowed(sdk::ECRecoverErr.as_str())))?;
let bit = match v {
0..=26 => v,
_ => v - 27,
};

if let Ok(recovery_id) = libsecp256k1::RecoveryId::parse(bit) {
if let Ok(public_key) = libsecp256k1::recover(&hash, &signature, &recovery_id) {
// recover returns a 65-byte key, but addresses come from the raw 64-byte key
let r = sha3::Keccak256::digest(&public_key.serialize()[1..]);
return Address::try_from_slice(&r[12..])
.map_err(|_| ExitError::Other(Borrowed("ERR_INCORRECT_ADDRESS")));
}
}

Err(ExitError::Other(Borrowed(sdk::ECRecoverErr.as_str())))
sdk::ecrecover(hash, signature).map_err(|e| ExitError::Other(Borrowed(e.as_str())))
}

pub struct ECRecover;
Expand Down Expand Up @@ -123,26 +90,6 @@ mod tests {
use super::*;
use crate::utils::new_context;

fn ecverify(hash: H256, signature: &[u8], signer: Address) -> bool {
matches!(ecrecover(hash, signature[0..consts::SIGNATURE_LENGTH].try_into().unwrap()), Ok(s) if s == signer)
}

#[test]
fn test_ecverify() {
let hash = H256::from_slice(
&hex::decode("1111111111111111111111111111111111111111111111111111111111111111")
.unwrap(),
);
let signature =
&hex::decode("b9f0bb08640d3c1c00761cdd0121209268f6fd3816bc98b9e6f3cc77bf82b69812ac7a61788a0fdc0e19180f14c945a8e1088a27d92a74dce81c0981fb6447441b")
.unwrap();
let signer = Address::try_from_slice(
&hex::decode("1563915e194D8CfBA1943570603F7606A3115508").unwrap(),
)
.unwrap();
assert!(ecverify(hash, signature, signer));
}

#[test]
fn test_ecrecover() {
let input = hex::decode("47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad000000000000000000000000000000000000000000000000000000000000001b650acf9d3f5f0a2c799776a1254355d5f4061762a237396a99a0e0e3fc2bcd6729514a0dacb2e623ac4abd157cb18163ff942280db4d5caad66ddf941ba12e03").unwrap();
Expand Down
6 changes: 5 additions & 1 deletion engine-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ autobenches = false
[dependencies]
aurora-engine-types.workspace = true
base64.workspace = true
libsecp256k1 = { workspace = true, features = ["static-context", "hmac"] }
sha2.workspace = true
sha3.workspace = true

[dev-dependencies]
hex.workspace = true

[lints]
workspace = true

[features]
std = ["aurora-engine-types/std", "sha3/std", "sha2/std", "base64/std"]
std = ["aurora-engine-types/std", "libsecp256k1/std", "sha3/std", "sha2/std", "base64/std"]
contract = []
log = []
all-promise-actions = []
Expand Down
Loading
Loading