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

Commit 106ccd5

Browse files
authored
Fix codehash phase; fix l1 tx nonce (#741)
* fix sender_nonce vs tx_nonce handling inside begin tx * v0.5.8: fix codehash phase * fix code_hash * Revert "Update create.rs" This reverts commit fb86b74. * disable copy test for testool * fix doc
1 parent faeced2 commit 106ccd5

File tree

8 files changed

+48
-35
lines changed

8 files changed

+48
-35
lines changed

testool/src/statetest/executor.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,9 @@ pub fn run_test(
349349
zkevm_circuits::evm_circuit::witness::block_convert(&builder.block, &builder.code_db)
350350
.unwrap();
351351

352-
CircuitTestBuilder::<1, 1>::new_from_block(block).run();
352+
CircuitTestBuilder::<1, 1>::new_from_block(block)
353+
.copy_checks(None)
354+
.run();
353355
} else {
354356
geth_data.sign(&wallets);
355357

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,11 @@ mod test {
251251
}
252252
let mut ctb = CircuitTestBuilder::new_from_test_ctx(ctx);
253253
if !ok {
254-
ctb = ctb.evm_checks(Box::new(|prover, gate_rows, lookup_rows| {
254+
ctb = ctb.evm_checks(Some(Box::new(|prover, gate_rows, lookup_rows| {
255255
assert!(prover
256256
.verify_at_rows_par(gate_rows.iter().cloned(), lookup_rows.iter().cloned())
257257
.is_err())
258-
}));
258+
})));
259259
};
260260
ctb.run()
261261
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ impl<F: Field> ExecutionGadget<F> for CallOpGadget<F> {
856856
self.code_hash_previous.assign(
857857
region,
858858
offset,
859-
region.word_rlc(code_hash_previous),
859+
region.code_hash(code_hash_previous),
860860
)?;
861861
rw_offset += 2; // codehash read and write
862862
#[cfg(feature = "scroll")]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<F: Field, const IS_CREATE2: bool, const S: ExecutionState> ExecutionGadget<
8484
fn configure(cb: &mut EVMConstraintBuilder<F>) -> Self {
8585
// Use rw_counter of the step which triggers next call as its call_id.
8686
let callee_call_id = cb.curr.state.rw_counter.clone();
87-
let code_hash_previous = cb.query_cell_phase2();
87+
let code_hash_previous = cb.query_cell();
8888
#[cfg(feature = "scroll")]
8989
let keccak_code_hash_previous = cb.query_cell_phase2();
9090
let opcode = cb.query_cell();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ mod test {
149149
assert_eq!(block.txs[0].steps.len(), 5);
150150
block.txs[0].steps[2].gas_left -= 1;
151151
}))
152-
.evm_checks(Box::new(|prover, gate_rows, lookup_rows| {
152+
.evm_checks(Some(Box::new(|prover, gate_rows, lookup_rows| {
153153
assert!(prover
154154
.verify_at_rows_par(gate_rows.iter().cloned(), lookup_rows.iter().cloned())
155155
.is_err())
156-
}))
156+
})))
157157
.run();
158158
}
159159
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,11 @@ mod test {
193193

194194
let mut ctb = CircuitTestBuilder::new_from_test_ctx(ctx);
195195
if !ok {
196-
ctb = ctb.evm_checks(Box::new(|prover, gate_rows, lookup_rows| {
196+
ctb = ctb.evm_checks(Some(Box::new(|prover, gate_rows, lookup_rows| {
197197
assert!(prover
198198
.verify_at_rows_par(gate_rows.iter().cloned(), lookup_rows.iter().cloned())
199199
.is_err())
200-
}));
200+
})));
201201
};
202202
ctb.run()
203203
}

zkevm-circuits/src/evm_circuit/util.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,14 @@ impl<'r, 'b, F: FieldExt> CachedRegion<'r, 'b, F> {
199199
}
200200

201201
pub fn code_hash(&self, n: U256) -> Value<F> {
202-
self.challenges.evm_word().map(|r| {
203-
if cfg!(feature = "poseidon-codehash") {
204-
// only FieldExt is not enough for ToScalar trait so we have to make workaround
205-
rlc::value(&n.to_le_bytes(), F::from(256u64))
206-
} else {
207-
rlc::value(&n.to_le_bytes(), r)
208-
}
209-
})
202+
if cfg!(feature = "poseidon-codehash") {
203+
// only FieldExt is not enough for ToScalar trait so we have to make workaround
204+
Value::known(rlc::value(&n.to_le_bytes(), F::from(256u64)))
205+
} else {
206+
self.challenges
207+
.evm_word()
208+
.map(|r| rlc::value(&n.to_le_bytes(), r))
209+
}
210210
}
211211

212212
pub fn keccak_rlc(&self, le_bytes: &[u8]) -> Value<F> {

zkevm-circuits/src/test_util.rs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,16 @@ fn init_env_logger() {
7272
///
7373
/// CircuitTestBuilder::new_from_test_ctx(ctx)
7474
/// .block_modifier(Box::new(|block| block.circuits_params.max_evm_rows = (1 << 18) - 100))
75-
/// .state_checks(Box::new(|prover, evm_rows, lookup_rows| assert!(prover.verify_at_rows_par(evm_rows.iter().cloned(), lookup_rows.iter().cloned()).is_err())))
75+
/// .state_checks(Some(Box::new(|prover, evm_rows, lookup_rows| assert!(prover.verify_at_rows_par(evm_rows.iter().cloned(), lookup_rows.iter().cloned()).is_err()))))
7676
/// .run();
7777
/// ```
7878
pub struct CircuitTestBuilder<const NACC: usize, const NTX: usize> {
7979
test_ctx: Option<TestContext<NACC, NTX>>,
8080
circuits_params: Option<CircuitsParams>,
8181
block: Option<Block<Fr>>,
82-
evm_checks: Box<dyn Fn(MockProver<Fr>, &Vec<usize>, &Vec<usize>)>,
83-
state_checks: Box<dyn Fn(MockProver<Fr>, &Vec<usize>, &Vec<usize>)>,
84-
copy_checks: Box<dyn Fn(MockProver<Fr>, &Vec<usize>, &Vec<usize>)>,
82+
evm_checks: Option<Box<dyn Fn(MockProver<Fr>, &Vec<usize>, &Vec<usize>)>>,
83+
state_checks: Option<Box<dyn Fn(MockProver<Fr>, &Vec<usize>, &Vec<usize>)>>,
84+
copy_checks: Option<Box<dyn Fn(MockProver<Fr>, &Vec<usize>, &Vec<usize>)>>,
8585
block_modifiers: Vec<Box<dyn Fn(&mut Block<Fr>)>>,
8686
geth_data_modifiers: Vec<Box<dyn Fn(&mut GethData)>>,
8787
}
@@ -93,24 +93,24 @@ impl<const NACC: usize, const NTX: usize> CircuitTestBuilder<NACC, NTX> {
9393
test_ctx: None,
9494
circuits_params: None,
9595
block: None,
96-
evm_checks: Box::new(|prover, gate_rows, lookup_rows| {
96+
evm_checks: Some(Box::new(|prover, gate_rows, lookup_rows| {
9797
assert_eq!(prover.verify_at_rows_par(
9898
gate_rows.iter().cloned(),
9999
lookup_rows.iter().cloned(),
100100
), Ok(()));
101-
}),
102-
state_checks: Box::new(|prover, gate_rows, lookup_rows| {
101+
})),
102+
state_checks: Some(Box::new(|prover, gate_rows, lookup_rows| {
103103
assert_eq!(prover.verify_at_rows_par(
104104
gate_rows.iter().cloned(),
105105
lookup_rows.iter().cloned(),
106106
), Ok(()));
107-
}),
108-
copy_checks: Box::new(|prover, gate_rows, lookup_rows| {
107+
})),
108+
copy_checks: Some(Box::new(|prover, gate_rows, lookup_rows| {
109109
assert_eq!(prover.verify_at_rows_par(
110110
gate_rows.iter().cloned(),
111111
lookup_rows.iter().cloned(),
112112
), Ok(()));
113-
}),
113+
})),
114114
block_modifiers: vec![],
115115
geth_data_modifiers: vec![],
116116
}
@@ -157,7 +157,7 @@ impl<const NACC: usize, const NTX: usize> CircuitTestBuilder<NACC, NTX> {
157157
/// Circuit verification.
158158
pub fn state_checks(
159159
mut self,
160-
state_checks: Box<dyn Fn(MockProver<Fr>, &Vec<usize>, &Vec<usize>)>,
160+
state_checks: Option<Box<dyn Fn(MockProver<Fr>, &Vec<usize>, &Vec<usize>)>>,
161161
) -> Self {
162162
self.state_checks = state_checks;
163163
self
@@ -168,12 +168,23 @@ impl<const NACC: usize, const NTX: usize> CircuitTestBuilder<NACC, NTX> {
168168
/// Circuit verification.
169169
pub fn evm_checks(
170170
mut self,
171-
evm_checks: Box<dyn Fn(MockProver<Fr>, &Vec<usize>, &Vec<usize>)>,
171+
evm_checks: Option<Box<dyn Fn(MockProver<Fr>, &Vec<usize>, &Vec<usize>)>>,
172172
) -> Self {
173173
self.evm_checks = evm_checks;
174174
self
175175
}
176176

177+
#[allow(clippy::type_complexity)]
178+
/// Allows to provide checks different than the default ones for the Copy
179+
/// Circuit verification.
180+
pub fn copy_checks(
181+
mut self,
182+
copy_checks: Option<Box<dyn Fn(MockProver<Fr>, &Vec<usize>, &Vec<usize>)>>,
183+
) -> Self {
184+
self.copy_checks = copy_checks;
185+
self
186+
}
187+
177188
#[allow(clippy::type_complexity)]
178189
/// Allows to provide modifier functions for the [`Block`] that will be
179190
/// generated within this builder.
@@ -235,18 +246,18 @@ impl<const NACC: usize, const NTX: usize> CircuitTestBuilder<NACC, NTX> {
235246

236247
const NUM_BLINDING_ROWS: usize = 64;
237248
// Run evm circuit test
238-
{
249+
if let Some(evm_checks) = &self.evm_checks {
239250
let k = block.get_test_degree();
240251
let (active_gate_rows, active_lookup_rows) = EvmCircuit::<Fr>::get_active_rows(&block);
241252

242253
let circuit = EvmCircuit::get_test_cicuit_from_block(block.clone());
243254
let prover = MockProver::<Fr>::run(k, &circuit, vec![]).unwrap();
244255

245-
self.evm_checks.as_ref()(prover, &active_gate_rows, &active_lookup_rows)
256+
evm_checks(prover, &active_gate_rows, &active_lookup_rows)
246257
}
247258

248259
// Run state circuit test
249-
{
260+
if let Some(state_checks) = &self.state_checks {
250261
let rows_needed = StateCircuit::<Fr>::min_num_rows_block(&block).1;
251262
let k = log2_ceil(rows_needed + NUM_BLINDING_ROWS);
252263
let state_circuit = StateCircuit::<Fr>::new(block.rws.clone(), params.max_rws);
@@ -260,11 +271,11 @@ impl<const NACC: usize, const NTX: usize> CircuitTestBuilder<NACC, NTX> {
260271
.count();
261272
let rows = (params.max_rws - non_start_rows_len..params.max_rws).collect();
262273

263-
self.state_checks.as_ref()(prover, &rows, &rows);
274+
state_checks(prover, &rows, &rows);
264275
}
265276

266277
// Run copy circuit test
267-
{
278+
if let Some(copy_checks) = &self.copy_checks {
268279
let (active_rows, max_rows) = CopyCircuit::<Fr>::min_num_rows_block(&block);
269280
let k1 = block.get_test_degree();
270281
let k2 = log2_ceil(max_rows + NUM_BLINDING_ROWS);
@@ -274,7 +285,7 @@ impl<const NACC: usize, const NTX: usize> CircuitTestBuilder<NACC, NTX> {
274285
let prover = MockProver::<Fr>::run(k, &copy_circuit, instance).unwrap();
275286
let rows = (0..active_rows).collect();
276287

277-
self.copy_checks.as_ref()(prover, &rows, &rows);
288+
copy_checks(prover, &rows, &rows);
278289
}
279290
}
280291
}

0 commit comments

Comments
 (0)