From 47e24306b7dc224383712a6700986331615f3e2a Mon Sep 17 00:00:00 2001 From: Robin Salen <30937548+Nashtare@users.noreply.github.com> Date: Tue, 5 Dec 2023 18:10:36 -0500 Subject: [PATCH] Remove GenerationOutputs (#1408) --- evm/src/generation/mod.rs | 20 +----- evm/src/generation/outputs.rs | 109 ----------------------------- evm/src/prover.rs | 21 +----- evm/tests/add11_yml.rs | 1 - evm/tests/basic_smart_contract.rs | 1 - evm/tests/empty_txn_list.rs | 1 - evm/tests/erc20.rs | 1 - evm/tests/log_opcode.rs | 3 - evm/tests/self_balance_gas_cost.rs | 1 - evm/tests/selfdestruct.rs | 1 - evm/tests/simple_transfer.rs | 1 - evm/tests/withdrawals.rs | 1 - 12 files changed, 4 insertions(+), 157 deletions(-) delete mode 100644 evm/src/generation/outputs.rs diff --git a/evm/src/generation/mod.rs b/evm/src/generation/mod.rs index afac60e2..ddb1fe5d 100644 --- a/evm/src/generation/mod.rs +++ b/evm/src/generation/mod.rs @@ -20,7 +20,6 @@ use crate::config::StarkConfig; use crate::cpu::columns::CpuColumnsView; use crate::cpu::kernel::aggregator::KERNEL; use crate::cpu::kernel::constants::global_metadata::GlobalMetadata; -use crate::generation::outputs::{get_outputs, GenerationOutputs}; use crate::generation::state::GenerationState; use crate::memory::segments::Segment; use crate::proof::{BlockHashes, BlockMetadata, ExtraBlockData, PublicValues, TrieRoots}; @@ -29,7 +28,6 @@ use crate::witness::memory::{MemoryAddress, MemoryChannel}; use crate::witness::transition::transition; pub mod mpt; -pub mod outputs; pub(crate) mod prover_input; pub(crate) mod rlp; pub(crate) mod state; @@ -61,13 +59,6 @@ pub struct GenerationInputs { pub block_metadata: BlockMetadata, pub block_hashes: BlockHashes, - - /// A list of known addresses in the input state trie (which itself doesn't hold addresses, - /// only state keys). This is only useful for debugging, so that we can return addresses in the - /// post-state rather than state keys. (See `GenerationOutputs`, and in particular - /// `AddressOrStateKey`.) If the caller is not interested in the post-state, this can be left - /// empty. - pub addresses: Vec
, } #[derive(Clone, Debug, Deserialize, Serialize, Default)] @@ -196,11 +187,7 @@ pub fn generate_traces, const D: usize>( inputs: GenerationInputs, config: &StarkConfig, timing: &mut TimingTree, -) -> anyhow::Result<( - [Vec>; NUM_TABLES], - PublicValues, - GenerationOutputs, -)> { +) -> anyhow::Result<([Vec>; NUM_TABLES], PublicValues)> { let mut state = GenerationState::::new(inputs.clone(), &KERNEL.code) .map_err(|err| anyhow!("Failed to parse all the initial prover inputs: {:?}", err))?; @@ -218,9 +205,6 @@ pub fn generate_traces, const D: usize>( state.traces.get_lengths() ); - let outputs = get_outputs(&mut state) - .map_err(|err| anyhow!("Failed to generate post-state info: {:?}", err))?; - let read_metadata = |field| state.memory.read_global_metadata(field); let trie_roots_before = TrieRoots { state_root: H256::from_uint(&read_metadata(StateTrieRootDigestBefore)), @@ -257,7 +241,7 @@ pub fn generate_traces, const D: usize>( "convert trace data to tables", state.traces.into_tables(all_stark, config, timing) ); - Ok((tables, public_values, outputs)) + Ok((tables, public_values)) } fn simulate_cpu, const D: usize>( diff --git a/evm/src/generation/outputs.rs b/evm/src/generation/outputs.rs deleted file mode 100644 index 0ce87082..00000000 --- a/evm/src/generation/outputs.rs +++ /dev/null @@ -1,109 +0,0 @@ -use std::collections::HashMap; - -use ethereum_types::{Address, BigEndianHash, H256, U256}; -use plonky2::field::types::Field; - -use crate::cpu::kernel::constants::global_metadata::GlobalMetadata::StateTrieRoot; -use crate::generation::state::GenerationState; -use crate::generation::trie_extractor::{ - read_state_trie_value, read_storage_trie_value, read_trie, AccountTrieRecord, -}; -use crate::util::u256_to_usize; -use crate::witness::errors::ProgramError; - -/// The post-state after trace generation; intended for debugging. -#[derive(Clone, Debug)] -pub struct GenerationOutputs { - pub accounts: HashMap, -} - -#[derive(Clone, Eq, PartialEq, Hash, Debug)] -pub enum AddressOrStateKey { - Address(Address), - StateKey(H256), -} - -#[derive(Clone, Debug)] -pub struct AccountOutput { - pub balance: U256, - pub nonce: u64, - pub code: Vec, - pub storage: HashMap, -} - -pub(crate) fn get_outputs( - state: &mut GenerationState, -) -> Result { - // First observe all addresses passed in by caller. - for address in state.inputs.addresses.clone() { - state.observe_address(address); - } - - let ptr = u256_to_usize(state.memory.read_global_metadata(StateTrieRoot))?; - let account_map = read_trie::(&state.memory, ptr, read_state_trie_value)?; - - let mut accounts = HashMap::with_capacity(account_map.len()); - - for (state_key_nibbles, account) in account_map.into_iter() { - if state_key_nibbles.count != 64 { - return Err(ProgramError::IntegerTooLarge); - } - let state_key_h256 = H256::from_uint(&state_key_nibbles.try_into_u256().unwrap()); - - let addr_or_state_key = - if let Some(address) = state.state_key_to_address.get(&state_key_h256) { - AddressOrStateKey::Address(*address) - } else { - AddressOrStateKey::StateKey(state_key_h256) - }; - - let account_output = account_trie_record_to_output(state, account)?; - accounts.insert(addr_or_state_key, account_output); - } - - Ok(GenerationOutputs { accounts }) -} - -fn account_trie_record_to_output( - state: &GenerationState, - account: AccountTrieRecord, -) -> Result { - let storage = get_storage(state, account.storage_ptr)?; - - // TODO: This won't work if the account was created during the txn. - // Need to track changes to code, similar to how we track addresses - // with observe_new_address. - let code = state - .inputs - .contract_code - .get(&account.code_hash) - .ok_or(ProgramError::UnknownContractCode)? - .clone(); - - Ok(AccountOutput { - balance: account.balance, - nonce: account.nonce, - storage, - code, - }) -} - -/// Get an account's storage trie, given a pointer to its root. -fn get_storage( - state: &GenerationState, - storage_ptr: usize, -) -> Result, ProgramError> { - let storage_trie = read_trie::(&state.memory, storage_ptr, |x| { - Ok(read_storage_trie_value(x)) - })?; - - let mut map = HashMap::with_capacity(storage_trie.len()); - for (storage_key_nibbles, value) in storage_trie.into_iter() { - if storage_key_nibbles.count != 64 { - return Err(ProgramError::IntegerTooLarge); - }; - map.insert(storage_key_nibbles.try_into_u256().unwrap(), value); - } - - Ok(map) -} diff --git a/evm/src/prover.rs b/evm/src/prover.rs index b4fc8edb..ab33a661 100644 --- a/evm/src/prover.rs +++ b/evm/src/prover.rs @@ -26,7 +26,6 @@ use crate::cross_table_lookup::{ GrandProductChallengeSet, }; use crate::evaluation_frame::StarkEvaluationFrame; -use crate::generation::outputs::GenerationOutputs; use crate::generation::{generate_traces, GenerationInputs}; use crate::get_challenges::observe_public_values; use crate::lookup::{lookup_helper_columns, Lookup, LookupCheckVars}; @@ -45,34 +44,18 @@ pub fn prove( inputs: GenerationInputs, timing: &mut TimingTree, ) -> Result> -where - F: RichField + Extendable, - C: GenericConfig, -{ - let (proof, _outputs) = prove_with_outputs(all_stark, config, inputs, timing)?; - Ok(proof) -} - -/// Generate traces, then create all STARK proofs. Returns information about the post-state, -/// intended for debugging, in addition to the proof. -pub fn prove_with_outputs( - all_stark: &AllStark, - config: &StarkConfig, - inputs: GenerationInputs, - timing: &mut TimingTree, -) -> Result<(AllProof, GenerationOutputs)> where F: RichField + Extendable, C: GenericConfig, { timed!(timing, "build kernel", Lazy::force(&KERNEL)); - let (traces, public_values, outputs) = timed!( + let (traces, public_values) = timed!( timing, "generate all traces", generate_traces(all_stark, inputs, config, timing)? ); let proof = prove_with_traces(all_stark, config, traces, public_values, timing)?; - Ok((proof, outputs)) + Ok(proof) } /// Compute all STARK proofs. diff --git a/evm/tests/add11_yml.rs b/evm/tests/add11_yml.rs index 105bca26..ab08fd02 100644 --- a/evm/tests/add11_yml.rs +++ b/evm/tests/add11_yml.rs @@ -165,7 +165,6 @@ fn add11_yml() -> anyhow::Result<()> { prev_hashes: vec![H256::default(); 256], cur_hash: H256::default(), }, - addresses: vec![], }; let mut timing = TimingTree::new("prove", log::Level::Debug); diff --git a/evm/tests/basic_smart_contract.rs b/evm/tests/basic_smart_contract.rs index 64e23e99..c520793f 100644 --- a/evm/tests/basic_smart_contract.rs +++ b/evm/tests/basic_smart_contract.rs @@ -197,7 +197,6 @@ fn test_basic_smart_contract() -> anyhow::Result<()> { prev_hashes: vec![H256::default(); 256], cur_hash: H256::default(), }, - addresses: vec![], }; let mut timing = TimingTree::new("prove", log::Level::Debug); diff --git a/evm/tests/empty_txn_list.rs b/evm/tests/empty_txn_list.rs index 0691049e..17e9b7bf 100644 --- a/evm/tests/empty_txn_list.rs +++ b/evm/tests/empty_txn_list.rs @@ -72,7 +72,6 @@ fn test_empty_txn_list() -> anyhow::Result<()> { prev_hashes: initial_block_hashes, cur_hash: H256::default(), }, - addresses: vec![], }; // Initialize the preprocessed circuits for the zkEVM. diff --git a/evm/tests/erc20.rs b/evm/tests/erc20.rs index 89a516bf..a71049f7 100644 --- a/evm/tests/erc20.rs +++ b/evm/tests/erc20.rs @@ -173,7 +173,6 @@ fn test_erc20() -> anyhow::Result<()> { prev_hashes: vec![H256::default(); 256], cur_hash: H256::default(), }, - addresses: vec![], }; let mut timing = TimingTree::new("prove", log::Level::Debug); diff --git a/evm/tests/log_opcode.rs b/evm/tests/log_opcode.rs index f872c41b..34844491 100644 --- a/evm/tests/log_opcode.rs +++ b/evm/tests/log_opcode.rs @@ -231,7 +231,6 @@ fn test_log_opcodes() -> anyhow::Result<()> { prev_hashes: vec![H256::default(); 256], cur_hash: H256::default(), }, - addresses: vec![], }; let mut timing = TimingTree::new("prove", log::Level::Debug); @@ -436,7 +435,6 @@ fn test_log_with_aggreg() -> anyhow::Result<()> { prev_hashes: vec![H256::default(); 256], cur_hash: H256::default(), }, - addresses: vec![], }; // Preprocess all circuits. @@ -562,7 +560,6 @@ fn test_log_with_aggreg() -> anyhow::Result<()> { prev_hashes: vec![H256::default(); 256], cur_hash: H256::default(), }, - addresses: vec![], }; let mut timing = TimingTree::new("prove root second", log::Level::Info); diff --git a/evm/tests/self_balance_gas_cost.rs b/evm/tests/self_balance_gas_cost.rs index 2f26edc1..2ddb0103 100644 --- a/evm/tests/self_balance_gas_cost.rs +++ b/evm/tests/self_balance_gas_cost.rs @@ -184,7 +184,6 @@ fn self_balance_gas_cost() -> anyhow::Result<()> { prev_hashes: vec![H256::default(); 256], cur_hash: H256::default(), }, - addresses: vec![], }; let mut timing = TimingTree::new("prove", log::Level::Debug); diff --git a/evm/tests/selfdestruct.rs b/evm/tests/selfdestruct.rs index 80b22599..03879de1 100644 --- a/evm/tests/selfdestruct.rs +++ b/evm/tests/selfdestruct.rs @@ -136,7 +136,6 @@ fn test_selfdestruct() -> anyhow::Result<()> { prev_hashes: vec![H256::default(); 256], cur_hash: H256::default(), }, - addresses: vec![], }; let mut timing = TimingTree::new("prove", log::Level::Debug); diff --git a/evm/tests/simple_transfer.rs b/evm/tests/simple_transfer.rs index e79c6c1f..7ef1d61a 100644 --- a/evm/tests/simple_transfer.rs +++ b/evm/tests/simple_transfer.rs @@ -152,7 +152,6 @@ fn test_simple_transfer() -> anyhow::Result<()> { prev_hashes: vec![H256::default(); 256], cur_hash: H256::default(), }, - addresses: vec![], }; let mut timing = TimingTree::new("prove", log::Level::Debug); diff --git a/evm/tests/withdrawals.rs b/evm/tests/withdrawals.rs index 830e36ba..50d490f6 100644 --- a/evm/tests/withdrawals.rs +++ b/evm/tests/withdrawals.rs @@ -82,7 +82,6 @@ fn test_withdrawals() -> anyhow::Result<()> { prev_hashes: vec![H256::default(); 256], cur_hash: H256::default(), }, - addresses: vec![], }; let mut timing = TimingTree::new("prove", log::Level::Debug);