Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

using real length in copy table lookup #565

Merged
merged 5 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
37 changes: 35 additions & 2 deletions zkevm-circuits/src/copy_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ impl<F: Field> SubCircuitConfig<F> for CopyCircuitConfig<F> {
let addr = copy_table.addr;
let src_addr_end = copy_table.src_addr_end;
let bytes_left = copy_table.bytes_left;
let real_bytes_left = copy_table.real_bytes_left;
let word_index = meta.advice_column();
let addr_slot = meta.advice_column();
let mask = meta.advice_column();
Expand Down Expand Up @@ -536,6 +537,27 @@ impl<F: Field> SubCircuitConfig<F> for CopyCircuitConfig<F> {
1.expr() - meta.query_advice(bytes_left, Rotation::cur()),
]),
);
cb.require_zero(
"real_bytes_left == 0 for last step",
and::expr([
meta.query_advice(is_last, Rotation::next()),
meta.query_advice(real_bytes_left, Rotation::cur()),
]),
);
cb.condition(
and::expr([
not::expr(meta.query_advice(is_last, Rotation::cur())),
not::expr(meta.query_advice(is_last, Rotation::next())),
]),
|cb| {
cb.require_equal(
"real_bytes_left[0] == real_bytes_left[2] + !mask",
meta.query_advice(real_bytes_left, Rotation::cur()),
meta.query_advice(real_bytes_left, Rotation(2))
+ not::expr(meta.query_advice(mask, Rotation::cur())),
);
},
);
cb.condition(
not::expr(meta.query_advice(is_last, Rotation::next()))
* (non_pad_non_mask.is_lt(meta, None)),
Expand Down Expand Up @@ -763,8 +785,12 @@ impl<F: Field> CopyCircuitConfig<F> {
.iter()
.zip_eq(table_row)
{
// Leave sr_addr_end and bytes_left unassigned when !is_read
if !is_read && (label == "src_addr_end" || label == "bytes_left") {
// Leave sr_addr_end and bytes_left and real_bytes_left unassigned when !is_read
if !is_read
&& (label == "src_addr_end"
|| label == "bytes_left"
|| label == "real_bytes_left")
{
} else {
region.assign_advice(
|| format!("{} at row: {}", label, offset),
Expand Down Expand Up @@ -1068,6 +1094,13 @@ impl<F: Field> CopyCircuitConfig<F> {
*offset,
|| Value::known(F::zero()),
)?;
// real_bytes_left
region.assign_advice(
|| format!("assign bytes_left {}", *offset),
self.copy_table.real_bytes_left,
*offset,
|| Value::known(F::zero()),
)?;
// value
region.assign_advice(
|| format!("assign value {}", *offset),
Expand Down
19 changes: 1 addition & 18 deletions zkevm-circuits/src/evm_circuit/execution/calldatacopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ pub(crate) struct CallDataCopyGadget<F> {
call_data_length: Cell<F>,
call_data_offset: Cell<F>, // Only used in the internal call
copy_rwc_inc: Cell<F>,
// include actual and padding to word bytes
bytes_length_word: Cell<F>,
memory_expansion: MemoryExpansionGadget<F, 1, N_BYTES_MEMORY_WORD_SIZE>,
memory_copier_gas: MemoryCopierGasGadget<F, { GasCost::COPY }>,
}
Expand All @@ -53,7 +51,6 @@ impl<F: Field> ExecutionGadget<F> for CallDataCopyGadget<F> {
let src_id = cb.query_cell();
let call_data_length = cb.query_cell();
let call_data_offset = cb.query_cell();
let bytes_length_word = cb.query_cell();

let length = cb.query_word_rlc();
let memory_offset = cb.query_cell_phase2();
Expand Down Expand Up @@ -135,7 +132,7 @@ impl<F: Field> ExecutionGadget<F> for CallDataCopyGadget<F> {
src_addr,
src_addr_end,
memory_address.offset(),
bytes_length_word.expr(),
memory_address.length(),
0.expr(), // for CALLDATACOPY rlc_acc is 0
copy_rwc_inc.expr(),
);
Expand Down Expand Up @@ -178,7 +175,6 @@ impl<F: Field> ExecutionGadget<F> for CallDataCopyGadget<F> {
call_data_length,
call_data_offset,
copy_rwc_inc,
bytes_length_word,
memory_expansion,
memory_copier_gas,
}
Expand Down Expand Up @@ -267,19 +263,6 @@ impl<F: Field> ExecutionGadget<F> for CallDataCopyGadget<F> {
),
)?;

let bytes_length_to_word = if call.is_root {
copy_rwc_inc * 32
} else {
// read/write
(copy_rwc_inc / 2) * 32
};

self.bytes_length_word.assign(
region,
offset,
Value::known(F::from(bytes_length_to_word)),
)?;

// Memory expansion
let (_, memory_expansion_gas_cost) = self.memory_expansion.assign(
region,
Expand Down
6 changes: 3 additions & 3 deletions zkevm-circuits/src/evm_circuit/execution/callop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ impl<F: Field> ExecutionGadget<F> for CallOpGadget<F> {
call_gadget.cd_address.offset(),
call_gadget.cd_address.address(),
0.expr(),
precompile_input_rws.expr() * 32.expr(),
call_gadget.cd_address.length(),
input_bytes_rlc.expr(),
precompile_input_rws.expr(), // reads
); // rwc_delta += `call_gadget.cd_address.length()` for precompile
Expand All @@ -398,7 +398,7 @@ impl<F: Field> ExecutionGadget<F> for CallOpGadget<F> {
0.expr(),
precompile_return_length.expr(),
0.expr(),
precompile_output_rws.expr() * 32.expr(),
precompile_return_length.expr(),
output_bytes_rlc.expr(),
precompile_output_rws.expr(), // writes.
); // rwc_delta += `precompile_return_length` for precompile
Expand Down Expand Up @@ -428,7 +428,7 @@ impl<F: Field> ExecutionGadget<F> for CallOpGadget<F> {
0.expr(),
return_data_copy_size.min(),
call_gadget.rd_address.offset(),
precompile_return_rws.expr() * 16.expr(),
return_data_copy_size.min(),
0.expr(),
precompile_return_rws.expr(), // writes
); // rwc_delta += `return_data_copy_size.min()` for precompile
Expand Down
14 changes: 1 addition & 13 deletions zkevm-circuits/src/evm_circuit/execution/codecopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ pub(crate) struct CodeCopyGadget<F> {
/// RW inverse counter from the copy table at the start of related copy
/// steps.
copy_rwc_inc: Cell<F>,
/// include actual and padding to word bytes
bytes_length_word: Cell<F>,
}

impl<F: Field> ExecutionGadget<F> for CodeCopyGadget<F> {
Expand All @@ -61,7 +59,6 @@ impl<F: Field> ExecutionGadget<F> for CodeCopyGadget<F> {
let size = cb.query_word_rlc();
let dst_memory_offset = cb.query_cell_phase2();
let code_offset = WordByteCapGadget::construct(cb, code_size.expr());
let bytes_length_word = cb.query_cell();

// Pop items from stack.
cb.stack_pop(dst_memory_offset.expr());
Expand Down Expand Up @@ -104,7 +101,7 @@ impl<F: Field> ExecutionGadget<F> for CodeCopyGadget<F> {
src_addr,
code_size.expr(),
dst_memory_addr.offset(),
bytes_length_word.expr(),
dst_memory_addr.length(),
0.expr(), // for CODECOPY, rlc_acc is 0
copy_rwc_inc.expr(),
);
Expand Down Expand Up @@ -140,7 +137,6 @@ impl<F: Field> ExecutionGadget<F> for CodeCopyGadget<F> {
memory_expansion,
memory_copier_gas,
copy_rwc_inc,
bytes_length_word,
}
}

Expand Down Expand Up @@ -211,14 +207,6 @@ impl<F: Field> ExecutionGadget<F> for CodeCopyGadget<F> {
),
)?;

let bytes_length_to_word = copy_rwc_inc * 32;

self.bytes_length_word.assign(
region,
offset,
Value::known(F::from(bytes_length_to_word)),
)?;

Ok(())
}
}
Expand Down
14 changes: 1 addition & 13 deletions zkevm-circuits/src/evm_circuit/execution/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ pub(crate) struct CreateGadget<F, const IS_CREATE2: bool, const S: ExecutionStat
// if code_hash_previous is zero, then no collision
not_address_collision: IsZeroGadget<F>,
copy_rwc_inc: Cell<F>,
/// include actual and padding to word bytes
bytes_length_word: Cell<F>,
}

impl<F: Field, const IS_CREATE2: bool, const S: ExecutionState> ExecutionGadget<F>
Expand All @@ -86,7 +84,6 @@ impl<F: Field, const IS_CREATE2: bool, const S: ExecutionState> ExecutionGadget<
let code_hash_previous = cb.query_cell();
let opcode = cb.query_cell();
let copy_rwc_inc = cb.query_cell();
let bytes_length_word = cb.query_cell();

cb.opcode_lookup(opcode.expr(), 1.expr());

Expand Down Expand Up @@ -160,8 +157,7 @@ impl<F: Field, const IS_CREATE2: bool, const S: ExecutionState> ExecutionGadget<
init_code.offset(),
init_code.address(),
0.expr(),
//init_code.length(),
bytes_length_word.expr(),
init_code.length(),
init_code_rlc.expr(),
//init_code.length(),
copy_rwc_inc.expr(),
Expand Down Expand Up @@ -510,7 +506,6 @@ impl<F: Field, const IS_CREATE2: bool, const S: ExecutionState> ExecutionGadget<
code_hash_previous,
not_address_collision,
copy_rwc_inc,
bytes_length_word,
}
}

Expand Down Expand Up @@ -779,13 +774,6 @@ impl<F: Field, const IS_CREATE2: bool, const S: ExecutionState> ExecutionGadget<
),
)?;

let bytes_length_to_word = copy_rwc_inc * 32;
self.bytes_length_word.assign(
region,
offset,
Value::known(F::from(bytes_length_to_word)),
)?;

Ok(())
}
}
Expand Down
13 changes: 1 addition & 12 deletions zkevm-circuits/src/evm_circuit/execution/extcodecopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ pub(crate) struct ExtcodecopyGadget<F> {
code_hash: Cell<F>,
code_size: Cell<F>,
copy_rwc_inc: Cell<F>,
/// include actual and padding to word bytes
bytes_length_word: Cell<F>,
memory_expansion: MemoryExpansionGadget<F, 1, N_BYTES_MEMORY_WORD_SIZE>,
memory_copier_gas: MemoryCopierGasGadget<F, { GasCost::COPY }>,
}
Expand All @@ -57,7 +55,6 @@ impl<F: Field> ExecutionGadget<F> for ExtcodecopyGadget<F> {
from_bytes::expr(&external_address_word.cells[..N_BYTES_ACCOUNT_ADDRESS]);

let code_size = cb.query_cell();
let bytes_length_word = cb.query_cell();

let memory_length = cb.query_word_rlc();
let memory_offset = cb.query_cell_phase2();
Expand Down Expand Up @@ -122,7 +119,7 @@ impl<F: Field> ExecutionGadget<F> for ExtcodecopyGadget<F> {
src_addr,
code_size.expr(),
memory_address.offset(),
bytes_length_word.expr(),
memory_address.length(),
0.expr(),
copy_rwc_inc.expr(),
);
Expand Down Expand Up @@ -159,7 +156,6 @@ impl<F: Field> ExecutionGadget<F> for ExtcodecopyGadget<F> {
code_hash,
code_size,
copy_rwc_inc,
bytes_length_word,
memory_expansion,
memory_copier_gas,
}
Expand Down Expand Up @@ -237,13 +233,6 @@ impl<F: Field> ExecutionGadget<F> for ExtcodecopyGadget<F> {
),
)?;

let bytes_length_to_word = copy_rwc_inc * 32;
self.bytes_length_word.assign(
region,
offset,
Value::known(F::from(bytes_length_to_word)),
)?;

let (_, memory_expansion_gas_cost) = self.memory_expansion.assign(
region,
offset,
Expand Down
14 changes: 1 addition & 13 deletions zkevm-circuits/src/evm_circuit/execution/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ pub(crate) struct LogGadget<F> {
is_persistent: Cell<F>,
tx_id: Cell<F>,
copy_rwc_inc: Cell<F>,
/// include actual and padding to word bytes
bytes_length_word: Cell<F>,

memory_expansion: MemoryExpansionGadget<F, 1, N_BYTES_MEMORY_WORD_SIZE>,
}
Expand All @@ -56,7 +54,6 @@ impl<F: Field> ExecutionGadget<F> for LogGadget<F> {
fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
let mstart_word = WordByteRangeGadget::construct(cb);
let msize = cb.query_word_rlc();
let bytes_length_word = cb.query_cell();

// Pop mstart_address, msize from stack
cb.stack_pop(mstart_word.original_word());
Expand Down Expand Up @@ -156,8 +153,7 @@ impl<F: Field> ExecutionGadget<F> for LogGadget<F> {
memory_address.offset(),
memory_address.address(),
dst_addr,
//memory_address.length(),
bytes_length_word.expr(),
memory_address.length(),
0.expr(), // for LOGN, rlc_acc is 0
copy_rwc_inc.expr(),
);
Expand Down Expand Up @@ -198,7 +194,6 @@ impl<F: Field> ExecutionGadget<F> for LogGadget<F> {
is_persistent,
tx_id,
copy_rwc_inc,
bytes_length_word,
memory_expansion,
}
}
Expand Down Expand Up @@ -289,13 +284,6 @@ impl<F: Field> ExecutionGadget<F> for LogGadget<F> {
),
)?;

let bytes_length_to_word = copy_rwc_inc * 32;
self.bytes_length_word.assign(
region,
offset,
Value::known(F::from(bytes_length_to_word)),
)?;

Ok(())
}
}
Expand Down
14 changes: 2 additions & 12 deletions zkevm-circuits/src/evm_circuit/execution/return_revert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ pub(crate) struct ReturnRevertGadget<F> {
caller_id: Cell<F>,
address: Cell<F>,
reversion_info: ReversionInfo<F>,
/// include actual and padding to word bytes
bytes_length_word: Cell<F>,
}

impl<F: Field> ExecutionGadget<F> for ReturnRevertGadget<F> {
Expand All @@ -61,7 +59,6 @@ impl<F: Field> ExecutionGadget<F> for ReturnRevertGadget<F> {

fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
let opcode = cb.query_cell();
let bytes_length_word = cb.query_cell();

cb.opcode_lookup(opcode.expr(), 1.expr());

Expand Down Expand Up @@ -133,8 +130,7 @@ impl<F: Field> ExecutionGadget<F> for ReturnRevertGadget<F> {
range.offset(),
range.address(),
0.expr(),
//range.length(),
bytes_length_word.expr(),
range.length(),
init_code_rlc.expr(),
copy_rw_increase.expr(),
);
Expand Down Expand Up @@ -260,12 +256,9 @@ impl<F: Field> ExecutionGadget<F> for ReturnRevertGadget<F> {
range.offset(),
range.address(),
return_data_offset.expr(),
//copy_length.min(),
bytes_length_word.expr(),
//64.expr(),
copy_length.min(),
0.expr(),
copy_rw_increase.expr(),
//4.expr(),
);
},
);
Expand Down Expand Up @@ -297,7 +290,6 @@ impl<F: Field> ExecutionGadget<F> for ReturnRevertGadget<F> {
address,
caller_id,
reversion_info,
bytes_length_word,
}
}

Expand Down Expand Up @@ -415,8 +407,6 @@ impl<F: Field> ExecutionGadget<F> for ReturnRevertGadget<F> {
.assign(region, offset, Value::known(F::from(copy_rw_increase)))?;
self.copy_rw_increase_is_zero
.assign(region, offset, F::from(copy_rw_increase))?;
self.bytes_length_word
.assign(region, offset, Value::known(F::from(copy_rwc_inc * 32)))?;

let is_contract_deployment = call.is_create && call.is_success && !length.is_zero();
if !call.is_root {
Expand Down
Loading