-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add precompile ed25519 verify (#1031)
Co-authored-by: shaorongqiang <shaorongqiang@email.com>
- Loading branch information
1 parent
b8e88ae
commit cc64a79
Showing
7 changed files
with
80 additions
and
7 deletions.
There are no files selected for viewing
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
14 changes: 14 additions & 0 deletions
14
src/components/contracts/modules/evm/precompile/ed25519_verify/Cargo.toml
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,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" } |
59 changes: 59 additions & 0 deletions
59
src/components/contracts/modules/evm/precompile/ed25519_verify/src/lib.rs
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,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(), | ||
}) | ||
} | ||
} |
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