Skip to content

Commit

Permalink
update bytecode len gadget constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
DreamWuGit committed Aug 14, 2024
1 parent a2fd554 commit 1d67f4a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
5 changes: 5 additions & 0 deletions zkevm-circuits/src/evm_circuit/execution/codecopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ impl<F: Field> ExecutionGadget<F> for CodeCopyGadget<F> {
code_size.expr(),
code_len_gadget.code_length.expr(),
);
cb.require_equal(
"code_len_gadget and same_context have the same is_first_bytecode_table",
code_len_gadget.is_first_bytecode_table.expr(),
same_context.is_first_sub_bytecode().expr(),
);

Self {
same_context,
Expand Down
6 changes: 6 additions & 0 deletions zkevm-circuits/src/evm_circuit/execution/codesize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ impl<F: Field> ExecutionGadget<F> for CodesizeGadget<F> {
from_bytes::expr(&codesize_bytes),
code_len_gadget.code_length.expr(),
);
cb.require_equal(
"code_len_gadget and same_context have the same is_first_bytecode_table",
code_len_gadget.is_first_bytecode_table.expr(),
same_context.is_first_sub_bytecode(),
);


Self {
same_context,
Expand Down
47 changes: 21 additions & 26 deletions zkevm-circuits/src/evm_circuit/execution/error_invalid_jump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,45 +78,40 @@ impl<F: Field> ExecutionGadget<F> for ErrorInvalidJumpGadget<F> {

// If destination is in valid range, lookup for the value.
cb.condition(dest.lt_cap(), |cb| {
// TODO: refactor later.
#[cfg(not(feature = "dual_bytecode"))]
cb.bytecode_lookup(
cb.curr.state.code_hash.expr(),
dest.valid_value(),
is_code.expr(),
value.expr(),
push_rlc.expr(),
);
cb.condition(code_len_gadget.is_first_bytecode_table.expr(), |cb| {
cb.bytecode_lookup(
cb.curr.state.code_hash.expr(),
dest.valid_value(),
is_code.expr(),
value.expr(),
push_rlc.expr(),
);
});

#[cfg(feature = "dual_bytecode")]
{
cb.condition(code_len_gadget.is_first_bytecode_table.expr(), |cb| {
cb.bytecode_lookup(
cb.condition(
not::expr(code_len_gadget.is_first_bytecode_table.expr()),
|cb| {
cb.bytecode_lookup2(
cb.curr.state.code_hash.expr(),
dest.valid_value(),
is_code.expr(),
value.expr(),
push_rlc.expr(),
);
});
cb.condition(
not::expr(code_len_gadget.is_first_bytecode_table.expr()),
|cb| {
cb.bytecode_lookup2(
cb.curr.state.code_hash.expr(),
dest.valid_value(),
is_code.expr(),
value.expr(),
push_rlc.expr(),
);
},
);
}
},
);

cb.require_zero(
"is_code is false or not JUMPDEST",
is_code.expr() * is_jump_dest.expr(),
);
});
cb.require_equal(
"code_len_gadget and common_error_gadget have the same is_first_bytecode_table",
code_len_gadget.is_first_bytecode_table.expr(),
common_error_gadget.is_first_bytecode_table.expr(),
);

Self {
opcode,
Expand Down
6 changes: 6 additions & 0 deletions zkevm-circuits/src/evm_circuit/execution/extcodecopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ impl<F: Field> ExecutionGadget<F> for ExtcodecopyGadget<F> {
..Default::default()
};
let same_context = SameContextGadget::construct(cb, opcode, step_state_transition);
cb.require_equal(
"code_len_gadget and same_context have the same is_first_bytecode_table",
code_len_gadget.is_first_bytecode_table.expr(),
same_context.is_first_sub_bytecode(),
);


Self {
same_context,
Expand Down
5 changes: 3 additions & 2 deletions zkevm-circuits/src/evm_circuit/util/common_gadget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl<F: Field> SameContextGadget<F> {
}

// Check if current bytecode is belong to first bytecode table.
// Note: always return true when feature 'dual_bytecode' is disabled.
pub(crate) fn is_first_sub_bytecode(&self) -> Expression<F> {
self.is_first_bytecode_table.expr()
}
Expand Down Expand Up @@ -174,7 +175,7 @@ impl<F: Field> BytecodeLengthGadget<F> {
block
.bytecodes
.get(code_hash)
.expect("could not find external bytecode")
.expect("could not find bytecode")
.bytes
.len() as u64
};
Expand Down Expand Up @@ -1572,7 +1573,7 @@ pub(crate) struct CommonErrorGadget<F> {
restore_context: RestoreContextGadget<F>,
// indicates current op code belongs to first or second bytecode table.
// should be bool type.
is_first_bytecode_table: Cell<F>,
pub(crate) is_first_bytecode_table: Cell<F>,
}

impl<F: Field> CommonErrorGadget<F> {
Expand Down

0 comments on commit 1d67f4a

Please sign in to comment.