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

[word lo/hi] retype table, common gadget and util design #1414

Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions zkevm-circuits/src/evm_circuit/execution/address.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
use crate::{
evm_circuit::{
execution::ExecutionGadget,
param::N_BYTES_HALF_WORD,
step::ExecutionState,
util::{
common_gadget::SameContextGadget,
constraint_builder::{EVMConstraintBuilder, StepStateTransition, Transition::Delta},
AccountAddress, CachedRegion,
CachedRegion,
},
witness::{Block, Call, ExecStep, Transaction},
},
table::CallContextFieldTag,
util::{word::WordExpr, Expr},
util::{
word::{WordCell, WordExpr},
Expr,
},
};
use bus_mapping::evm::OpcodeId;
use eth_types::{Field, ToAddress, ToLittleEndian};
Expand All @@ -19,7 +23,7 @@ use halo2_proofs::plonk::Error;
#[derive(Clone, Debug)]
pub(crate) struct AddressGadget<F> {
same_context: SameContextGadget<F>,
address: AccountAddress<F>,
address: WordCell<F>,
}

impl<F: Field> ExecutionGadget<F> for AddressGadget<F> {
Expand All @@ -28,7 +32,7 @@ impl<F: Field> ExecutionGadget<F> for AddressGadget<F> {
const EXECUTION_STATE: ExecutionState = ExecutionState::ADDRESS;

fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
let address = cb.query_account_address();
hero78119 marked this conversation as resolved.
Show resolved Hide resolved
let address = cb.query_word_unchecked();

// Lookup callee address in call context.
cb.call_context_lookup_read(None, CallContextFieldTag::CalleeAddress, address.to_word());
Expand Down Expand Up @@ -66,8 +70,12 @@ impl<F: Field> ExecutionGadget<F> for AddressGadget<F> {
let address = block.rws[step.rw_indices[1]].stack_value();
debug_assert_eq!(call.address, address.to_address());

self.address
.assign(region, offset, Some(address.to_le_bytes()))?;
self.address.assign_lo_hi(
region,
offset,
address.to_le_bytes()[0..N_BYTES_HALF_WORD].try_into().ok(),
address.to_le_bytes()[N_BYTES_HALF_WORD..].try_into().ok(),
)?;

Ok(())
}
Expand Down
29 changes: 14 additions & 15 deletions zkevm-circuits/src/evm_circuit/execution/caller.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use crate::{
evm_circuit::{
execution::ExecutionGadget,
param::N_BYTES_ACCOUNT_ADDRESS,
param::N_BYTES_HALF_WORD,
step::ExecutionState,
util::{
common_gadget::SameContextGadget,
constraint_builder::{EVMConstraintBuilder, StepStateTransition, Transition::Delta},
from_bytes, CachedRegion, RandomLinearCombination,
CachedRegion,
},
witness::{Block, Call, ExecStep, Transaction},
},
table::CallContextFieldTag,
util::Expr,
util::{
word::{WordCell, WordExpr},
Expr,
},
};
use bus_mapping::evm::OpcodeId;
use eth_types::{Field, ToLittleEndian};
Expand All @@ -21,7 +24,7 @@ use halo2_proofs::plonk::Error;
pub(crate) struct CallerGadget<F> {
same_context: SameContextGadget<F>,
// Using RLC to match against rw_table->stack_op value
caller_address: RandomLinearCombination<F, N_BYTES_ACCOUNT_ADDRESS>,
caller_address: WordCell<F>,
}

impl<F: Field> ExecutionGadget<F> for CallerGadget<F> {
Expand All @@ -30,18 +33,17 @@ impl<F: Field> ExecutionGadget<F> for CallerGadget<F> {
const EXECUTION_STATE: ExecutionState = ExecutionState::CALLER;

fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
let caller_address = cb.query_word_rlc();
let caller_address = cb.query_word_unchecked();

// Lookup rw_table -> call_context with caller address
cb.call_context_lookup(
false.expr(),
cb.call_context_lookup_read(
hero78119 marked this conversation as resolved.
Show resolved Hide resolved
None, // cb.curr.state.call_id
CallContextFieldTag::CallerAddress,
from_bytes::expr(&caller_address.cells),
caller_address.to_word(),
);

// Push the value to the stack
cb.stack_push(caller_address.expr());
cb.stack_push(caller_address.to_word());

// State transition
let opcode = cb.query_cell();
Expand Down Expand Up @@ -73,14 +75,11 @@ impl<F: Field> ExecutionGadget<F> for CallerGadget<F> {

let caller = block.rws[step.rw_indices[1]].stack_value();

self.caller_address.assign(
self.caller_address.assign_lo_hi(
region,
offset,
Some(
caller.to_le_bytes()[..N_BYTES_ACCOUNT_ADDRESS]
.try_into()
.unwrap(),
),
caller.to_le_bytes()[..N_BYTES_HALF_WORD].try_into().ok(),
caller.to_le_bytes()[N_BYTES_HALF_WORD..].try_into().ok(),
)?;

Ok(())
Expand Down
3 changes: 3 additions & 0 deletions zkevm-circuits/src/evm_circuit/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ pub(crate) const MAX_N_BYTES_INTEGER: usize = 31;
// Number of bytes an EVM word has.
pub(crate) const N_BYTES_WORD: usize = 32;

// Number of bytes an half EVM word has.
pub(crate) const N_BYTES_HALF_WORD: usize = 16;

// Number of bytes an u64 has.
pub(crate) const N_BYTES_U64: usize = 8;

Expand Down
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
38 changes: 10 additions & 28 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 Expand Up @@ -835,25 +835,16 @@ impl<F: Field> SloadGasGadget<F> {
}

#[derive(Clone, Debug)]
pub(crate) struct SstoreGasGadget<F> {
value: WordCell<F>,
value_prev: WordCell<F>,
original_value: WordCell<F>,
pub(crate) struct SstoreGasGadget<F, T> {
is_warm: Cell<F>,
gas_cost: Expression<F>,
value_eq_prev: IsEqualWordGadget<F, WordCell<F>, WordCell<F>>,
original_eq_prev: IsEqualWordGadget<F, WordCell<F>, WordCell<F>>,
original_is_zero: IsZeroWordGadget<F, WordCell<F>>,
value_eq_prev: IsEqualWordGadget<F, T, T>,
original_eq_prev: IsEqualWordGadget<F, T, T>,
original_is_zero: IsZeroWordGadget<F, T>,
}

impl<F: Field> SstoreGasGadget<F> {
pub(crate) fn construct(
cb: &mut EVMConstraintBuilder<F>,
value: WordCell<F>,
value_prev: WordCell<F>,
original_value: WordCell<F>,
is_warm: Cell<F>,
) -> Self {
impl<F: Field, T: WordExpr<F>> SstoreGasGadget<F, T> {
pub(crate) fn construct(cb: &mut EVMConstraintBuilder<F>, is_warm: Cell<F>, value: T, value_prev: T, original_value: T) -> Self {
let value_eq_prev = IsEqualWordGadget::construct(cb, value, value_prev);
let original_eq_prev = IsEqualWordGadget::construct(cb, original_value, value_prev);
let original_is_zero = IsZeroWordGadget::construct(cb, original_value);
Expand All @@ -877,9 +868,6 @@ impl<F: Field> SstoreGasGadget<F> {
);

Self {
value,
value_prev,
original_value,
is_warm,
gas_cost,
value_eq_prev,
Expand All @@ -901,12 +889,6 @@ impl<F: Field> SstoreGasGadget<F> {
original_value: eth_types::Word,
is_warm: bool,
) -> Result<(), Error> {
self.value
.assign(region, offset, Some(value.to_le_bytes()))?;
self.value_prev
.assign(region, offset, Some(value_prev.to_le_bytes()))?;
self.original_value
.assign(region, offset, Some(original_value.to_le_bytes()))?;
ed255 marked this conversation as resolved.
Show resolved Hide resolved
self.is_warm
.assign(region, offset, Value::known(F::from(is_warm as u64)))?;
self.value_eq_prev.assign_value(
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
Loading