Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Create State and Transition traits #1

Merged
merged 10 commits into from
Feb 27, 2024
29 changes: 17 additions & 12 deletions evm_arithmetization/src/cpu/kernel/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,6 @@ impl<F: Field> Interpreter<F> {
for i in range {
let term = self.generation_state.memory.get(
MemoryAddress::new(0, segment, i),
true,
&self.preinitialized_segments,
);
output.push(term);
Expand Down Expand Up @@ -683,7 +682,6 @@ impl<F: Field> Interpreter<F> {
segment: Segment::JumpdestBits.unscale(),
virt: offset,
},
false,
&HashMap::default(),
)
} else {
Expand Down Expand Up @@ -765,35 +763,31 @@ impl<F: Field> State<F> for Interpreter<F> {
}

fn incr_gas(&mut self, n: u64) {
self.generation_state.registers.gas_used += n;
self.generation_state.incr_gas(n);
}

fn incr_pc(&mut self, n: usize) {
self.generation_state.registers.program_counter += n;
self.generation_state.incr_pc(n);
}

fn get_registers(&self) -> RegistersState {
self.generation_state.registers
self.generation_state.get_registers()
}

fn get_mut_registers(&mut self) -> &mut RegistersState {
&mut self.generation_state.registers
self.generation_state.get_mut_registers()
}

fn get_from_memory(&mut self, address: MemoryAddress) -> U256 {
self.generation_state
.memory
.get(address, true, &self.preinitialized_segments)
.get(address, &self.preinitialized_segments)
}

fn get_mut_generation_state(&mut self) -> &mut GenerationState<F> {
&mut self.generation_state
}

fn is_generation_state(&mut self) -> bool {
false
}

fn incr_interpreter_clock(&mut self) {
self.clock += 1
}
Expand Down Expand Up @@ -866,12 +860,23 @@ impl<F: Field> State<F> for Interpreter<F> {

self.perform_state_op(opcode, op, row)
}

fn clear_traces(&mut self) {
let generation_state = self.get_mut_generation_state();
generation_state.traces.arithmetic_ops = vec![];
generation_state.traces.arithmetic_ops = vec![];
generation_state.traces.byte_packing_ops = vec![];
generation_state.traces.cpu = vec![];
generation_state.traces.logic_ops = vec![];
generation_state.traces.keccak_inputs = vec![];
generation_state.traces.keccak_sponge_ops = vec![];
}
}

impl<F: Field> Transition<F> for Interpreter<F> {
fn generate_jumpdest_analysis(&mut self, dst: usize) -> bool {
if self.is_jumpdest_analysis && !self.generation_state.registers.is_kernel {
self.add_jumpdest_offset(dst as usize);
self.add_jumpdest_offset(dst);
true
} else {
false
Expand Down
11 changes: 0 additions & 11 deletions evm_arithmetization/src/cpu/kernel/tests/core/access_lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ fn test_init_access_lists() -> Result<()> {
.map(|i| {
interpreter.generation_state.memory.get(
MemoryAddress::new(0, Segment::AccessedAddresses, i),
false,
&HashMap::default(),
)
})
Expand All @@ -44,7 +43,6 @@ fn test_init_access_lists() -> Result<()> {
.map(|i| {
interpreter.generation_state.memory.get(
MemoryAddress::new(0, Segment::AccessedStorageKeys, i),
false,
&HashMap::default(),
)
})
Expand Down Expand Up @@ -115,7 +113,6 @@ fn test_insert_address() -> Result<()> {
assert_eq!(
interpreter.generation_state.memory.get(
MemoryAddress::new_bundle(U256::from(AccessedAddressesLen as usize)).unwrap(),
false,
&HashMap::default(),
),
U256::from(Segment::AccessedAddresses as usize + 4)
Expand Down Expand Up @@ -170,7 +167,6 @@ fn test_insert_accessed_addresses() -> Result<()> {
assert_eq!(
interpreter.generation_state.memory.get(
MemoryAddress::new_bundle(U256::from(AccessedAddressesLen as usize)).unwrap(),
false,
&HashMap::default(),
),
U256::from(offset + 2 * (n + 1))
Expand All @@ -187,15 +183,13 @@ fn test_insert_accessed_addresses() -> Result<()> {
assert_eq!(
interpreter.generation_state.memory.get(
MemoryAddress::new_bundle(U256::from(AccessedAddressesLen as usize)).unwrap(),
false,
&HashMap::default(),
),
U256::from(offset + 2 * (n + 2))
);
assert_eq!(
interpreter.generation_state.memory.get(
MemoryAddress::new(0, AccessedAddresses, 2 * (n + 1)),
false,
&HashMap::default(),
),
U256::from(addr_not_in_list.0.as_slice())
Expand Down Expand Up @@ -259,7 +253,6 @@ fn test_insert_accessed_storage_keys() -> Result<()> {
assert_eq!(
interpreter.generation_state.memory.get(
MemoryAddress::new_bundle(U256::from(AccessedStorageKeysLen as usize)).unwrap(),
false,
&HashMap::default(),
),
U256::from(offset + 4 * (n + 1))
Expand All @@ -281,31 +274,27 @@ fn test_insert_accessed_storage_keys() -> Result<()> {
assert_eq!(
interpreter.generation_state.memory.get(
MemoryAddress::new_bundle(U256::from(AccessedStorageKeysLen as usize)).unwrap(),
false,
&HashMap::default()
),
U256::from(offset + 4 * (n + 2))
);
assert_eq!(
interpreter.generation_state.memory.get(
MemoryAddress::new(0, AccessedStorageKeys, 4 * (n + 1)),
false,
&HashMap::default(),
),
U256::from(storage_key_not_in_list.0 .0.as_slice())
);
assert_eq!(
interpreter.generation_state.memory.get(
MemoryAddress::new(0, AccessedStorageKeys, 4 * (n + 1) + 1),
false,
&HashMap::default()
),
storage_key_not_in_list.1
);
assert_eq!(
interpreter.generation_state.memory.get(
MemoryAddress::new(0, AccessedStorageKeys, 4 * (n + 1) + 2),
false,
&HashMap::default()
),
storage_key_not_in_list.2
Expand Down
1 change: 0 additions & 1 deletion evm_arithmetization/src/cpu/kernel/tests/ecc/curve_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ mod bn {
segment: Segment::BnTableQ.unscale(),
virt: i,
},
false,
&HashMap::default(),
));
}
Expand Down
4 changes: 0 additions & 4 deletions evm_arithmetization/src/generation/prover_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,6 @@ impl<F: Field> GenerationState<F> {
.map(|i| {
u256_to_u8(self.memory.get(
MemoryAddress::new(context, Segment::Code, i),
false,
&HashMap::default(),
))
})
Expand All @@ -438,7 +437,6 @@ impl<F: Field> GenerationState<F> {
Segment::ContextMetadata,
ContextMetadata::CodeSize.unscale(),
),
false,
&HashMap::default(),
))?;
Ok(code_len)
Expand Down Expand Up @@ -477,7 +475,6 @@ impl<F: Field> GenerationState<F> {
fn get_global_metadata(&self, data: GlobalMetadata) -> U256 {
self.memory.get(
MemoryAddress::new(0, Segment::GlobalMetadata, data.unscale()),
false,
&HashMap::default(),
)
}
Expand All @@ -493,7 +490,6 @@ impl<F: Field> GenerationState<F> {
Segment::GlobalMetadata,
GlobalMetadata::AccessedStorageKeysLen.unscale(),
),
false,
&HashMap::default(),
) - Segment::AccessedStorageKeys as usize,
)?;
Expand Down
46 changes: 13 additions & 33 deletions evm_arithmetization/src/generation/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ pub(crate) trait State<F: Field> {
/// Returns a mutable `GenerationState` from a `State`.
fn get_mut_generation_state(&mut self) -> &mut GenerationState<F>;

/// Returns true if a `State` is a `GenerationState` and false otherwise.
fn is_generation_state(&mut self) -> bool;
// /// Returns true if a `State` is a `GenerationState` and false otherwise.
// fn is_generation_state(&mut self) -> bool;
LindaGuiga marked this conversation as resolved.
Show resolved Hide resolved

/// Increments the clock of an `Interpreter`'s clock.
fn incr_interpreter_clock(&mut self);
Expand Down Expand Up @@ -129,20 +129,12 @@ pub(crate) trait State<F: Field> {
_ => bail!("TODO: figure out what to do with this..."),
};

let (checkpoint, is_generation) = (self.checkpoint(), self.is_generation_state());
let checkpoint = self.checkpoint();

let (row, _) = self.base_row();
generate_exception(
exc_code,
self.get_mut_generation_state(),
row,
is_generation,
);
generate_exception(exc_code, self.get_mut_generation_state(), row);

// We only clear traces for the interpreter
if !self.is_generation_state() {
self.clear_traces()
}
self.clear_traces();

self.apply_ops(checkpoint);

Expand Down Expand Up @@ -181,16 +173,7 @@ pub(crate) trait State<F: Field> {

/// Clears all traces from `GenerationState` except for
/// memory_ops, which are necessary to apply operations.
fn clear_traces(&mut self) {
let generation_state = self.get_mut_generation_state();
generation_state.traces.arithmetic_ops = vec![];
generation_state.traces.arithmetic_ops = vec![];
generation_state.traces.byte_packing_ops = vec![];
generation_state.traces.cpu = vec![];
generation_state.traces.logic_ops = vec![];
generation_state.traces.keccak_inputs = vec![];
generation_state.traces.keccak_sponge_ops = vec![];
}
fn clear_traces(&mut self) {}

fn try_perform_instruction(&mut self) -> Result<Operation, ProgramError>;

Expand Down Expand Up @@ -335,11 +318,8 @@ impl<F: Field> GenerationState<F> {
let returndata_offset = ContextMetadata::ReturndataSize.unscale();
let returndata_size_addr =
MemoryAddress::new(ctx, Segment::ContextMetadata, returndata_offset);
let returndata_size = u256_to_usize(self.memory.get(
returndata_size_addr,
false,
&HashMap::default(),
))?;
let returndata_size =
u256_to_usize(self.memory.get(returndata_size_addr, &HashMap::default()))?;
let code = self.memory.contexts[ctx].segments[Segment::Returndata.unscale()].content
[..returndata_size]
.iter()
Expand Down Expand Up @@ -415,18 +395,18 @@ impl<F: Field> State<F> for GenerationState<F> {

/// Returns the value stored at address `address` in a `State`.
fn get_from_memory(&mut self, address: MemoryAddress) -> U256 {
self.memory.get(address, false, &HashMap::default())
self.memory.get(address, &HashMap::default())
}

/// Returns a mutable `GenerationState` from a `State`.
fn get_mut_generation_state(&mut self) -> &mut GenerationState<F> {
self
}

/// Returns true if a `State` is a `GenerationState` and false otherwise.
fn is_generation_state(&mut self) -> bool {
true
}
// /// Returns true if a `State` is a `GenerationState` and false otherwise.
// fn is_generation_state(&mut self) -> bool {
// true
// }
LindaGuiga marked this conversation as resolved.
Show resolved Hide resolved

/// Increments the clock of an `Interpreter`'s clock.
fn incr_interpreter_clock(&mut self) {}
Expand Down
2 changes: 0 additions & 2 deletions evm_arithmetization/src/witness/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ impl MemoryState {
pub(crate) fn get(
&self,
address: MemoryAddress,
is_interpreter: bool,
preinitialized_segments: &HashMap<Segment, MemorySegmentState, RandomState>,
) -> U256 {
match self.get_option(address) {
Expand Down Expand Up @@ -271,7 +270,6 @@ impl MemoryState {
pub(crate) fn read_global_metadata(&self, field: GlobalMetadata) -> U256 {
self.get(
MemoryAddress::new_bundle(U256::from(field as usize)).unwrap(),
false,
&HashMap::default(),
)
}
Expand Down
Loading