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

Commit

Permalink
better generic design
Browse files Browse the repository at this point in the history
  • Loading branch information
Wu Sung-Ming committed May 15, 2023
1 parent fa10ccf commit e9faa3d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 26 deletions.
4 changes: 2 additions & 2 deletions zkevm-circuits/src/evm_circuit/execution/addmod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ pub(crate) struct AddModGadget<F> {
muladd_d_n_r: MulAddWords512Gadget<F>,

n_is_zero: IsZeroWordGadget<F>,
cmp_r_n: CmpWordsGadget<F, Word32Cell<F>>,
cmp_areduced_n: CmpWordsGadget<F, Word32Cell<F>>,
cmp_r_n: CmpWordsGadget<F, Word32Cell<F>, Word32Cell<F>>,
cmp_areduced_n: CmpWordsGadget<F, Word32Cell<F>, Word32Cell<F>>,
}

impl<F: Field> ExecutionGadget<F> for AddModGadget<F> {
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/evm_circuit/util/common_gadget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ pub(crate) struct CommonCallGadget<F, const IS_SUCCESS_CALL: bool> {
value_is_zero: IsZeroWordGadget<F>,
pub has_value: Expression<F>,
pub callee_code_hash: Word32Cell<F>,
pub is_empty_code_hash: IsEqualWordGadget<F, Word<Expression<F>>>,
pub is_empty_code_hash: IsEqualWordGadget<F, Word<Expression<F>>, Word<Expression<F>>>,

pub callee_not_exists: IsZeroWordGadget<F>,
}
Expand Down
26 changes: 12 additions & 14 deletions zkevm-circuits/src/evm_circuit/util/math_gadget/cmp_words.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
use std::marker::PhantomData;

use crate::{
evm_circuit::util::{
constraint_builder::EVMConstraintBuilder, from_bytes, math_gadget::*, select, CachedRegion,
},
util::{word::WordExpr, Expr},
util::{word::WordExpr, Expr, PhantomData2},
};
use eth_types::{Field, ToLittleEndian, Word};
use halo2_proofs::plonk::{Error, Expression};

#[derive(Clone, Debug)]
/// CmpWordsGadget compares two words, exposing `eq` and `lt`
pub(crate) struct CmpWordsGadget<F, T> {
pub(crate) struct CmpWordsGadget<F, T1, T2> {
comparison_lo: ComparisonGadget<F, 16>,
comparison_hi: ComparisonGadget<F, 16>,
pub eq: Expression<F>,
pub lt: Expression<F>,
marker: PhantomData<T>,
_marker: PhantomData2<T1, T2>,
}

impl<F: Field, T: WordExpr<F>> CmpWordsGadget<F, T> {
pub(crate) fn construct(cb: &mut EVMConstraintBuilder<F>, a: T, b: T) -> Self {
impl<F: Field, T1: WordExpr<F>, T2: WordExpr<F>> CmpWordsGadget<F, T1, T2> {
pub(crate) fn construct(cb: &mut EVMConstraintBuilder<F>, a: T1, b: T2) -> Self {
let (a_lo, a_hi) = a.to_word().to_lo_hi();
let (b_lo, b_hi) = b.to_word().to_lo_hi();
// `a.lo <= b.lo`
Expand All @@ -45,7 +43,7 @@ impl<F: Field, T: WordExpr<F>> CmpWordsGadget<F, T> {
comparison_hi,
lt,
eq,
marker: Default::default(),
_marker: Default::default(),
}
}

Expand Down Expand Up @@ -85,14 +83,14 @@ mod tests {

#[derive(Clone)]
/// CmpWordGadgetTestContainer: require(a == b if CHECK_EQ else a < b)
struct CmpWordGadgetTestContainer<F, const CHECK_EQ: bool, T: WordExpr<F>> {
cmp_gadget: CmpWordsGadget<F, T>,
a_word: T,
b_word: T,
struct CmpWordGadgetTestContainer<F, const CHECK_EQ: bool, T1, T2> {
cmp_gadget: CmpWordsGadget<F, T1, T2>,
a_word: T1,
b_word: T2,
}

impl<F: Field, const CHECK_EQ: bool, T: WordExpr<F>> MathGadgetContainer<F>
for CmpWordGadgetTestContainer<F, CHECK_EQ, T>
impl<F: Field, const CHECK_EQ: bool, T1: WordExpr<F>, T2: WordExpr<F>> MathGadgetContainer<F>
for CmpWordGadgetTestContainer<F, CHECK_EQ, T1, T2>
{
fn configure_gadget_container(cb: &mut EVMConstraintBuilder<F>) -> Self {
let a_word = cb.query_word_unchecked();
Expand Down
17 changes: 9 additions & 8 deletions zkevm-circuits/src/evm_circuit/util/math_gadget/is_equal_word.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::marker::PhantomData;

use eth_types::Field;
use gadgets::util::and;
use halo2_proofs::{
Expand All @@ -11,21 +9,24 @@ use crate::{
evm_circuit::util::{
constraint_builder::EVMConstraintBuilder, transpose_val_ret, CachedRegion,
},
util::word::{Word, WordExpr},
util::{
word::{Word, WordExpr},
PhantomData2,
},
};

use super::IsZeroGadget;

/// Returns `1` when `lhs == rhs`, and returns `0` otherwise.
#[derive(Clone, Debug)]
pub struct IsEqualWordGadget<F, T> {
pub struct IsEqualWordGadget<F, T1, T2> {
is_zero_lo: IsZeroGadget<F>,
is_zero_hi: IsZeroGadget<F>,
marker: PhantomData<T>,
_marker: PhantomData2<T1, T2>,
}

impl<F: Field, T: WordExpr<F>> IsEqualWordGadget<F, T> {
pub(crate) fn construct(cb: &mut EVMConstraintBuilder<F>, lhs: T, rhs: T) -> Self {
impl<F: Field, T1: WordExpr<F>, T2: WordExpr<F>> IsEqualWordGadget<F, T1, T2> {
pub(crate) fn construct(cb: &mut EVMConstraintBuilder<F>, lhs: T1, rhs: T2) -> Self {
let (lhs_lo, lhs_hi) = lhs.to_lo_hi();
let (rhs_lo, rhs_hi) = rhs.to_lo_hi();
let is_zero_lo = IsZeroGadget::construct(cb, lhs_lo.clone() - rhs_lo.clone());
Expand All @@ -34,7 +35,7 @@ impl<F: Field, T: WordExpr<F>> IsEqualWordGadget<F, T> {
Self {
is_zero_lo,
is_zero_hi,
marker: Default::default(),
_marker: Default::default(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/evm_circuit/util/math_gadget/modulo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub(crate) struct ModGadget<F> {
mul_add_words: MulAddWordsGadget<F>,
n_is_zero: IsZeroWordGadget<F>,
a_or_is_zero: IsZeroWordGadget<F>,
eq: IsEqualWordGadget<F, word::Word<Expression<F>>>,
eq: IsEqualWordGadget<F, word::Word<Expression<F>>, word::Word<Expression<F>>>,
lt: LtWordGadget<F>,
}
impl<F: Field> ModGadget<F> {
Expand Down
4 changes: 4 additions & 0 deletions zkevm-circuits/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ use eth_types::{Field, ToAddress, Word};
pub use ethers_core::types::{Address, U256};
pub use gadgets::util::Expr;

/// extend PhantomData with 2 generics
#[derive(PartialEq, Default)]
pub(crate) struct PhantomData2<A, B>(A, PhantomData<B>);

pub(crate) fn query_expression<F: FieldExt, T>(
meta: &mut ConstraintSystem<F>,
mut f: impl FnMut(&mut VirtualCells<F>) -> T,
Expand Down
2 changes: 2 additions & 0 deletions zkevm-circuits/src/util/word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// - Limbs: An EVN word is 256 bits. Limbs N means split 256 into N limb. For example, N = 4, each
// limb is 256/4 = 64 bits

use std::marker::PhantomData;

use eth_types::ToLittleEndian;
use gadgets::util::{not, or, Expr};
use halo2_proofs::{
Expand Down

0 comments on commit e9faa3d

Please sign in to comment.