Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
rakita committed Oct 19, 2023
1 parent 2059232 commit cd67913
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 68 deletions.
4 changes: 2 additions & 2 deletions crates/precompile/src/blake2.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{Error, PrecompileAddress, StandardPrecompileFn};
use crate::{Error, PrecompileWithAddress, StandardPrecompileFn};
use crate::{Precompile, PrecompileResult};
use core::convert::TryInto;

const F_ROUND: u64 = 1;
const INPUT_LENGTH: usize = 213;

pub const FUN: PrecompileAddress = PrecompileAddress(
pub const FUN: PrecompileWithAddress = PrecompileWithAddress(
crate::u64_to_address(9),
Precompile::Standard(run as StandardPrecompileFn),
);
Expand Down
14 changes: 7 additions & 7 deletions crates/precompile/src/bn128.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{primitives::U256, Address, Error, Precompile, PrecompileAddress, PrecompileResult};
use crate::{primitives::U256, Address, Error, Precompile, PrecompileWithAddress, PrecompileResult};
use alloc::vec::Vec;

pub mod add {
use super::*;
const ADDRESS: Address = crate::u64_to_address(6);

pub const ISTANBUL: PrecompileAddress = PrecompileAddress(
pub const ISTANBUL: PrecompileWithAddress = PrecompileWithAddress(
ADDRESS,
Precompile::Standard(|input: &[u8], target_gas: u64| -> PrecompileResult {
if 150 > target_gas {
Expand All @@ -15,7 +15,7 @@ pub mod add {
}),
);

pub const BYZANTIUM: PrecompileAddress = PrecompileAddress(
pub const BYZANTIUM: PrecompileWithAddress = PrecompileWithAddress(
ADDRESS,
Precompile::Standard(|input: &[u8], target_gas: u64| -> PrecompileResult {
if 500 > target_gas {
Expand All @@ -29,7 +29,7 @@ pub mod add {
pub mod mul {
use super::*;
const ADDRESS: Address = crate::u64_to_address(7);
pub const ISTANBUL: PrecompileAddress = PrecompileAddress(
pub const ISTANBUL: PrecompileWithAddress = PrecompileWithAddress(
ADDRESS,
Precompile::Standard(|input: &[u8], gas_limit: u64| -> PrecompileResult {
if 6_000 > gas_limit {
Expand All @@ -39,7 +39,7 @@ pub mod mul {
}),
);

pub const BYZANTIUM: PrecompileAddress = PrecompileAddress(
pub const BYZANTIUM: PrecompileWithAddress = PrecompileWithAddress(
ADDRESS,
Precompile::Standard(|input: &[u8], gas_limit: u64| -> PrecompileResult {
if 40_000 > gas_limit {
Expand All @@ -56,7 +56,7 @@ pub mod pair {

const ISTANBUL_PAIR_PER_POINT: u64 = 34_000;
const ISTANBUL_PAIR_BASE: u64 = 45_000;
pub const ISTANBUL: PrecompileAddress = PrecompileAddress(
pub const ISTANBUL: PrecompileWithAddress = PrecompileWithAddress(
ADDRESS,
Precompile::Standard(|input: &[u8], target_gas: u64| -> PrecompileResult {
super::run_pair(
Expand All @@ -70,7 +70,7 @@ pub mod pair {

const BYZANTIUM_PAIR_PER_POINT: u64 = 80_000;
const BYZANTIUM_PAIR_BASE: u64 = 100_000;
pub const BYZANTIUM: PrecompileAddress = PrecompileAddress(
pub const BYZANTIUM: PrecompileWithAddress = PrecompileWithAddress(
ADDRESS,
Precompile::Standard(|input: &[u8], target_gas: u64| -> PrecompileResult {
super::run_pair(
Expand Down
6 changes: 3 additions & 3 deletions crates/precompile/src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::calc_linear_cost_u32;
use crate::{Error, Precompile, PrecompileAddress, PrecompileResult, StandardPrecompileFn};
use crate::{Error, Precompile, PrecompileWithAddress, PrecompileResult, StandardPrecompileFn};
use sha2::*;

pub const SHA256: PrecompileAddress = PrecompileAddress(
pub const SHA256: PrecompileWithAddress = PrecompileWithAddress(
crate::u64_to_address(2),
Precompile::Standard(sha256_run as StandardPrecompileFn),
);
pub const RIPEMD160: PrecompileAddress = PrecompileAddress(
pub const RIPEMD160: PrecompileWithAddress = PrecompileWithAddress(
crate::u64_to_address(3),
Precompile::Standard(ripemd160_run as StandardPrecompileFn),
);
Expand Down
4 changes: 2 additions & 2 deletions crates/precompile/src/identity.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::calc_linear_cost_u32;
use crate::{Error, Precompile, PrecompileAddress, PrecompileResult, StandardPrecompileFn};
use crate::{Error, Precompile, PrecompileWithAddress, PrecompileResult, StandardPrecompileFn};

pub const FUN: PrecompileAddress = PrecompileAddress(
pub const FUN: PrecompileWithAddress = PrecompileWithAddress(
crate::u64_to_address(4),
Precompile::Standard(identity_run as StandardPrecompileFn),
);
Expand Down
4 changes: 2 additions & 2 deletions crates/precompile/src/kzg_point_evaluation.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{Address, Error, Precompile, PrecompileAddress, PrecompileResult};
use crate::{Address, Error, Precompile, PrecompileWithAddress, PrecompileResult};
use c_kzg::{Bytes32, Bytes48, KzgProof, KzgSettings};
use revm_primitives::{hex_literal::hex, Env};
use sha2::{Digest, Sha256};

pub const POINT_EVALUATION: PrecompileAddress = PrecompileAddress(ADDRESS, Precompile::Env(run));
pub const POINT_EVALUATION: PrecompileWithAddress = PrecompileWithAddress(ADDRESS, Precompile::Env(run));

const ADDRESS: Address = crate::u64_to_address(0x0A);
const GAS_COST: u64 = 50_000;
Expand Down
81 changes: 35 additions & 46 deletions crates/precompile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl PrecompileOutput {
}
#[derive(Clone, Debug)]
pub struct Precompiles {
pub fun: HashMap<Address, Precompile>,
pub inner: Vec<PrecompileWithAddress>,
}

impl Default for Precompiles {
Expand All @@ -86,10 +86,10 @@ impl fmt::Debug for Precompile {
}

#[derive(Clone, Debug)]
pub struct PrecompileAddress(Address, Precompile);
pub struct PrecompileWithAddress(Address, Precompile);

impl From<PrecompileAddress> for (Address, Precompile) {
fn from(value: PrecompileAddress) -> Self {
impl From<PrecompileWithAddress> for (Address, Precompile) {
fn from(value: PrecompileWithAddress) -> Self {
(value.0, value.1)
}
}
Expand Down Expand Up @@ -131,36 +131,31 @@ impl Precompiles {
pub fn homestead() -> &'static Self {
static INSTANCE: OnceBox<Precompiles> = OnceBox::new();
INSTANCE.get_or_init(|| {
let fun = [
let inner = vec![
secp256k1::ECRECOVER,
hash::SHA256,
hash::RIPEMD160,
identity::FUN,
]
.into_iter()
.map(From::from)
.collect();
Box::new(Self { fun })
];
inner.sort_unstable_by_key(|i| i.0);
Box::new(Self { inner })
})
}

pub fn byzantium() -> &'static Self {
static INSTANCE: OnceBox<Precompiles> = OnceBox::new();
INSTANCE.get_or_init(|| {
let mut precompiles = Box::new(Self::homestead().clone());
precompiles.fun.extend(
[
// EIP-196: Precompiled contracts for addition and scalar multiplication on the elliptic curve alt_bn128.
// EIP-197: Precompiled contracts for optimal ate pairing check on the elliptic curve alt_bn128.
bn128::add::BYZANTIUM,
bn128::mul::BYZANTIUM,
bn128::pair::BYZANTIUM,
// EIP-198: Big integer modular exponentiation.
modexp::BYZANTIUM,
]
.into_iter()
.map(From::from),
);
precompiles.inner.extend([
// EIP-196: Precompiled contracts for addition and scalar multiplication on the elliptic curve alt_bn128.
// EIP-197: Precompiled contracts for optimal ate pairing check on the elliptic curve alt_bn128.
bn128::add::BYZANTIUM,
bn128::mul::BYZANTIUM,
bn128::pair::BYZANTIUM,
// EIP-198: Big integer modular exponentiation.
modexp::BYZANTIUM,
]);
precompiles.inner.sort_unstable_by_key(|i| i.0);
precompiles
})
}
Expand All @@ -169,18 +164,15 @@ impl Precompiles {
static INSTANCE: OnceBox<Precompiles> = OnceBox::new();
INSTANCE.get_or_init(|| {
let mut precompiles = Box::new(Self::byzantium().clone());
precompiles.fun.extend(
[
// EIP-152: Add BLAKE2 compression function `F` precompile.
blake2::FUN,
// EIP-1108: Reduce alt_bn128 precompile gas costs.
bn128::add::ISTANBUL,
bn128::mul::ISTANBUL,
bn128::pair::ISTANBUL,
]
.into_iter()
.map(From::from),
);
precompiles.inner.extend([
// EIP-152: Add BLAKE2 compression function `F` precompile.
blake2::FUN,
// EIP-1108: Reduce alt_bn128 precompile gas costs.
bn128::add::ISTANBUL,
bn128::mul::ISTANBUL,
bn128::pair::ISTANBUL,
]);
precompiles.inner.sort_unstable_by_key(|i| i.0);
precompiles
})
}
Expand All @@ -189,14 +181,11 @@ impl Precompiles {
static INSTANCE: OnceBox<Precompiles> = OnceBox::new();
INSTANCE.get_or_init(|| {
let mut precompiles = Box::new(Self::istanbul().clone());
precompiles.fun.extend(
[
// EIP-2565: ModExp Gas Cost.
modexp::BERLIN,
]
.into_iter()
.map(From::from),
);
precompiles.inner.extend([
// EIP-2565: ModExp Gas Cost.
modexp::BERLIN,
]);
precompiles.inner.sort_unstable_by_key(|i| i.0);
precompiles
})
}
Expand All @@ -209,7 +198,7 @@ impl Precompiles {
#[cfg(feature = "c-kzg")]
{
let mut precompiles = Box::new(Self::berlin().clone());
precompiles.fun.extend(
precompiles.inner.extend(
[
// EIP-4844: Shard Blob Transactions
kzg_point_evaluation::POINT_EVALUATION,
Expand Down Expand Up @@ -242,11 +231,11 @@ impl Precompiles {
}

pub fn addresses(&self) -> impl IntoIterator<Item = &Address> {
self.fun.keys()
self.inner.iter().map(|i| &i.0)
}

pub fn contains(&self, address: &Address) -> bool {
self.fun.contains_key(address)
self.inner.binary_search_by_key(address, |i| i.0)
}

pub fn get(&self, address: &Address) -> Option<Precompile> {
Expand Down
6 changes: 3 additions & 3 deletions crates/precompile/src/modexp.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use crate::{
primitives::U256,
utilities::{get_right_padded, get_right_padded_vec, left_padding, left_padding_vec},
Error, Precompile, PrecompileAddress, PrecompileResult, StandardPrecompileFn,
Error, Precompile, PrecompileWithAddress, PrecompileResult, StandardPrecompileFn,
};
use alloc::vec::Vec;
use aurora_engine_modexp::modexp;
use core::cmp::{max, min};

pub const BYZANTIUM: PrecompileAddress = PrecompileAddress(
pub const BYZANTIUM: PrecompileWithAddress = PrecompileWithAddress(
crate::u64_to_address(5),
Precompile::Standard(byzantium_run as StandardPrecompileFn),
);

pub const BERLIN: PrecompileAddress = PrecompileAddress(
pub const BERLIN: PrecompileWithAddress = PrecompileWithAddress(
crate::u64_to_address(5),
Precompile::Standard(berlin_run as StandardPrecompileFn),
);
Expand Down
4 changes: 2 additions & 2 deletions crates/precompile/src/secp256k1.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{Error, Precompile, PrecompileAddress, PrecompileResult, StandardPrecompileFn};
use crate::{Error, Precompile, PrecompileWithAddress, PrecompileResult, StandardPrecompileFn};
use alloc::vec::Vec;
use core::cmp::min;

pub const ECRECOVER: PrecompileAddress = PrecompileAddress(
pub const ECRECOVER: PrecompileWithAddress = PrecompileWithAddress(
crate::u64_to_address(1),
Precompile::Standard(ec_recover_run as StandardPrecompileFn),
);
Expand Down
2 changes: 1 addition & 1 deletion documentation/src/crates/precompile.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Types and Constants:
- `Log`: Represents an Ethereum log, with an address, a list of topics, and associated data.
- `Precompiles`: A collection of precompiled contracts available in a particular hard fork of Ethereum.
- `Precompile`: Represents a precompiled contract, which can either be a standard Ethereum precompile, or a custom precompile.
- `PrecompileAddress`: Associates a precompiled contract with its address.
- `PrecompileWithAddress`: Associates a precompiled contract with its address.
- `SpecId`: An enumeration representing different hard fork specifications in Ethereum, such as Homestead, Byzantium, Istanbul, Berlin, and Latest.

Functions:
Expand Down

0 comments on commit cd67913

Please sign in to comment.