Skip to content

Commit 1211b4a

Browse files
committed
Merge remote-tracking branch 'origin/develop' into feat/agg_recursion
2 parents 52c5f31 + ede39de commit 1211b4a

File tree

7 files changed

+112
-59
lines changed

7 files changed

+112
-59
lines changed

prover/src/zkevm/circuit/builder.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
use crate::{utils::read_env_var, zkevm::SubCircuitRowUsage};
22
use anyhow::{bail, Result};
3-
use bus_mapping::circuit_input_builder::{self, CircuitInputBuilder};
4-
use eth_types::{
5-
l2_types::BlockTrace,
6-
state_db::{CodeDB, StateDB},
7-
ToWord,
8-
};
3+
use bus_mapping::circuit_input_builder::CircuitInputBuilder;
4+
use eth_types::{l2_types::BlockTrace, ToWord};
95
use itertools::Itertools;
106
use mpt_zktrie::state::ZkTrieHash;
117
use std::sync::LazyLock;
@@ -104,11 +100,7 @@ pub fn validite_block_traces(block_traces: &[BlockTrace]) -> Result<()> {
104100

105101
pub fn dummy_witness_block() -> Result<Block> {
106102
log::debug!("generate dummy witness block");
107-
let builder_block = circuit_input_builder::Blocks::init(*CHAIN_ID, get_super_circuit_params());
108-
let mut builder: CircuitInputBuilder =
109-
CircuitInputBuilder::new(StateDB::new(), CodeDB::new(), &builder_block);
110-
builder.finalize_building()?;
111-
let witness_block = block_convert(&builder.block, &builder.code_db)?;
103+
let witness_block = zkevm_circuits::witness::dummy_witness_block(*CHAIN_ID);
112104
log::debug!("generate dummy witness block done");
113105
Ok(witness_block)
114106
}

zkevm-circuits/src/evm_circuit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ impl<F: Field> EvmCircuit<F> {
285285
}
286286
}
287287

288-
const FIXED_TABLE_ROWS_NO_BITWISE: usize = 3662;
288+
const FIXED_TABLE_ROWS_NO_BITWISE: usize = 3659;
289289
const FIXED_TABLE_ROWS: usize = FIXED_TABLE_ROWS_NO_BITWISE + 3 * 65536;
290290

291291
impl<F: Field> SubCircuit<F> for EvmCircuit<F> {

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

+44-32
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ pub(crate) struct ErrorOOGMemoryCopyGadget<F> {
4141
external_address: Word<F>,
4242

4343
addr_expansion_gadget: MemoryAddrExpandGadget<F>,
44+
// mcopy expansion
45+
memory_expansion_mcopy: MemoryExpansionGadget<F, 2, N_BYTES_MEMORY_WORD_SIZE>,
4446
// other kind(CALLDATACOPY, CODECOPY, EXTCODECOPY, RETURNDATACOPY) expansion
4547
memory_expansion_normal: MemoryExpansionGadget<F, 1, N_BYTES_MEMORY_WORD_SIZE>,
4648
memory_copier_gas: MemoryCopierGasGadget<F, { GasCost::COPY }>,
@@ -91,12 +93,16 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGMemoryCopyGadget<F> {
9193
cb.stack_pop(external_address.expr());
9294
});
9395

94-
let addr_expansion_gadget = MemoryAddrExpandGadget::construct(cb, is_mcopy.expr());
96+
let addr_expansion_gadget = MemoryAddrExpandGadget::construct(cb);
9597

9698
cb.stack_pop(addr_expansion_gadget.dst_memory_addr.offset_rlc());
9799
cb.stack_pop(addr_expansion_gadget.src_memory_addr.offset_rlc());
98100
cb.stack_pop(addr_expansion_gadget.dst_memory_addr.length_rlc());
99101

102+
// for mcopy
103+
let memory_expansion_mcopy =
104+
addr_expansion_gadget.build_memory_expansion_mcopy(cb, is_mcopy.expr());
105+
100106
// for others (CALLDATACOPY, CODECOPY, EXTCODECOPY, RETURNDATACOPY)
101107
let memory_expansion_normal = cb.condition(not::expr(is_mcopy.expr()), |cb| {
102108
MemoryExpansionGadget::construct(
@@ -107,7 +113,7 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGMemoryCopyGadget<F> {
107113

108114
let memory_expansion_cost = select::expr(
109115
is_mcopy.expr(),
110-
addr_expansion_gadget.memory_expansion_mcopy.gas_cost(),
116+
memory_expansion_mcopy.gas_cost(),
111117
memory_expansion_normal.gas_cost(),
112118
);
113119
let memory_copier_gas = MemoryCopierGasGadget::construct(
@@ -160,6 +166,7 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGMemoryCopyGadget<F> {
160166
tx_id,
161167
external_address,
162168
addr_expansion_gadget,
169+
memory_expansion_mcopy,
163170
memory_expansion_normal,
164171
memory_copier_gas,
165172
insufficient_gas,
@@ -227,13 +234,12 @@ impl<F: Field> ExecutionGadget<F> for ErrorOOGMemoryCopyGadget<F> {
227234
)?;
228235

229236
// assign memory_expansion_mcopy
230-
let (_, memory_expansion_cost_mcopy) =
231-
self.addr_expansion_gadget.memory_expansion_mcopy.assign(
232-
region,
233-
offset,
234-
step.memory_word_size(),
235-
[src_memory_addr, dst_memory_addr],
236-
)?;
237+
let (_, memory_expansion_cost_mcopy) = self.memory_expansion_mcopy.assign(
238+
region,
239+
offset,
240+
step.memory_word_size(),
241+
[src_memory_addr, dst_memory_addr],
242+
)?;
237243

238244
let memory_copier_gas = self.memory_copier_gas.assign(
239245
region,
@@ -291,33 +297,38 @@ pub(crate) struct MemoryAddrExpandGadget<F> {
291297
src_memory_addr: MemoryExpandedAddressGadget<F>,
292298
/// Destination offset and size to copy
293299
dst_memory_addr: MemoryExpandedAddressGadget<F>,
294-
// mcopy expansion
295-
memory_expansion_mcopy: MemoryExpansionGadget<F, 2, N_BYTES_MEMORY_WORD_SIZE>,
296300
}
297301

298302
// construct src_memory_addr, dst_memory_addr and memory_expansion_mcopy.
299303
impl<F: Field> MemoryAddrExpandGadget<F> {
300-
fn construct(cb: &mut EVMConstraintBuilder<F>, is_mcopy: Expression<F>) -> Self {
304+
fn construct(cb: &mut EVMConstraintBuilder<F>) -> Self {
301305
let dst_memory_addr = MemoryExpandedAddressGadget::construct_self(cb);
302306
// src can also be possible to overflow for mcopy.
303307
let src_memory_addr = MemoryExpandedAddressGadget::construct_self(cb);
304-
// for mcopy
305-
let memory_expansion_mcopy = cb.condition(is_mcopy.expr(), |cb| {
308+
Self {
309+
src_memory_addr,
310+
dst_memory_addr,
311+
}
312+
}
313+
fn build_memory_expansion_mcopy(
314+
&self,
315+
cb: &mut EVMConstraintBuilder<F>,
316+
is_mcopy: Expression<F>,
317+
) -> MemoryExpansionGadget<F, 2, N_BYTES_MEMORY_WORD_SIZE> {
318+
cb.condition(is_mcopy.expr(), |cb| {
306319
cb.require_equal(
307320
"mcopy src_address length == dst_address length",
308-
src_memory_addr.length_rlc(),
309-
dst_memory_addr.length_rlc(),
321+
self.src_memory_addr.length_rlc(),
322+
self.dst_memory_addr.length_rlc(),
310323
);
311324
MemoryExpansionGadget::construct(
312325
cb,
313-
[src_memory_addr.end_offset(), dst_memory_addr.end_offset()],
326+
[
327+
self.src_memory_addr.end_offset(),
328+
self.dst_memory_addr.end_offset(),
329+
],
314330
)
315-
});
316-
Self {
317-
src_memory_addr,
318-
dst_memory_addr,
319-
memory_expansion_mcopy,
320-
}
331+
})
321332
}
322333
}
323334

@@ -707,16 +718,21 @@ mod tests {
707718
#[derive(Clone)]
708719
struct ErrOOGMemoryCopyGadgetTestContainer<F> {
709720
gadget: MemoryAddrExpandGadget<F>,
721+
memory_expansion_mcopy: MemoryExpansionGadget<F, 2, N_BYTES_MEMORY_WORD_SIZE>,
710722
is_mcopy: Cell<F>,
711723
}
712724

713725
impl<F: Field> MathGadgetContainer<F> for ErrOOGMemoryCopyGadgetTestContainer<F> {
714726
fn configure_gadget_container(cb: &mut EVMConstraintBuilder<F>) -> Self {
715727
let is_mcopy = cb.query_cell();
716728
cb.require_boolean("is_mcopy is bool", is_mcopy.expr());
717-
let gadget = MemoryAddrExpandGadget::<F>::construct(cb, is_mcopy.expr());
718-
719-
ErrOOGMemoryCopyGadgetTestContainer { gadget, is_mcopy }
729+
let gadget = MemoryAddrExpandGadget::<F>::construct(cb);
730+
let memory_expansion_mcopy = gadget.build_memory_expansion_mcopy(cb, is_mcopy.expr());
731+
ErrOOGMemoryCopyGadgetTestContainer {
732+
gadget,
733+
memory_expansion_mcopy,
734+
is_mcopy,
735+
}
720736
}
721737

722738
fn assign_gadget_container(
@@ -743,12 +759,8 @@ mod tests {
743759
copy_size.into(),
744760
)?;
745761
// assign memory_expansion_mcopy
746-
self.gadget.memory_expansion_mcopy.assign(
747-
region,
748-
0,
749-
0,
750-
[src_memory_addr, dst_memory_addr],
751-
)?;
762+
self.memory_expansion_mcopy
763+
.assign(region, 0, 0, [src_memory_addr, dst_memory_addr])?;
752764
Ok(())
753765
}
754766
}

zkevm-circuits/src/evm_circuit/table.rs

+17-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{
55
util::Field,
66
};
77
use bus_mapping::{evm::OpcodeId, precompile::PrecompileCalls};
8+
use eth_types::forks::HardforkId;
89
use gadgets::util::Expr;
910
use halo2_proofs::plonk::Expression;
1011
use strum::IntoEnumIterator;
@@ -142,16 +143,22 @@ impl FixedTableTag {
142143
F::from(precompile.base_gas_cost().0),
143144
]
144145
})),
145-
Self::ChainFork => Box::new(eth_types::forks::hardfork_heights().into_iter().map(
146-
move |(fork, chain_id, height)| {
147-
[
148-
tag,
149-
F::from(fork as u64),
150-
F::from(chain_id),
151-
F::from(height),
152-
]
153-
},
154-
)),
146+
Self::ChainFork => Box::new(
147+
eth_types::forks::hardfork_heights()
148+
.into_iter()
149+
.filter(move |(f, _, _)| {
150+
// other fork info is not needed in circuit now.
151+
*f == HardforkId::Curie
152+
})
153+
.map(move |(fork, chain_id, height)| {
154+
[
155+
tag,
156+
F::from(fork as u64),
157+
F::from(chain_id),
158+
F::from(height),
159+
]
160+
}),
161+
),
155162
}
156163
}
157164
}

zkevm-circuits/src/super_circuit/test.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#![allow(unused_imports)]
2+
use crate::witness::dummy_witness_block;
3+
24
pub use super::*;
35
use bus_mapping::{
46
circuit_input_builder::CircuitInputBuilder,
@@ -7,13 +9,18 @@ use bus_mapping::{
79
precompile::PrecompileCalls,
810
};
911
use ethers_signers::{LocalWallet, Signer};
10-
use halo2_proofs::{dev::MockProver, halo2curves::bn256::Fr};
12+
use halo2_proofs::{
13+
dev::MockProver,
14+
halo2curves::bn256::{Bn256, Fr},
15+
plonk::keygen_vk,
16+
};
1117
use log::error;
1218
#[cfg(not(feature = "scroll"))]
1319
use mock::MOCK_DIFFICULTY;
1420
#[cfg(feature = "scroll")]
1521
use mock::MOCK_DIFFICULTY_L2GETH as MOCK_DIFFICULTY;
1622
use mock::{eth, TestContext, MOCK_CHAIN_ID};
23+
use params::ScrollSuperCircuit;
1724
use rand::SeedableRng;
1825
use rand_chacha::ChaCha20Rng;
1926
use std::env::set_var;
@@ -56,6 +63,27 @@ fn super_circuit_degree() {
5663
assert!(cs.degree() <= 9);
5764
}
5865

66+
// This circuit is used to prevent unexpected changes in circuit vk.
67+
// This test can run successfully now standalone `RUST_LOG=info cargo test --release --features=scroll super_circuit_vk -- --ignored`
68+
// but will fail in CI. I don't understand, may due to env var like COINBASE/DIFFICULT/KECCAK_ROWS?
69+
// So have to ignore it now.
70+
#[ignore = "enable this when we want to prevent unexpected changes in circuit"]
71+
#[test]
72+
fn super_circuit_vk() {
73+
use halo2_proofs::poly::kzg::commitment::ParamsKZG;
74+
let params = ParamsKZG::<Bn256>::unsafe_setup_with_s(20, Fr::from(1234u64));
75+
// chain_id is not related to vk, so we can use any value here.
76+
let chain_id = 534351;
77+
let circuit = ScrollSuperCircuit::new_from_block(&dummy_witness_block(chain_id));
78+
let vk = keygen_vk(&params, &circuit).unwrap();
79+
let protocol_hash = vk.transcript_repr();
80+
log::info!("transcript_repr {:?}", protocol_hash);
81+
assert_eq!(
82+
"0x1b3d158be8148c9e8ac9fce6eff2c576027c356ee1ff68ad7662d61556d5a7d7",
83+
format!("{:?}", protocol_hash)
84+
);
85+
}
86+
5987
#[cfg(feature = "scroll")]
6088
fn test_super_circuit<
6189
const MAX_TXS: usize,

zkevm-circuits/src/witness.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! used to generate witnesses for circuits.
44
55
mod block;
6-
pub use block::{block_convert, Block, BlockContext, BlockContexts};
6+
pub use block::{block_convert, dummy_witness_block, Block, BlockContext, BlockContexts};
77

88
/// Keccak witness
99
pub mod keccak;

zkevm-circuits/src/witness/block.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@ use crate::evm_circuit::{detect_fixed_table_tags, EvmCircuit};
77

88
use crate::{
99
evm_circuit::util::rlc,
10+
super_circuit::params::get_super_circuit_params,
1011
table::{BlockContextFieldTag, RwTableTag},
1112
util::{Field, SubCircuit},
1213
witness::keccak::keccak_inputs,
1314
};
1415
use bus_mapping::{
1516
circuit_input_builder::{
16-
self, BigModExp, CircuitsParams, CopyEvent, EcAddOp, EcMulOp, EcPairingOp, ExpEvent,
17-
PrecompileEvents, SHA256,
17+
self, BigModExp, CircuitInputBuilder, CircuitsParams, CopyEvent, EcAddOp, EcMulOp,
18+
EcPairingOp, ExpEvent, PrecompileEvents, SHA256,
1819
},
1920
Error,
2021
};
21-
use eth_types::{sign_types::SignData, Address, ToLittleEndian, Word, H256, U256};
22+
use eth_types::{
23+
sign_types::SignData,
24+
state_db::{CodeDB, StateDB},
25+
Address, ToLittleEndian, Word, H256, U256,
26+
};
2227
use halo2_proofs::{circuit::Value, halo2curves::bn256::Fr};
2328
use itertools::Itertools;
2429

@@ -611,3 +616,12 @@ pub fn block_convert(
611616
};
612617
Ok(block)
613618
}
619+
620+
/// Generate a empty witness block, which can be used for key-gen.
621+
pub fn dummy_witness_block(chain_id: u64) -> Block {
622+
let builder_block = circuit_input_builder::Blocks::init(chain_id, get_super_circuit_params());
623+
let mut builder: CircuitInputBuilder =
624+
CircuitInputBuilder::new(StateDB::new(), CodeDB::new(), &builder_block);
625+
builder.finalize_building().expect("should not fail");
626+
block_convert(&builder.block, &builder.code_db).expect("should not fail")
627+
}

0 commit comments

Comments
 (0)