From 41a29f069b6731c4af2644337959fbad8c771c77 Mon Sep 17 00:00:00 2001 From: Robin Salen <30937548+Nashtare@users.noreply.github.com> Date: Mon, 9 Oct 2023 09:07:01 -0400 Subject: [PATCH] Remove some dead_code in EVM crate (#1281) * Remove unnecessary CpuArithmeticView. * Remove AllChallengerState * Remove RecursiveAllProof * Remove unused generate methods * Remove dead_code from cpu/columns * Remove todo --------- Co-authored-by: Linda Guiga --- evm/src/cpu/columns/general.rs | 17 ------- evm/src/cpu/columns/mod.rs | 3 -- evm/src/cpu/columns/ops.rs | 9 +--- evm/src/cpu/cpu_stark.rs | 14 ++---- evm/src/cpu/decode.rs | 45 ----------------- evm/src/cpu/membus.rs | 14 ------ evm/src/get_challenges.rs | 34 ------------- evm/src/proof.rs | 11 +---- evm/src/recursive_verifier.rs | 88 +++------------------------------- 9 files changed, 11 insertions(+), 224 deletions(-) diff --git a/evm/src/cpu/columns/general.rs b/evm/src/cpu/columns/general.rs index 91a35c21..57eb16fc 100644 --- a/evm/src/cpu/columns/general.rs +++ b/evm/src/cpu/columns/general.rs @@ -6,7 +6,6 @@ use std::mem::{size_of, transmute}; /// operation is occurring at this row. #[derive(Clone, Copy)] pub(crate) union CpuGeneralColumnsView { - arithmetic: CpuArithmeticView, exception: CpuExceptionView, logic: CpuLogicView, jumps: CpuJumpsView, @@ -14,16 +13,6 @@ pub(crate) union CpuGeneralColumnsView { } impl CpuGeneralColumnsView { - // SAFETY: Each view is a valid interpretation of the underlying array. - pub(crate) fn arithmetic(&self) -> &CpuArithmeticView { - unsafe { &self.arithmetic } - } - - // SAFETY: Each view is a valid interpretation of the underlying array. - pub(crate) fn arithmetic_mut(&mut self) -> &mut CpuArithmeticView { - unsafe { &mut self.arithmetic } - } - // SAFETY: Each view is a valid interpretation of the underlying array. pub(crate) fn exception(&self) -> &CpuExceptionView { unsafe { &self.exception } @@ -94,12 +83,6 @@ impl BorrowMut<[T; NUM_SHARED_COLUMNS]> for CpuGeneralColumnsView { } } -#[derive(Copy, Clone)] -pub(crate) struct CpuArithmeticView { - // TODO: Add "looking" columns for the arithmetic CTL. - tmp: T, // temporary, to suppress errors -} - #[derive(Copy, Clone)] pub(crate) struct CpuExceptionView { // Exception code as little-endian bits. diff --git a/evm/src/cpu/columns/mod.rs b/evm/src/cpu/columns/mod.rs index cc98fceb..b7b4f780 100644 --- a/evm/src/cpu/columns/mod.rs +++ b/evm/src/cpu/columns/mod.rs @@ -1,6 +1,3 @@ -// TODO: remove when possible. -#![allow(dead_code)] - use std::borrow::{Borrow, BorrowMut}; use std::fmt::Debug; use std::mem::{size_of, transmute}; diff --git a/evm/src/cpu/columns/ops.rs b/evm/src/cpu/columns/ops.rs index d4d753f7..64474c98 100644 --- a/evm/src/cpu/columns/ops.rs +++ b/evm/src/cpu/columns/ops.rs @@ -2,7 +2,7 @@ use std::borrow::{Borrow, BorrowMut}; use std::mem::{size_of, transmute}; use std::ops::{Deref, DerefMut}; -use crate::util::{indices_arr, transmute_no_compile_time_size_checks}; +use crate::util::transmute_no_compile_time_size_checks; #[repr(C)] #[derive(Clone, Copy, Eq, PartialEq, Debug)] @@ -73,10 +73,3 @@ impl DerefMut for OpsColumnsView { unsafe { transmute(self) } } } - -const fn make_col_map() -> OpsColumnsView { - let indices_arr = indices_arr::(); - unsafe { transmute::<[usize; NUM_OPS_COLUMNS], OpsColumnsView>(indices_arr) } -} - -pub const COL_MAP: OpsColumnsView = make_col_map(); diff --git a/evm/src/cpu/cpu_stark.rs b/evm/src/cpu/cpu_stark.rs index 82ca5452..a77adbcb 100644 --- a/evm/src/cpu/cpu_stark.rs +++ b/evm/src/cpu/cpu_stark.rs @@ -1,4 +1,4 @@ -use std::borrow::{Borrow, BorrowMut}; +use std::borrow::Borrow; use std::iter::repeat; use std::marker::PhantomData; @@ -9,10 +9,11 @@ use plonky2::field::types::Field; use plonky2::hash::hash_types::RichField; use plonky2::iop::ext_target::ExtensionTarget; +use super::columns::CpuColumnsView; use super::halt; use crate::all_stark::Table; use crate::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; -use crate::cpu::columns::{CpuColumnsView, COL_MAP, NUM_CPU_COLUMNS}; +use crate::cpu::columns::{COL_MAP, NUM_CPU_COLUMNS}; use crate::cpu::membus::NUM_GP_CHANNELS; use crate::cpu::{ bootstrap_kernel, contextops, control_flow, decode, dup_swap, gas, jumps, membus, memio, @@ -198,15 +199,6 @@ pub struct CpuStark { pub f: PhantomData, } -impl CpuStark { - // TODO: Remove? - pub fn generate(&self, local_values: &mut [F; NUM_CPU_COLUMNS]) { - let local_values: &mut CpuColumnsView<_> = local_values.borrow_mut(); - decode::generate(local_values); - membus::generate(local_values); - } -} - impl, const D: usize> Stark for CpuStark { type EvaluationFrame = StarkFrame where diff --git a/evm/src/cpu/decode.rs b/evm/src/cpu/decode.rs index c1c43a0b..ba4aa0c6 100644 --- a/evm/src/cpu/decode.rs +++ b/evm/src/cpu/decode.rs @@ -61,51 +61,6 @@ const COMBINED_OPCODES: [usize; 6] = [ COL_MAP.op.m_op_general, ]; -pub fn generate(lv: &mut CpuColumnsView) { - let cycle_filter: F = COL_MAP.op.iter().map(|&col_i| lv[col_i]).sum(); - - // This assert is not _strictly_ necessary, but I include it as a sanity check. - assert_eq!(cycle_filter, F::ONE, "cycle_filter should be 0 or 1"); - - // Validate all opcode bits. - for bit in lv.opcode_bits.into_iter() { - assert!(bit.to_canonical_u64() <= 1); - } - let opcode = lv - .opcode_bits - .into_iter() - .enumerate() - .map(|(i, bit)| bit.to_canonical_u64() << i) - .sum::() as u8; - - let top_bits: [u8; 9] = [ - 0, - opcode & 0x80, - opcode & 0xc0, - opcode & 0xe0, - opcode & 0xf0, - opcode & 0xf8, - opcode & 0xfc, - opcode & 0xfe, - opcode, - ]; - - let kernel = lv.is_kernel_mode.to_canonical_u64(); - assert!(kernel <= 1); - let kernel = kernel != 0; - - for (oc, block_length, kernel_only, col) in OPCODES { - let available = !kernel_only || kernel; - let opcode_match = top_bits[8 - block_length] == oc; - let flag = available && opcode_match; - lv[col] = F::from_bool(flag); - } - - if opcode == 0xfb || opcode == 0xfc { - lv.op.m_op_general = F::from_bool(kernel); - } -} - /// Break up an opcode (which is 8 bits long) into its eight bits. const fn bits_from_opcode(opcode: u8) -> [bool; 8] { [ diff --git a/evm/src/cpu/membus.rs b/evm/src/cpu/membus.rs index bf7a03ae..10dc25a4 100644 --- a/evm/src/cpu/membus.rs +++ b/evm/src/cpu/membus.rs @@ -1,10 +1,8 @@ use plonky2::field::extension::Extendable; use plonky2::field::packed::PackedField; -use plonky2::field::types::PrimeField64; use plonky2::hash::hash_types::RichField; use plonky2::iop::ext_target::ExtensionTarget; -use super::columns::COL_MAP; use crate::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer}; use crate::cpu::columns::CpuColumnsView; @@ -33,18 +31,6 @@ pub mod channel_indices { /// These limitations save us numerous columns in the CPU table. pub const NUM_CHANNELS: usize = channel_indices::GP.end; -/// Calculates `lv.stack_len_bounds_aux`. Note that this must be run after decode. -pub fn generate(lv: &CpuColumnsView) { - let cycle_filter: F = COL_MAP.op.iter().map(|&col_i| lv[col_i]).sum(); - if cycle_filter != F::ZERO { - assert!(lv.is_kernel_mode.to_canonical_u64() <= 1); - } - - for channel in lv.mem_channels { - assert!(channel.used.to_canonical_u64() <= 1); - } -} - pub fn eval_packed( lv: &CpuColumnsView

, yield_constr: &mut ConstraintConsumer

, diff --git a/evm/src/get_challenges.rs b/evm/src/get_challenges.rs index ed8ff915..e9e5de93 100644 --- a/evm/src/get_challenges.rs +++ b/evm/src/get_challenges.rs @@ -6,7 +6,6 @@ use plonky2::iop::challenger::{Challenger, RecursiveChallenger}; use plonky2::plonk::circuit_builder::CircuitBuilder; use plonky2::plonk::config::{AlgebraicHasher, GenericConfig}; -use crate::all_stark::{AllStark, NUM_TABLES}; use crate::config::StarkConfig; use crate::cross_table_lookup::get_grand_product_challenge_set; use crate::proof::*; @@ -234,39 +233,6 @@ impl, C: GenericConfig, const D: usize> A ctl_challenges, }) } - - #[allow(unused)] // TODO: should be used soon - pub(crate) fn get_challenger_states( - &self, - all_stark: &AllStark, - config: &StarkConfig, - ) -> AllChallengerState { - let mut challenger = Challenger::::new(); - - for proof in &self.stark_proofs { - challenger.observe_cap(&proof.proof.trace_cap); - } - - observe_public_values::(&mut challenger, &self.public_values); - - let ctl_challenges = - get_grand_product_challenge_set(&mut challenger, config.num_challenges); - - let lookups = all_stark.num_lookups_helper_columns(config); - - let mut challenger_states = vec![challenger.compact()]; - for i in 0..NUM_TABLES { - self.stark_proofs[i] - .proof - .get_challenges(&mut challenger, config); - challenger_states.push(challenger.compact()); - } - - AllChallengerState { - states: challenger_states.try_into().unwrap(), - ctl_challenges, - } - } } impl StarkProof diff --git a/evm/src/proof.rs b/evm/src/proof.rs index 3f744a61..43561e8c 100644 --- a/evm/src/proof.rs +++ b/evm/src/proof.rs @@ -39,14 +39,6 @@ pub(crate) struct AllProofChallenges, const D: usiz pub ctl_challenges: GrandProductChallengeSet, } -#[allow(unused)] // TODO: should be used soon -pub(crate) struct AllChallengerState, H: Hasher, const D: usize> { - /// Sponge state of the challenger before starting each proof, - /// along with the final state after all proofs are done. This final state isn't strictly needed. - pub states: [H::Permutation; NUM_TABLES + 1], - pub ctl_challenges: GrandProductChallengeSet, -} - /// Memory values which are public. #[derive(Debug, Clone, Default, Deserialize, Serialize)] pub struct PublicValues { @@ -697,8 +689,7 @@ where C: GenericConfig, { pub(crate) init_challenger_state: >::Permutation, - // TODO: set it back to pub(crate) when cpu trace len is a public input - pub proof: StarkProof, + pub(crate) proof: StarkProof, } impl, C: GenericConfig, const D: usize> StarkProof { diff --git a/evm/src/recursive_verifier.rs b/evm/src/recursive_verifier.rs index 8c99ca5d..d88d8471 100644 --- a/evm/src/recursive_verifier.rs +++ b/evm/src/recursive_verifier.rs @@ -1,6 +1,6 @@ use std::fmt::Debug; -use anyhow::{ensure, Result}; +use anyhow::Result; use ethereum_types::{BigEndianHash, U256}; use plonky2::field::extension::Extendable; use plonky2::field::types::Field; @@ -10,13 +10,13 @@ use plonky2::gates::gate::GateRef; use plonky2::gates::noop::NoopGate; use plonky2::hash::hash_types::RichField; use plonky2::hash::hashing::PlonkyPermutation; -use plonky2::iop::challenger::{Challenger, RecursiveChallenger}; +use plonky2::iop::challenger::RecursiveChallenger; use plonky2::iop::ext_target::ExtensionTarget; use plonky2::iop::target::Target; use plonky2::iop::witness::{PartialWitness, Witness, WitnessWrite}; use plonky2::plonk::circuit_builder::CircuitBuilder; -use plonky2::plonk::circuit_data::{CircuitConfig, CircuitData, VerifierCircuitData}; -use plonky2::plonk::config::{AlgebraicHasher, GenericConfig, Hasher}; +use plonky2::plonk::circuit_data::{CircuitConfig, CircuitData}; +use plonky2::plonk::config::{AlgebraicHasher, GenericConfig}; use plonky2::plonk::proof::{ProofWithPublicInputs, ProofWithPublicInputsTarget}; use plonky2::util::reducing::ReducingFactorTarget; use plonky2::util::serialization::{ @@ -25,13 +25,12 @@ use plonky2::util::serialization::{ use plonky2::with_context; use plonky2_util::log2_ceil; -use crate::all_stark::{Table, NUM_TABLES}; +use crate::all_stark::Table; use crate::config::StarkConfig; use crate::constraint_consumer::RecursiveConstraintConsumer; use crate::cpu::kernel::constants::global_metadata::GlobalMetadata; use crate::cross_table_lookup::{ - get_grand_product_challenge_set, verify_cross_table_lookups, CrossTableLookup, - CtlCheckVarsTarget, GrandProductChallenge, GrandProductChallengeSet, + CrossTableLookup, CtlCheckVarsTarget, GrandProductChallenge, GrandProductChallengeSet, }; use crate::evaluation_frame::StarkEvaluationFrame; use crate::lookup::LookupCheckVarsTarget; @@ -48,15 +47,6 @@ use crate::util::{h256_limbs, u256_limbs, u256_to_u32, u256_to_u64}; use crate::vanishing_poly::eval_vanishing_poly_circuit; use crate::witness::errors::ProgramError; -/// Table-wise recursive proofs of an `AllProof`. -pub struct RecursiveAllProof< - F: RichField + Extendable, - C: GenericConfig, - const D: usize, -> { - pub recursive_proofs: [ProofWithPublicInputs; NUM_TABLES], -} - pub(crate) struct PublicInputs> { pub(crate) trace_cap: Vec>, @@ -98,72 +88,6 @@ impl> Public } } -impl, C: GenericConfig, const D: usize> - RecursiveAllProof -{ - /// Verify every recursive proof. - pub fn verify( - self, - verifier_data: &[VerifierCircuitData; NUM_TABLES], - cross_table_lookups: Vec>, - inner_config: &StarkConfig, - ) -> Result<()> { - let pis: [_; NUM_TABLES] = core::array::from_fn(|i| { - PublicInputs::>::Permutation>::from_vec( - &self.recursive_proofs[i].public_inputs, - inner_config, - ) - }); - - let mut challenger = Challenger::::new(); - for pi in &pis { - for h in &pi.trace_cap { - challenger.observe_elements(h); - } - } - - // TODO: Observe public values if the code isn't deprecated. - - let ctl_challenges = - get_grand_product_challenge_set(&mut challenger, inner_config.num_challenges); - // Check that the correct CTL challenges are used in every proof. - for pi in &pis { - ensure!(ctl_challenges == pi.ctl_challenges); - } - - let state = challenger.compact(); - ensure!(state == pis[0].challenger_state_before); - // Check that the challenger state is consistent between proofs. - for i in 1..NUM_TABLES { - ensure!(pis[i].challenger_state_before == pis[i - 1].challenger_state_after); - } - - // Dummy values which will make the check fail. - // TODO: Fix this if the code isn't deprecated. - let mut extra_looking_products = Vec::new(); - for i in 0..NUM_TABLES { - extra_looking_products.push(Vec::new()); - for _ in 0..inner_config.num_challenges { - extra_looking_products[i].push(F::ONE); - } - } - - // Verify the CTL checks. - verify_cross_table_lookups::( - &cross_table_lookups, - pis.map(|p| p.ctl_zs_first), - extra_looking_products, - inner_config, - )?; - - // Verify the proofs. - for (proof, verifier_data) in self.recursive_proofs.into_iter().zip(verifier_data) { - verifier_data.verify(proof)?; - } - Ok(()) - } -} - /// Represents a circuit which recursively verifies a STARK proof. #[derive(Eq, PartialEq, Debug)] pub(crate) struct StarkWrapperCircuit