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

Commit

Permalink
EVM/stop: supporting incomplete bytecodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphael Toledo committed Jul 20, 2023
1 parent 7c98b14 commit 6117f85
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions zkevm-circuits/src/evm_circuit/execution/stop.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use crate::{
evm_circuit::{
execution::ExecutionGadget,
param::N_BYTES_PROGRAM_COUNTER,
step::ExecutionState,
util::{
common_gadget::RestoreContextGadget,
constraint_builder::{
ConstrainBuilderCommon, EVMConstraintBuilder, StepStateTransition,
Transition::{Delta, Same},
},
math_gadget::IsZeroGadget,
math_gadget::ComparisonGadget,
CachedRegion, Cell,
},
witness::{Block, Call, ExecStep, Transaction},
Expand All @@ -26,7 +27,7 @@ use halo2_proofs::{circuit::Value, plonk::Error};
#[derive(Clone, Debug)]
pub(crate) struct StopGadget<F> {
code_length: Cell<F>,
is_out_of_range: IsZeroGadget<F>,
out_of_range: ComparisonGadget<F, N_BYTES_PROGRAM_COUNTER>,
opcode: Cell<F>,
restore_context: RestoreContextGadget<F>,
}
Expand All @@ -39,12 +40,17 @@ impl<F: Field> ExecutionGadget<F> for StopGadget<F> {
fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
let code_length = cb.query_cell();
cb.bytecode_length(cb.curr.state.code_hash.to_word(), code_length.expr());
let is_out_of_range = IsZeroGadget::construct(

let out_of_range = ComparisonGadget::construct(
cb,
code_length.expr() - cb.curr.state.program_counter.expr(),
code_length.expr(),
cb.curr.state.program_counter.expr(),
);
let (lt, eq) = out_of_range.expr();
let is_out_of_range = lt + eq;

let opcode = cb.query_cell();
cb.condition(1.expr() - is_out_of_range.expr(), |cb| {
cb.condition(1.expr() - is_out_of_range, |cb| {
cb.opcode_lookup(opcode.expr(), 1.expr());
});

Expand Down Expand Up @@ -91,7 +97,7 @@ impl<F: Field> ExecutionGadget<F> for StopGadget<F> {

Self {
code_length,
is_out_of_range,
out_of_range,
opcode,
restore_context,
}
Expand All @@ -110,17 +116,13 @@ impl<F: Field> ExecutionGadget<F> for StopGadget<F> {
.bytecodes
.get(&call.code_hash.to_word())
.expect("could not find current environment's bytecode");
self.code_length.assign(
region,
offset,
Value::known(F::from(code.bytes.len() as u64)),
)?;

self.is_out_of_range.assign(
region,
offset,
F::from(code.bytes.len() as u64) - F::from(step.pc),
)?;

let code_length = code.bytes.len() as u64;
self.code_length
.assign(region, offset, Value::known(F::from(code_length)))?;

self.out_of_range
.assign(region, offset, F::from(code_length), F::from(step.pc))?;

let opcode = step.opcode().unwrap();
self.opcode
Expand Down

0 comments on commit 6117f85

Please sign in to comment.