Skip to content

Commit

Permalink
add precompile ed25519 verify (#1031)
Browse files Browse the repository at this point in the history
Co-authored-by: shaorongqiang <shaorongqiang@email.com>
  • Loading branch information
shaorongqiang and shaorongqiang authored Dec 1, 2023
1 parent b8e88ae commit cc64a79
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ members = [
"src/components/contracts/modules/evm/precompile/anemoi",
"src/components/contracts/modules/evm/precompile/blake2",
"src/components/contracts/modules/evm/precompile/bn128",
"src/components/contracts/modules/evm/precompile/ed25519_verify",
"src/components/contracts/modules/evm/precompile/utils",
"src/components/contracts/modules/evm/precompile/utils/macro",
"src/components/contracts/modules/xhub",
Expand Down
1 change: 1 addition & 0 deletions src/components/contracts/modules/evm/precompile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ evm-precompile-sha3fips = {path = "./sha3fips"}
evm-precompile-anemoi = {path = "./anemoi"}
evm-precompile-blake2 = {path = "./blake2"}
evm-precompile-bn128 = {path = "./bn128"}
evm-precompile-ed25519-verify = {path = "./ed25519_verify"}
fp-core = {path = "../../../primitives/core"}
module-evm = {path = "../../../modules/evm"}
parking_lot = "0.12"
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "evm-precompile-ed25519-verify"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
evm = { version = "0.35.0", default-features = false, features = ["with-serde"] }
evm-precompile-utils = { path = "../utils"}
tracing = "0.1"
module-evm = { path = "../../../../modules/evm"}
num_enum = { version = "0.5.4", default-features = false }
zei = { git = "https://github.com/FindoraNetwork/zei", branch = "stable-main" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use evm::executor::stack::{PrecompileFailure, PrecompileOutput};
use evm::{Context, ExitError, ExitSucceed};
use module_evm::precompile::{FinState, Precompile, PrecompileId, PrecompileResult};
use zei::{
serialization::ZeiFromToBytes,
xfr::sig::{XfrPublicKey, XfrSignature},
};

pub struct Ed25519Verify;

impl Ed25519Verify {
const GAS_COST: u64 = 50000; // https://eips.ethereum.org/EIPS/eip-1108
}

impl PrecompileId for Ed25519Verify {
fn contract_id() -> u64 {
0x2003
}
}

impl Precompile for Ed25519Verify {
fn execute(
input: &[u8],
_target_gas: Option<u64>,
_context: &Context,
_state: &FinState,
) -> PrecompileResult {
if input.len() < 128 {
return Err(PrecompileFailure::Error {
exit_status: ExitError::Other("input must contain 128 bytes".into()),
});
};
let pk = &input[0..32];
let msg = &input[32..64];
let sig = &input[64..128];
let pub_key =
XfrPublicKey::zei_from_bytes(pk).map_err(|_| PrecompileFailure::Error {
exit_status: ExitError::Other("Public key recover failed".into()),
})?;
let sig =
XfrSignature::zei_from_bytes(sig).map_err(|_| PrecompileFailure::Error {
exit_status: ExitError::Other("Signature recover failed".into()),
})?;

let mut buf = [0u8; 4];
if pub_key.verify(msg, &sig).is_ok() {
buf[3] = 0u8;
} else {
buf[3] = 1u8;
};

Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
cost: Self::GAS_COST,
output: buf.to_vec(),
logs: Default::default(),
})
}
}
4 changes: 4 additions & 0 deletions src/components/contracts/modules/evm/precompile/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use ethereum_types::H160;
use evm::{executor::stack::PrecompileSet, Context};
use evm_precompile_ed25519_verify::Ed25519Verify;
use module_evm::precompile::{Precompile, PrecompileResult};
use std::marker::PhantomData;

Expand Down Expand Up @@ -79,6 +80,9 @@ where
a if a == H160::from_low_u64_be(Anemoi::contract_id()) => {
Some(Anemoi::execute(input, target_gas, context, ctx))
}
a if a == H160::from_low_u64_be(Ed25519Verify::contract_id()) => {
Some(Ed25519Verify::execute(input, target_gas, context, ctx))
}
//a if a == H160::from_low_u64_be(EthPairing::contract_id()) => {
// Some(EthPairing::execute(input, target_gas, context, ctx))
//}
Expand Down
2 changes: 1 addition & 1 deletion src/components/finutils/src/bins/fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ fn run() -> Result<()> {
let td_addr = m.value_of("validator-td-addr");
common::unstake(am, staker.as_deref(), td_addr).c(d!())?;
} else if let Some(m) = matches.subcommand_matches("claim") {
let am = m.value_of("amount");
let am = None;
let seckey = match m.value_of("seckey") {
Some(path) => {
Some(fs::read_to_string(path).c(d!("Failed to read seckey file"))?)
Expand Down
6 changes: 0 additions & 6 deletions src/components/finutils/src/bins/fn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,6 @@ subcommands:
long: validator-td-addr
takes_value: true
value_name: TendermintAddr
- amount:
help: how much `FRA unit`s to claim
short: n
long: amount
takes_value: true
value_name: Amount
- seckey:
help: the file which contains base64-formated `XfrPrivateKey` of an existing wallet
long: seckey
Expand Down

0 comments on commit cc64a79

Please sign in to comment.