Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

continuations: initial cleanup pass #181

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions evm_arithmetization/src/all_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ use crate::keccak::keccak_stark::KeccakStark;
use crate::keccak_sponge::columns::KECCAK_RATE_BYTES;
use crate::keccak_sponge::keccak_sponge_stark;
use crate::keccak_sponge::keccak_sponge_stark::KeccakSpongeStark;
use crate::logic;
use crate::logic::LogicStark;
use crate::memory::memory_stark::MemoryStark;
use crate::memory::memory_stark::{self, ctl_context_pruning_looking};
use crate::memory_continuation::memory_continuation_stark::{self, MemoryContinuationStark};
use crate::{logic, memory_continuation};

/// Structure containing all STARKs and the cross-table lookups.
#[derive(Clone)]
Expand Down Expand Up @@ -346,7 +346,7 @@ fn ctl_mem_before<F: Field>() -> CrossTableLookup<F> {
memory_stark::ctl_looking_mem(),
memory_stark::ctl_filter_mem_before(),
);
let mut all_lookers = vec![memory_looking];
let all_lookers = vec![memory_looking];
let mem_before_looked = TableWithColumns::new(
*Table::MemBefore,
memory_continuation_stark::ctl_data(),
Expand All @@ -363,7 +363,7 @@ fn ctl_mem_after<F: Field>() -> CrossTableLookup<F> {
memory_stark::ctl_looking_mem(),
memory_stark::ctl_filter_mem_after(),
);
let mut all_lookers = vec![memory_looking];
let all_lookers = vec![memory_looking];
let mem_after_looked = TableWithColumns::new(
*Table::MemAfter,
memory_continuation_stark::ctl_data(),
Expand Down
22 changes: 3 additions & 19 deletions evm_arithmetization/src/cpu/kernel/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::witness::errors::ProgramError;
use crate::witness::memory::{
MemoryAddress, MemoryContextState, MemoryOp, MemoryOpKind, MemorySegmentState, MemoryState,
};
use crate::witness::operation::{Operation, CONTEXT_SCALING_FACTOR};
use crate::witness::operation::Operation;
use crate::witness::state::RegistersState;
use crate::witness::transition::{
decode, fill_op_flag, get_op_special_length, log_kernel_instruction, Transition,
Expand Down Expand Up @@ -924,20 +924,6 @@ impl<F: Field> State<F> for Interpreter<F> {
}
}

/// Inserts a preinitialized segment, given as a [Segment],
/// into the `preinitialized_segments` memory field.
fn insert_preinitialized_segment(&mut self, segment: Segment, values: MemorySegmentState) {
self.generation_state
.memory
.insert_preinitialized_segment(segment, values);
}

fn is_preinitialized_segment(&self, segment: usize) -> bool {
self.generation_state
.memory
.is_preinitialized_segment(segment)
}

fn incr_gas(&mut self, n: u64) {
self.generation_state.incr_gas(n);
}
Expand Down Expand Up @@ -1067,9 +1053,7 @@ impl<F: Field> State<F> for Interpreter<F> {
// Might write in general CPU columns when it shouldn't, but the correct values
// will overwrite these ones during the op generation.
if let Some(special_len) = get_op_special_length(op) {
let special_len_f = F::from_canonical_usize(special_len);
let diff = row.stack_len - special_len_f;
if (generation_state.stack().len() != special_len) {
if generation_state.stack().len() != special_len {
// If the `State` is an interpreter, we cannot rely on the row to carry out the
// check.
generation_state.registers.is_stack_top_read = true;
Expand All @@ -1086,7 +1070,7 @@ impl<F: Field> State<F> for Interpreter<F> {
log::debug!("{}", msg);
}

fn log_info(&self, msg: String) {}
fn log_info(&self, _msg: String) {}

fn log_log(&self, level: Level, msg: String) {
log::log!(level, "{}", msg);
Expand Down
5 changes: 1 addition & 4 deletions evm_arithmetization/src/cpu/kernel/tests/add11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ use crate::cpu::kernel::aggregator::KERNEL;
use crate::cpu::kernel::interpreter::Interpreter;
use crate::generation::mpt::{AccountRlp, LegacyReceiptRlp};
use crate::generation::TrieInputs;
use crate::memory::segments::Segment;
use crate::proof::{BlockHashes, BlockMetadata, MemCap, TrieRoots};
use crate::util::h2u;
use crate::witness::state::RegistersState;
use crate::proof::{BlockHashes, BlockMetadata, TrieRoots};
use crate::GenerationInputs;

#[test]
Expand Down
11 changes: 4 additions & 7 deletions evm_arithmetization/src/fixed_recursive_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ use plonky2::fri::oracle::PolynomialBatch;
use plonky2::fri::FriParams;
use plonky2::gates::constant::ConstantGate;
use plonky2::gates::noop::NoopGate;
use plonky2::hash::hash_types::{HashOutTarget, MerkleCapTarget, RichField};
use plonky2::hash::merkle_tree::MerkleCap;
use plonky2::hash::hash_types::{MerkleCapTarget, RichField};
use plonky2::iop::challenger::RecursiveChallenger;
use plonky2::iop::target::{BoolTarget, Target};
use plonky2::iop::witness::{PartialWitness, WitnessWrite};
Expand All @@ -42,12 +41,11 @@ use starky::stark::Stark;

use crate::all_stark::{all_cross_table_lookups, AllStark, Table, NUM_TABLES};
use crate::cpu::kernel::aggregator::KERNEL;
use crate::generation::state::GenerationState;
use crate::generation::GenerationInputs;
use crate::get_challenges::observe_public_values_target;
use crate::memory::segments::Segment;
use crate::proof::{
AllProof, BlockHashesTarget, BlockMetadataTarget, ExtraBlockData, ExtraBlockDataTarget, MemCap,
AllProof, BlockHashesTarget, BlockMetadataTarget, ExtraBlockData, ExtraBlockDataTarget,
MemCapTarget, PublicValues, PublicValuesTarget, RegistersDataTarget, TrieRoots,
TrieRootsTarget,
};
Expand All @@ -57,8 +55,7 @@ use crate::recursive_verifier::{
recursive_stark_circuit, set_public_value_targets, PlonkWrapperCircuit, PublicInputs,
StarkWrapperCircuit,
};
use crate::util::{h160_limbs, h256_limbs, u256_limbs};
use crate::witness::memory::MemoryAddress;
use crate::util::{h256_limbs, u256_limbs};

/// The recursion threshold. We end a chain of recursive proofs once we reach
/// this size.
Expand Down Expand Up @@ -945,7 +942,7 @@ where
// Check the initial and final register values.
Self::connect_initial_final_segment(&mut builder, &public_values);

// Check the initial `MemBefore` `MerklCap` value.
// Check the initial `MemBefore` `MerkleCap` value.
Self::check_init_merkle_cap(&mut builder, &agg_pv, stark_config);

let cyclic_vk = builder.add_verifier_data_public_inputs();
Expand Down
28 changes: 7 additions & 21 deletions evm_arithmetization/src/generation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,24 @@ use GlobalMetadata::{
use crate::all_stark::{AllStark, NUM_TABLES};
use crate::cpu::columns::CpuColumnsView;
use crate::cpu::kernel::aggregator::KERNEL;
use crate::cpu::kernel::assembler::Kernel;
use crate::cpu::kernel::constants::context_metadata::ContextMetadata;
use crate::cpu::kernel::constants::global_metadata::GlobalMetadata;
use crate::generation::state::{GenerationState, State};
use crate::generation::trie_extractor::{get_receipt_trie, get_state_trie, get_txn_trie};
use crate::memory::segments::Segment;
use crate::proof::{
BlockHashes, BlockMetadata, ExtraBlockData, MemCap, PublicValues, RegistersData, TrieRoots,
};
use crate::prover::{check_abort_signal, get_mem_after_value_from_row};
use crate::util::{h2u, u256_to_usize};
use crate::witness::errors::{ProgramError, ProverInputError};
use crate::witness::memory::{MemoryAddress, MemoryChannel, MemoryState};
use crate::witness::state::RegistersState;
use crate::witness::traces::Traces;

pub mod mpt;
pub(crate) mod prover_input;
pub(crate) mod rlp;
pub(crate) mod state;
mod trie_extractor;

use self::mpt::{load_all_mpts, TrieRootPtrs};
use crate::witness::util::{mem_write_log, mem_write_log_timestamp_zero};
use crate::witness::util::mem_write_log;

/// Number of cycles to go after having reached the halting state. It is
/// equal to the number of cycles in `exc_stop` + 1.
Expand Down Expand Up @@ -335,11 +329,7 @@ pub(crate) fn debug_inputs(inputs: &GenerationInputs) {
log::debug!("Input contract_code: {:?}", &inputs.contract_code);
}

type TablesWithPVsAndFinalMem<F> = (
[Vec<PolynomialValues<F>>; NUM_TABLES],
PublicValues,
Vec<Vec<F>>,
);
type TablesWithPVsAndFinalMem<F> = ([Vec<PolynomialValues<F>>; NUM_TABLES], PublicValues);
pub fn generate_traces<F: RichField + Extendable<D>, const D: usize>(
all_stark: &AllStark<F, D>,
inputs: GenerationInputs,
Expand Down Expand Up @@ -371,16 +361,12 @@ pub fn generate_traces<F: RichField + Extendable<D>, const D: usize>(
"simulate CPU",
simulate_cpu(&mut state, max_cpu_len_log)
);
let (final_registers, mem_after) = if let Ok(res) = cpu_res {
res
} else {
output_debug_tries(&state);

if cpu_res.is_err() {
output_debug_tries(&state)?;
cpu_res?;
(RegistersState::default(), None)
};

let mut trace_lengths = state.traces.get_lengths();
let trace_lengths = state.traces.get_lengths();

let read_metadata = |field| state.memory.read_global_metadata(field);
let trie_roots_before = TrieRoots {
Expand Down Expand Up @@ -420,7 +406,7 @@ pub fn generate_traces<F: RichField + Extendable<D>, const D: usize>(
mem_after: MemCap::default(),
};

let (tables, final_values) = timed!(
let tables = timed!(
timing,
"convert trace data to tables",
state.traces.into_tables(
Expand All @@ -432,7 +418,7 @@ pub fn generate_traces<F: RichField + Extendable<D>, const D: usize>(
timing
)
);
Ok((tables, public_values, final_values))
Ok((tables, public_values))
}

fn simulate_cpu<F: Field>(
Expand Down
2 changes: 1 addition & 1 deletion evm_arithmetization/src/generation/mpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::ops::Deref;
use std::collections::HashMap;

use bytes::Bytes;
use ethereum_types::{Address, BigEndianHash, H256, U256, U512};
use ethereum_types::{Address, BigEndianHash, H256, U256};
use keccak_hash::keccak;
use mpt_trie::nibbles::{Nibbles, NibblesIntern};
use mpt_trie::partial_trie::{HashedPartialTrie, PartialTrie};
Expand Down
37 changes: 11 additions & 26 deletions evm_arithmetization/src/generation/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use crate::memory::segments::Segment;
use crate::util::u256_to_usize;
use crate::witness::errors::ProgramError;
use crate::witness::memory::MemoryChannel::GeneralPurpose;
use crate::witness::memory::MemoryOpKind;
use crate::witness::memory::{MemoryAddress, MemoryContextState, MemoryOp, MemoryState};
use crate::witness::memory::{MemoryOpKind, MemorySegmentState};
use crate::witness::operation::{generate_exception, Operation};
use crate::witness::state::RegistersState;
use crate::witness::traces::{TraceCheckpoint, Traces};
Expand Down Expand Up @@ -85,8 +85,11 @@ pub(crate) trait State<F: Field> {

/// Checks whether we have reached the `halt` label in kernel mode.
fn at_halt(&self) -> bool {
let halt = KERNEL.global_labels["halt"];
let halt_final = KERNEL.global_labels["halt_final"];
let registers = self.get_registers();
registers.is_kernel && registers.program_counter == KERNEL.global_labels["halt"]
registers.is_kernel
&& (registers.program_counter == halt || registers.program_counter == halt_final)
}

/// Returns the context in which the jumpdest analysis should end.
Expand Down Expand Up @@ -153,13 +156,7 @@ pub(crate) trait State<F: Field> {
/// Return the offsets at which execution must halt
fn get_halt_offsets(&self) -> Vec<usize>;

/// Inserts a preinitialized segment, given as a [Segment],
/// into the `preinitialized_segments` memory field.
fn insert_preinitialized_segment(&mut self, segment: Segment, values: MemorySegmentState);

fn is_preinitialized_segment(&self, segment: usize) -> bool;

fn update_interpreter_final_registers(&mut self, final_registers: RegistersState) {}
fn update_interpreter_final_registers(&mut self, _final_registers: RegistersState) {}

fn get_full_memory(&self) -> Option<MemoryState> {
None
Expand All @@ -176,10 +173,8 @@ pub(crate) trait State<F: Field> {
{
let halt_offsets = self.get_halt_offsets();

let halt_pc = KERNEL.global_labels["halt"];
let halt_final_pc = KERNEL.global_labels["halt_final"];
let mut final_registers = RegistersState::default();
let mut final_mem = self.get_mut_generation_state().memory.clone();
let final_mem = self.get_generation_state().memory.clone();
let mut running = true;
let mut final_clock = 0;
loop {
Expand Down Expand Up @@ -316,15 +311,15 @@ pub(crate) trait State<F: Field> {
}

/// Logs `msg` in `debug` mode, in the interpreter.
fn log_debug(&self, msg: String) {}
fn log_debug(&self, _msg: String) {}

/// Logs `msg` in `info` mode, in the interpreter.
fn log_info(&self, msg: String) {
log::info!("{}", msg);
}

/// Logs `msg` at `level`, during witness generation.
fn log_log(&self, level: Level, msg: String) {}
fn log_log(&self, _level: Level, _msg: String) {}
}

#[derive(Debug)]
Expand Down Expand Up @@ -499,16 +494,6 @@ impl<F: Field> State<F> for GenerationState<F> {
}
}

fn insert_preinitialized_segment(&mut self, segment: Segment, values: MemorySegmentState) {
panic!(
"A `GenerationState` cannot have a nonempty `preinitialized_segment` field in memory."
)
}

fn is_preinitialized_segment(&self, segment: usize) -> bool {
false
}

fn incr_gas(&mut self, n: u64) {
self.registers.gas_used += n;
}
Expand Down Expand Up @@ -587,7 +572,7 @@ impl<F: Field> State<F> for GenerationState<F> {
row.general.stack_mut().stack_inv = inv;
row.general.stack_mut().stack_inv_aux = F::ONE;
self.registers.is_stack_top_read = true;
} else if (self.stack().len() != special_len) {
} else if self.stack().len() != special_len {
// If the `State` is an interpreter, we cannot rely on the row to carry out the
// check.
self.registers.is_stack_top_read = true;
Expand All @@ -606,7 +591,7 @@ impl<F: Field> Transition<F> for GenerationState<F> {
Ok(op)
}

fn generate_jumpdest_analysis(&mut self, dst: usize) -> bool {
fn generate_jumpdest_analysis(&mut self, _dst: usize) -> bool {
false
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::borrow::Borrow;
use core::iter::{self, once, repeat};
use core::iter::{self, repeat};
use core::marker::PhantomData;
use core::mem::size_of;

Expand Down
4 changes: 2 additions & 2 deletions evm_arithmetization/src/memory/memory_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl<F: RichField + Extendable<D>, const D: usize> MemoryStark<F, D> {
/// a trace in column-major form.
/// Also generates the `STALE_CONTEXTS`, `STALE_CONTEXTS_FREQUENCIES` and
/// `MEM_AFTER_FILTER` columns.
fn generate_trace_col_major(trace_col_vecs: &mut [Vec<F>], stale_contexts: Vec<usize>) {
fn generate_trace_col_major(trace_col_vecs: &mut [Vec<F>]) {
let height = trace_col_vecs[0].len();
trace_col_vecs[COUNTER] = (0..height).map(|i| F::from_canonical_usize(i)).collect();

Expand Down Expand Up @@ -397,7 +397,7 @@ impl<F: RichField + Extendable<D>, const D: usize> MemoryStark<F, D> {
let mut trace_col_vecs = transpose(&trace_row_vecs);

// A few final generation steps, which work better in column-major form.
Self::generate_trace_col_major(&mut trace_col_vecs, stale_contexts);
Self::generate_trace_col_major(&mut trace_col_vecs);

let final_rows = transpose(&trace_col_vecs);

Expand Down
Loading