diff --git a/evm_arithmetization/src/arithmetic/addcy.rs b/evm_arithmetization/src/arithmetic/addcy.rs index c7a130b20..6de47845f 100644 --- a/evm_arithmetization/src/arithmetic/addcy.rs +++ b/evm_arithmetization/src/arithmetic/addcy.rs @@ -170,7 +170,6 @@ pub(crate) fn eval_packed_generic( eval_packed_generic_addcy(yield_constr, is_gt, in0, aux, in1, out, false); } -#[allow(clippy::needless_collect)] pub(crate) fn eval_ext_circuit_addcy, const D: usize>( builder: &mut CircuitBuilder, yield_constr: &mut RecursiveConstraintConsumer, diff --git a/evm_arithmetization/src/cpu/columns/general.rs b/evm_arithmetization/src/cpu/columns/general.rs index 8b2c2bc41..9e2713b9e 100644 --- a/evm_arithmetization/src/cpu/columns/general.rs +++ b/evm_arithmetization/src/cpu/columns/general.rs @@ -78,7 +78,6 @@ impl CpuGeneralColumnsView { } impl PartialEq for CpuGeneralColumnsView { - #[allow(clippy::unconditional_recursion)] // false positive fn eq(&self, other: &Self) -> bool { let self_arr: &[T; NUM_SHARED_COLUMNS] = self.borrow(); let other_arr: &[T; NUM_SHARED_COLUMNS] = other.borrow(); diff --git a/evm_arithmetization/src/cpu/kernel/constants/txn_fields.rs b/evm_arithmetization/src/cpu/kernel/constants/txn_fields.rs index e61c6ee51..9d38a8cf1 100644 --- a/evm_arithmetization/src/cpu/kernel/constants/txn_fields.rs +++ b/evm_arithmetization/src/cpu/kernel/constants/txn_fields.rs @@ -5,7 +5,6 @@ use crate::memory::segments::Segment; /// /// Each value is directly scaled by the corresponding `Segment::TxnFields` /// value for faster memory access in the kernel. -#[allow(dead_code)] #[allow(clippy::enum_clike_unportable_variant)] #[repr(usize)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, Debug)] @@ -41,6 +40,7 @@ impl NormalizedTxnField { pub(crate) const COUNT: usize = 17; /// Unscales this virtual offset by their respective `Segment` value. + #[cfg(test)] pub(crate) const fn unscale(&self) -> usize { *self as usize - Segment::TxnFields as usize } diff --git a/evm_arithmetization/src/cpu/kernel/interpreter.rs b/evm_arithmetization/src/cpu/kernel/interpreter.rs index 1197d2f83..6fdc0f231 100644 --- a/evm_arithmetization/src/cpu/kernel/interpreter.rs +++ b/evm_arithmetization/src/cpu/kernel/interpreter.rs @@ -51,6 +51,7 @@ pub(crate) struct Interpreter { /// halt_context pub(crate) halt_context: Option, /// Counts the number of appearances of each opcode. For debugging purposes. + #[allow(unused)] pub(crate) opcode_count: [usize; 0x100], jumpdest_table: HashMap>, /// `true` if the we are currently carrying out a jumpdest analysis. @@ -616,6 +617,7 @@ impl Transition for Interpreter { } } +#[cfg(debug_assertions)] fn get_mnemonic(opcode: u8) -> &'static str { match opcode { 0x00 => "STOP", diff --git a/evm_arithmetization/src/cpu/kernel/mod.rs b/evm_arithmetization/src/cpu/kernel/mod.rs index d39b015cf..8c9d1bf88 100644 --- a/evm_arithmetization/src/cpu/kernel/mod.rs +++ b/evm_arithmetization/src/cpu/kernel/mod.rs @@ -13,6 +13,7 @@ mod utils; pub(crate) mod interpreter; pub use constants::cancun_constants; +pub use constants::global_exit_root; #[cfg(test)] mod tests; diff --git a/evm_arithmetization/src/cpu/kernel/parser.rs b/evm_arithmetization/src/cpu/kernel/parser.rs index e21c6169e..7864acfe0 100644 --- a/evm_arithmetization/src/cpu/kernel/parser.rs +++ b/evm_arithmetization/src/cpu/kernel/parser.rs @@ -1,5 +1,3 @@ -#![allow(clippy::empty_docs)] - use std::str::FromStr; use ethereum_types::U256; diff --git a/evm_arithmetization/src/cpu/kernel/tests/account_code.rs b/evm_arithmetization/src/cpu/kernel/tests/account_code.rs index 5ad2d8485..125760ee5 100644 --- a/evm_arithmetization/src/cpu/kernel/tests/account_code.rs +++ b/evm_arithmetization/src/cpu/kernel/tests/account_code.rs @@ -144,7 +144,7 @@ fn prepare_interpreter( ); let hash = H256::from_uint(&interpreter.stack()[1]); - state_trie.insert(k, rlp::encode(account).to_vec()); + state_trie.insert(k, rlp::encode(account).to_vec())?; let expected_state_trie_hash = state_trie.hash(); assert_eq!(hash, expected_state_trie_hash); @@ -201,7 +201,9 @@ fn test_extcodecopy() -> Result<()> { // Pre-initialize the accessed addresses list. let init_accessed_addresses = KERNEL.global_labels["init_access_lists"]; interpreter.generation_state.registers.program_counter = init_accessed_addresses; - interpreter.push(0xdeadbeefu32.into()); + interpreter + .push(0xdeadbeefu32.into()) + .expect("The stack should not overflow"); interpreter.run()?; let extcodecopy = KERNEL.global_labels["sys_extcodecopy"]; @@ -321,7 +323,7 @@ fn sstore() -> Result<()> { let mut state_trie_before = HashedPartialTrie::from(Node::Empty); - state_trie_before.insert(addr_nibbles, rlp::encode(&account_before).to_vec()); + state_trie_before.insert(addr_nibbles, rlp::encode(&account_before).to_vec())?; let trie_inputs = TrieInputs { state_trie: state_trie_before.clone(), @@ -336,7 +338,9 @@ fn sstore() -> Result<()> { // Pre-initialize the accessed addresses list. let init_accessed_addresses = KERNEL.global_labels["init_access_lists"]; interpreter.generation_state.registers.program_counter = init_accessed_addresses; - interpreter.push(0xdeadbeefu32.into()); + interpreter + .push(0xdeadbeefu32.into()) + .expect("The stack should not overflow"); interpreter.run()?; // Prepare the interpreter by inserting the account in the state trie. @@ -384,7 +388,7 @@ fn sstore() -> Result<()> { let hash = H256::from_uint(&interpreter.stack()[1]); let mut expected_state_trie_after = HashedPartialTrie::from(Node::Empty); - expected_state_trie_after.insert(addr_nibbles, rlp::encode(&account_after).to_vec()); + expected_state_trie_after.insert(addr_nibbles, rlp::encode(&account_after).to_vec())?; let expected_state_trie_hash = expected_state_trie_after.hash(); assert_eq!(hash, expected_state_trie_hash); @@ -417,7 +421,7 @@ fn sload() -> Result<()> { let mut state_trie_before = HashedPartialTrie::from(Node::Empty); - state_trie_before.insert(addr_nibbles, rlp::encode(&account_before).to_vec()); + state_trie_before.insert(addr_nibbles, rlp::encode(&account_before).to_vec())?; let trie_inputs = TrieInputs { state_trie: state_trie_before.clone(), @@ -432,7 +436,9 @@ fn sload() -> Result<()> { // Pre-initialize the accessed addresses list. let init_accessed_addresses = KERNEL.global_labels["init_access_lists"]; interpreter.generation_state.registers.program_counter = init_accessed_addresses; - interpreter.push(0xdeadbeefu32.into()); + interpreter + .push(0xdeadbeefu32.into()) + .expect("The stack should not overflow"); interpreter.run()?; // Prepare the interpreter by inserting the account in the state trie. diff --git a/evm_arithmetization/src/cpu/kernel/tests/add11.rs b/evm_arithmetization/src/cpu/kernel/tests/add11.rs index 8d85c5a1e..ae5ac3871 100644 --- a/evm_arithmetization/src/cpu/kernel/tests/add11.rs +++ b/evm_arithmetization/src/cpu/kernel/tests/add11.rs @@ -122,26 +122,36 @@ fn test_add11_yml() { &mut beacon_roots_account_storage, block_metadata.block_timestamp, block_metadata.parent_beacon_block_root, - ); + ) + .unwrap(); let beacon_roots_account = beacon_roots_contract_from_storage(&beacon_roots_account_storage); let mut expected_state_trie_after = HashedPartialTrie::from(Node::Empty); - expected_state_trie_after.insert( - beneficiary_nibbles, - rlp::encode(&beneficiary_account_after).to_vec(), - ); expected_state_trie_after - .insert(sender_nibbles, rlp::encode(&sender_account_after).to_vec()); - expected_state_trie_after.insert(to_nibbles, rlp::encode(&to_account_after).to_vec()); - expected_state_trie_after.insert( - beacon_roots_account_nibbles(), - rlp::encode(&beacon_roots_account).to_vec(), - ); - expected_state_trie_after.insert( - ger_account_nibbles(), - rlp::encode(&GLOBAL_EXIT_ROOT_ACCOUNT).to_vec(), - ); + .insert( + beneficiary_nibbles, + rlp::encode(&beneficiary_account_after).to_vec(), + ) + .unwrap(); + expected_state_trie_after + .insert(sender_nibbles, rlp::encode(&sender_account_after).to_vec()) + .unwrap(); + expected_state_trie_after + .insert(to_nibbles, rlp::encode(&to_account_after).to_vec()) + .unwrap(); + expected_state_trie_after + .insert( + beacon_roots_account_nibbles(), + rlp::encode(&beacon_roots_account).to_vec(), + ) + .unwrap(); + expected_state_trie_after + .insert( + ger_account_nibbles(), + rlp::encode(&GLOBAL_EXIT_ROOT_ACCOUNT).to_vec(), + ) + .unwrap(); expected_state_trie_after }; let receipt_0 = LegacyReceiptRlp { @@ -151,10 +161,12 @@ fn test_add11_yml() { logs: vec![], }; let mut receipts_trie = HashedPartialTrie::from(Node::Empty); - receipts_trie.insert( - Nibbles::from_str("0x80").unwrap(), - rlp::encode(&receipt_0).to_vec(), - ); + receipts_trie + .insert( + Nibbles::from_str("0x80").unwrap(), + rlp::encode(&receipt_0).to_vec(), + ) + .unwrap(); let transactions_trie: HashedPartialTrie = Node::Leaf { nibbles: Nibbles::from_str("0x80").unwrap(), value: txn.to_vec(), @@ -290,26 +302,36 @@ fn test_add11_yml_with_exception() { &mut beacon_roots_account_storage, block_metadata.block_timestamp, block_metadata.parent_beacon_block_root, - ); + ) + .unwrap(); let beacon_roots_account = beacon_roots_contract_from_storage(&beacon_roots_account_storage); let mut expected_state_trie_after = HashedPartialTrie::from(Node::Empty); - expected_state_trie_after.insert( - beneficiary_nibbles, - rlp::encode(&beneficiary_account_after).to_vec(), - ); expected_state_trie_after - .insert(sender_nibbles, rlp::encode(&sender_account_after).to_vec()); - expected_state_trie_after.insert(to_nibbles, rlp::encode(&to_account_after).to_vec()); - expected_state_trie_after.insert( - beacon_roots_account_nibbles(), - rlp::encode(&beacon_roots_account).to_vec(), - ); - expected_state_trie_after.insert( - ger_account_nibbles(), - rlp::encode(&GLOBAL_EXIT_ROOT_ACCOUNT).to_vec(), - ); + .insert( + beneficiary_nibbles, + rlp::encode(&beneficiary_account_after).to_vec(), + ) + .unwrap(); + expected_state_trie_after + .insert(sender_nibbles, rlp::encode(&sender_account_after).to_vec()) + .unwrap(); + expected_state_trie_after + .insert(to_nibbles, rlp::encode(&to_account_after).to_vec()) + .unwrap(); + expected_state_trie_after + .insert( + beacon_roots_account_nibbles(), + rlp::encode(&beacon_roots_account).to_vec(), + ) + .unwrap(); + expected_state_trie_after + .insert( + ger_account_nibbles(), + rlp::encode(&GLOBAL_EXIT_ROOT_ACCOUNT).to_vec(), + ) + .unwrap(); expected_state_trie_after }; @@ -320,10 +342,12 @@ fn test_add11_yml_with_exception() { logs: vec![], }; let mut receipts_trie = HashedPartialTrie::from(Node::Empty); - receipts_trie.insert( - Nibbles::from_str("0x80").unwrap(), - rlp::encode(&receipt_0).to_vec(), - ); + receipts_trie + .insert( + Nibbles::from_str("0x80").unwrap(), + rlp::encode(&receipt_0).to_vec(), + ) + .unwrap(); let transactions_trie: HashedPartialTrie = Node::Leaf { nibbles: Nibbles::from_str("0x80").unwrap(), value: txn.to_vec(), diff --git a/evm_arithmetization/src/cpu/kernel/tests/balance.rs b/evm_arithmetization/src/cpu/kernel/tests/balance.rs index 034df53a8..2b8f8c241 100644 --- a/evm_arithmetization/src/cpu/kernel/tests/balance.rs +++ b/evm_arithmetization/src/cpu/kernel/tests/balance.rs @@ -96,7 +96,7 @@ fn prepare_interpreter( ); let hash = H256::from_uint(&interpreter.stack()[1]); - state_trie.insert(k, rlp::encode(account).to_vec()); + state_trie.insert(k, rlp::encode(account).to_vec())?; let expected_state_trie_hash = state_trie.hash(); assert_eq!(hash, expected_state_trie_hash); diff --git a/evm_arithmetization/src/cpu/kernel/tests/blobhash.rs b/evm_arithmetization/src/cpu/kernel/tests/blobhash.rs index 1ff200873..429bd729f 100644 --- a/evm_arithmetization/src/cpu/kernel/tests/blobhash.rs +++ b/evm_arithmetization/src/cpu/kernel/tests/blobhash.rs @@ -31,8 +31,12 @@ fn test_valid_blobhash() -> Result<()> { interpreter.set_context_metadata_field(1, GasLimit, U256::from(1000000000000u64)); - interpreter.push(index.into()); // target hash index - interpreter.push(retdest); // kexit_info + interpreter + .push(index.into()) + .expect("The stack should not overflow"); // target hash index + interpreter + .push(retdest) + .expect("The stack should not overflow"); // kexit_info interpreter.run()?; @@ -68,8 +72,12 @@ fn test_invalid_blobhash() -> Result<()> { interpreter.set_context_metadata_field(1, GasLimit, U256::from(1000000000000u64)); - interpreter.push(index.into()); // target hash index - interpreter.push(retdest); // kexit_info + interpreter + .push(index.into()) + .expect("The stack should not overflow"); // target hash index + interpreter + .push(retdest) + .expect("The stack should not overflow"); // kexit_info interpreter.run()?; diff --git a/evm_arithmetization/src/cpu/kernel/tests/bls381.rs b/evm_arithmetization/src/cpu/kernel/tests/bls381.rs index 56d6c1f7a..9fba3e7b4 100644 --- a/evm_arithmetization/src/cpu/kernel/tests/bls381.rs +++ b/evm_arithmetization/src/cpu/kernel/tests/bls381.rs @@ -1,7 +1,6 @@ use anyhow::Result; use ethereum_types::U256; use hex_literal::hex; -use keccak_hash::keccak; use plonky2::field::goldilocks_field::GoldilocksField as F; use rand::Rng; @@ -9,12 +8,10 @@ use super::{run_interpreter_with_memory, InterpreterMemoryInitialization}; use crate::cpu::kernel::aggregator::KERNEL; use crate::cpu::kernel::cancun_constants::POINT_EVALUATION_PRECOMPILE_RETURN_VALUE; use crate::cpu::kernel::constants::cancun_constants::KZG_VERSIONED_HASH; -use crate::cpu::kernel::constants::context_metadata::ContextMetadata; use crate::cpu::kernel::interpreter::Interpreter; use crate::extension_tower::{Fp2, Stack, BLS381}; -use crate::memory::segments::Segment::{self, KernelGeneral}; +use crate::memory::segments::Segment::KernelGeneral; use crate::util::sha2; -use crate::witness::errors::ProgramError; #[test] fn test_bls_fp2_mul() -> Result<()> { diff --git a/evm_arithmetization/src/cpu/kernel/tests/core/access_lists.rs b/evm_arithmetization/src/cpu/kernel/tests/core/access_lists.rs index 4b3aae8cc..d1f94c5cb 100644 --- a/evm_arithmetization/src/cpu/kernel/tests/core/access_lists.rs +++ b/evm_arithmetization/src/cpu/kernel/tests/core/access_lists.rs @@ -102,8 +102,12 @@ fn test_insert_address() -> Result<()> { assert!(address != H160::zero(), "Cosmic luck or bad RNG?"); - interpreter.push(retaddr); - interpreter.push(U256::from(address.0.as_slice())); + interpreter + .push(retaddr) + .expect("The stack should not overflow"); + interpreter + .push(U256::from(address.0.as_slice())) + .expect("The stack should not overflow"); interpreter.generation_state.registers.program_counter = insert_accessed_addresses; interpreter.run()?; @@ -146,8 +150,12 @@ fn test_insert_accessed_addresses() -> Result<()> { let offset = Segment::AccessedAddresses as usize; for i in 0..n { let addr = U256::from(addresses[i].0.as_slice()); - interpreter.push(0xdeadbeefu32.into()); - interpreter.push(addr); + interpreter + .push(0xdeadbeefu32.into()) + .expect("The stack should not overflow"); + interpreter + .push(addr) + .expect("The stack should not overflow"); interpreter.generation_state.registers.program_counter = insert_accessed_addresses; interpreter.run()?; assert_eq!(interpreter.pop().unwrap(), U256::one()); @@ -156,8 +164,12 @@ fn test_insert_accessed_addresses() -> Result<()> { for i in 0..n { // Test for address already in list. let addr_in_list = addresses[i]; - interpreter.push(retaddr); - interpreter.push(U256::from(addr_in_list.0.as_slice())); + interpreter + .push(retaddr) + .expect("The stack should not overflow"); + interpreter + .push(U256::from(addr_in_list.0.as_slice())) + .expect("The stack should not overflow"); interpreter.generation_state.registers.program_counter = insert_accessed_addresses; interpreter.run()?; assert_eq!(interpreter.pop().unwrap(), U256::zero()); @@ -170,8 +182,12 @@ fn test_insert_accessed_addresses() -> Result<()> { } // Test for address not in list. - interpreter.push(retaddr); - interpreter.push(U256::from(addr_not_in_list.0.as_slice())); + interpreter + .push(retaddr) + .expect("The stack should not overflow"); + interpreter + .push(U256::from(addr_not_in_list.0.as_slice())) + .expect("The stack should not overflow"); interpreter.generation_state.registers.program_counter = insert_accessed_addresses; interpreter.run()?; @@ -222,9 +238,15 @@ fn test_insert_accessed_storage_keys() -> Result<()> { for i in 0..n { let addr = U256::from(storage_keys[i].0 .0.as_slice()); let key = storage_keys[i].1; - interpreter.push(retaddr); - interpreter.push(key); - interpreter.push(addr); + interpreter + .push(retaddr) + .expect("The stack should not overflow"); + interpreter + .push(key) + .expect("The stack should not overflow"); + interpreter + .push(addr) + .expect("The stack should not overflow"); interpreter.generation_state.registers.program_counter = insert_accessed_storage_keys; interpreter.run()?; assert_eq!(interpreter.pop().unwrap(), U256::one()); @@ -234,9 +256,15 @@ fn test_insert_accessed_storage_keys() -> Result<()> { for i in 0..10 { // Test for storage key already in list. let (addr, key) = storage_keys[i]; - interpreter.push(retaddr); - interpreter.push(key); - interpreter.push(U256::from(addr.0.as_slice())); + interpreter + .push(retaddr) + .expect("The stack should not overflow"); + interpreter + .push(key) + .expect("The stack should not overflow"); + interpreter + .push(U256::from(addr.0.as_slice())) + .expect("The stack should not overflow"); interpreter.generation_state.registers.program_counter = insert_accessed_storage_keys; interpreter.run()?; assert_eq!(interpreter.pop().unwrap(), U256::zero()); @@ -250,9 +278,15 @@ fn test_insert_accessed_storage_keys() -> Result<()> { } // Test for storage key not in list. - interpreter.push(retaddr); - interpreter.push(storage_key_not_in_list.1); - interpreter.push(U256::from(storage_key_not_in_list.0 .0.as_slice())); + interpreter + .push(retaddr) + .expect("The stack should not overflow"); + interpreter + .push(storage_key_not_in_list.1) + .expect("The stack should not overflow"); + interpreter + .push(U256::from(storage_key_not_in_list.0 .0.as_slice())) + .expect("The stack should not overflow"); interpreter.generation_state.registers.program_counter = insert_accessed_storage_keys; interpreter.run()?; diff --git a/evm_arithmetization/src/cpu/kernel/tests/core/jumpdest_analysis.rs b/evm_arithmetization/src/cpu/kernel/tests/core/jumpdest_analysis.rs index dd9a295e2..61a580de7 100644 --- a/evm_arithmetization/src/cpu/kernel/tests/core/jumpdest_analysis.rs +++ b/evm_arithmetization/src/cpu/kernel/tests/core/jumpdest_analysis.rs @@ -72,9 +72,15 @@ fn test_jumpdest_analysis() -> Result<()> { // Run jumpdest analysis with context = 3 interpreter.generation_state.registers.context = CONTEXT; - interpreter.push(0xDEADBEEFu32.into()); - interpreter.push(code_len.into()); - interpreter.push(U256::from(CONTEXT) << CONTEXT_SCALING_FACTOR); + interpreter + .push(0xDEADBEEFu32.into()) + .expect("The stack should not overflow"); + interpreter + .push(code_len.into()) + .expect("The stack should not overflow"); + interpreter + .push(U256::from(CONTEXT) << CONTEXT_SCALING_FACTOR) + .expect("The stack should not overflow"); // We need to manually pop the jumpdest_table and push its value on the top of // the stack @@ -86,7 +92,9 @@ fn test_jumpdest_analysis() -> Result<()> { .get_mut(&CONTEXT) .unwrap() .pop(); - interpreter.push(41.into()); + interpreter + .push(41.into()) + .expect("The stack should not overflow"); interpreter.run()?; assert_eq!(interpreter.stack(), vec![]); @@ -195,14 +203,14 @@ fn test_verify_non_jumpdest() -> Result<()> { code[i] -= 1; // We check that all non jumpdests are indeed non jumpdests - for (j, &opcode) in code - .iter() - .enumerate() - .filter(|&(j, _)| j != 1 && j != 5 && j != 7) - { + for j in (0..code.len()).filter(|&i| i != 1 && i != 5 && i != 7) { interpreter.generation_state.registers.program_counter = verify_non_jumpdest; - interpreter.push(0xDEADBEEFu32.into()); - interpreter.push(j.into()); + interpreter + .push(0xDEADBEEFu32.into()) + .expect("The stack should not overflow"); + interpreter + .push(j.into()) + .expect("The stack should not overflow"); interpreter.run()?; assert!(interpreter.stack().is_empty()); assert_eq!(interpreter.get_jumpdest_bit(j), U256::zero()); diff --git a/evm_arithmetization/src/cpu/kernel/tests/mpt/insert.rs b/evm_arithmetization/src/cpu/kernel/tests/mpt/insert.rs index 771921173..d25138631 100644 --- a/evm_arithmetization/src/cpu/kernel/tests/mpt/insert.rs +++ b/evm_arithmetization/src/cpu/kernel/tests/mpt/insert.rs @@ -234,7 +234,7 @@ fn test_state_trie( ); let hash = H256::from_uint(&interpreter.stack()[1]); - state_trie.insert(k, rlp::encode(&account).to_vec()); + state_trie.insert(k, rlp::encode(&account).to_vec())?; let expected_state_trie_hash = state_trie.hash(); assert_eq!(hash, expected_state_trie_hash); diff --git a/evm_arithmetization/src/cpu/kernel/tests/transaction_parsing/parse_type_0_txn.rs b/evm_arithmetization/src/cpu/kernel/tests/transaction_parsing/parse_type_0_txn.rs index d9a952eed..08a74ee4c 100644 --- a/evm_arithmetization/src/cpu/kernel/tests/transaction_parsing/parse_type_0_txn.rs +++ b/evm_arithmetization/src/cpu/kernel/tests/transaction_parsing/parse_type_0_txn.rs @@ -15,7 +15,7 @@ fn process_type_0_txn() -> Result<()> { let process_normalized_txn = KERNEL.global_labels["process_normalized_txn"]; let retaddr = 0xDEADBEEFu32.into(); - const INITIAL_TXN_RLP_ADDR: usize = (Segment::RlpRaw as usize + 1); + const INITIAL_TXN_RLP_ADDR: usize = Segment::RlpRaw as usize + 1; let mut interpreter: Interpreter = Interpreter::new( process_type_0_txn, vec![retaddr, INITIAL_TXN_RLP_ADDR.into()], diff --git a/evm_arithmetization/src/cpu/kernel/tests/transient_storage.rs b/evm_arithmetization/src/cpu/kernel/tests/transient_storage.rs index d1332c586..f1ec59618 100644 --- a/evm_arithmetization/src/cpu/kernel/tests/transient_storage.rs +++ b/evm_arithmetization/src/cpu/kernel/tests/transient_storage.rs @@ -1,22 +1,16 @@ -use std::array; - use anyhow::Result; -use ethereum_types::{Address, U256}; +use ethereum_types::U256; use once_cell::sync::Lazy; -use pest::error::Error; use plonky2::field::goldilocks_field::GoldilocksField as F; -use rand::{thread_rng, Rng}; use crate::cpu::kernel::aggregator::{ combined_kernel_from_files, KERNEL_FILES, NUMBER_KERNEL_FILES, }; use crate::cpu::kernel::assembler::Kernel; use crate::cpu::kernel::constants::context_metadata::ContextMetadata; -use crate::cpu::kernel::constants::global_metadata::GlobalMetadata; -use crate::cpu::kernel::interpreter::{self, Interpreter}; +use crate::cpu::kernel::interpreter::Interpreter; use crate::generation::state::GenerationState; use crate::memory::segments::Segment; -use crate::witness::errors::ProgramError; use crate::witness::memory::MemoryAddress; use crate::GenerationInputs; @@ -119,8 +113,12 @@ fn test_tstore_tload() -> Result<()> { + (U256::from(interpreter.generation_state.registers.gas_used) << 192); interpreter.generation_state.registers.program_counter = sys_tload; interpreter.generation_state.registers.is_kernel = true; - interpreter.push(2.into()); - interpreter.push(kexit_info); + interpreter + .push(2.into()) + .expect("The stack should not overflow"); + interpreter + .push(kexit_info) + .expect("The stack should not overflow"); interpreter.run()?; @@ -134,8 +132,12 @@ fn test_tstore_tload() -> Result<()> { interpreter.generation_state.registers.program_counter = sys_tload; interpreter.generation_state.registers.is_kernel = true; let slot: U256 = 4.into(); - interpreter.push(slot); - interpreter.push(kexit_info); + interpreter + .push(slot) + .expect("The stack should not overflow"); + interpreter + .push(kexit_info) + .expect("The stack should not overflow"); interpreter.run()?; @@ -175,7 +177,7 @@ fn test_many_tstore_many_tload() -> Result<()> { let sys_tstore = crate::cpu::kernel::aggregator::KERNEL.global_labels["sys_tstore"]; - for i in (0..10) { + for i in 0..10 { interpreter.generation_state.registers.program_counter = sys_tstore; interpreter.generation_state.registers.is_kernel = true; let kexit_info = U256::from(0xdeadbeefu32) @@ -183,9 +185,15 @@ fn test_many_tstore_many_tload() -> Result<()> { + (U256::from(interpreter.generation_state.registers.gas_used) << 192); let val: U256 = i.into(); let slot: U256 = i.into(); - interpreter.push(val); - interpreter.push(slot); - interpreter.push(kexit_info); + interpreter + .push(val) + .expect("The stack should not overflow"); + interpreter + .push(slot) + .expect("The stack should not overflow"); + interpreter + .push(kexit_info) + .expect("The stack should not overflow"); interpreter.run()?; assert_eq!( @@ -196,15 +204,19 @@ fn test_many_tstore_many_tload() -> Result<()> { let sys_tload = crate::cpu::kernel::aggregator::KERNEL.global_labels["sys_tload"]; - for i in (0..10) { + for i in 0..10 { interpreter.generation_state.registers.program_counter = sys_tload; interpreter.generation_state.registers.is_kernel = true; let kexit_info = U256::from(0xdeadbeefu32) + (U256::from(u64::from(true)) << 32) + (U256::from(interpreter.generation_state.registers.gas_used) << 192); let slot: U256 = i.into(); - interpreter.push(slot); - interpreter.push(kexit_info); + interpreter + .push(slot) + .expect("The stack should not overflow"); + interpreter + .push(kexit_info) + .expect("The stack should not overflow"); interpreter.run()?; assert_eq!( @@ -257,7 +269,7 @@ fn test_revert() -> Result<()> { interpreter.generation_state.memory.set(addr_addr, 3.into()); // Store different values at slot 1 - for i in (0..10) { + for i in 0..10 { interpreter.generation_state.registers.program_counter = sys_tstore; interpreter.generation_state.registers.is_kernel = true; let kexit_info = U256::from(0xdeadbeefu32) @@ -265,9 +277,15 @@ fn test_revert() -> Result<()> { + (U256::from(interpreter.generation_state.registers.gas_used) << 192); let val: U256 = i.into(); let slot: U256 = 1.into(); - interpreter.push(val); - interpreter.push(slot); - interpreter.push(kexit_info); + interpreter + .push(val) + .expect("The stack should not overflow"); + interpreter + .push(slot) + .expect("The stack should not overflow"); + interpreter + .push(kexit_info) + .expect("The stack should not overflow"); interpreter.run()?; assert_eq!( @@ -282,7 +300,9 @@ fn test_revert() -> Result<()> { let checkpoint = KERNEL.global_labels["checkpoint"]; interpreter.generation_state.registers.program_counter = checkpoint; interpreter.generation_state.registers.is_kernel = true; - interpreter.push(0xdeadbeefu32.into()); + interpreter + .push(0xdeadbeefu32.into()) + .expect("The stack should not overflow"); interpreter.run()?; assert!(interpreter.stack().is_empty()); @@ -290,7 +310,7 @@ fn test_revert() -> Result<()> { interpreter.generation_state.registers.gas_used = gas_before_checkpoint; // Now we change `val` 10 more times - for i in (10..20) { + for i in 10..20 { interpreter.generation_state.registers.program_counter = sys_tstore; interpreter.generation_state.registers.is_kernel = true; let kexit_info = U256::from(0xdeadbeefu32) @@ -298,9 +318,15 @@ fn test_revert() -> Result<()> { + (U256::from(interpreter.generation_state.registers.gas_used) << 192); let val: U256 = i.into(); let slot: U256 = 1.into(); - interpreter.push(val); - interpreter.push(slot); - interpreter.push(kexit_info); + interpreter + .push(val) + .expect("The stack should not overflow"); + interpreter + .push(slot) + .expect("The stack should not overflow"); + interpreter + .push(kexit_info) + .expect("The stack should not overflow"); interpreter.run()?; assert_eq!( @@ -315,9 +341,15 @@ fn test_revert() -> Result<()> { let kexit_info = U256::from(0xdeadbeefu32) + (U256::from(u64::from(true)) << 32) + (U256::from(interpreter.generation_state.registers.gas_used) << 192); - interpreter.push(3.into()); // val - interpreter.push(2.into()); // slot - interpreter.push(kexit_info); + interpreter + .push(3.into()) + .expect("The stack should not overflow"); // val + interpreter + .push(2.into()) + .expect("The stack should not overflow"); // slot + interpreter + .push(kexit_info) + .expect("The stack should not overflow"); assert!(interpreter.run().is_err()); // Now we should load the value before the revert @@ -326,8 +358,12 @@ fn test_revert() -> Result<()> { interpreter.generation_state.registers.gas_used = 0; let kexit_info = U256::from(0xdeadbeefu32) + (U256::from(u64::from(true)) << 32); interpreter.generation_state.registers.is_kernel = true; - interpreter.push(1.into()); - interpreter.push(kexit_info); + interpreter + .push(1.into()) + .expect("The stack should not overflow"); + interpreter + .push(kexit_info) + .expect("The stack should not overflow"); interpreter.run()?; diff --git a/evm_arithmetization/src/curve_pairings.rs b/evm_arithmetization/src/curve_pairings.rs index 2d9f8248d..f52d024d8 100644 --- a/evm_arithmetization/src/curve_pairings.rs +++ b/evm_arithmetization/src/curve_pairings.rs @@ -5,7 +5,7 @@ use rand::distributions::Standard; use rand::prelude::Distribution; use rand::Rng; -use crate::extension_tower::{Adj, FieldExt, Fp12, Fp2, Fp6, Stack, BLS381, BN254}; +use crate::extension_tower::{Adj, FieldExt, Fp12, Fp2, Fp6, Stack, BLS381}; #[derive(Debug, Copy, Clone, PartialEq)] pub(crate) struct CurveAff @@ -42,6 +42,7 @@ impl Stack for CurveAff { } } +#[cfg(test)] impl CurveAff where T: FieldExt, @@ -151,16 +152,6 @@ where pub z: T, } -impl CurveProj { - pub(crate) const fn unit() -> Self { - CurveProj { - x: T::ZERO, - y: T::ZERO, - z: T::ZERO, - } - } -} - impl Stack for CurveProj { const SIZE: usize = 3 * T::SIZE; @@ -182,6 +173,7 @@ impl Stack for CurveProj { /// The tangent and chord functions output sparse Fp12 elements. /// This map embeds the nonzero coefficients into an Fp12. +#[cfg(test)] pub(crate) const fn sparse_embed(g000: F, g01: Fp2, g11: Fp2) -> Fp12 where F: FieldExt, @@ -205,21 +197,8 @@ where Fp12 { z0: g0, z1: g1 } } -pub(crate) fn check_curve_eq_aff(p: CurveAff, b_coeff: T) -> bool -where - T: FieldExt, -{ - p.y * p.y == p.x * p.x * p.x + b_coeff -} - -pub(crate) fn check_curve_eq_proj(p: CurveProj, b_coeff: T) -> bool -where - T: FieldExt, -{ - p.y * p.y == p.x * p.x * p.x + b_coeff -} - /// Generates a sparse, random Fp12 element. +#[cfg(test)] pub(crate) fn gen_fp12_sparse(rng: &mut R) -> Fp12 where F: FieldExt, @@ -229,8 +208,10 @@ where sparse_embed::(rng.gen::(), rng.gen::>(), rng.gen::>()) } +#[cfg(test)] pub mod bn254 { use super::*; + use crate::extension_tower::BN254; /// The BN curve consists of pairs /// (x, y): (BN254, BN254) | y^2 = x^3 + 3 @@ -769,7 +750,6 @@ pub mod bls381 { z: Fp2::::UNIT, }; let mut acc: Fp12 = Fp12::::UNIT; - let mut line: Fp12; let mut found_one = false; for i in (0..64).rev().map(|b| (((X_GENERATOR >> 1) >> b) & 1) == 1) { @@ -1005,9 +985,8 @@ pub mod bls381 { #[cfg(test)] mod tests { - use rand::thread_rng; - use super::*; + use crate::extension_tower::BN254; #[test] fn test_bls_pairing() { diff --git a/evm_arithmetization/src/generation/prover_input.rs b/evm_arithmetization/src/generation/prover_input.rs index 2e05f8a2f..1ec692c0d 100644 --- a/evm_arithmetization/src/generation/prover_input.rs +++ b/evm_arithmetization/src/generation/prover_input.rs @@ -1,4 +1,3 @@ -use core::cmp::min; use core::mem::transmute; use core::ops::Neg; use std::collections::{BTreeSet, HashMap}; @@ -7,7 +6,6 @@ use std::str::FromStr; use anyhow::{bail, Error, Result}; use ethereum_types::{BigEndianHash, H256, U256, U512}; use itertools::Itertools; -use keccak_hash::keccak; use num_bigint::BigUint; use plonky2::field::types::Field; use serde::{Deserialize, Serialize}; @@ -29,7 +27,7 @@ use crate::generation::prover_input::FieldOp::{Inverse, Sqrt}; use crate::generation::state::GenerationState; use crate::memory::segments::Segment; use crate::memory::segments::Segment::BnPairing; -use crate::util::{biguint_to_mem_vec, h2u, mem_vec_to_biguint, sha2, u256_to_u8, u256_to_usize}; +use crate::util::{biguint_to_mem_vec, mem_vec_to_biguint, sha2, u256_to_u8, u256_to_usize}; use crate::witness::errors::ProverInputError::*; use crate::witness::errors::{ProgramError, ProverInputError}; use crate::witness::memory::MemoryAddress; @@ -304,8 +302,6 @@ impl GenerationState { )); }; - let jd_len = jumpdest_table.len(); - if let Some(ctx_jumpdest_table) = jumpdest_table.get_mut(&context) && let Some(next_jumpdest_address) = ctx_jumpdest_table.pop() { @@ -325,8 +321,6 @@ impl GenerationState { )); }; - let jd_len = jumpdest_table.len(); - if let Some(ctx_jumpdest_table) = jumpdest_table.get_mut(&context) && let Some(next_jumpdest_proof) = ctx_jumpdest_table.pop() { @@ -526,7 +520,7 @@ impl GenerationState { let mut z_bytes = [0u8; 32]; z.to_big_endian(&mut z_bytes); let mut acc = CurveAff::>::unit(); - for (i, &byte) in z_bytes.iter().enumerate() { + for byte in z_bytes.into_iter() { acc = acc * 256_i32; acc = acc + (CurveAff::>::GENERATOR * byte as i32); } diff --git a/evm_arithmetization/src/generation/state.rs b/evm_arithmetization/src/generation/state.rs index f62fe9902..bc1a02203 100644 --- a/evm_arithmetization/src/generation/state.rs +++ b/evm_arithmetization/src/generation/state.rs @@ -163,7 +163,7 @@ pub(crate) trait State { } } else { #[cfg(not(test))] - self.log_info(format!("CPU halted after {} cycles", self.get_clock())); + log::info!("CPU halted after {} cycles", self.get_clock()); return Ok(()); } } @@ -262,12 +262,6 @@ pub(crate) trait State { log::debug!("{}", msg); } - /// Logs `msg` in `info` mode. - #[inline] - fn log_info(&self, msg: String) { - log::info!("{}", msg); - } - /// Logs `msg` at `level`. #[inline] fn log(&self, level: Level, msg: String) { diff --git a/evm_arithmetization/src/generation/trie_extractor.rs b/evm_arithmetization/src/generation/trie_extractor.rs index 0d825643f..48fc28f53 100644 --- a/evm_arithmetization/src/generation/trie_extractor.rs +++ b/evm_arithmetization/src/generation/trie_extractor.rs @@ -1,9 +1,7 @@ //! Code for extracting trie data after witness generation. This is intended //! only for debugging. -use std::collections::HashMap; - -use ethereum_types::{BigEndianHash, H256, U256, U512}; +use ethereum_types::{BigEndianHash, H256, U256}; use mpt_trie::nibbles::{Nibbles, NibblesIntern}; use mpt_trie::partial_trie::{HashedPartialTrie, Node, PartialTrie, WrappedNode}; @@ -14,108 +12,10 @@ use crate::util::{u256_to_bool, u256_to_h160, u256_to_u8, u256_to_usize}; use crate::witness::errors::ProgramError; use crate::witness::memory::{MemoryAddress, MemoryState}; -/// Account data as it's stored in the state trie, with a pointer to the storage -/// trie. -#[derive(Debug)] -pub(crate) struct AccountTrieRecord { - pub(crate) nonce: u64, - pub(crate) balance: U256, - pub(crate) storage_ptr: usize, - pub(crate) code_hash: H256, -} - -pub(crate) fn read_state_trie_value( - slice: &[Option], -) -> Result { - Ok(AccountTrieRecord { - nonce: slice[0].unwrap_or_default().low_u64(), - balance: slice[1].unwrap_or_default(), - storage_ptr: u256_to_usize(slice[2].unwrap_or_default())?, - code_hash: H256::from_uint(&slice[3].unwrap_or_default()), - }) -} - pub(crate) fn read_storage_trie_value(slice: &[Option]) -> U256 { slice[0].unwrap_or_default() } -pub(crate) fn read_trie( - memory: &MemoryState, - ptr: usize, - read_value: fn(&[Option]) -> Result, -) -> Result, ProgramError> { - let mut res = HashMap::new(); - let empty_nibbles = Nibbles { - count: 0, - packed: NibblesIntern::zero(), - }; - read_trie_helper::(memory, ptr, read_value, empty_nibbles, &mut res)?; - Ok(res) -} - -pub(crate) fn read_trie_helper( - memory: &MemoryState, - ptr: usize, - read_value: fn(&[Option]) -> Result, - prefix: Nibbles, - res: &mut HashMap, -) -> Result<(), ProgramError> { - let load = |offset| memory.contexts[0].segments[Segment::TrieData as usize].content[offset]; - let load_slice_from = |init_offset| { - &memory.contexts[0].segments[Segment::TrieData.unscale()].content[init_offset..] - }; - - let trie_type = PartialTrieType::all()[u256_to_usize(load(ptr).unwrap_or_default())?]; - match trie_type { - PartialTrieType::Empty => Ok(()), - PartialTrieType::Hash => Ok(()), - PartialTrieType::Branch => { - let ptr_payload = ptr + 1; - for i in 0u8..16 { - let child_ptr = u256_to_usize(load(ptr_payload + i as usize).unwrap_or_default())?; - read_trie_helper::(memory, child_ptr, read_value, prefix.merge_nibble(i), res)?; - } - let value_ptr = u256_to_usize(load(ptr_payload + 16).unwrap_or_default())?; - if value_ptr != 0 { - res.insert(prefix, read_value(load_slice_from(value_ptr))?); - }; - - Ok(()) - } - PartialTrieType::Extension => { - let count = u256_to_usize(load(ptr + 1).unwrap_or_default())?; - let packed = load(ptr + 2).unwrap_or_default(); - let nibbles = Nibbles { - count, - packed: packed.into(), - }; - let child_ptr = u256_to_usize(load(ptr + 3).unwrap_or_default())?; - read_trie_helper::( - memory, - child_ptr, - read_value, - prefix.merge_nibbles(&nibbles), - res, - ) - } - PartialTrieType::Leaf => { - let count = u256_to_usize(load(ptr + 1).unwrap_or_default())?; - let packed = load(ptr + 2).unwrap_or_default(); - let nibbles = Nibbles { - count, - packed: packed.into(), - }; - let value_ptr = u256_to_usize(load(ptr + 3).unwrap_or_default())?; - res.insert( - prefix.merge_nibbles(&nibbles), - read_value(load_slice_from(value_ptr))?, - ); - - Ok(()) - } - } -} - pub(crate) fn read_receipt_trie_value( slice: &[Option], ) -> Result<(Option, LegacyReceiptRlp), ProgramError> { diff --git a/evm_arithmetization/src/lib.rs b/evm_arithmetization/src/lib.rs index 3809cc023..36814576c 100644 --- a/evm_arithmetization/src/lib.rs +++ b/evm_arithmetization/src/lib.rs @@ -181,7 +181,6 @@ #![allow(clippy::needless_range_loop)] #![allow(clippy::too_many_arguments)] #![allow(clippy::field_reassign_with_default)] -#![allow(unused)] #![feature(let_chains)] // Individual STARK processing units diff --git a/evm_arithmetization/src/memory/segments.rs b/evm_arithmetization/src/memory/segments.rs index 67aa93a6d..896d5571f 100644 --- a/evm_arithmetization/src/memory/segments.rs +++ b/evm_arithmetization/src/memory/segments.rs @@ -3,7 +3,6 @@ pub(crate) const SEGMENT_SCALING_FACTOR: usize = 32; /// This contains all the existing memory segments. The values in the enum are /// shifted by 32 bits to allow for convenient address components (context / /// segment / virtual) bundling in the kernel. -#[allow(dead_code)] #[allow(clippy::enum_clike_unportable_variant)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, Debug)] pub(crate) enum Segment { diff --git a/evm_arithmetization/src/testing_utils.rs b/evm_arithmetization/src/testing_utils.rs index a72c1359f..bb1f07b64 100644 --- a/evm_arithmetization/src/testing_utils.rs +++ b/evm_arithmetization/src/testing_utils.rs @@ -59,19 +59,6 @@ pub fn create_account_storage(storage_pairs: &[(U256, U256)]) -> anyhow::Result< Ok(trie) } -/// Creates the storage trie of the beacon roots contract account at the -/// provided timestamp. Not passing any parent root will consider the parent -/// root at genesis, i.e. the empty hash. -fn beacon_roots_contract_storage( - timestamp: U256, - parent_root: H256, -) -> anyhow::Result { - let timestamp_idx = timestamp % HISTORY_BUFFER_LENGTH.1; - let root_idx = timestamp_idx + HISTORY_BUFFER_LENGTH.1; - - create_account_storage(&[(timestamp_idx, timestamp), (root_idx, h2u(parent_root))]) -} - /// Updates the beacon roots account storage with the provided timestamp and /// block parent root. pub fn update_beacon_roots_account_storage( diff --git a/evm_arithmetization/src/util.rs b/evm_arithmetization/src/util.rs index 63adf7103..38503a091 100644 --- a/evm_arithmetization/src/util.rs +++ b/evm_arithmetization/src/util.rs @@ -127,17 +127,6 @@ pub(crate) fn h256_limbs(h256: H256) -> [F; 8] { .unwrap() } -/// Returns the 32-bit limbs of a `U160`. -pub(crate) fn h160_limbs(h160: H160) -> [F; 5] { - h160.0 - .chunks(4) - .map(|chunk| u32::from_le_bytes(chunk.try_into().unwrap())) - .map(F::from_canonical_u32) - .collect_vec() - .try_into() - .unwrap() -} - pub(crate) const fn indices_arr() -> [usize; N] { let mut indices_arr = [0; N]; let mut i = 0; diff --git a/evm_arithmetization/src/verifier.rs b/evm_arithmetization/src/verifier.rs index 4de9b1b17..90a287f1e 100644 --- a/evm_arithmetization/src/verifier.rs +++ b/evm_arithmetization/src/verifier.rs @@ -294,6 +294,7 @@ where running_sum + challenge.combine(row.iter()).inverse() } +#[cfg(debug_assertions)] pub(crate) mod debug_utils { use super::*;