From ab16fe9109387cb35bf0edf6a81093f62fdbe6b7 Mon Sep 17 00:00:00 2001 From: MikkySnow Date: Tue, 8 Oct 2024 15:31:13 +0000 Subject: [PATCH] chore: add missing files to git --- crates/static-precompiles/Cargo.toml | 30 ++++++++++++++ crates/static-precompiles/src/lib.rs | 61 ++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 crates/static-precompiles/Cargo.toml create mode 100644 crates/static-precompiles/src/lib.rs diff --git a/crates/static-precompiles/Cargo.toml b/crates/static-precompiles/Cargo.toml new file mode 100644 index 00000000..69fb357f --- /dev/null +++ b/crates/static-precompiles/Cargo.toml @@ -0,0 +1,30 @@ +[package] +name = "static-precompiles" +version = "0.1.0" +edition = "2021" + +[dependencies] +primitive-types = { version = "^0.12", default-features = false, features = ["rlp"] } +evm = { git = "https://github.com/SigmaGmbH/evm.git", default-features = false, commit = "b76ffcde60078059e99f5f34a32b2b743767619b"} +substrate-bn = { version = "0.6.0", default-features = false } +tiny-keccak = { version = "2.0.2", features = ["fips202"] } +sha2 = { version = "0.9.5", default-features=false } +k256 = { version = "0.11.6", default-features = false, features = ["keccak256", "sha2", "ecdsa"] } +sha3 = { version = "0.10", default-features = false } +num = { version = "0.4", default-features = false, features = ["alloc"] } +ed25519-dalek = { version = "2.0.0", default-features=false } +curve25519-dalek = { version = "=4.1.1", default-features = false, features = ["alloc"] } +sgx_tstd = { git = "https://github.com/apache/teaclave-sgx-sdk.git", rev = "3c903bda", features = ["net", "backtrace"], optional = true } +p256 = { version = "0.13.2",default-features = false, features = ["ecdsa"] } +ripemd = { version = "0.1.3", default-features = false } + +[dev-dependencies] +hex = "0.4.3" + +[features] +default = [] +std = [] +sgx = ["sgx_tstd"] + +[patch."https://github.com/apache/teaclave-sgx-sdk.git"] +sgx_tstd = { path = "../../sgxvm/sgx-sdk/sgx_tstd" } \ No newline at end of file diff --git a/crates/static-precompiles/src/lib.rs b/crates/static-precompiles/src/lib.rs new file mode 100644 index 00000000..d2e33489 --- /dev/null +++ b/crates/static-precompiles/src/lib.rs @@ -0,0 +1,61 @@ +#![cfg_attr(not(feature = "std"), no_std)] + +use evm::GasMutState; +use evm::interpreter::error::{ExitError, ExitException, ExitResult}; +use evm::interpreter::runtime::RuntimeState; + +#[cfg(feature = "std")] +use std::vec::Vec; + +#[cfg(not(feature = "std"))] +use sgx_tstd::vec::Vec; + +pub mod blake2f; +pub mod bn128; +pub mod curve25519; +pub mod modexp; +pub mod sha3fips; +pub mod ec_recover; +pub mod sha256; +pub mod ripemd160; +pub mod datacopy; +pub mod secp256r1; + +pub trait Precompile { + fn execute(input: &[u8], gasometer: &mut G) -> (ExitResult, Vec); +} + +pub trait LinearCostPrecompile { + const BASE: u64; + const WORD: u64; + + fn raw_execute( + input: &[u8], + cost: u64, + ) -> (ExitResult, Vec); +} + +impl + GasMutState> Precompile for T { + fn execute(input: &[u8], gasometer: &mut G) -> (ExitResult, Vec) { + let cost = match linear_cost(input.len() as u64, T::BASE, T::WORD) { + Ok(cost) => cost, + Err(e) => return (Err(e), Vec::new()), + }; + if let Err(err) = gasometer.record_gas(cost.into()) { + return (err.into(), Vec::new()); + }; + + T::raw_execute(input, cost) + } +} + +pub fn linear_cost(len: u64, base: u64, word: u64) -> Result { + let cost = base + .checked_add( + word.checked_mul(len.saturating_add(31) / 32) + .ok_or(ExitException::OutOfGas)?, + ) + .ok_or(ExitException::OutOfGas)?; + + Ok(cost) +} \ No newline at end of file