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

Commit

Permalink
Remove code_hash(rlc) and rename code_hash_word to code_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
leolara committed Jun 17, 2023
1 parent 84c48c7 commit 66c3d9c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 68 deletions.
94 changes: 34 additions & 60 deletions zkevm-circuits/src/bytecode_circuit/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
evm_circuit::util::{
and,
constraint_builder::{BaseConstraintBuilder, ConstrainBuilderCommon},
not, or, rlc, select,
not, or, select,
},
table::{BytecodeFieldTag, BytecodeTable, KeccakTable, LookupTable},
util::{
Expand All @@ -13,7 +13,7 @@ use crate::{
witness,
};
use bus_mapping::state_db::EMPTY_CODE_HASH_LE;
use eth_types::{Field, ToLittleEndian};
use eth_types::Field;
use gadgets::is_zero::{IsZeroChip, IsZeroConfig, IsZeroInstruction};
use halo2_proofs::{
circuit::{Layouter, Region, Value},
Expand Down Expand Up @@ -243,29 +243,17 @@ impl<F: Field> SubCircuitConfig<F> for BytecodeCircuitConfig<F> {
meta.query_advice(length, Rotation::cur()),
);

let empty_hash = rlc::expr(
&EMPTY_CODE_HASH_LE.map(|v| Expression::Constant(F::from(v as u64))),
challenges.evm_word(),
);

let empty_hash_word: Word<Expression<F>> =
Word32::new(EMPTY_CODE_HASH_LE.map(|v| Expression::Constant(F::from(v as u64))))
.to_word();

cb.require_equal(
"assert cur.hash == EMPTY_HASH",
meta.query_advice(bytecode_table.code_hash, Rotation::cur()),
empty_hash,
);
cb.require_equal(
cb.require_equal_word(
"assert cur.hash == EMPTY_HASH",
meta.query_advice(bytecode_table.code_hash_word.lo(), Rotation::cur()),
empty_hash_word.lo(),
);
cb.require_equal(
"assert cur.hash == EMPTY_HASH",
meta.query_advice(bytecode_table.code_hash_word.hi(), Rotation::cur()),
empty_hash_word.hi(),
Word::new([
meta.query_advice(bytecode_table.code_hash.lo(), Rotation::cur()),
meta.query_advice(bytecode_table.code_hash.hi(), Rotation::cur()),
]),
empty_hash_word,
);

cb.gate(and::expr(vec![
Expand Down Expand Up @@ -303,10 +291,16 @@ impl<F: Field> SubCircuitConfig<F> for BytecodeCircuitConfig<F> {
1.expr(),
);

cb.require_equal(
cb.require_equal_word(
"next.hash == cur.hash",
meta.query_advice(bytecode_table.code_hash, Rotation::next()),
meta.query_advice(bytecode_table.code_hash, Rotation::cur()),
Word::new([
meta.query_advice(bytecode_table.code_hash.lo(), Rotation::next()),
meta.query_advice(bytecode_table.code_hash.hi(), Rotation::next()),
]),
Word::new([
meta.query_advice(bytecode_table.code_hash.lo(), Rotation::cur()),
meta.query_advice(bytecode_table.code_hash.hi(), Rotation::cur()),
]),
);

cb.require_equal(
Expand Down Expand Up @@ -346,10 +340,16 @@ impl<F: Field> SubCircuitConfig<F> for BytecodeCircuitConfig<F> {
meta.query_advice(bytecode_table.index, Rotation::cur()) + 1.expr(),
);

cb.require_equal(
cb.require_equal_word(
"next.hash == cur.hash",
meta.query_advice(bytecode_table.code_hash, Rotation::next()),
meta.query_advice(bytecode_table.code_hash, Rotation::cur()),
Word::new([
meta.query_advice(bytecode_table.code_hash.lo(), Rotation::next()),
meta.query_advice(bytecode_table.code_hash.hi(), Rotation::next()),
]),
Word::new([
meta.query_advice(bytecode_table.code_hash.lo(), Rotation::cur()),
meta.query_advice(bytecode_table.code_hash.hi(), Rotation::cur()),
]),
);

cb.require_equal(
Expand Down Expand Up @@ -426,7 +426,7 @@ impl<F: Field> SubCircuitConfig<F> for BytecodeCircuitConfig<F> {
)];

for (circuit_column, table_column) in
keccak_table.match_columns(value_rlc, length, bytecode_table.code_hash_word)
keccak_table.match_columns(value_rlc, length, bytecode_table.code_hash)
{
constraints.push((
enable.clone() * meta.query_advice(circuit_column, Rotation::cur()),
Expand Down Expand Up @@ -495,10 +495,6 @@ impl<F: Field> BytecodeCircuitConfig<F> {
last_row_offset
);

let empty_hash = challenges
.evm_word()
.map(|challenge| rlc::value(EMPTY_CODE_HASH_LE.as_ref(), challenge));

layouter.assign_region(
|| "assign bytecode",
|mut region| {
Expand All @@ -513,7 +509,6 @@ impl<F: Field> BytecodeCircuitConfig<F> {
challenges,
&push_data_left_is_zero_chip,
&index_length_diff_is_zero_chip,
empty_hash,
&mut offset,
last_row_offset,
fail_fast,
Expand All @@ -526,7 +521,6 @@ impl<F: Field> BytecodeCircuitConfig<F> {
&mut region,
&push_data_left_is_zero_chip,
&index_length_diff_is_zero_chip,
empty_hash,
idx,
last_row_offset,
)?;
Expand Down Expand Up @@ -562,13 +556,7 @@ impl<F: Field> BytecodeCircuitConfig<F> {
value_rlc = challenges.keccak_input().map(|_| F::ZERO);
}

let code_hash = challenges
.evm_word()
.map(|challenge| rlc::value(&row.code_hash.to_le_bytes(), challenge));
for (name, column, value) in [
("code_hash", self.bytecode_table.code_hash, code_hash),
("value_rlc", self.value_rlc, value_rlc),
] {
for (name, column, value) in [("value_rlc", self.value_rlc, value_rlc)] {
region.assign_advice(
|| format!("assign {} {}", name, offset),
column,
Expand All @@ -590,7 +578,6 @@ impl<F: Field> BytecodeCircuitConfig<F> {
challenges: &Challenges<Value<F>>,
push_data_left_is_zero_chip: &IsZeroChip<F>,
index_length_diff_is_zero_chip: &IsZeroChip<F>,
empty_hash: Value<F>,
offset: &mut usize,
last_row_offset: usize,
fail_fast: bool,
Expand All @@ -602,12 +589,7 @@ impl<F: Field> BytecodeCircuitConfig<F> {
let mut value_rlc = challenges.keccak_input().map(|_| F::ZERO);
let length = F::from(bytecode.bytes.len() as u64);

// Code hash with challenge is calculated only using the first row of the
// bytecode (header row), the rest of the code_hash in other rows are ignored.
let code_hash = challenges
.evm_word()
.map(|challenge| rlc::value(&bytecode.rows[0].code_hash.to_le_bytes(), challenge));
let code_hash_word = Word::from(bytecode.rows[0].code_hash).into_value();
let code_hash = Word::from(bytecode.rows[0].code_hash).into_value();

for (idx, row) in bytecode.rows.iter().enumerate() {
if fail_fast && *offset > last_row_offset {
Expand Down Expand Up @@ -648,7 +630,6 @@ impl<F: Field> BytecodeCircuitConfig<F> {
true,
*offset == last_row_offset,
code_hash,
code_hash_word,
row.tag,
row.index,
row.is_code,
Expand Down Expand Up @@ -682,7 +663,6 @@ impl<F: Field> BytecodeCircuitConfig<F> {
region,
push_data_left_is_zero_chip,
index_length_diff_is_zero_chip,
empty_hash,
*offset,
last_row_offset,
)?;
Expand All @@ -697,7 +677,6 @@ impl<F: Field> BytecodeCircuitConfig<F> {
region: &mut Region<'_, F>,
push_data_left_is_zero_chip: &IsZeroChip<F>,
index_length_diff_is_zero_chip: &IsZeroChip<F>,
empty_hash: Value<F>,
offset: usize,
last_row_offset: usize,
) -> Result<(), Error> {
Expand All @@ -708,7 +687,6 @@ impl<F: Field> BytecodeCircuitConfig<F> {
offset,
offset <= last_row_offset,
offset == last_row_offset,
empty_hash,
empty_code_hash_word_value(),
F::from(BytecodeFieldTag::Header as u64),
F::ZERO,
Expand All @@ -730,8 +708,7 @@ impl<F: Field> BytecodeCircuitConfig<F> {
offset: usize,
enable: bool,
last: bool,
code_hash: Value<F>,
code_hash_word: Word<Value<F>>,
code_hash: Word<Value<F>>,
tag: F,
index: F,
is_code: F,
Expand Down Expand Up @@ -787,10 +764,7 @@ impl<F: Field> BytecodeCircuitConfig<F> {
|| Value::known(value),
)?;
}
for (name, column, value) in [
("code_hash", self.bytecode_table.code_hash, code_hash),
("value_rlc", self.value_rlc, value_rlc),
] {
for (name, column, value) in [("value_rlc", self.value_rlc, value_rlc)] {
region.assign_advice(
|| format!("assign {} {}", name, offset),
column,
Expand All @@ -799,10 +773,10 @@ impl<F: Field> BytecodeCircuitConfig<F> {
)?;
}

code_hash_word.assign_advice(
code_hash.assign_advice(
region,
|| format!("assign code_hash_word {}", offset),
self.bytecode_table.code_hash_word,
|| format!("assign code_hash {}", offset),
self.bytecode_table.code_hash,
offset,
)?;

Expand Down
12 changes: 4 additions & 8 deletions zkevm-circuits/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,10 +695,7 @@ impl_expr!(BytecodeFieldTag);
#[derive(Clone, Debug)]
pub struct BytecodeTable {
/// Code Hash
pub code_hash_word: word::Word<Column<Advice>>,
#[deprecated]
/// Code Hash
pub code_hash: Column<Advice>,
pub code_hash: word::Word<Column<Advice>>,
/// Tag
pub tag: Column<Advice>,
/// Index
Expand All @@ -716,8 +713,7 @@ impl BytecodeTable {
let code_hash_word = word::Word::new([meta.advice_column(), meta.advice_column()]);
let code_hash = meta.advice_column_in(SecondPhase);
Self {
code_hash_word,
code_hash,
code_hash: code_hash_word,
tag,
index,
is_code,
Expand Down Expand Up @@ -771,8 +767,8 @@ impl BytecodeTable {
impl<F: Field> LookupTable<F> for BytecodeTable {
fn columns(&self) -> Vec<Column<Any>> {
vec![
self.code_hash_word.lo().into(),
self.code_hash_word.hi().into(),
self.code_hash.lo().into(),
self.code_hash.hi().into(),
self.tag.into(),
self.index.into(),
self.is_code.into(),
Expand Down
1 change: 1 addition & 0 deletions zkevm-circuits/src/util/word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ impl<F: Field> From<WordLegacy<F>> for Word32Cell<F> {
}
}

/// Return the hash of the empty code as a Word<Value<F>>
pub fn empty_code_hash_word_value<F: Field>() -> Word<Value<F>> {
Word::new(
EMPTY_CODE_HASH_LE
Expand Down

0 comments on commit 66c3d9c

Please sign in to comment.