Skip to content

Commit

Permalink
begining of precompiles
Browse files Browse the repository at this point in the history
  • Loading branch information
max-wickham committed Jan 3, 2025
1 parent ccc32d8 commit b110474
Show file tree
Hide file tree
Showing 24 changed files with 744 additions and 802 deletions.
125 changes: 119 additions & 6 deletions Cargo.lock

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

10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
workspace = { members = [ "official_test_types","util"] }
workspace = { members = ["official_test_types", "util"] }
[package]
name = "ethereum_evm"
version = "0.1.0"
Expand All @@ -22,8 +22,12 @@ sha3 = "0.10.8"
thiserror = "1.0.58"
num256 = "0.5.1"
lazy_static = "1.4.0"
phf = {version = "0.11.2", features = ["macros"]}
phf = { version = "0.11.2", features = ["macros"] }
quote = "1.0.35"
secp256k1 = { version = "0.30", features = ["recovery"] }
keccak-hash = "0.11.0"
sha2 = "0.10.8"
hex-literal = "0.3"

[profile.dev]
opt-level = 3 # Use slightly better optimizations.
opt-level = 3 # Use slightly better optimizations.
36 changes: 27 additions & 9 deletions src/configs/gas_costs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::ops::Div;

use precompile_costs::G_SHA256;
use primitive_types::{ H256, U256 };
use static_costs::{ G_COLD_ACCOUNT_ACCESS, G_NEW_ACCOUNT, G_SELF_DESTRUCT };

Expand Down Expand Up @@ -43,6 +44,11 @@ pub mod static_costs {
pub const G_BLOCK_HASH: u64 = 20;
}

pub mod precompile_costs {
pub const G_ECRECOVER: u64 = 3000;
pub const G_SHA256: u64 = 60;
}

pub enum DynamicCosts {
ExtCodeSize {
target_is_cold: bool,
Expand Down Expand Up @@ -145,8 +151,7 @@ impl DynamicCosts {
}
DynamicCosts::Call { value, empty_account, target_is_cold, is_delegate, is_code } => {
// println!("empty_account: {}", empty_account);
// println!("target_is_cold {}", target_is_cold);

println!("target_is_cold {}", target_is_cold);
0 +
(if *target_is_cold {
static_costs::G_COLD_ACCOUNT_ACCESS
Expand Down Expand Up @@ -227,14 +232,11 @@ impl DynamicCosts {
DynamicCosts::SelfDestruct { address_exists, is_cold, positive_balance } => {
G_SELF_DESTRUCT +
(if !*address_exists {
if *positive_balance {
G_NEW_ACCOUNT
} else{
0
}
if *positive_balance { G_NEW_ACCOUNT } else { 0 }
} else {
if *is_cold { G_COLD_ACCOUNT_ACCESS } else { 0 }
})
0
}) +
(if *is_cold { G_COLD_ACCOUNT_ACCESS } else { 0 })
}
_ => 0,
}
Expand All @@ -253,3 +255,19 @@ impl DynamicCosts {
}
}
}

pub enum DynamicPreCompileCosts {
Sha256 {
data_word_size: usize,
},
}

impl DynamicPreCompileCosts {
pub fn cost(&self) -> u64 {
match self {
DynamicPreCompileCosts::Sha256 { data_word_size } => {
G_SHA256 + 12 * (*data_word_size as u64)
}
}
}
}
1 change: 1 addition & 0 deletions src/configs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod bytecode_spec;
pub mod gas_costs;
pub mod precompiles;
35 changes: 35 additions & 0 deletions src/configs/precompiles.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use lazy_static::lazy_static;
use primitive_types::U256;

lazy_static! {
/// 0x01: ecrecover
pub static ref ECRECOVER_PRECOMPILE: U256 = U256::from(1);

/// 0x02: sha256
pub static ref SHA256_PRECOMPILE: U256 = U256::from(2);

/// 0x03: ripemd160
pub static ref RIPEMD160_PRECOMPILE: U256 = U256::from(3);

/// 0x04: identity
pub static ref IDENTITY_PRECOMPILE: U256 = U256::from(4);

/// 0x05: modexp (EIP-198)
pub static ref MODEXP_PRECOMPILE: U256 = U256::from(5);

/// 0x06: alt_bn128-add (EIP-196)
pub static ref ALTBN128_ADD_PRECOMPILE: U256 = U256::from(6);

/// 0x07: alt_bn128-mul (EIP-196)
pub static ref ALTBN128_MUL_PRECOMPILE: U256 = U256::from(7);

/// 0x08: alt_bn128-pairing (EIP-197)
pub static ref ALTBN128_PAIRING_PRECOMPILE: U256 = U256::from(8);

/// 0x09: blake2-f (EIP-152)
pub static ref BLAKE2_F_PRECOMPILE: U256 = U256::from(9);
}

pub fn is_precompile(address: &U256) -> bool {
address.ge(&*ECRECOVER_PRECOMPILE) && address.le(&*BLAKE2_F_PRECOMPILE)
}
Loading

0 comments on commit b110474

Please sign in to comment.