From b7bee7278f7f0b7480f2be2c114298d87389edf8 Mon Sep 17 00:00:00 2001 From: Robin Salen <30937548+Nashtare@users.noreply.github.com> Date: Wed, 11 Sep 2024 18:30:27 -0400 Subject: [PATCH] Tweak naming and move to common crate (#619) --- Cargo.lock | 1 + common/src/lib.rs | 22 +++++++++++++++++++- evm_arithmetization/Cargo.toml | 1 + evm_arithmetization/src/testing_utils.rs | 6 ------ evm_arithmetization/tests/selfdestruct.rs | 3 ++- evm_arithmetization/tests/simple_transfer.rs | 3 ++- trace_decoder/src/decoding.rs | 8 ++----- 7 files changed, 29 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dcd859ede..85268325c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2075,6 +2075,7 @@ dependencies = [ "static_assertions", "thiserror", "tiny-keccak", + "zk_evm_common", "zk_evm_proc_macro", ] diff --git a/common/src/lib.rs b/common/src/lib.rs index 9f33bfaf1..bac6dd440 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -1,4 +1,4 @@ -use ethereum_types::H256; +use ethereum_types::{H256, U256}; /// The hash value of an account empty EVM code. /// 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 @@ -14,6 +14,26 @@ pub const EMPTY_TRIE_HASH: H256 = H256([ 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33, ]); +/// Converts an amount in `ETH` to `wei` units. +pub fn eth_to_wei(eth: U256) -> U256 { + // 1 ether = 10^18 wei. + eth * U256::from(10).pow(18.into()) +} + +/// Converts an amount in `gwei` to `wei` units. +/// This also works for converting `ETH` to `gwei`. +pub fn gwei_to_wei(eth: U256) -> U256 { + // 1 ether = 10^9 gwei = 10^18 wei. + eth * U256::from(10).pow(9.into()) +} + +#[test] +fn test_eth_conversion() { + assert_eq!( + eth_to_wei(U256::one()), + gwei_to_wei(gwei_to_wei(U256::one())) + ); +} #[test] fn test_empty_code_hash() { assert_eq!(EMPTY_CODE_HASH, keccak_hash::keccak([])); diff --git a/evm_arithmetization/Cargo.toml b/evm_arithmetization/Cargo.toml index a2b38cc7e..90d01c071 100644 --- a/evm_arithmetization/Cargo.toml +++ b/evm_arithmetization/Cargo.toml @@ -50,6 +50,7 @@ serde-big-array = { workspace = true } mpt_trie = { workspace = true } smt_trie = { workspace = true, optional = true } zk_evm_proc_macro = { workspace = true } +zk_evm_common = { workspace = true } [dev-dependencies] criterion = { workspace = true } diff --git a/evm_arithmetization/src/testing_utils.rs b/evm_arithmetization/src/testing_utils.rs index 4152da5b6..36b4dedb1 100644 --- a/evm_arithmetization/src/testing_utils.rs +++ b/evm_arithmetization/src/testing_utils.rs @@ -161,9 +161,3 @@ pub fn scalable_contract_from_storage(storage_trie: &HashedPartialTrie) -> Accou ..Default::default() } } - -/// Converts an amount in `ETH` to `wei` units. -pub fn eth_to_wei(eth: U256) -> U256 { - // 1 ether = 10^18 wei. - eth * U256::from(10).pow(18.into()) -} diff --git a/evm_arithmetization/tests/selfdestruct.rs b/evm_arithmetization/tests/selfdestruct.rs index 271630512..dde580498 100644 --- a/evm_arithmetization/tests/selfdestruct.rs +++ b/evm_arithmetization/tests/selfdestruct.rs @@ -9,7 +9,7 @@ use evm_arithmetization::generation::{GenerationInputs, TrieInputs}; use evm_arithmetization::proof::{BlockHashes, BlockMetadata, TrieRoots}; use evm_arithmetization::prover::testing::prove_all_segments; use evm_arithmetization::testing_utils::{ - beacon_roots_account_nibbles, beacon_roots_contract_from_storage, eth_to_wei, init_logger, + beacon_roots_account_nibbles, beacon_roots_contract_from_storage, init_logger, preinitialized_state_and_storage_tries, update_beacon_roots_account_storage, }; use evm_arithmetization::verifier::testing::verify_all_proofs; @@ -21,6 +21,7 @@ use mpt_trie::partial_trie::{HashedPartialTrie, PartialTrie}; use plonky2::field::goldilocks_field::GoldilocksField; use plonky2::plonk::config::KeccakGoldilocksConfig; use plonky2::util::timing::TimingTree; +use zk_evm_common::eth_to_wei; type F = GoldilocksField; const D: usize = 2; diff --git a/evm_arithmetization/tests/simple_transfer.rs b/evm_arithmetization/tests/simple_transfer.rs index 81454fb0e..2e371071c 100644 --- a/evm_arithmetization/tests/simple_transfer.rs +++ b/evm_arithmetization/tests/simple_transfer.rs @@ -10,7 +10,7 @@ use evm_arithmetization::generation::{GenerationInputs, TrieInputs}; use evm_arithmetization::proof::{BlockHashes, BlockMetadata, TrieRoots}; use evm_arithmetization::prover::testing::prove_all_segments; use evm_arithmetization::testing_utils::{ - beacon_roots_account_nibbles, beacon_roots_contract_from_storage, eth_to_wei, init_logger, + beacon_roots_account_nibbles, beacon_roots_contract_from_storage, init_logger, preinitialized_state_and_storage_tries, update_beacon_roots_account_storage, }; use evm_arithmetization::verifier::testing::verify_all_proofs; @@ -22,6 +22,7 @@ use mpt_trie::partial_trie::{HashedPartialTrie, PartialTrie}; use plonky2::field::goldilocks_field::GoldilocksField; use plonky2::plonk::config::KeccakGoldilocksConfig; use plonky2::util::timing::TimingTree; +use zk_evm_common::eth_to_wei; type F = GoldilocksField; const D: usize = 2; diff --git a/trace_decoder/src/decoding.rs b/trace_decoder/src/decoding.rs index b0f909374..59af0dfb2 100644 --- a/trace_decoder/src/decoding.rs +++ b/trace_decoder/src/decoding.rs @@ -20,6 +20,7 @@ use mpt_trie::{ trie_ops::TrieOpError, utils::{IntoTrieKey as _, TriePath}, }; +use zk_evm_common::gwei_to_wei; use crate::{ hash, @@ -442,7 +443,7 @@ fn add_withdrawals_to_txns( ) -> anyhow::Result<()> { // Scale withdrawals amounts. for (_addr, amt) in withdrawals.iter_mut() { - *amt = eth_to_gwei(*amt) + *amt = gwei_to_wei(*amt) } let withdrawals_with_hashed_addrs_iter = || { @@ -692,11 +693,6 @@ fn create_trie_subset_wrapped( .context(format!("missing keys when creating {}", trie_type)) } -fn eth_to_gwei(eth: U256) -> U256 { - // 1 ether = 10^9 gwei. - eth * U256::from(10).pow(9.into()) -} - // This is just `rlp(0)`. const ZERO_STORAGE_SLOT_VAL_RLPED: [u8; 1] = [128];