Skip to content

Commit

Permalink
chore: add missing files to git
Browse files Browse the repository at this point in the history
  • Loading branch information
MikkySnow committed Oct 8, 2024
1 parent 3f0134e commit ab16fe9
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
30 changes: 30 additions & 0 deletions crates/static-precompiles/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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" }
61 changes: 61 additions & 0 deletions crates/static-precompiles/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<G> {
fn execute(input: &[u8], gasometer: &mut G) -> (ExitResult, Vec<u8>);
}

pub trait LinearCostPrecompile {
const BASE: u64;
const WORD: u64;

fn raw_execute(
input: &[u8],
cost: u64,
) -> (ExitResult, Vec<u8>);
}

impl<T: LinearCostPrecompile, G: AsRef<RuntimeState> + GasMutState> Precompile<G> for T {
fn execute(input: &[u8], gasometer: &mut G) -> (ExitResult, Vec<u8>) {
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<u64, ExitError> {
let cost = base
.checked_add(
word.checked_mul(len.saturating_add(31) / 32)
.ok_or(ExitException::OutOfGas)?,
)
.ok_or(ExitException::OutOfGas)?;

Ok(cost)
}

0 comments on commit ab16fe9

Please sign in to comment.