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

Commit

Permalink
Improve code readabiliy
Browse files Browse the repository at this point in the history
  • Loading branch information
leolara committed Jun 20, 2023
1 parent 278d706 commit 75741d2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
28 changes: 6 additions & 22 deletions zkevm-circuits/src/bytecode_circuit/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,11 @@ impl<F: Field> SubCircuitConfig<F> for BytecodeCircuitConfig<F> {
);

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

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

Expand Down Expand Up @@ -326,14 +322,8 @@ impl<F: Field> SubCircuitConfig<F> for BytecodeCircuitConfig<F> {

cb.require_equal_word(
"next.hash == cur.hash",
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()),
]),
bytecode_table.code_hash.query(meta, Rotation::next()),
bytecode_table.code_hash.query(meta, Rotation::cur()),
);

cb.require_equal(
Expand Down Expand Up @@ -375,14 +365,8 @@ impl<F: Field> SubCircuitConfig<F> for BytecodeCircuitConfig<F> {

cb.require_equal_word(
"next.hash == cur.hash",
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()),
]),
bytecode_table.code_hash.query(meta, Rotation::next()),
bytecode_table.code_hash.query(meta, Rotation::cur()),
);

cb.require_equal(
Expand Down
28 changes: 27 additions & 1 deletion zkevm-circuits/src/util/word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use eth_types::{Field, ToLittleEndian, H160};
use gadgets::util::{not, or, Expr};
use halo2_proofs::{
circuit::{AssignedCell, Region, Value},
plonk::{Advice, Column, Error, Expression},
plonk::{Advice, Column, Error, Expression, VirtualCells},
poly::Rotation,
};
use itertools::Itertools;

Expand Down Expand Up @@ -47,6 +48,24 @@ impl<T, const N: usize> WordLimbs<T, N> {
}
}

impl<const N: usize> WordLimbs<Column<Advice>, N> {
/// Query advice of WordLibs of columns advice
pub fn query<F: Field>(
&self,
meta: &mut VirtualCells<F>,
at: Rotation,
) -> WordLimbs<Expression<F>, N> {
WordLimbs::new(self.limbs.map(|column| meta.query_advice(column, at)))
}
}

impl<const N: usize> WordLimbs<u8, N> {
/// Convert WordLimbs of u8 to WordLimbs of expressions
pub fn to_expr<F: Field>(&self) -> WordLimbs<Expression<F>, N> {
WordLimbs::new(self.limbs.map(|v| Expression::Constant(F::from(v as u64))))
}
}

impl<T: Default, const N: usize> Default for WordLimbs<T, N> {
fn default() -> Self {
Self {
Expand Down Expand Up @@ -372,6 +391,13 @@ impl<F: Field> Word<Value<F>> {
}
}

impl Word<Column<Advice>> {
/// Query advice of Word of columns advice
pub fn query<F: Field>(&self, meta: &mut VirtualCells<F>, at: Rotation) -> Word<Expression<F>> {
self.0.query(meta, at).to_word()
}
}

impl<F: Field> WordExpr<F> for Word<Cell<F>> {
fn to_word(&self) -> Word<Expression<F>> {
self.word_expr().to_word()
Expand Down

0 comments on commit 75741d2

Please sign in to comment.