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

Commit 828497a

Browse files
authored
Merge branch 'memory_opt' into mem-opt/fix-precompiled-call-test-case
2 parents 729bb3b + ea76c6e commit 828497a

File tree

6 files changed

+82
-75
lines changed

6 files changed

+82
-75
lines changed

bus-mapping/src/circuit_input_builder/execution.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -255,22 +255,19 @@ impl CopyEvent {
255255
pub fn rw_counter_step(&self, step_index: usize) -> u64 {
256256
let mut rw_counter = u64::try_from(self.rw_counter_start.0).unwrap();
257257
let mut rw_counter_increase = self.rw_counter_increase(step_index);
258-
let mut rw_counter_increase_log = 0u64;
259-
if self.dst_type == CopyDataType::TxLog {
260-
rw_counter_increase_log = self.rw_counter_increase_log(step_index);
261-
rw_counter_increase =
262-
rw_counter_increase - rw_counter_increase_log + rw_counter_increase_log / 2
263-
}
258+
// let mut rw_counter_increase_log = 0u64;
259+
// if self.dst_type == CopyDataType::TxLog {
260+
// rw_counter_increase_log = self.rw_counter_increase_log(step_index);
261+
// rw_counter_increase =
262+
// rw_counter_increase - rw_counter_increase_log + rw_counter_increase_log / 2
263+
// }
264264
rw_counter = rw_counter + rw_counter_increase;
265265

266-
// step_index == self.bytes.len() when caculate total rw increasing.
266+
// // step_index == self.bytes.len() when caculate total rw increasing.
267267
if self.dst_type == CopyDataType::TxLog && step_index != self.bytes.len() * 2 {
268-
if step_index % 2 == 0 {
269-
// memory reading
270-
rw_counter = rw_counter - rw_counter_increase + step_index as u64 / 2 / 32;
271-
} else {
268+
if step_index % 32 == 31 && step_index % 2 == 1 {
272269
// log writing
273-
rw_counter = rw_counter + self.bytes.len() as u64 / 32;
270+
rw_counter = rw_counter + 1;
274271
}
275272
}
276273

@@ -282,9 +279,9 @@ impl CopyEvent {
282279
if self.rw_counter_step(self.bytes.len() * 2) < self.rw_counter_step(step_index) {
283280
let rw_counter = self.rw_counter_step(self.bytes.len() * 2);
284281
let rw_prevous = self.rw_counter_step(step_index);
285-
return 0u64;
282+
panic!("prev rw_counter_step > total tw_counter");
286283
}
287-
284+
// self.rw_counter_step(self.bytes.len() * 2) - self.rw_counter_step(step_index)
288285
self.rw_counter_step(self.bytes.len() * 2) - self.rw_counter_step(step_index)
289286
}
290287

@@ -303,7 +300,7 @@ impl CopyEvent {
303300
let destination_rw_increase = match self.dst_type {
304301
CopyDataType::RlcAcc | CopyDataType::Bytecode => 0,
305302
CopyDataType::Memory => (step_index as u64 / 2) / 32,
306-
CopyDataType::TxLog => u64::try_from(step_index).unwrap() / 2,
303+
CopyDataType::TxLog => u64::try_from(step_index).unwrap() / 2 / 32,
307304
CopyDataType::TxCalldata | CopyDataType::Padding => unreachable!(),
308305
};
309306
source_rw_increase + destination_rw_increase

bus-mapping/src/circuit_input_builder/input_state_ref.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,7 +1772,17 @@ impl<'a> CircuitInputStateRef<'a> {
17721772
// memory word writes to destination word
17731773
for chunk in log_slot_bytes.chunks(32) {
17741774
let dest_word = Word::from_big_endian(&chunk);
1775+
// read memory
17751776
self.memory_read_word(exec_step, chunk_index.into(), dest_word)?;
1777+
// write log
1778+
self.tx_log_write(
1779+
exec_step,
1780+
self.tx_ctx.id(),
1781+
self.tx_ctx.log_id + 1,
1782+
TxLogField::Data,
1783+
(chunk_index - dst_begin_slot) as usize,
1784+
dest_word,
1785+
)?;
17761786
chunk_index = chunk_index + 32;
17771787
}
17781788

@@ -1791,15 +1801,6 @@ impl<'a> CircuitInputStateRef<'a> {
17911801
first_set = false;
17921802
}
17931803
copy_steps.push((value, false, false));
1794-
// Write log
1795-
self.tx_log_write(
1796-
exec_step,
1797-
self.tx_ctx.id(),
1798-
self.tx_ctx.log_id + 1,
1799-
TxLogField::Data,
1800-
idx - copy_start as usize,
1801-
Word::from(value),
1802-
)?;
18031804
}
18041805
}
18051806

zkevm-circuits/src/copy_circuit.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,13 @@ impl<F: Field> SubCircuitConfig<F> for CopyCircuitConfig<F> {
232232
tag.value(Rotation::cur())(meta),
233233
tag.value(Rotation(2))(meta),
234234
);
235-
cb.require_equal(
236-
"rows[0].addr + 1 == rows[2].addr",
237-
meta.query_advice(addr, Rotation::cur()) + 1.expr(),
238-
meta.query_advice(addr, Rotation(2)),
239-
);
235+
// not work for this tx log, log index use addr_slot as index
236+
// TODO: handle tx log case
237+
// cb.require_equal(
238+
// "rows[0].addr + 1 == rows[2].addr",
239+
// meta.query_advice(addr, Rotation::cur()) + 1.expr(),
240+
// meta.query_advice(addr, Rotation(2)),
241+
// );
240242
cb.require_equal(
241243
"rows[0].src_addr_end == rows[2].src_addr_end for non-last step",
242244
meta.query_advice(src_addr_end, Rotation::cur()),
@@ -419,7 +421,8 @@ impl<F: Field> SubCircuitConfig<F> for CopyCircuitConfig<F> {
419421
meta.lookup_any("TxLog lookup", |meta| {
420422
let cond = meta.query_fixed(q_enable, Rotation::cur())
421423
* tag.value_equals(CopyDataType::TxLog, Rotation::cur())(meta)
422-
* not::expr(meta.query_advice(mask, Rotation::cur()));
424+
//* not::expr(meta.query_advice(mask, Rotation::cur()));
425+
* not::expr(is_word_index_end.is_lt(meta, None));
423426

424427
vec![
425428
meta.query_advice(rw_counter, Rotation::cur()),
@@ -429,7 +432,8 @@ impl<F: Field> SubCircuitConfig<F> for CopyCircuitConfig<F> {
429432
meta.query_advice(addr, Rotation::cur()), // byte_index || field_tag || log_id
430433
0.expr(),
431434
0.expr(),
432-
meta.query_advice(value, Rotation::cur()),
435+
meta.query_advice(value_wrod_rlc, Rotation::cur()),
436+
//meta.query_advice(value, Rotation::cur()),
433437
0.expr(),
434438
0.expr(),
435439
0.expr(),
@@ -1178,7 +1182,7 @@ mod tests {
11781182
block.clone(),
11791183
CircuitsParams {
11801184
max_rws: 2000,
1181-
max_copy_rows: 0x200 * 2 + 2,
1185+
max_copy_rows: 0x200 * 2 + 200,
11821186
..Default::default()
11831187
},
11841188
)

zkevm-circuits/src/evm_circuit/execution/logs.rs

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,11 @@ impl<F: Field> ExecutionGadget<F> for LogGadget<F> {
273273
let memory_start_slot = memory_start.low_u64() - shift;
274274
let memory_end = memory_start.low_u64() + msize.low_u64();
275275
let memory_end_slot = memory_end - memory_end % 32;
276-
let copy_rwc_inc = (memory_end_slot - memory_start_slot) / 32 + msize.low_u64();
276+
let copy_rwc_inc = if msize.low_u64() == 0 || !call.is_persistent {
277+
0
278+
} else {
279+
((memory_end_slot - memory_start_slot) / 32 + 1)
280+
};
277281

278282
self.copy_rwc_inc.assign(
279283
region,
@@ -285,7 +289,7 @@ impl<F: Field> ExecutionGadget<F> for LogGadget<F> {
285289
),
286290
)?;
287291

288-
let bytes_length_to_word = (copy_rwc_inc - msize.low_u64() + 1) * 32;
292+
let bytes_length_to_word = copy_rwc_inc * 32;
289293

290294
self.bytes_length_word.assign(
291295
region,
@@ -310,43 +314,43 @@ mod test {
310314
// zero topic: log0
311315
test_log_ok(&[], true, None);
312316
// one topic: log1
313-
//test_log_ok(&[Word::from(0xA0)], true, None);
314-
// // two topics: log2
315-
// test_log_ok(&[Word::from(0xA0), Word::from(0xef)], true, None);
316-
// // three topics: log3
317-
// test_log_ok(
318-
// &[Word::from(0xA0), Word::from(0xef), Word::from(0xb0)],
319-
// true,
320-
// None,
321-
// );
322-
// // four topics: log4
323-
// test_log_ok(
324-
// &[
325-
// Word::from(0xA0),
326-
// Word::from(0xef),
327-
// Word::from(0xb0),
328-
// Word::from(0x37),
329-
// ],
330-
// true,
331-
// None,
332-
// );
333-
334-
// // 2. tests for is_persistent = false cases
335-
// // log0
336-
// test_log_ok(&[], false, None);
337-
// // log1
338-
// test_log_ok(&[Word::from(0xA0)], false, None);
339-
// // log4
340-
// test_log_ok(
341-
// &[
342-
// Word::from(0xA0),
343-
// Word::from(0xef),
344-
// Word::from(0xb0),
345-
// Word::from(0x37),
346-
// ],
347-
// false,
348-
// None,
349-
// );
317+
test_log_ok(&[Word::from(0xA0)], true, None);
318+
// two topics: log2
319+
test_log_ok(&[Word::from(0xA0), Word::from(0xef)], true, None);
320+
// three topics: log3
321+
test_log_ok(
322+
&[Word::from(0xA0), Word::from(0xef), Word::from(0xb0)],
323+
true,
324+
None,
325+
);
326+
// four topics: log4
327+
test_log_ok(
328+
&[
329+
Word::from(0xA0),
330+
Word::from(0xef),
331+
Word::from(0xb0),
332+
Word::from(0x37),
333+
],
334+
true,
335+
None,
336+
);
337+
338+
// 2. tests for is_persistent = false cases
339+
// log0
340+
test_log_ok(&[], false, None);
341+
// log1
342+
test_log_ok(&[Word::from(0xA0)], false, None);
343+
// log4
344+
test_log_ok(
345+
&[
346+
Word::from(0xA0),
347+
Word::from(0xef),
348+
Word::from(0xb0),
349+
Word::from(0x37),
350+
],
351+
false,
352+
None,
353+
);
350354
}
351355

352356
#[test]

zkevm-circuits/src/table.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use halo2_proofs::{
2626
plonk::{Advice, Any, Column, ConstraintSystem, Error, Expression, Fixed, VirtualCells},
2727
poly::Rotation,
2828
};
29+
use rand::seq::index;
2930
use std::{io::copy, iter::repeat};
3031

3132
#[cfg(feature = "onephase")]
@@ -1404,7 +1405,6 @@ impl CopyTable {
14041405
{
14051406
if (step_idx / 2) % 32 == 0 && step_idx != 0 {
14061407
// reset
1407-
//println!("value_word_rlc {:?} of step {} ", value_word_rlc, step_idx);
14081408
value_word_rlc = Value::known(F::zero());
14091409
value_word_rlc = value_word_rlc * challenges.evm_word()
14101410
+ Value::known(F::from(copy_step.value as u64));
@@ -1441,7 +1441,7 @@ impl CopyTable {
14411441
let addr = if tag == CopyDataType::TxLog {
14421442
Value::known(
14431443
build_tx_log_address(
1444-
copy_step_addr,
1444+
addr_slot,
14451445
TxLogFieldTag::Data,
14461446
copy_event.log_id.unwrap(),
14471447
)
@@ -1472,8 +1472,8 @@ impl CopyTable {
14721472
let rw_count = F::from(copy_event.rw_counter_step(step_idx));
14731473
let rwc_inc_left = F::from(copy_event.rw_counter_increase_left(step_idx));
14741474
println!(
1475-
"step_idx: {} , rw_count {:?}, tag {:?}, addr {:?} rwc_inc_left {:?} word_index {:?}, ",
1476-
step_idx, rw_count, tag, addr, rwc_inc_left, word_index,
1475+
"step_idx: {},rw_count {:?}, tag {:?}, addr_slot {} addr {:?} word_index {:?}, value_word_rlc {:?}",
1476+
step_idx, rw_count, tag, addr_slot, addr, word_index, value_word_rlc,
14771477
);
14781478

14791479
// is_code

zkevm-circuits/src/witness/rw.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@ impl Rw {
655655
field_tag, value, ..
656656
} => match field_tag {
657657
TxLogFieldTag::Topic => rlc::value(&value.to_le_bytes(), randomness),
658+
TxLogFieldTag::Data => rlc::value(&value.to_le_bytes(), randomness),
658659
_ => value.to_scalar().unwrap(),
659660
},
660661

0 commit comments

Comments
 (0)