Skip to content

Commit

Permalink
fix: warm up 0x0a precompile (#895)
Browse files Browse the repository at this point in the history
* fix: warm up 0x0a precompile

* fix tests
  • Loading branch information
enitrat authored Sep 4, 2024
1 parent 9912d60 commit 44bc76b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 26 deletions.
3 changes: 2 additions & 1 deletion crates/contracts/src/kakarot_core/kakarot.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub mod KakarotCore {
Transfer, Message, Environment, TransactionResult, TransactionResultTrait, ExecutionSummary,
ExecutionSummaryTrait, Address, AddressTrait
};
use evm::precompiles::eth_precompile_addresses;
use evm::state::{State, StateTrait};
use super::{INVOKE_ETH_CALL_FORBIDDEN};
use utils::address::compute_contract_address;
Expand Down Expand Up @@ -327,7 +328,7 @@ pub mod KakarotCore {
accessed_addresses.add(env.coinbase);
accessed_addresses.add(to.evm);
accessed_addresses.add(origin.evm);
accessed_addresses.extend(constants::precompile_addresses().spanset());
accessed_addresses.extend(eth_precompile_addresses().spanset());

let mut accessed_storage_keys: Set<(EthAddress, u256)> = Default::default();

Expand Down
2 changes: 1 addition & 1 deletion crates/evm/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mod memory;
mod model;

// instructions module
mod precompiles;
pub mod precompiles;

// Stack module
mod stack;
Expand Down
15 changes: 8 additions & 7 deletions crates/evm/src/model.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ use core::num::traits::{CheckedAdd, CheckedSub, CheckedMul};
use core::starknet::{EthAddress, get_contract_address, ContractAddress};
use evm::errors::{EVMError, CONTRACT_SYSCALL_FAILED};
use evm::model::account::{Account, AccountTrait};
use evm::precompiles::{
FIRST_ROLLUP_PRECOMPILE_ADDRESS, FIRST_ETHEREUM_PRECOMPILE_ADDRESS,
LAST_ETHEREUM_PRECOMPILE_ADDRESS
};
use evm::state::State;
use utils::fmt::{TSpanSetDebug};
use utils::helpers::{ResultExTrait};
use utils::set::{Set, SpanSet};
use utils::traits::{EthAddressDefault, ContractAddressDefault, SpanDefault};

const FIRST_ROLLUP_PRECOMPILE_ADDRESS: u256 = 0x100;
const LAST_ETHEREUM_PRECOMPILE_ADDRESS: u256 = 0x0a;
const ZERO: felt252 = 0x0;

#[derive(Destruct, Default)]
struct Environment {
origin: EthAddress,
Expand Down Expand Up @@ -187,9 +187,10 @@ impl AddressImpl of AddressTrait {
/// Check whether an address for a call-family opcode is a precompile.
fn is_precompile(self: EthAddress) -> bool {
let self: felt252 = self.into();
return self != ZERO
&& (self.into() <= LAST_ETHEREUM_PRECOMPILE_ADDRESS
|| self.into() == FIRST_ROLLUP_PRECOMPILE_ADDRESS);
return self != 0x00
&& (FIRST_ETHEREUM_PRECOMPILE_ADDRESS <= self.into()
&& self.into() <= LAST_ETHEREUM_PRECOMPILE_ADDRESS)
|| self.into() == FIRST_ROLLUP_PRECOMPILE_ADDRESS;
}
}

Expand Down
45 changes: 44 additions & 1 deletion crates/evm/src/precompiles.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ mod modexp;
mod p256verify;
mod sha256;
use core::starknet::EthAddress;

use core::traits::Into;
use evm::errors::EVMError;
use evm::model::vm::VM;
Expand All @@ -19,6 +18,24 @@ use evm::precompiles::modexp::ModExp;
use evm::precompiles::p256verify::P256Verify;
use evm::precompiles::sha256::Sha256;

use utils::set::{Set};


pub const FIRST_ETHEREUM_PRECOMPILE_ADDRESS: u256 = 0x01;
pub const LAST_ETHEREUM_PRECOMPILE_ADDRESS: u256 = 0x0a;
pub const FIRST_ROLLUP_PRECOMPILE_ADDRESS: u256 = 0x100;

pub fn eth_precompile_addresses() -> Set<EthAddress> {
let mut precompile_addresses: Array<EthAddress> = array![];
//TODO(2.8) use range operator
let mut i = FIRST_ETHEREUM_PRECOMPILE_ADDRESS;
while i <= LAST_ETHEREUM_PRECOMPILE_ADDRESS {
precompile_addresses.append(i.try_into().unwrap());
i = i + 1;
};
Set { inner: precompile_addresses }
}


trait Precompile {
fn address() -> EthAddress;
Expand Down Expand Up @@ -92,3 +109,29 @@ impl PrecompilesImpl of Precompiles {
return Result::Ok(());
}
}


#[cfg(test)]
mod tests {
use super::eth_precompile_addresses;

#[test]
fn test_eth_precompile_addresses() {
let addresses = eth_precompile_addresses();
assert_eq!(
addresses.inner.span(),
[
0x01.try_into().unwrap(),
0x02.try_into().unwrap(),
0x03.try_into().unwrap(),
0x04.try_into().unwrap(),
0x05.try_into().unwrap(),
0x06.try_into().unwrap(),
0x07.try_into().unwrap(),
0x08.try_into().unwrap(),
0x09.try_into().unwrap(),
0x0a.try_into().unwrap()
].span()
);
}
}
18 changes: 2 additions & 16 deletions crates/utils/src/constants.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::starknet::EthAddress;
use utils::set::{Set};
use evm::precompiles::{FIRST_ETHEREUM_PRECOMPILE_ADDRESS, LAST_ETHEREUM_PRECOMPILE_ADDRESS};
use utils::traits::{U8IntoEthAddress};

// FELT PRIME
// 2^251 + 17 * 2^192 + 1
const FELT252_PRIME: u256 = 0x800000000000011000000000000000000000000000000000000000000000001;
Expand Down Expand Up @@ -36,21 +37,6 @@ const EMPTY_KECCAK: u256 = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bf

const BURN_ADDRESS: felt252 = 0xdead;

//PRECOMPILES
pub fn precompile_addresses() -> Set<EthAddress> {
let inner: Array<EthAddress> = array![
0x01_u8.into(),
0x02_u8.into(),
0x03_u8.into(),
0x04_u8.into(),
0x05_u8.into(),
0x06_u8.into(),
0x07_u8.into(),
0x08_u8.into(),
0x09_u8.into()
];
Set { inner }
}

// Numeric constants
pub const POW_256_0: u128 = 0x1;
Expand Down

0 comments on commit 44bc76b

Please sign in to comment.