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

Commit

Permalink
fix few common gadgets
Browse files Browse the repository at this point in the history
  • Loading branch information
Wu Sung-Ming committed May 17, 2023
1 parent d89eef8 commit 1897888
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 58 deletions.
9 changes: 5 additions & 4 deletions zkevm-circuits/src/evm_circuit/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ pub struct RwValues<F> {
pub storage_key: Word<Expression<F>>,
pub value: Word<Expression<F>>,
pub value_prev: Word<Expression<F>>,
pub aux1: Expression<F>,
pub aux2: Expression<F>,
pub aux1: Expression<F>, // for AccountStorage::tx_id
pub aux2: Word<Expression<F>>, // for AccountStorage committed value
}

impl<F: Field> RwValues<F> {
Expand All @@ -156,7 +156,7 @@ impl<F: Field> RwValues<F> {
value: Word<Expression<F>>,
value_prev: Word<Expression<F>>,
aux1: Expression<F>,
aux2: Expression<F>,
aux2: Word<Expression<F>>,
) -> Self {
Self {
id,
Expand Down Expand Up @@ -338,7 +338,8 @@ impl<F: Field> Lookup<F> {
values.value_prev.lo().clone(),
values.value_prev.hi().clone(),
values.aux1.clone(),
values.aux2.clone(),
values.aux2.lo().clone(),
values.aux2.hi().clone(),
],
Self::Bytecode {
hash,
Expand Down
8 changes: 4 additions & 4 deletions zkevm-circuits/src/evm_circuit/util/common_gadget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,10 +622,10 @@ impl<F: Field, const IS_SUCCESS_CALL: bool> CommonCallGadget<F, IS_SUCCESS_CALL>
let gas_word = cb.query_word32();
let callee_address_word = cb.query_account_address();
let value = cb.query_word32();
let cd_offset = cb.query_word32();
let cd_length = cb.query_word32();
let rd_offset = cb.query_word32();
let rd_length = cb.query_word32();
let cd_offset = cb.query_word_unchecked();
let cd_length = cb.query_memory_address();
let rd_offset = cb.query_word_unchecked();
let rd_length = cb.query_memory_address();
let is_success = cb.query_bool();

// Lookup values from stack
Expand Down
34 changes: 17 additions & 17 deletions zkevm-circuits/src/evm_circuit/util/constraint_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
value,
value_prev,
0.expr(),
0.expr(),
Word::zero(),
),
reversion_info,
);
Expand All @@ -923,7 +923,7 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
value.clone(),
value,
0.expr(),
0.expr(),
Word::zero(),
),
);
}
Expand All @@ -948,7 +948,7 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
value,
value_prev,
0.expr(),
0.expr(),
Word::zero(),
),
reversion_info,
);
Expand All @@ -973,7 +973,7 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
value.clone(),
value,
0.expr(),
0.expr(),
Word::zero(),
),
);
}
Expand All @@ -993,7 +993,7 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
value.clone(),
value,
0.expr(),
0.expr(),
Word::zero(),
),
);
}
Expand All @@ -1016,7 +1016,7 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
value,
value_prev,
0.expr(),
0.expr(),
Word::zero(),
),
reversion_info,
);
Expand All @@ -1042,7 +1042,7 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
value.clone(),
value,
0.expr(),
0.expr(),
Word::zero(),
),
);
}
Expand All @@ -1066,7 +1066,7 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
value,
value_prev,
0.expr(),
0.expr(),
Word::zero(),
),
reversion_info,
);
Expand All @@ -1080,7 +1080,7 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
key: Word<Expression<F>>,
value: Word<Expression<F>>,
tx_id: Expression<F>,
committed_value: Expression<F>,
committed_value: Word<Expression<F>>,
) {
self.rw_lookup(
"account_storage_read",
Expand All @@ -1107,7 +1107,7 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
value: Word<Expression<F>>,
value_prev: Word<Expression<F>>,
tx_id: Expression<F>,
committed_value: Expression<F>,
committed_value: Word<Expression<F>>,
reversion_info: Option<&mut ReversionInfo<F>>,
) {
self.reversible_write(
Expand Down Expand Up @@ -1175,7 +1175,7 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
value,
Word::zero(),
0.expr(),
0.expr(),
Word::zero(),
),
);
}
Expand All @@ -1198,7 +1198,7 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
value,
Word::zero(),
0.expr(),
0.expr(),
Word::zero(),
),
);
}
Expand Down Expand Up @@ -1287,7 +1287,7 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
value,
Word::zero(),
0.expr(),
0.expr(),
Word::zero(),
),
);
}
Expand All @@ -1314,7 +1314,7 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
Word::from_lo_unchecked(byte),
Word::zero(),
0.expr(),
0.expr(),
Word::zero(),
),
);
}
Expand All @@ -1340,7 +1340,7 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
Word::from_lo_unchecked(value),
Word::zero(),
0.expr(),
0.expr(),
Word::zero(),
),
);
}
Expand All @@ -1367,7 +1367,7 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
Word::from_lo_unchecked(value),
Word::zero(),
0.expr(),
0.expr(),
Word::zero(),
),
);
}
Expand All @@ -1388,7 +1388,7 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
value: Word::zero(),
value_prev: Word::zero(),
aux1: 0.expr(),
aux2: 0.expr(),
aux2: Word::zero(),
},
);
}
Expand Down
3 changes: 2 additions & 1 deletion zkevm-circuits/src/evm_circuit/util/math_gadget/lt_word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ mod tests {
fn configure_gadget_container(cb: &mut EVMConstraintBuilder<F>) -> Self {
let a = cb.query_word32();
let b = cb.query_word32();
let ltword_gadget = LtWordGadget::<F>::construct(cb, &a, &b);
let ltword_gadget =
LtWordGadget::<F, Word32Cell<F>, Word32Cell<F>>::construct(cb, a, b);
cb.require_equal("a < b", ltword_gadget.expr(), 1.expr());
LtWordTestContainer {
ltword_gadget,
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 @@ -40,7 +40,7 @@ impl<F: Field> ModGadget<F> {
let a_or_is_zero = IsZeroWordGadget::construct(cb, a_or_zero.clone());
let mul_add_words = MulAddWordsGadget::construct(cb, [&k, n, r, &a_or_zero]);
let eq = IsEqualWordGadget::construct(cb, a.clone(), a_or_zero);
let lt = LtWordGadget::construct(cb, r, n);
let lt = LtWordGadget::construct(cb, r.clone(), n.clone());
// Constrain the aux variable a_or_zero to be =a or =0 if n==0:
// (a == a_or_zero) ^ (n == 0 & a_or_zero == 0)
cb.add_constraint(
Expand Down
61 changes: 37 additions & 24 deletions zkevm-circuits/src/evm_circuit/util/memory_gadget.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
use super::{constraint_builder::ConstrainBuilderCommon, CachedRegion};
use super::{constraint_builder::ConstrainBuilderCommon, CachedRegion, MemoryAddress};
use crate::{
evm_circuit::{
param::{N_BYTES_GAS, N_BYTES_MEMORY_ADDRESS, N_BYTES_MEMORY_WORD_SIZE},
util::{
constraint_builder::EVMConstraintBuilder,
from_bytes,
math_gadget::{
ConstantDivisionGadget, IsZeroGadget, IsZeroWordGadget, MinMaxGadget,
RangeCheckGadget,
},
math_gadget::{ConstantDivisionGadget, IsZeroGadget, MinMaxGadget, RangeCheckGadget},
select, sum, Cell,
},
},
util::{
word::{Word, Word32Cell},
word::{Word, WordCell, WordExpr},
Expr,
},
};
Expand Down Expand Up @@ -69,21 +66,31 @@ pub(crate) mod address_high {
/// the RLC value for `memory_offset` need not match the bytes.
#[derive(Clone, Debug)]
pub(crate) struct MemoryAddressGadget<F> {
memory_offset: Word32Cell<F>,
memory_length: Word32Cell<F>,
memory_length_is_zero: IsZeroWordGadget<F, Word32Cell<F>>,
memory_offset_bytes: MemoryAddress<F>,
memory_length: MemoryAddress<F>,
memory_length_is_zero: IsZeroGadget<F>,
}

impl<F: Field> MemoryAddressGadget<F> {
pub(crate) fn construct(
cb: &mut EVMConstraintBuilder<F>,
memory_offset: Word32Cell<F>,
memory_length: Word32Cell<F>,
memory_offset_word: WordCell<F>,
memory_length: MemoryAddress<F>,
) -> Self {
let memory_length_is_zero = IsZeroWordGadget::construct(cb, memory_length);
let memory_length_is_zero = IsZeroGadget::construct(cb, sum::expr(&memory_length.cells));
let memory_offset_bytes = cb.query_memory_address();

let has_length = 1.expr() - memory_length_is_zero.expr();
cb.condition(has_length, |cb| {
cb.require_equal_word(
"Offset decomposition into 5 bytes",
Word::from_lo_unchecked(memory_offset_bytes.expr()),
memory_offset_word.to_word(),
);
});

Self {
memory_offset,
memory_offset_bytes,
memory_length,
memory_length_is_zero,
}
Expand All @@ -96,27 +103,33 @@ impl<F: Field> MemoryAddressGadget<F> {
memory_offset: U256,
memory_length: U256,
) -> Result<u64, Error> {
let memory_offset_bytes = memory_offset.to_le_bytes();
let memory_length_bytes = memory_length.to_le_bytes();
let memory_length_is_zero = memory_length.is_zero();
self.memory_offset.assign(
self.memory_offset_bytes.assign(
region,
offset,
if memory_length_is_zero {
Some([0u8; 32])
Some([0; 5])
} else {
Some(memory_offset.to_le_bytes())
memory_offset_bytes[..N_BYTES_MEMORY_ADDRESS]
.try_into()
.ok()
},
)?;

self.memory_length
.assign(region, offset, Some(memory_length.to_le_bytes()))?;

self.memory_length.assign(
region,
offset,
memory_length_bytes[..N_BYTES_MEMORY_ADDRESS]
.try_into()
.ok(),
)?;
self.memory_length_is_zero
.assign(region, offset, Word::from_u256(memory_length))?;
.assign(region, offset, sum::value(&memory_length_bytes))?;
Ok(if memory_length_is_zero {
0
} else {
address_low::value(memory_offset.to_le_bytes())
+ address_low::value(memory_length.to_le_bytes())
address_low::value(memory_offset_bytes) + address_low::value(memory_length_bytes)
})
}

Expand Down Expand Up @@ -205,7 +218,7 @@ impl<F: Field, const N: usize, const N_BYTES_MEMORY_WORD_SIZE: usize>
// The memory size needs to be updated if this memory access
// requires expanding the memory.
// `next_memory_word_size < 256**MAX_MEMORY_SIZE_IN_BYTES`
let curr_memory_word_size = cb.curr.state.memory_word_size.word_expr();
let curr_memory_word_size = cb.curr.state.memory_word_size.expr();
let mut next_memory_word_size = curr_memory_word_size.clone();
let max_memory_word_sizes = array_init(|idx| {
let max_memory_word_size = MinMaxGadget::construct(
Expand Down
10 changes: 6 additions & 4 deletions zkevm-circuits/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ pub struct RwTable {
/// Aux1
pub aux1: Column<Advice>,
/// Aux2 (Committed Value)
pub aux2: Column<Advice>,
pub aux2: word::Word<Column<Advice>>,
}

impl<F: Field> LookupTable<F> for RwTable {
Expand All @@ -471,7 +471,8 @@ impl<F: Field> LookupTable<F> for RwTable {
self.value_prev.lo().clone().into(),
self.value_prev.hi().clone().into(),
self.aux1.into(),
self.aux2.into(),
self.aux2.lo().clone().into(),
self.aux2.hi().clone().into(),
]
}

Expand All @@ -490,7 +491,8 @@ impl<F: Field> LookupTable<F> for RwTable {
String::from("value_prev_lo"),
String::from("value_prev_hi"),
String::from("aux1"),
String::from("aux2"),
String::from("aux2_lo"),
String::from("aux2_hi"),
]
}
}
Expand All @@ -510,7 +512,7 @@ impl RwTable {
// It seems that aux1 for the moment is not using randomness
// TODO check in a future review
aux1: meta.advice_column_in(SecondPhase),
aux2: meta.advice_column_in(SecondPhase),
aux2: word::Word::new([meta.advice_column(), meta.advice_column()]),
}
}
fn assign<F: Field>(
Expand Down
7 changes: 4 additions & 3 deletions zkevm-circuits/src/witness/rw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,11 @@ pub struct RwRow<F> {
pub(crate) value: word::Word<F>,
pub(crate) value_prev: word::Word<F>,
pub(crate) aux1: F,
pub(crate) aux2: F,
pub(crate) aux2: word::Word<F>,
}

impl<F: Field> RwRow<F> {
pub(crate) fn values(&self) -> [F; 14] {
pub(crate) fn values(&self) -> [F; 15] {
[
self.rw_counter,
self.is_write,
Expand All @@ -276,7 +276,8 @@ impl<F: Field> RwRow<F> {
self.value_prev.lo().clone(),
self.value_prev.hi().clone(),
self.aux1,
self.aux2,
self.aux2.lo().clone(),
self.aux2.hi().clone(),
]
}
pub(crate) fn rlc(&self, randomness: F) -> F {
Expand Down

0 comments on commit 1897888

Please sign in to comment.