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

Commit

Permalink
Replace word::Word with WordLoHi (#1747)
Browse files Browse the repository at this point in the history
### Description

Replacing Word with WordHiLo.

### Issue Link

#1736

### Type of change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] This change requires a documentation update
- [x] Refactor

### Contents

- Replace word::Word with WordLoHi
- Replace word::WordCell with WordLoHiCell

### Rationale

- WordLimbs and WordCell is used somewhere so couldn't go with that. 
- ZWord and CWord could be used, however, I saw that Word is actually an
alias for Word2
([source](https://github.com/privacy-scaling-explorations/zkevm-circuits/blob/7f356548dd6dd614289b9700a40fbc80c4949b47/zkevm-circuits/src/util/word.rs#L203)).
Which is an alias of WordLimbs<2>. So Word2 could be better than ZWord
or CWord, but I think WordHiLo is even better and makes the code more
obvious, since we have hi-lo terminology in many places.

### How Has This Been Tested?

Built all packages locally.
  • Loading branch information
zemse authored Feb 7, 2024
1 parent be9fed5 commit 8ba838b
Show file tree
Hide file tree
Showing 109 changed files with 968 additions and 935 deletions.
1 change: 1 addition & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] This change requires a documentation update
- [ ] Refactor (no updates to logic)

### Contents

Expand Down
14 changes: 7 additions & 7 deletions light-client-poc/src/circuit/equal_words.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ use halo2_proofs::{
plonk::{Advice, Column, ConstraintSystem, Error, Expression, Selector},
poly::Rotation,
};
use zkevm_circuits::util::word::Word;
use zkevm_circuits::util::word::WordLoHi;

#[derive(Clone, Debug)]
pub struct EqualWordsConfig<F: Field>(Word<IsZeroConfig<F>>);
pub struct EqualWordsConfig<F: Field>(WordLoHi<IsZeroConfig<F>>);
impl<F: Field> EqualWordsConfig<F> {
pub fn configure(
meta: &mut ConstraintSystem<F>,
q_enable: Selector,
first: (Word<Column<Advice>>, Rotation),
second: (Word<Column<Advice>>, Rotation),
first: (WordLoHi<Column<Advice>>, Rotation),
second: (WordLoHi<Column<Advice>>, Rotation),
) -> Self {
let lo_inv = meta.advice_column();
let lo = IsZeroChip::configure(
Expand All @@ -42,7 +42,7 @@ impl<F: Field> EqualWordsConfig<F> {
hi_inv,
);

Self(Word::new([lo, hi]))
Self(WordLoHi::new([lo, hi]))
}

pub fn expr(&self) -> Expression<F> {
Expand All @@ -54,8 +54,8 @@ impl<F: Field> EqualWordsConfig<F> {
region: &mut Region<'_, F>,
offset: usize,
name: &str,
first: &Word<F>,
second: &Word<F>,
first: &WordLoHi<F>,
second: &WordLoHi<F>,
) -> Result<(), Error> {
region.name_column(|| format!("{}_lo_inv", name), self.0.lo().value_inv);
region.name_column(|| format!("{}_hi_inv", name), self.0.hi().value_inv);
Expand Down
12 changes: 6 additions & 6 deletions light-client-poc/src/circuit/state_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use halo2_proofs::{
use zkevm_circuits::{
mpt_circuit::{MPTCircuit, MPTCircuitParams, MPTConfig},
table::{KeccakTable, MptTable},
util::{word, Challenges},
util::{word::WordLoHi, Challenges},
};

use super::witness::{
Expand Down Expand Up @@ -117,12 +117,12 @@ impl<F: Field> Circuit<F> for StateUpdateCircuit<F> {
let pi_instance = meta.instance_column();
let pi_mpt = MptTable {
address: meta.advice_column(),
storage_key: word::Word::new([meta.advice_column(), meta.advice_column()]),
storage_key: WordLoHi::new([meta.advice_column(), meta.advice_column()]),
proof_type: meta.advice_column(),
new_root: word::Word::new([meta.advice_column(), meta.advice_column()]),
old_root: word::Word::new([meta.advice_column(), meta.advice_column()]),
new_value: word::Word::new([meta.advice_column(), meta.advice_column()]),
old_value: word::Word::new([meta.advice_column(), meta.advice_column()]),
new_root: WordLoHi::new([meta.advice_column(), meta.advice_column()]),
old_root: WordLoHi::new([meta.advice_column(), meta.advice_column()]),
new_value: WordLoHi::new([meta.advice_column(), meta.advice_column()]),
old_value: WordLoHi::new([meta.advice_column(), meta.advice_column()]),
};

for col in [
Expand Down
20 changes: 9 additions & 11 deletions light-client-poc/src/circuit/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ use eyre::Result;

use mpt_witness_generator::{ProofType, TrieModification};
use zkevm_circuits::{
mpt_circuit::witness_row::Node,
table::mpt_table::MPTProofType,
util::word::{self, Word},
mpt_circuit::witness_row::Node, table::mpt_table::MPTProofType, util::word::WordLoHi,
};

#[derive(Default, Debug, Clone)]
Expand Down Expand Up @@ -82,10 +80,10 @@ pub struct StateUpdateWitness<F: Field> {
pub struct SingleTrieModification<F: Field> {
pub typ: F,
pub address: F,
pub value: word::Word<F>,
pub key: word::Word<F>,
pub old_root: word::Word<F>,
pub new_root: word::Word<F>,
pub value: WordLoHi<F>,
pub key: WordLoHi<F>,
pub old_root: WordLoHi<F>,
pub new_root: WordLoHi<F>,
}

#[derive(Default, Clone)]
Expand Down Expand Up @@ -350,10 +348,10 @@ impl<F: Field> StateUpdateWitness<F> {
let lc_proof = SingleTrieModification::<F> {
typ: F::from(proof_type as u64),
address: address.to_scalar().unwrap(),
value: Word::<F>::from(value),
key: Word::<F>::from(key),
old_root: Word::<F>::from(from_root),
new_root: Word::<F>::from(to_root),
value: WordLoHi::<F>::from(value),
key: WordLoHi::<F>::from(key),
old_root: WordLoHi::<F>::from(from_root),
new_root: WordLoHi::<F>::from(to_root),
};
lc_proofs.push(lc_proof);
}
Expand Down
18 changes: 12 additions & 6 deletions zkevm-circuits/src/bytecode_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use crate::{
},
table::{BytecodeFieldTag, BytecodeTable, KeccakTable, LookupTable},
util::{
self, get_push_size,
word::{empty_code_hash_word_value, Word, Word32, WordExpr},
get_push_size,
word::{empty_code_hash_word_value, Word32, WordExpr, WordLoHi},
Challenges, Expr, SubCircuit, SubCircuitConfig,
},
witness::{self},
Expand All @@ -40,7 +40,7 @@ const PUSH_TABLE_WIDTH: usize = 2;
#[derive(Debug, Clone, Default)]
/// Row for assignment
pub(crate) struct BytecodeCircuitRow<F: Field> {
pub(crate) code_hash: Word<Value<F>>,
pub(crate) code_hash: WordLoHi<Value<F>>,
tag: F,
pub(crate) index: F,
pub(crate) is_code: F,
Expand All @@ -52,7 +52,13 @@ pub(crate) struct BytecodeCircuitRow<F: Field> {
}
impl<F: Field> BytecodeCircuitRow<F> {
#[cfg(test)]
pub(crate) fn new(code_hash: Word<Value<F>>, tag: F, index: F, is_code: F, value: F) -> Self {
pub(crate) fn new(
code_hash: WordLoHi<Value<F>>,
tag: F,
index: F,
is_code: F,
value: F,
) -> Self {
Self {
code_hash,
tag,
Expand Down Expand Up @@ -89,7 +95,7 @@ impl<F: Field> From<Vec<Bytecode>> for BytecodeCircuitAssignment<F> {
fn from(codes: Vec<Bytecode>) -> Self {
let mut rows = vec![];
for bytecode in codes.iter() {
let code_hash = util::word::Word::from(bytecode.hash()).into_value();
let code_hash = WordLoHi::from(bytecode.hash()).into_value();
let code_size = bytecode.codesize();
let head = BytecodeCircuitRow {
code_hash,
Expand Down Expand Up @@ -366,7 +372,7 @@ impl<F: Field> SubCircuitConfig<F> for BytecodeCircuitConfig<F> {
meta.query_advice(length, Rotation::cur()),
);

let empty_hash_word: Word<Expression<F>> =
let empty_hash_word: WordLoHi<Expression<F>> =
Word32::new(*EMPTY_CODE_HASH_LE).to_expr().to_word();

cb.require_equal_word(
Expand Down
10 changes: 5 additions & 5 deletions zkevm-circuits/src/circuit_tools/cell_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
circuit_tools::cached_region::CachedRegion,
evm_circuit::util::rlc,
table::LookupTable,
util::{query_expression, word::Word, Expr},
util::{query_expression, word::WordLoHi, Expr},
};
use eth_types::Field;
use halo2_proofs::{
Expand Down Expand Up @@ -103,11 +103,11 @@ impl<F: Field> Expr<F> for &Cell<F> {
}
}

pub(crate) type WordCell<F> = Word<Cell<F>>;
pub(crate) type WordLoHiCell<F> = WordLoHi<Cell<F>>;

impl<F: Field> WordCell<F> {
pub fn expr(&self) -> Word<Expression<F>> {
Word::new([self.lo().expr(), self.hi().expr()])
impl<F: Field> WordLoHiCell<F> {
pub fn expr(&self) -> WordLoHi<Expression<F>> {
WordLoHi::new([self.lo().expr(), self.hi().expr()])
}
}

Expand Down
10 changes: 5 additions & 5 deletions zkevm-circuits/src/circuit_tools/constraint_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
use crate::{
evm_circuit::util::rlc,
table::LookupTable,
util::{query_expression, word::Word, Expr},
util::{query_expression, word::WordLoHi, Expr},
};
use eth_types::Field;
use gadgets::util::{and, sum, Scalar};
Expand All @@ -18,7 +18,7 @@ use itertools::Itertools;

use super::{
cached_region::StoredExpression,
cell_manager::{Cell, CellManager, CellType, WordCell},
cell_manager::{Cell, CellManager, CellType, WordLoHiCell},
};

fn get_condition_expr<F: Field>(conditions: &Vec<Expression<F>>) -> Expression<F> {
Expand Down Expand Up @@ -352,8 +352,8 @@ impl<F: Field, C: CellType> ConstraintBuilder<F, C> {
}

// default query_word is 2 limbs. Each limb is not guaranteed to be 128 bits.
pub(crate) fn query_word_unchecked(&mut self) -> WordCell<F> {
Word::new(self.query_cells_dyn(C::default(), 2).try_into().unwrap())
pub(crate) fn query_word_unchecked(&mut self) -> WordLoHiCell<F> {
WordLoHi::new(self.query_cells_dyn(C::default(), 2).try_into().unwrap())
}

pub(crate) fn validate_degree(&self, degree: usize, name: &'static str) {
Expand Down Expand Up @@ -718,7 +718,7 @@ impl<F: Field, E: Expr<F>> ExprVec<F> for &[E] {
}
}

impl<F: Field, E: Expr<F> + Clone> ExprVec<F> for Word<E> {
impl<F: Field, E: Expr<F> + Clone> ExprVec<F> for WordLoHi<E> {
fn to_expr_vec(&self) -> Vec<Expression<F>> {
vec![self.lo().expr(), self.hi().expr()]
}
Expand Down
16 changes: 8 additions & 8 deletions zkevm-circuits/src/circuit_tools/gadgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use halo2_proofs::{

use crate::{
evm_circuit::util::{from_bytes, pow_of_two, transpose_val_ret},
util::word::{Word, WordExpr},
util::word::{WordExpr, WordLoHi},
};

use super::{
Expand Down Expand Up @@ -107,8 +107,8 @@ pub struct IsEqualWordGadget<F> {
impl<F: Field> IsEqualWordGadget<F> {
pub(crate) fn construct<C: CellType>(
cb: &mut ConstraintBuilder<F, C>,
lhs: &Word<Expression<F>>,
rhs: &Word<Expression<F>>,
lhs: &WordLoHi<Expression<F>>,
rhs: &WordLoHi<Expression<F>>,
) -> Self {
let (lhs_lo, lhs_hi) = lhs.to_word().to_lo_hi();
let (rhs_lo, rhs_hi) = rhs.to_word().to_lo_hi();
Expand All @@ -129,8 +129,8 @@ impl<F: Field> IsEqualWordGadget<F> {
&self,
region: &mut CachedRegion<'_, '_, F>,
offset: usize,
lhs: Word<F>,
rhs: Word<F>,
lhs: WordLoHi<F>,
rhs: WordLoHi<F>,
) -> Result<F, Error> {
let (lhs_lo, lhs_hi) = lhs.to_lo_hi();
let (rhs_lo, rhs_hi) = rhs.to_lo_hi();
Expand All @@ -143,8 +143,8 @@ impl<F: Field> IsEqualWordGadget<F> {
&self,
region: &mut CachedRegion<'_, '_, F>,
offset: usize,
lhs: Value<Word<F>>,
rhs: Value<Word<F>>,
lhs: Value<WordLoHi<F>>,
rhs: Value<WordLoHi<F>>,
) -> Result<Value<F>, Error> {
transpose_val_ret(
lhs.zip(rhs)
Expand All @@ -159,7 +159,7 @@ impl<F: Field> IsEqualWordGadget<F> {
lhs: eth_types::Word,
rhs: eth_types::Word,
) -> Result<F, Error> {
self.assign(region, offset, Word::from(lhs), Word::from(rhs))
self.assign(region, offset, WordLoHi::from(lhs), WordLoHi::from(rhs))
}
}

Expand Down
8 changes: 4 additions & 4 deletions zkevm-circuits/src/copy_circuit/util.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::util::word::Word;
use crate::util::word::WordLoHi;
use bus_mapping::circuit_input_builder::NumberOrHash;
use eth_types::Field;
use halo2_proofs::circuit::Value;

/// Encode the type `NumberOrHash` into a field element
pub fn number_or_hash_to_word<F: Field>(v: &NumberOrHash) -> Word<Value<F>> {
pub fn number_or_hash_to_word<F: Field>(v: &NumberOrHash) -> WordLoHi<Value<F>> {
match v {
NumberOrHash::Number(n) => Word::from(*n as u64).into_value(),
NumberOrHash::Hash(h) => Word::from(*h).into_value(),
NumberOrHash::Number(n) => WordLoHi::from(*n as u64).into_value(),
NumberOrHash::Hash(h) => WordLoHi::from(*h).into_value(),
}
}
6 changes: 3 additions & 3 deletions zkevm-circuits/src/evm_circuit/execution/add_sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
witness::{Block, Call, ExecStep, Transaction},
},
util::{
word::{Word, WordExpr},
word::{WordExpr, WordLoHi},
Expr,
},
};
Expand Down Expand Up @@ -54,9 +54,9 @@ impl<F: Field> ExecutionGadget<F> for AddSubGadget<F> {
// ADD: Pop a and b from the stack, push c on the stack
// SUB: Pop c and b from the stack, push a on the stack

cb.stack_pop(Word::select(is_sub.expr().0, c.to_word(), a.to_word()));
cb.stack_pop(WordLoHi::select(is_sub.expr().0, c.to_word(), a.to_word()));
cb.stack_pop(b.to_word());
cb.stack_push(Word::select(is_sub.expr().0, a.to_word(), c.to_word()));
cb.stack_push(WordLoHi::select(is_sub.expr().0, a.to_word(), c.to_word()));

// State transition
let step_state_transition = StepStateTransition {
Expand Down
6 changes: 3 additions & 3 deletions zkevm-circuits/src/evm_circuit/execution/addmod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
witness::{Block, Call, ExecStep, Transaction},
},
util::{
word::{Word, Word32Cell, WordExpr},
word::{Word32Cell, WordExpr, WordLoHi},
Expr,
},
};
Expand Down Expand Up @@ -94,7 +94,7 @@ impl<F: Field> ExecutionGadget<F> for AddModGadget<F> {
cb.require_equal_word(
"check a_reduced + b 512 bit carry if n != 0",
sum_areduced_b_overflow.to_word(),
Word::from_lo_unchecked(sum_areduced_b.carry().clone().unwrap().expr())
WordLoHi::from_lo_unchecked(sum_areduced_b.carry().clone().unwrap().expr())
.mul_selector(not::expr(n_is_zero.expr())),
);

Expand Down Expand Up @@ -220,7 +220,7 @@ impl<F: Field> ExecutionGadget<F> for AddModGadget<F> {
self.cmp_areduced_n.assign(region, offset, a_reduced, n)?;

self.n_is_zero
.assign_value(region, offset, Value::known(Word::from(n)))?;
.assign_value(region, offset, Value::known(WordLoHi::from(n)))?;

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
},
table::CallContextFieldTag,
util::{
word::{WordCell, WordExpr},
word::{WordExpr, WordLoHiCell},
Expr,
},
};
Expand All @@ -22,7 +22,7 @@ use halo2_proofs::plonk::Error;
#[derive(Clone, Debug)]
pub(crate) struct AddressGadget<F> {
same_context: SameContextGadget<F>,
address: WordCell<F>,
address: WordLoHiCell<F>,
}

impl<F: Field> ExecutionGadget<F> for AddressGadget<F> {
Expand Down
8 changes: 4 additions & 4 deletions zkevm-circuits/src/evm_circuit/execution/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
},
table::{AccountFieldTag, CallContextFieldTag},
util::{
word::{Word, Word32Cell, WordCell, WordExpr},
word::{Word32Cell, WordExpr, WordLoHi, WordLoHiCell},
Expr,
},
};
Expand All @@ -29,8 +29,8 @@ pub(crate) struct BalanceGadget<F> {
reversion_info: ReversionInfo<F>,
tx_id: Cell<F>,
is_warm: Cell<F>,
code_hash: WordCell<F>,
not_exists: IsZeroWordGadget<F, WordCell<F>>,
code_hash: WordLoHiCell<F>,
not_exists: IsZeroWordGadget<F, WordLoHiCell<F>>,
balance: Word32Cell<F>,
}

Expand Down Expand Up @@ -139,7 +139,7 @@ impl<F: Field> ExecutionGadget<F> for BalanceGadget<F> {
self.code_hash
.assign_u256(region, offset, code_hash.to_word())?;
self.not_exists
.assign_value(region, offset, Value::known(Word::from(code_hash)))?;
.assign_value(region, offset, Value::known(WordLoHi::from(code_hash)))?;
let balance = if code_hash.is_zero() {
eth_types::Word::zero()
} else {
Expand Down
Loading

0 comments on commit 8ba838b

Please sign in to comment.