diff --git a/.github/release-please/manifest.json b/.github/release-please/manifest.json index ae82647..e5606aa 100644 --- a/.github/release-please/manifest.json +++ b/.github/release-please/manifest.json @@ -1,3 +1,3 @@ { - ".": "0.150.9" + ".": "0.150.12" } diff --git a/CHANGELOG.md b/CHANGELOG.md index 4621e14..52d3fb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,26 @@ # Changelog +## [0.150.12](https://github.com/matter-labs/zksync-protocol/compare/v0.150.11...v0.150.12) (2024-11-06) + + +### Bug Fixes + +* Compression setup generation ([#63](https://github.com/matter-labs/zksync-protocol/issues/63)) ([a17c0c0](https://github.com/matter-labs/zksync-protocol/commit/a17c0c0425e3c3c13f7c546d7c3f58ef264a502a)) + +## [0.150.11](https://github.com/matter-labs/zksync-protocol/compare/v0.150.10...v0.150.11) (2024-10-31) + + +### Features + +* Generate light setup keys ([#56](https://github.com/matter-labs/zksync-protocol/issues/56)) ([587aaa1](https://github.com/matter-labs/zksync-protocol/commit/587aaa1530e0f44300530865a01777c42a3b1d85)) + +## [0.150.10](https://github.com/matter-labs/zksync-protocol/compare/v0.150.9...v0.150.10) (2024-10-31) + + +### Miscellaneous Chores + +* bump crypto deps ([#59](https://github.com/matter-labs/zksync-protocol/issues/59)) ([e4d42e2](https://github.com/matter-labs/zksync-protocol/commit/e4d42e2ab1ff9c3f7767a1515f8407bb651c106f)) + ## [0.150.9](https://github.com/matter-labs/zksync-protocol/compare/v0.150.8...v0.150.9) (2024-10-29) diff --git a/Cargo.toml b/Cargo.toml index f55e8f5..597dd27 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ resolver = "2" [workspace.package] # All the packages in the workspace should have the same version -version = "0.150.9" +version = "0.150.12" edition = "2021" authors = ["The Matter Labs Team "] homepage = "https://zksync.io/" @@ -16,20 +16,20 @@ keywords = ["blockchain", "zksync"] categories = ["cryptography"] [workspace.dependencies] -circuit_definitions = { version = "=0.150.9", path = "crates/circuit_definitions" } -circuit_encodings = { version = "=0.150.9", path = "crates/circuit_encodings" } -circuit_sequencer_api = { version = "=0.150.9", path = "crates/circuit_sequencer_api" } -kzg = { version = "=0.150.9", path = "crates/kzg", package = "zksync_kzg" } -zk_evm = { version = "=0.150.9", path = "crates/zk_evm" } -zk_evm_abstractions = { version = "=0.150.9", path = "crates/zk_evm_abstractions" } -zkevm_circuits = { version = "=0.150.9", path = "crates/zkevm_circuits" } -zkevm_opcode_defs = { version = "=0.150.9", path = "crates/zkevm_opcode_defs" } -zkevm_test_harness = { version = "=0.150.9", path = "crates/zkevm_test_harness" } -zkevm-assembly = { version = "=0.150.9", path = "crates/zkEVM-assembly" } +circuit_definitions = { version = "=0.150.12", path = "crates/circuit_definitions" } +circuit_encodings = { version = "=0.150.12", path = "crates/circuit_encodings" } +circuit_sequencer_api = { version = "=0.150.12", path = "crates/circuit_sequencer_api" } +kzg = { version = "=0.150.12", path = "crates/kzg", package = "zksync_kzg" } +zk_evm = { version = "=0.150.12", path = "crates/zk_evm" } +zk_evm_abstractions = { version = "=0.150.12", path = "crates/zk_evm_abstractions" } +zkevm_circuits = { version = "=0.150.12", path = "crates/zkevm_circuits" } +zkevm_opcode_defs = { version = "=0.150.12", path = "crates/zkevm_opcode_defs" } +zkevm_test_harness = { version = "=0.150.12", path = "crates/zkevm_test_harness" } +zkevm-assembly = { version = "=0.150.12", path = "crates/zkEVM-assembly" } # `zksync-crypto` repository -snark_wrapper = "=0.30.3" -bellman = { package = "zksync_bellman", version = "=0.30.3" } -boojum = "=0.30.3" -cs_derive = { package = "zksync_cs_derive", version = "=0.30.3" } +snark_wrapper = "=0.30.6" +bellman = { package = "zksync_bellman", version = "=0.30.6" } +boojum = "=0.30.6" +cs_derive = { package = "zksync_cs_derive", version = "=0.30.6" } diff --git a/crates/zkevm_test_harness/src/compute_setups.rs b/crates/zkevm_test_harness/src/compute_setups.rs deleted file mode 100644 index 1073673..0000000 --- a/crates/zkevm_test_harness/src/compute_setups.rs +++ /dev/null @@ -1,725 +0,0 @@ -//! Functions to compute setup and verification keys for different circuit types. - -use std::sync::Arc; - -use crate::zkevm_circuits::recursion::leaf_layer::input::RecursionLeafParametersWitness; -use crate::zkevm_circuits::recursion::NUM_BASE_LAYER_CIRCUITS; -use circuit_definitions::boojum::gadgets::traits::allocatable::CSAllocatable; -use circuit_definitions::{ - circuit_definitions::{ - base_layer::{ - ZkSyncBaseLayerCircuit, ZkSyncBaseLayerFinalizationHint, ZkSyncBaseLayerVerificationKey, - }, - ZkSyncUniformCircuitInstance, - }, - recursion_layer_proof_config, - zkevm_circuits::eip_4844::input::ELEMENTS_PER_4844_BLOCK, - zkevm_circuits::scheduler::aux::BaseLayerCircuitType, - RECURSION_LAYER_CAP_SIZE, RECURSION_LAYER_FRI_LDE_FACTOR, -}; - -use crossbeam::atomic::AtomicCell; -use rayon::iter::{IntoParallelIterator, ParallelIterator}; -use rayon::ThreadPoolBuilder; - -use self::toolset::GeometryConfig; - -use super::*; -use crate::boojum::{ - algebraic_props::{round_function::AbsorptionModeOverwrite, sponge::GoldilocksPoseidon2Sponge}, - cs::{ - implementations::{ - hints::{DenseVariablesCopyHint, DenseWitnessCopyHint}, - polynomial_storage::{SetupBaseStorage, SetupStorage}, - setup::FinalizationHintsForProver, - verifier::VerificationKey, - }, - oracle::merkle_tree::MerkleTreeWithCap, - }, - worker::Worker, -}; - -use crate::data_source::SetupDataSource; -use crate::zkevm_circuits::base_structures::vm_state::FULL_SPONGE_QUEUE_STATE_WIDTH; - -use circuit_definitions::circuit_definitions::recursion_layer::leaf_layer::*; -use circuit_definitions::circuit_definitions::recursion_layer::*; -use circuit_definitions::{BASE_LAYER_CAP_SIZE, BASE_LAYER_FRI_LDE_FACTOR}; -use std::collections::VecDeque; - -use crate::prover_utils::*; - -/// Returns all types of basic circuits, with empty witnesses. -/// Can be used for things like verification key generation. -fn get_all_basic_circuits(geometry: &GeometryConfig) -> Vec { - vec![ - ZkSyncBaseLayerCircuit::MainVM(ZkSyncUniformCircuitInstance { - witness: AtomicCell::new(None), - config: Arc::new(geometry.cycles_per_vm_snapshot as usize), - round_function: Arc::new(Poseidon2Goldilocks), - expected_public_input: None, - }), - ZkSyncBaseLayerCircuit::CodeDecommittmentsSorter(ZkSyncUniformCircuitInstance { - witness: AtomicCell::new(None), - config: Arc::new(geometry.cycles_code_decommitter_sorter as usize), - round_function: Arc::new(Poseidon2Goldilocks), - expected_public_input: None, - }), - ZkSyncBaseLayerCircuit::CodeDecommitter(ZkSyncUniformCircuitInstance { - witness: AtomicCell::new(None), - - config: Arc::new(geometry.cycles_per_code_decommitter as usize), - round_function: Arc::new(Poseidon2Goldilocks), - expected_public_input: None, - }), - ZkSyncBaseLayerCircuit::LogDemuxer(ZkSyncUniformCircuitInstance { - witness: AtomicCell::new(None), - config: Arc::new(geometry.cycles_per_log_demuxer as usize), - round_function: Arc::new(Poseidon2Goldilocks), - expected_public_input: None, - }), - ZkSyncBaseLayerCircuit::KeccakRoundFunction(ZkSyncUniformCircuitInstance { - witness: AtomicCell::new(None), - config: Arc::new(geometry.cycles_per_keccak256_circuit as usize), - round_function: Arc::new(Poseidon2Goldilocks), - expected_public_input: None, - }), - ZkSyncBaseLayerCircuit::Sha256RoundFunction(ZkSyncUniformCircuitInstance { - witness: AtomicCell::new(None), - config: Arc::new(geometry.cycles_per_sha256_circuit as usize), - round_function: Arc::new(Poseidon2Goldilocks), - expected_public_input: None, - }), - ZkSyncBaseLayerCircuit::ECRecover(ZkSyncUniformCircuitInstance { - witness: AtomicCell::new(None), - config: Arc::new(geometry.cycles_per_ecrecover_circuit as usize), - round_function: Arc::new(Poseidon2Goldilocks), - expected_public_input: None, - }), - ZkSyncBaseLayerCircuit::RAMPermutation(ZkSyncUniformCircuitInstance { - witness: AtomicCell::new(None), - config: Arc::new(geometry.cycles_per_ram_permutation as usize), - round_function: Arc::new(Poseidon2Goldilocks), - expected_public_input: None, - }), - ZkSyncBaseLayerCircuit::StorageSorter(ZkSyncUniformCircuitInstance { - witness: AtomicCell::new(None), - config: Arc::new(geometry.cycles_per_storage_sorter as usize), - round_function: Arc::new(Poseidon2Goldilocks), - expected_public_input: None, - }), - ZkSyncBaseLayerCircuit::StorageApplication(ZkSyncUniformCircuitInstance { - witness: AtomicCell::new(None), - config: Arc::new(geometry.cycles_per_storage_application as usize), - round_function: Arc::new(Poseidon2Goldilocks), - expected_public_input: None, - }), - ZkSyncBaseLayerCircuit::EventsSorter(ZkSyncUniformCircuitInstance { - witness: AtomicCell::new(None), - config: Arc::new(geometry.cycles_per_events_or_l1_messages_sorter as usize), - round_function: Arc::new(Poseidon2Goldilocks), - expected_public_input: None, - }), - ZkSyncBaseLayerCircuit::L1MessagesSorter(ZkSyncUniformCircuitInstance { - witness: AtomicCell::new(None), - config: Arc::new(geometry.cycles_per_events_or_l1_messages_sorter as usize), - round_function: Arc::new(Poseidon2Goldilocks), - expected_public_input: None, - }), - ZkSyncBaseLayerCircuit::L1MessagesHasher(ZkSyncUniformCircuitInstance { - witness: AtomicCell::new(None), - config: Arc::new(geometry.limit_for_l1_messages_pudata_hasher as usize), - round_function: Arc::new(Poseidon2Goldilocks), - expected_public_input: None, - }), - ZkSyncBaseLayerCircuit::TransientStorageSorter(ZkSyncUniformCircuitInstance { - witness: AtomicCell::new(None), - config: Arc::new(geometry.cycles_per_transient_storage_sorter as usize), - round_function: Arc::new(Poseidon2Goldilocks), - expected_public_input: None, - }), - ZkSyncBaseLayerCircuit::Secp256r1Verify(ZkSyncUniformCircuitInstance { - witness: AtomicCell::new(None), - config: Arc::new(geometry.cycles_per_secp256r1_verify_circuit as usize), - round_function: Arc::new(Poseidon2Goldilocks), - expected_public_input: None, - }), - ZkSyncBaseLayerCircuit::EIP4844Repack(ZkSyncUniformCircuitInstance { - witness: AtomicCell::new(None), - config: Arc::new(ELEMENTS_PER_4844_BLOCK as usize), - round_function: Arc::new(Poseidon2Goldilocks), - expected_public_input: None, - }), - ] -} - -/// Returns all the recursive circuits (including leaves, nodes and scheduler). -/// Source must contain the verification keys for basic layer, leaf and node. -fn get_all_recursive_circuits( - source: &mut dyn SetupDataSource, -) -> crate::data_source::SourceResult> { - let mut result = get_leaf_circuits(source)?; - - result.push(get_node_circuit(source)?); - result.push(get_recursion_tip_circuit(source)?); - result.push(get_scheduler_circuit(source)?); - return Ok(result); -} - -/// Returns all the leaf circuits. -fn get_leaf_circuits( - source: &mut dyn SetupDataSource, -) -> crate::data_source::SourceResult> { - let mut result = vec![]; - - for base_circuit_type in ((BaseLayerCircuitType::VM as u8) - ..=(BaseLayerCircuitType::Secp256r1Verify as u8)) - .chain(std::iter::once(BaseLayerCircuitType::EIP4844Repack as u8)) - { - let _recursive_circuit_type = base_circuit_type_into_recursive_leaf_circuit_type( - BaseLayerCircuitType::from_numeric_value(base_circuit_type), - ); - - use crate::zkevm_circuits::recursion::leaf_layer::input::*; - let input = RecursionLeafInput::placeholder_witness(); - let vk = source.get_base_layer_vk(base_circuit_type)?; - - use crate::boojum::gadgets::queue::full_state_queue::FullStateCircuitQueueRawWitness; - let witness = RecursionLeafInstanceWitness { - input, - vk_witness: vk.clone().into_inner(), - queue_witness: FullStateCircuitQueueRawWitness { - elements: VecDeque::new(), - }, - proof_witnesses: VecDeque::new(), - }; - - use crate::zkevm_circuits::recursion::leaf_layer::LeafLayerRecursionConfig; - let config = LeafLayerRecursionConfig { - proof_config: recursion_layer_proof_config(), - vk_fixed_parameters: vk.into_inner().fixed_parameters, - capacity: RECURSION_ARITY, - _marker: std::marker::PhantomData, - }; - - let circuit = ZkSyncLeafLayerRecursiveCircuit { - base_layer_circuit_type: BaseLayerCircuitType::from_numeric_value(base_circuit_type), - witness: witness, - config: config, - transcript_params: (), - _marker: std::marker::PhantomData, - }; - - let circuit = ZkSyncRecursiveLayerCircuit::leaf_circuit_from_base_type( - BaseLayerCircuitType::from_numeric_value(base_circuit_type), - circuit, - ); - result.push(circuit); - } - return Ok(result); -} - -/// Returns the node circuit. -fn get_node_circuit( - source: &mut dyn SetupDataSource, -) -> crate::data_source::SourceResult { - use crate::zkevm_circuits::recursion::node_layer::input::*; - let input = RecursionNodeInput::placeholder_witness(); - let vk = source - .get_recursion_layer_vk(ZkSyncRecursionLayerStorageType::LeafLayerCircuitForMainVM as u8)?; - - // the only thing to setup here is to have proper number of split points - use crate::boojum::gadgets::queue::QueueTailState; - let split_points = vec![ - QueueTailState::::placeholder_witness(); - RECURSION_ARITY - 1 - ]; - let witness = RecursionNodeInstanceWitness { - input, - vk_witness: vk.clone().into_inner(), - split_points: split_points.into(), - proof_witnesses: VecDeque::new(), - }; - - use crate::zkevm_circuits::recursion::node_layer::NodeLayerRecursionConfig; - use circuit_definitions::circuit_definitions::recursion_layer::node_layer::ZkSyncNodeLayerRecursiveCircuit; - let config = NodeLayerRecursionConfig { - proof_config: recursion_layer_proof_config(), - vk_fixed_parameters: vk.into_inner().fixed_parameters, - leaf_layer_capacity: RECURSION_ARITY, - node_layer_capacity: RECURSION_ARITY, - _marker: std::marker::PhantomData, - }; - let circuit = ZkSyncNodeLayerRecursiveCircuit { - witness, - config, - transcript_params: (), - _marker: std::marker::PhantomData, - }; - - Ok(ZkSyncRecursiveLayerCircuit::NodeLayerCircuit(circuit)) -} - -/// Returns the recursion tip circuit -fn get_recursion_tip_circuit( - source: &mut dyn SetupDataSource, -) -> crate::data_source::SourceResult { - use crate::zkevm_circuits::recursion::recursion_tip::input::*; - let input = RecursionTipInput::placeholder_witness(); - let vk = source.get_recursion_layer_node_vk()?.into_inner(); - - let witness = RecursionTipInstanceWitness { - input, - vk_witness: vk.clone(), - proof_witnesses: VecDeque::new(), - }; - - use crate::zkevm_circuits::recursion::recursion_tip::*; - use circuit_definitions::circuit_definitions::recursion_layer::recursion_tip::*; - - let config = RecursionTipConfig { - proof_config: recursion_layer_proof_config(), - vk_fixed_parameters: vk.fixed_parameters, - _marker: std::marker::PhantomData, - }; - - let circuit = RecursionTipCircuit { - witness, - config, - transcript_params: (), - _marker: std::marker::PhantomData, - }; - - Ok(ZkSyncRecursiveLayerCircuit::RecursionTipCircuit(circuit)) -} - -/// Returns the scheduler circuit. -/// Source must contain the leafs, node and tip verification keys. -fn get_scheduler_circuit( - source: &mut dyn SetupDataSource, -) -> crate::data_source::SourceResult { - use crate::zkevm_circuits::scheduler::SchedulerConfig; - use circuit_definitions::circuit_definitions::recursion_layer::scheduler::SchedulerCircuit; - - println!("Computing leaf params"); - let leaf_layer_params = compute_leaf_params(source)?; - println!("Obtaining node VK"); - let node_vk = source.get_recursion_layer_node_vk()?.into_inner(); - println!("Obtaining recursion tip VK"); - let recursion_tip_vk = source.get_recursion_tip_vk()?.into_inner(); - - let leaf_layer_params: [RecursionLeafParametersWitness; - NUM_BASE_LAYER_CIRCUITS] = leaf_layer_params - .into_iter() - .map(|el| el.1) - .collect::>() - .try_into() - .unwrap(); - - let config = SchedulerConfig { - proof_config: recursion_layer_proof_config(), - leaf_layer_parameters: leaf_layer_params, - node_layer_vk: node_vk, - recursion_tip_vk: recursion_tip_vk.clone(), - vk_fixed_parameters: recursion_tip_vk.fixed_parameters.clone(), - capacity: SCHEDULER_CAPACITY, - _marker: std::marker::PhantomData, - }; - - use crate::zkevm_circuits::scheduler::input::SchedulerCircuitInstanceWitness; - let scheduler_witness = SchedulerCircuitInstanceWitness::placeholder(); - - let scheduler_circuit = SchedulerCircuit { - witness: scheduler_witness, - config, - transcript_params: (), - _marker: std::marker::PhantomData, - }; - - Ok(ZkSyncRecursiveLayerCircuit::SchedulerCircuit( - scheduler_circuit, - )) -} - -/// Contains all the information that prover needs to setup and verify the given circuit. -pub struct CircuitSetupData { - pub setup_base: SetupBaseStorage, - pub setup: SetupStorage, - pub vk: VerificationKey>, - pub setup_tree: - MerkleTreeWithCap>, - pub vars_hint: DenseVariablesCopyHint, - pub wits_hint: DenseWitnessCopyHint, - pub finalization_hint: FinalizationHintsForProver, -} - -/// Generate verification, and setup keys for a given circuit type from a base layer. -/// If generating the setup data for recursion layers, the 'source' must have verification keys for basic circuits, leaf and node. -pub fn generate_circuit_setup_data( - is_base_layer: bool, - circuit_type: u8, - source: &mut dyn SetupDataSource, -) -> crate::data_source::SourceResult { - let geometry = crate::geometry_config::ProtocolGeometry::latest().config(); - let worker = Worker::new(); - - let (setup_base, setup, vk, setup_tree, vars_hint, wits_hint, finalization_hint) = - if is_base_layer { - let circuit = get_all_basic_circuits(&geometry) - .iter() - .find(|circuit| circuit.numeric_circuit_type() == circuit_type) - .expect(&format!( - "Could not find circuit matching {:?}", - circuit_type - )) - .clone(); - - create_base_layer_setup_data( - circuit, - &worker, - BASE_LAYER_FRI_LDE_FACTOR, - BASE_LAYER_CAP_SIZE, - ) - } else { - let circuit = get_all_recursive_circuits(source)? - .iter() - .find(|circuit| circuit.numeric_circuit_type() == circuit_type) - .expect(&format!( - "Could not find circuit matching {:?}", - circuit_type - )) - .clone(); - - create_recursive_layer_setup_data( - circuit, - &worker, - BASE_LAYER_FRI_LDE_FACTOR, - BASE_LAYER_CAP_SIZE, - ) - }; - - Ok(CircuitSetupData { - setup_base, - setup, - vk, - setup_tree, - vars_hint, - wits_hint, - finalization_hint, - }) -} - -/// For backwards compatibility (as zksync-era uses this method). -/// For new cases please use generate_base_layer_vks directly. -pub fn generate_base_layer_vks_and_proofs( - source: &mut dyn SetupDataSource, -) -> crate::data_source::SourceResult<()> { - generate_base_layer_vks(source, None, || {}) -} - -/// Returns number of basic verification keys. -pub fn basic_vk_count() -> usize { - BaseLayerCircuitType::as_iter_u8().count() -} - -/// Generate Verification keys for all base layer circuits. -/// num_threads control how many VKs are generated in parallel - each one takes around 30GB of RAM. -/// if not specified, will run them sequencially. -/// CB callback will be called on each finished VK (to track progress). -pub fn generate_base_layer_vks( - source: &mut dyn SetupDataSource, - num_threads: Option, - cb: CB, -) -> crate::data_source::SourceResult<()> { - let geometry = crate::geometry_config::ProtocolGeometry::latest().config(); - let worker = Worker::new(); - - let num_threads = num_threads.unwrap_or(1); - - let pool = ThreadPoolBuilder::new() - .num_threads(num_threads) - .build() - .unwrap(); - - let r: Vec<_> = pool.install(|| { - get_all_basic_circuits(&geometry) - .into_par_iter() - .map(|circuit| { - let result = generate_vk_and_finalization_hint(circuit, &worker); - cb(); - result - }) - .collect() - }); - - for (vk, hint) in r.into_iter() { - source.set_base_layer_finalization_hint(hint)?; - source.set_base_layer_vk(vk)?; - } - - Ok(()) -} - -fn generate_vk_and_finalization_hint( - circuit: ZkSyncBaseLayerCircuit, - worker: &Worker, -) -> ( - ZkSyncBaseLayerVerificationKey, - ZkSyncBaseLayerFinalizationHint, -) { - let circuit_type = circuit.numeric_circuit_type(); - - let (_, _, vk, _, _, _, finalization_hint) = create_base_layer_setup_data( - circuit, - &worker, - BASE_LAYER_FRI_LDE_FACTOR, - BASE_LAYER_CAP_SIZE, - ); - - let typed_vk = ZkSyncBaseLayerVerificationKey::from_inner(circuit_type, vk.clone()); - let typed_finalization_hint = - ZkSyncBaseLayerFinalizationHint::from_inner(circuit_type, finalization_hint.clone()); - (typed_vk, typed_finalization_hint) -} - -/// For backwards compatibility (as zksync-era uses this method). -/// For new cases please use generate_recursive_layer_vks directly. -pub fn generate_recursive_layer_vks_and_proofs( - source: &mut dyn SetupDataSource, -) -> crate::data_source::SourceResult<()> { - generate_recursive_layer_vks(source, None, || {}) -} - -fn generate_vk_and_finalization_hint_for_recursion( - circuit: ZkSyncRecursiveLayerCircuit, - worker: &Worker, -) -> ( - ZkSyncRecursionLayerVerificationKey, - ZkSyncRecursionLayerFinalizationHint, -) { - println!( - "Computing leaf layer VK for type {:?}", - circuit.numeric_circuit_type() - ); - - let numeric_circuit_type = circuit.numeric_circuit_type(); - let (_setup_base, _setup, vk, _setup_tree, _vars_hint, _wits_hint, finalization_hint) = - create_recursive_layer_setup_data( - circuit, - &worker, - RECURSION_LAYER_FRI_LDE_FACTOR, - RECURSION_LAYER_CAP_SIZE, - ); - - let typed_vk = - ZkSyncRecursionLayerVerificationKey::from_inner(numeric_circuit_type, vk.clone()); - - let typed_finalization_hint = ZkSyncRecursionLayerFinalizationHint::from_inner( - numeric_circuit_type, - finalization_hint.clone(), - ); - (typed_vk, typed_finalization_hint) -} - -/// Returns number of recursive layer verification keys. -pub fn recursive_layer_vk_count() -> usize { - // Leafs (one per base layer) + node + recursion + scheduler - basic_vk_count() + 3 -} - -/// num_threads control how many VKs are generated in parallel - each one takes around 25GB of RAM. -/// if not specified, will run them sequencially. -pub fn generate_recursive_layer_vks( - source: &mut dyn SetupDataSource, - num_threads: Option, - cb: CB, -) -> crate::data_source::SourceResult<()> { - // here we rely ONLY on VKs and proofs from the setup, so we keep the geometries and circuits - // via padding proofs - let worker = Worker::new(); - let num_threads = num_threads.unwrap_or(1); - - println!("Computing leaf vks"); - - let pool = ThreadPoolBuilder::new() - .num_threads(num_threads) - .build() - .unwrap(); - - let leaf_circuits = get_leaf_circuits(source)?; - - let r: Vec<_> = pool.install(|| { - leaf_circuits - .into_par_iter() - .map(|circuit| { - let result = generate_vk_and_finalization_hint_for_recursion(circuit, &worker); - cb(); - result - }) - .collect() - }); - - for (vk, hint) in r.into_iter() { - source.set_recursion_layer_finalization_hint(hint)?; - source.set_recursion_layer_vk(vk)?; - } - - println!("Computing node vk"); - - { - let circuit = get_node_circuit(source)?; - - let (_setup_base, _setup, vk, _setup_tree, _vars_hint, _wits_hint, finalization_hint) = - create_recursive_layer_setup_data( - circuit, - &worker, - RECURSION_LAYER_FRI_LDE_FACTOR, - RECURSION_LAYER_CAP_SIZE, - ); - - let typed_finalization_hint = - ZkSyncRecursionLayerFinalizationHint::NodeLayerCircuit(finalization_hint.clone()); - source.set_recursion_layer_node_finalization_hint(typed_finalization_hint)?; - let typed_vk = ZkSyncRecursionLayerVerificationKey::NodeLayerCircuit(vk.clone()); - source.set_recursion_layer_node_vk(typed_vk)?; - } - cb(); - - println!("Computing recursion tip vk"); - generate_recursion_tip_vk(source)?; - cb(); - - println!("Computing scheduler vk"); - generate_scheduler_vk(source)?; - cb(); - - Ok(()) -} - -pub fn generate_recursion_tip_vk( - source: &mut dyn SetupDataSource, -) -> crate::data_source::SourceResult<()> { - let worker = Worker::new(); - let recursion_tip_circuit = get_recursion_tip_circuit(source)?; - - let (_setup_base, _setup, vk, _setup_tree, _vars_hint, _wits_hint, finalization_hint) = - create_recursive_layer_setup_data( - recursion_tip_circuit, - &worker, - RECURSION_LAYER_FRI_LDE_FACTOR, - RECURSION_LAYER_CAP_SIZE, - ); - - source.set_recursion_tip_vk(ZkSyncRecursionLayerVerificationKey::RecursionTipCircuit( - vk.clone(), - ))?; - source.set_recursion_tip_finalization_hint( - ZkSyncRecursionLayerFinalizationHint::RecursionTipCircuit(finalization_hint.clone()), - )?; - Ok(()) -} - -pub fn generate_scheduler_vk( - source: &mut dyn SetupDataSource, -) -> crate::data_source::SourceResult<()> { - let worker = Worker::new(); - let scheduler_circuit = get_scheduler_circuit(source)?; - - let (_setup_base, _setup, vk, _setup_tree, _vars_hint, _wits_hint, finalization_hint) = - create_recursive_layer_setup_data( - scheduler_circuit, - &worker, - RECURSION_LAYER_FRI_LDE_FACTOR, - RECURSION_LAYER_CAP_SIZE, - ); - - source.set_recursion_layer_vk(ZkSyncRecursionLayerVerificationKey::SchedulerCircuit( - vk.clone(), - ))?; - source.set_recursion_layer_finalization_hint( - ZkSyncRecursionLayerFinalizationHint::SchedulerCircuit(finalization_hint.clone()), - )?; - - Ok(()) -} - -pub fn compute_leaf_params( - source: &mut dyn SetupDataSource, -) -> crate::data_source::SourceResult)>> { - use crate::witness::recursive_aggregation::compute_leaf_params; - let mut leaf_vk_commits = vec![]; - - for circuit_type in ((BaseLayerCircuitType::VM as u8) - ..=(BaseLayerCircuitType::Secp256r1Verify as u8)) - .chain(std::iter::once(BaseLayerCircuitType::EIP4844Repack as u8)) - { - let recursive_circuit_type = base_circuit_type_into_recursive_leaf_circuit_type( - BaseLayerCircuitType::from_numeric_value(circuit_type), - ); - let base_vk = source.get_base_layer_vk(circuit_type)?; - let leaf_vk = source.get_recursion_layer_vk(recursive_circuit_type as u8)?; - let params = compute_leaf_params(circuit_type, base_vk, leaf_vk); - leaf_vk_commits.push((circuit_type, params)); - } - - Ok(leaf_vk_commits) -} - -#[cfg(test)] -mod test { - use std::sync::Mutex; - - use indicatif::{ProgressBar, ProgressStyle}; - - use self::data_source::local_file_data_source::LocalFileDataSource; - - use super::*; - - #[ignore = "too slow"] - #[test] - fn test_run_create_base_layer_vks_and_proofs() { - let mut source = LocalFileDataSource::default(); - source.create_folders_for_storing_data(); - let count = basic_vk_count(); - let progress_bar = ProgressBar::new(count as u64); - progress_bar.set_style(ProgressStyle::default_bar() - .template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {pos:>7}/{len:7} ({eta})") - .progress_chars("#>-")); - - let pb = Arc::new(Mutex::new(progress_bar)); - - generate_base_layer_vks(&mut source, None, || { - pb.lock().unwrap().inc(1); - }) - .expect("must compute setup"); - pb.lock().unwrap().finish_with_message("done"); - } - - #[ignore = "too slow"] - #[test] - fn test_run_create_recursion_layer_vks_and_proofs() { - let mut source = LocalFileDataSource::default(); - source.create_folders_for_storing_data(); - generate_recursive_layer_vks(&mut source, None, || {}).expect("must compute setup"); - } - - #[ignore = "too slow"] - #[test] - fn test_generate_recursion_tip() { - let mut src = LocalFileDataSource::default(); - src.create_folders_for_storing_data(); - let source = &mut src; - - generate_recursion_tip_vk(source).unwrap(); - } - - #[ignore = "too slow"] - #[test] - fn test_generate_scheduler() { - let mut src = LocalFileDataSource::default(); - src.create_folders_for_storing_data(); - let source = &mut src; - - generate_scheduler_vk(source).unwrap(); - } -} diff --git a/crates/zkevm_test_harness/src/compute_setups/full.rs b/crates/zkevm_test_harness/src/compute_setups/full.rs new file mode 100644 index 0000000..4805cc9 --- /dev/null +++ b/crates/zkevm_test_harness/src/compute_setups/full.rs @@ -0,0 +1,474 @@ +//! Functions to compute setup and verification keys for different circuit types. + +use std::sync::Arc; + +use crate::zkevm_circuits::recursion::leaf_layer::input::RecursionLeafParametersWitness; +use crate::zkevm_circuits::recursion::NUM_BASE_LAYER_CIRCUITS; +use circuit_definitions::boojum::gadgets::traits::allocatable::CSAllocatable; +use circuit_definitions::circuit_definitions::aux_layer::{ + ZkSyncCompressionForWrapperCircuit, ZkSyncCompressionLayerCircuit, +}; +use circuit_definitions::{ + circuit_definitions::{ + base_layer::{ + ZkSyncBaseLayerCircuit, ZkSyncBaseLayerFinalizationHint, ZkSyncBaseLayerVerificationKey, + }, + ZkSyncUniformCircuitInstance, + }, + recursion_layer_proof_config, + zkevm_circuits::eip_4844::input::ELEMENTS_PER_4844_BLOCK, + zkevm_circuits::scheduler::aux::BaseLayerCircuitType, + RECURSION_LAYER_CAP_SIZE, RECURSION_LAYER_FRI_LDE_FACTOR, +}; + +use crossbeam::atomic::AtomicCell; +use rayon::iter::{IntoParallelIterator, ParallelIterator}; +use rayon::ThreadPoolBuilder; + +use crate::toolset::GeometryConfig; + +use crate::boojum::{ + algebraic_props::{round_function::AbsorptionModeOverwrite, sponge::GoldilocksPoseidon2Sponge}, + cs::{ + implementations::{ + hints::{DenseVariablesCopyHint, DenseWitnessCopyHint}, + polynomial_storage::{SetupBaseStorage, SetupStorage}, + setup::FinalizationHintsForProver, + verifier::VerificationKey, + }, + oracle::merkle_tree::MerkleTreeWithCap, + }, + worker::Worker, +}; + +use crate::data_source::SetupDataSource; + +use super::*; + +use circuit_definitions::circuit_definitions::aux_layer::compression::{ + CompressionMode1Circuit, CompressionMode1ForWrapperCircuit, CompressionMode2Circuit, + CompressionMode2ForWrapperCircuit, CompressionMode3Circuit, CompressionMode3ForWrapperCircuit, + CompressionMode4Circuit, CompressionMode4ForWrapperCircuit, CompressionMode5Circuit, + CompressionMode5ForWrapperCircuit, ProofCompressionFunction, +}; +use circuit_definitions::circuit_definitions::aux_layer::compression_modes::{ + CompressionMode1, CompressionMode2, CompressionMode3, CompressionMode4, CompressionMode5, +}; +use circuit_definitions::circuit_definitions::recursion_layer::leaf_layer::*; +use circuit_definitions::circuit_definitions::recursion_layer::*; +use circuit_definitions::zkevm_circuits::recursion::compression::CompressionRecursionConfig; +use circuit_definitions::{BASE_LAYER_CAP_SIZE, BASE_LAYER_FRI_LDE_FACTOR}; +use std::collections::VecDeque; + +use crate::prover_utils::*; + +/// Contains all the information that prover needs to setup and verify the given circuit. +pub struct CircuitSetupData { + pub setup_base: SetupBaseStorage, + pub setup: SetupStorage, + pub vk: VerificationKey>, + pub setup_tree: + MerkleTreeWithCap>, + pub vars_hint: DenseVariablesCopyHint, + pub wits_hint: DenseWitnessCopyHint, + pub finalization_hint: FinalizationHintsForProver, +} + +/// Generate verification, and setup keys for a given circuit type from a base layer. +/// If generating the setup data for recursion layers, the 'source' must have verification keys for basic circuits, leaf and node. +pub fn generate_circuit_setup_data( + proving_stage: u8, + circuit_type: u8, + source: &mut dyn SetupDataSource, +) -> crate::data_source::SourceResult { + let geometry = crate::geometry_config::get_geometry_config(); + let worker = Worker::new(); + + let (setup_base, setup, vk, setup_tree, vars_hint, wits_hint, finalization_hint) = + match proving_stage { + // basic circuits + 0 => { + let circuit = get_all_basic_circuits(&geometry) + .iter() + .find(|circuit| circuit.numeric_circuit_type() == circuit_type) + .expect(&format!( + "Could not find circuit matching {:?}", + circuit_type + )) + .clone(); + + create_base_layer_setup_data( + circuit, + &worker, + BASE_LAYER_FRI_LDE_FACTOR, + BASE_LAYER_CAP_SIZE, + ) + } + // recursive circuits + 1..=4 => { + let circuit = get_all_recursive_circuits(source)? + .iter() + .find(|circuit| circuit.numeric_circuit_type() == circuit_type) + .expect(&format!( + "Could not find circuit matching {:?}", + circuit_type + )) + .clone(); + + create_recursive_layer_setup_data( + circuit, + &worker, + RECURSION_LAYER_FRI_LDE_FACTOR, + RECURSION_LAYER_CAP_SIZE, + ) + } + // compression circuits + 5 => { + let circuit = get_compression_circuits(source) + .iter() + .find(|circuit| circuit.numeric_circuit_type() == circuit_type) + .expect(&format!( + "Could not find circuit matching {:?}", + circuit_type + )) + .clone(); + + create_compression_layer_setup_data( + circuit.clone(), + &worker, + circuit.clone().proof_config_for_compression_step().fri_lde_factor, + circuit + .proof_config_for_compression_step() + .merkle_tree_cap_size, + ) + } + // compression for wrapper circuits + 6 => panic!("Full generation of setup for wrapper circuits should be generated with light setup"), + _ => unreachable!("Invalid proving stage"), + }; + + Ok(CircuitSetupData { + setup_base, + setup, + vk, + setup_tree, + vars_hint, + wits_hint, + finalization_hint, + }) +} + +/// For backwards compatibility (as zksync-era uses this method). +/// For new cases please use generate_base_layer_vks directly. +pub fn generate_base_layer_vks_and_proofs( + source: &mut dyn SetupDataSource, +) -> crate::data_source::SourceResult<()> { + generate_base_layer_vks(source, None, || {}) +} + +/// Returns number of basic verification keys. +pub fn basic_vk_count() -> usize { + BaseLayerCircuitType::as_iter_u8().count() +} + +/// Generate Verification keys for all base layer circuits. +/// num_threads control how many VKs are generated in parallel - each one takes around 30GB of RAM. +/// if not specified, will run them sequencially. +/// CB callback will be called on each finished VK (to track progress). +pub fn generate_base_layer_vks( + source: &mut dyn SetupDataSource, + num_threads: Option, + cb: CB, +) -> crate::data_source::SourceResult<()> { + let geometry = crate::geometry_config::get_geometry_config(); + let worker = Worker::new(); + + let num_threads = num_threads.unwrap_or(1); + + let pool = ThreadPoolBuilder::new() + .num_threads(num_threads) + .build() + .unwrap(); + + let r: Vec<_> = pool.install(|| { + get_all_basic_circuits(&geometry) + .into_par_iter() + .map(|circuit| { + let result = generate_vk_and_finalization_hint(circuit, &worker); + cb(); + result + }) + .collect() + }); + + for (vk, hint) in r.into_iter() { + source.set_base_layer_finalization_hint(hint)?; + source.set_base_layer_vk(vk)?; + } + + Ok(()) +} + +fn generate_vk_and_finalization_hint( + circuit: ZkSyncBaseLayerCircuit, + worker: &Worker, +) -> ( + ZkSyncBaseLayerVerificationKey, + ZkSyncBaseLayerFinalizationHint, +) { + let circuit_type = circuit.numeric_circuit_type(); + + let (_, _, vk, _, _, _, finalization_hint) = create_base_layer_setup_data( + circuit, + &worker, + BASE_LAYER_FRI_LDE_FACTOR, + BASE_LAYER_CAP_SIZE, + ); + + let typed_vk = ZkSyncBaseLayerVerificationKey::from_inner(circuit_type, vk.clone()); + let typed_finalization_hint = + ZkSyncBaseLayerFinalizationHint::from_inner(circuit_type, finalization_hint.clone()); + (typed_vk, typed_finalization_hint) +} + +/// For backwards compatibility (as zksync-era uses this method). +/// For new cases please use generate_recursive_layer_vks directly. +pub fn generate_recursive_layer_vks_and_proofs( + source: &mut dyn SetupDataSource, +) -> crate::data_source::SourceResult<()> { + generate_recursive_layer_vks(source, None, || {}) +} + +fn generate_vk_and_finalization_hint_for_recursion( + circuit: ZkSyncRecursiveLayerCircuit, + worker: &Worker, +) -> ( + ZkSyncRecursionLayerVerificationKey, + ZkSyncRecursionLayerFinalizationHint, +) { + println!( + "Computing leaf layer VK for type {:?}", + circuit.numeric_circuit_type() + ); + + let numeric_circuit_type = circuit.numeric_circuit_type(); + let (_setup_base, _setup, vk, _setup_tree, _vars_hint, _wits_hint, finalization_hint) = + create_recursive_layer_setup_data( + circuit, + &worker, + RECURSION_LAYER_FRI_LDE_FACTOR, + RECURSION_LAYER_CAP_SIZE, + ); + + let typed_vk = + ZkSyncRecursionLayerVerificationKey::from_inner(numeric_circuit_type, vk.clone()); + + let typed_finalization_hint = ZkSyncRecursionLayerFinalizationHint::from_inner( + numeric_circuit_type, + finalization_hint.clone(), + ); + (typed_vk, typed_finalization_hint) +} + +/// Returns number of recursive layer verification keys. +pub fn recursive_layer_vk_count() -> usize { + // Leafs (one per base layer) + node + recursion + scheduler + basic_vk_count() + 3 +} + +/// num_threads control how many VKs are generated in parallel - each one takes around 25GB of RAM. +/// if not specified, will run them sequencially. +pub fn generate_recursive_layer_vks( + source: &mut dyn SetupDataSource, + num_threads: Option, + cb: CB, +) -> crate::data_source::SourceResult<()> { + // here we rely ONLY on VKs and proofs from the setup, so we keep the geometries and circuits + // via padding proofs + let worker = Worker::new(); + let num_threads = num_threads.unwrap_or(1); + + println!("Computing leaf vks"); + + let pool = ThreadPoolBuilder::new() + .num_threads(num_threads) + .build() + .unwrap(); + + let leaf_circuits = get_leaf_circuits(source)?; + + let r: Vec<_> = pool.install(|| { + leaf_circuits + .into_par_iter() + .map(|circuit| { + let result = generate_vk_and_finalization_hint_for_recursion(circuit, &worker); + cb(); + result + }) + .collect() + }); + + for (vk, hint) in r.into_iter() { + source.set_recursion_layer_finalization_hint(hint)?; + source.set_recursion_layer_vk(vk)?; + } + + println!("Computing node vk"); + + { + let circuit = get_node_circuit(source)?; + + let (_setup_base, _setup, vk, _setup_tree, _vars_hint, _wits_hint, finalization_hint) = + create_recursive_layer_setup_data( + circuit, + &worker, + RECURSION_LAYER_FRI_LDE_FACTOR, + RECURSION_LAYER_CAP_SIZE, + ); + + let typed_finalization_hint = + ZkSyncRecursionLayerFinalizationHint::NodeLayerCircuit(finalization_hint.clone()); + source.set_recursion_layer_node_finalization_hint(typed_finalization_hint)?; + let typed_vk = ZkSyncRecursionLayerVerificationKey::NodeLayerCircuit(vk.clone()); + source.set_recursion_layer_node_vk(typed_vk)?; + } + cb(); + + println!("Computing recursion tip vk"); + generate_recursion_tip_vk(source)?; + cb(); + + println!("Computing scheduler vk"); + generate_scheduler_vk(source)?; + cb(); + + Ok(()) +} + +pub fn generate_recursion_tip_vk( + source: &mut dyn SetupDataSource, +) -> crate::data_source::SourceResult<()> { + let worker = Worker::new(); + let recursion_tip_circuit = get_recursion_tip_circuit(source)?; + + let (_setup_base, _setup, vk, _setup_tree, _vars_hint, _wits_hint, finalization_hint) = + create_recursive_layer_setup_data( + recursion_tip_circuit, + &worker, + RECURSION_LAYER_FRI_LDE_FACTOR, + RECURSION_LAYER_CAP_SIZE, + ); + + source.set_recursion_tip_vk(ZkSyncRecursionLayerVerificationKey::RecursionTipCircuit( + vk.clone(), + ))?; + source.set_recursion_tip_finalization_hint( + ZkSyncRecursionLayerFinalizationHint::RecursionTipCircuit(finalization_hint.clone()), + )?; + Ok(()) +} + +pub fn generate_scheduler_vk( + source: &mut dyn SetupDataSource, +) -> crate::data_source::SourceResult<()> { + let worker = Worker::new(); + let scheduler_circuit = get_scheduler_circuit(source)?; + + let (_setup_base, _setup, vk, _setup_tree, _vars_hint, _wits_hint, finalization_hint) = + create_recursive_layer_setup_data( + scheduler_circuit, + &worker, + RECURSION_LAYER_FRI_LDE_FACTOR, + RECURSION_LAYER_CAP_SIZE, + ); + + source.set_recursion_layer_vk(ZkSyncRecursionLayerVerificationKey::SchedulerCircuit( + vk.clone(), + ))?; + source.set_recursion_layer_finalization_hint( + ZkSyncRecursionLayerFinalizationHint::SchedulerCircuit(finalization_hint.clone()), + )?; + + Ok(()) +} + +pub fn compute_leaf_params( + source: &mut dyn SetupDataSource, +) -> crate::data_source::SourceResult)>> { + use crate::witness::recursive_aggregation::compute_leaf_params; + let mut leaf_vk_commits = vec![]; + + for circuit_type in ((BaseLayerCircuitType::VM as u8) + ..=(BaseLayerCircuitType::Secp256r1Verify as u8)) + .chain(std::iter::once(BaseLayerCircuitType::EIP4844Repack as u8)) + { + let recursive_circuit_type = base_circuit_type_into_recursive_leaf_circuit_type( + BaseLayerCircuitType::from_numeric_value(circuit_type), + ); + let base_vk = source.get_base_layer_vk(circuit_type)?; + let leaf_vk = source.get_recursion_layer_vk(recursive_circuit_type as u8)?; + let params = compute_leaf_params(circuit_type, base_vk, leaf_vk); + leaf_vk_commits.push((circuit_type, params)); + } + + Ok(leaf_vk_commits) +} + +#[cfg(test)] +mod test { + use std::sync::Mutex; + + use indicatif::{ProgressBar, ProgressStyle}; + + use crate::data_source::local_file_data_source::LocalFileDataSource; + + use super::*; + + #[ignore = "too slow"] + #[test] + fn test_run_create_base_layer_vks_and_proofs() { + let mut source = LocalFileDataSource::default(); + source.create_folders_for_storing_data(); + let count = basic_vk_count(); + let progress_bar = ProgressBar::new(count as u64); + progress_bar.set_style(ProgressStyle::default_bar() + .template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {pos:>7}/{len:7} ({eta})") + .progress_chars("#>-")); + + let pb = Arc::new(Mutex::new(progress_bar)); + + generate_base_layer_vks(&mut source, None, || { + pb.lock().unwrap().inc(1); + }) + .expect("must compute setup"); + pb.lock().unwrap().finish_with_message("done"); + } + + #[ignore = "too slow"] + #[test] + fn test_run_create_recursion_layer_vks_and_proofs() { + let mut source = LocalFileDataSource::default(); + source.create_folders_for_storing_data(); + generate_recursive_layer_vks(&mut source, None, || {}).expect("must compute setup"); + } + + #[ignore = "too slow"] + #[test] + fn test_generate_recursion_tip() { + let mut src = LocalFileDataSource::default(); + src.create_folders_for_storing_data(); + let source = &mut src; + + generate_recursion_tip_vk(source).unwrap(); + } + + #[ignore = "too slow"] + #[test] + fn test_generate_scheduler() { + let mut src = LocalFileDataSource::default(); + src.create_folders_for_storing_data(); + let source = &mut src; + + generate_scheduler_vk(source).unwrap(); + } +} diff --git a/crates/zkevm_test_harness/src/compute_setups/light.rs b/crates/zkevm_test_harness/src/compute_setups/light.rs new file mode 100644 index 0000000..05f1435 --- /dev/null +++ b/crates/zkevm_test_harness/src/compute_setups/light.rs @@ -0,0 +1,179 @@ +//! Functions to compute setup and verification keys for different circuit types. + +use std::sync::Arc; + +use crate::zkevm_circuits::recursion::leaf_layer::input::RecursionLeafParametersWitness; +use crate::zkevm_circuits::recursion::NUM_BASE_LAYER_CIRCUITS; +use circuit_definitions::boojum::gadgets::traits::allocatable::CSAllocatable; +use circuit_definitions::circuit_definitions::aux_layer::{ + ZkSyncCompressionForWrapperCircuit, ZkSyncCompressionLayerCircuit, +}; +use circuit_definitions::{ + circuit_definitions::{ + base_layer::{ + ZkSyncBaseLayerCircuit, ZkSyncBaseLayerFinalizationHint, ZkSyncBaseLayerVerificationKey, + }, + ZkSyncUniformCircuitInstance, + }, + recursion_layer_proof_config, + zkevm_circuits::eip_4844::input::ELEMENTS_PER_4844_BLOCK, + zkevm_circuits::scheduler::aux::BaseLayerCircuitType, + RECURSION_LAYER_CAP_SIZE, RECURSION_LAYER_FRI_LDE_FACTOR, +}; + +use crossbeam::atomic::AtomicCell; +use rayon::iter::{IntoParallelIterator, ParallelIterator}; +use rayon::ThreadPoolBuilder; + +use crate::toolset::GeometryConfig; + +use crate::boojum::{ + algebraic_props::{round_function::AbsorptionModeOverwrite, sponge::GoldilocksPoseidon2Sponge}, + cs::{ + implementations::{ + hints::{DenseVariablesCopyHint, DenseWitnessCopyHint}, + polynomial_storage::{SetupBaseStorage, SetupStorage}, + setup::FinalizationHintsForProver, + verifier::VerificationKey, + }, + oracle::merkle_tree::MerkleTreeWithCap, + }, + worker::Worker, +}; + +use crate::data_source::SetupDataSource; +use crate::zkevm_circuits::base_structures::vm_state::FULL_SPONGE_QUEUE_STATE_WIDTH; + +use super::*; + +use crate::boojum::cs::implementations::verifier::VerificationKeyCircuitGeometry; +use circuit_definitions::circuit_definitions::aux_layer::compression::{ + CompressionMode1Circuit, CompressionMode1ForWrapperCircuit, CompressionMode2Circuit, + CompressionMode2ForWrapperCircuit, CompressionMode3Circuit, CompressionMode3ForWrapperCircuit, + CompressionMode4Circuit, CompressionMode4ForWrapperCircuit, CompressionMode5Circuit, + CompressionMode5ForWrapperCircuit, ProofCompressionFunction, +}; +use circuit_definitions::circuit_definitions::aux_layer::compression_modes::{ + CompressionMode1, CompressionMode2, CompressionMode3, CompressionMode4, CompressionMode5, +}; +use circuit_definitions::circuit_definitions::recursion_layer::leaf_layer::*; +use circuit_definitions::circuit_definitions::recursion_layer::*; +use circuit_definitions::zkevm_circuits::recursion::compression::CompressionRecursionConfig; +use circuit_definitions::{BASE_LAYER_CAP_SIZE, BASE_LAYER_FRI_LDE_FACTOR}; +use std::collections::VecDeque; + +use crate::prover_utils::light::{ + create_light_base_layer_setup_data, create_light_compression_for_wrapper_setup_data, + create_light_compression_layer_setup_data, create_light_recursive_layer_setup_data, +}; +use crate::prover_utils::*; + +/// Contains all the information that prover needs to setup and verify the given circuit. +pub struct LightCircuitSetupData { + pub setup_base: SetupBaseStorage, + pub vk_geometry: VerificationKeyCircuitGeometry, + pub vars_hint: DenseVariablesCopyHint, + pub wits_hint: DenseWitnessCopyHint, + pub finalization_hint: FinalizationHintsForProver, +} + +/// Generate verification, and setup keys for a given circuit type from a base layer. +/// If generating the setup data for recursion layers, the 'source' must have verification keys for basic circuits, leaf and node. +pub fn generate_light_circuit_setup_data( + proving_stage: u8, + circuit_type: u8, + source: &mut dyn SetupDataSource, +) -> crate::data_source::SourceResult { + let geometry = crate::geometry_config::get_geometry_config(); + let worker = Worker::new(); + + let (setup_base, vk_geometry, vars_hint, wits_hint, finalization_hint) = match proving_stage { + 0 => { + let circuit = get_all_basic_circuits(&geometry) + .iter() + .find(|circuit| circuit.numeric_circuit_type() == circuit_type) + .expect(&format!( + "Could not find circuit matching {:?}", + circuit_type + )) + .clone(); + + create_light_base_layer_setup_data( + circuit, + &worker, + BASE_LAYER_FRI_LDE_FACTOR, + BASE_LAYER_CAP_SIZE, + ) + } + 1..=4 => { + let circuit = get_all_recursive_circuits(source)? + .iter() + .find(|circuit| circuit.numeric_circuit_type() == circuit_type) + .expect(&format!( + "Could not find circuit matching {:?}", + circuit_type + )) + .clone(); + + create_light_recursive_layer_setup_data( + circuit, + &worker, + RECURSION_LAYER_FRI_LDE_FACTOR, + RECURSION_LAYER_CAP_SIZE, + ) + } + 5 => { + let circuit = get_compression_circuits(source) + .iter() + .find(|circuit| circuit.numeric_circuit_type() == circuit_type) + .expect(&format!( + "Could not find circuit matching {:?}", + circuit_type + )) + .clone(); + + create_light_compression_layer_setup_data( + circuit.clone(), + &worker, + circuit + .clone() + .proof_config_for_compression_step() + .fri_lde_factor, + circuit + .proof_config_for_compression_step() + .merkle_tree_cap_size, + ) + } + 6 => { + let circuit = get_compression_for_wrapper_circuits(source) + .iter() + .find(|circuit| circuit.numeric_circuit_type() == circuit_type) + .expect(&format!( + "Could not find circuit matching {:?}", + circuit_type + )) + .clone(); + + create_light_compression_for_wrapper_setup_data( + circuit.clone(), + &worker, + circuit + .clone() + .proof_config_for_compression_step() + .fri_lde_factor, + circuit + .proof_config_for_compression_step() + .merkle_tree_cap_size, + ) + } + _ => unreachable!("Invalid proving stage"), + }; + + Ok(LightCircuitSetupData { + setup_base, + vk_geometry, + vars_hint, + wits_hint, + finalization_hint, + }) +} diff --git a/crates/zkevm_test_harness/src/compute_setups/mod.rs b/crates/zkevm_test_harness/src/compute_setups/mod.rs new file mode 100644 index 0000000..ac35e1b --- /dev/null +++ b/crates/zkevm_test_harness/src/compute_setups/mod.rs @@ -0,0 +1,458 @@ +use super::*; +use crate::boojum::gadgets::traits::allocatable::CSAllocatable; +use crate::data_source::SetupDataSource; +use circuit_definitions::boojum::cs::implementations::verifier::VerificationKey; +use circuit_definitions::circuit_definitions::aux_layer::compression::{ + CompressionMode1Circuit, CompressionMode1ForWrapperCircuit, CompressionMode2Circuit, + CompressionMode2ForWrapperCircuit, CompressionMode3Circuit, CompressionMode3ForWrapperCircuit, + CompressionMode4Circuit, CompressionMode4ForWrapperCircuit, CompressionMode5Circuit, + CompressionMode5ForWrapperCircuit, ProofCompressionFunction, +}; +use circuit_definitions::circuit_definitions::aux_layer::compression_modes::{ + CompressionMode1, CompressionMode2, CompressionMode3, CompressionMode4, +}; +use circuit_definitions::circuit_definitions::aux_layer::{ + ZkSyncCompressionForWrapperCircuit, ZkSyncCompressionLayerCircuit, +}; +use circuit_definitions::circuit_definitions::base_layer::ZkSyncBaseLayerCircuit; +use circuit_definitions::circuit_definitions::recursion_layer::leaf_layer::ZkSyncLeafLayerRecursiveCircuit; +use circuit_definitions::circuit_definitions::recursion_layer::{ + base_circuit_type_into_recursive_leaf_circuit_type, circuit_def, + ZkSyncRecursionLayerStorageType, ZkSyncRecursiveLayerCircuit, RECURSION_ARITY, + SCHEDULER_CAPACITY, +}; +use circuit_definitions::circuit_definitions::ZkSyncUniformCircuitInstance; +use circuit_definitions::recursion_layer_proof_config; +use circuit_definitions::zkevm_circuits::eip_4844::input::ELEMENTS_PER_4844_BLOCK; +use circuit_definitions::zkevm_circuits::recursion::compression::CompressionRecursionConfig; +use circuit_definitions::zkevm_circuits::recursion::leaf_layer::input::RecursionLeafParametersWitness; +use circuit_definitions::zkevm_circuits::recursion::NUM_BASE_LAYER_CIRCUITS; +use circuit_definitions::zkevm_circuits::scheduler::aux::BaseLayerCircuitType; +use circuit_sequencer_api::toolset::GeometryConfig; +use crossbeam::atomic::AtomicCell; +use std::collections::VecDeque; +use std::sync::Arc; + +use crate::zkevm_circuits::base_structures::vm_state::FULL_SPONGE_QUEUE_STATE_WIDTH; + +mod full; +pub mod light; + +pub use full::*; + +fn get_compression_circuits( + source: &mut dyn SetupDataSource, +) -> Vec { + vec![ + ZkSyncCompressionLayerCircuit::CompressionMode1Circuit(CompressionMode1Circuit { + witness: None, + config: CompressionRecursionConfig { + proof_config: recursion_layer_proof_config(), + // recursion circuit 1 is scheduler + verification_key: source.get_recursion_layer_vk(1).unwrap().into_inner(), + _marker: Default::default(), + }, + transcript_params: (), + _marker: Default::default(), + }), + ZkSyncCompressionLayerCircuit::CompressionMode2Circuit(CompressionMode2Circuit { + witness: None, + config: CompressionRecursionConfig { + proof_config: CompressionMode1::proof_config_for_compression_step(), + verification_key: source.get_compression_vk(1).unwrap().into_inner(), + _marker: Default::default(), + }, + transcript_params: (), + _marker: Default::default(), + }), + ZkSyncCompressionLayerCircuit::CompressionMode3Circuit(CompressionMode3Circuit { + witness: None, + config: CompressionRecursionConfig { + proof_config: CompressionMode2::proof_config_for_compression_step(), + verification_key: source.get_compression_vk(2).unwrap().into_inner(), + _marker: Default::default(), + }, + transcript_params: (), + _marker: Default::default(), + }), + ZkSyncCompressionLayerCircuit::CompressionMode4Circuit(CompressionMode4Circuit { + witness: None, + config: CompressionRecursionConfig { + proof_config: CompressionMode3::proof_config_for_compression_step(), + verification_key: source.get_compression_vk(3).unwrap().into_inner(), + _marker: Default::default(), + }, + transcript_params: (), + _marker: Default::default(), + }), + ZkSyncCompressionLayerCircuit::CompressionMode5Circuit(CompressionMode5Circuit { + witness: None, + config: CompressionRecursionConfig { + proof_config: CompressionMode4::proof_config_for_compression_step(), + verification_key: source.get_compression_vk(4).unwrap().into_inner(), + _marker: Default::default(), + }, + transcript_params: (), + _marker: Default::default(), + }), + ] +} + +fn get_compression_for_wrapper_circuits( + source: &mut dyn SetupDataSource, +) -> Vec { + vec![ + ZkSyncCompressionForWrapperCircuit::CompressionMode1Circuit( + CompressionMode1ForWrapperCircuit { + witness: None, + config: CompressionRecursionConfig { + proof_config: recursion_layer_proof_config(), + verification_key: source.get_recursion_layer_vk(1).unwrap().into_inner(), + _marker: Default::default(), + }, + transcript_params: (), + _marker: Default::default(), + }, + ), + ZkSyncCompressionForWrapperCircuit::CompressionMode2Circuit( + CompressionMode2ForWrapperCircuit { + witness: None, + config: CompressionRecursionConfig { + proof_config: CompressionMode1::proof_config_for_compression_step(), + verification_key: source.get_compression_vk(1).unwrap().into_inner(), + _marker: Default::default(), + }, + transcript_params: (), + _marker: Default::default(), + }, + ), + ZkSyncCompressionForWrapperCircuit::CompressionMode3Circuit( + CompressionMode3ForWrapperCircuit { + witness: None, + config: CompressionRecursionConfig { + proof_config: CompressionMode2::proof_config_for_compression_step(), + verification_key: source.get_compression_vk(2).unwrap().into_inner(), + _marker: Default::default(), + }, + transcript_params: (), + _marker: Default::default(), + }, + ), + ZkSyncCompressionForWrapperCircuit::CompressionMode4Circuit( + CompressionMode4ForWrapperCircuit { + witness: None, + config: CompressionRecursionConfig { + proof_config: CompressionMode3::proof_config_for_compression_step(), + verification_key: source.get_compression_vk(3).unwrap().into_inner(), + _marker: Default::default(), + }, + transcript_params: (), + _marker: Default::default(), + }, + ), + ZkSyncCompressionForWrapperCircuit::CompressionMode5Circuit( + CompressionMode5ForWrapperCircuit { + witness: None, + config: CompressionRecursionConfig { + proof_config: CompressionMode4::proof_config_for_compression_step(), + verification_key: source.get_compression_vk(4).unwrap().into_inner(), + _marker: Default::default(), + }, + transcript_params: (), + _marker: Default::default(), + }, + ), + ] +} + +/// Returns all types of basic circuits, with empty witnesses. +/// Can be used for things like verification key generation. +fn get_all_basic_circuits(geometry: &GeometryConfig) -> Vec { + vec![ + ZkSyncBaseLayerCircuit::MainVM(ZkSyncUniformCircuitInstance { + witness: AtomicCell::new(None), + config: Arc::new(geometry.cycles_per_vm_snapshot as usize), + round_function: Arc::new(Poseidon2Goldilocks), + expected_public_input: None, + }), + ZkSyncBaseLayerCircuit::CodeDecommittmentsSorter(ZkSyncUniformCircuitInstance { + witness: AtomicCell::new(None), + config: Arc::new(geometry.cycles_code_decommitter_sorter as usize), + round_function: Arc::new(Poseidon2Goldilocks), + expected_public_input: None, + }), + ZkSyncBaseLayerCircuit::CodeDecommitter(ZkSyncUniformCircuitInstance { + witness: AtomicCell::new(None), + + config: Arc::new(geometry.cycles_per_code_decommitter as usize), + round_function: Arc::new(Poseidon2Goldilocks), + expected_public_input: None, + }), + ZkSyncBaseLayerCircuit::LogDemuxer(ZkSyncUniformCircuitInstance { + witness: AtomicCell::new(None), + config: Arc::new(geometry.cycles_per_log_demuxer as usize), + round_function: Arc::new(Poseidon2Goldilocks), + expected_public_input: None, + }), + ZkSyncBaseLayerCircuit::KeccakRoundFunction(ZkSyncUniformCircuitInstance { + witness: AtomicCell::new(None), + config: Arc::new(geometry.cycles_per_keccak256_circuit as usize), + round_function: Arc::new(Poseidon2Goldilocks), + expected_public_input: None, + }), + ZkSyncBaseLayerCircuit::Sha256RoundFunction(ZkSyncUniformCircuitInstance { + witness: AtomicCell::new(None), + config: Arc::new(geometry.cycles_per_sha256_circuit as usize), + round_function: Arc::new(Poseidon2Goldilocks), + expected_public_input: None, + }), + ZkSyncBaseLayerCircuit::ECRecover(ZkSyncUniformCircuitInstance { + witness: AtomicCell::new(None), + config: Arc::new(geometry.cycles_per_ecrecover_circuit as usize), + round_function: Arc::new(Poseidon2Goldilocks), + expected_public_input: None, + }), + ZkSyncBaseLayerCircuit::RAMPermutation(ZkSyncUniformCircuitInstance { + witness: AtomicCell::new(None), + config: Arc::new(geometry.cycles_per_ram_permutation as usize), + round_function: Arc::new(Poseidon2Goldilocks), + expected_public_input: None, + }), + ZkSyncBaseLayerCircuit::StorageSorter(ZkSyncUniformCircuitInstance { + witness: AtomicCell::new(None), + config: Arc::new(geometry.cycles_per_storage_sorter as usize), + round_function: Arc::new(Poseidon2Goldilocks), + expected_public_input: None, + }), + ZkSyncBaseLayerCircuit::StorageApplication(ZkSyncUniformCircuitInstance { + witness: AtomicCell::new(None), + config: Arc::new(geometry.cycles_per_storage_application as usize), + round_function: Arc::new(Poseidon2Goldilocks), + expected_public_input: None, + }), + ZkSyncBaseLayerCircuit::EventsSorter(ZkSyncUniformCircuitInstance { + witness: AtomicCell::new(None), + config: Arc::new(geometry.cycles_per_events_or_l1_messages_sorter as usize), + round_function: Arc::new(Poseidon2Goldilocks), + expected_public_input: None, + }), + ZkSyncBaseLayerCircuit::L1MessagesSorter(ZkSyncUniformCircuitInstance { + witness: AtomicCell::new(None), + config: Arc::new(geometry.cycles_per_events_or_l1_messages_sorter as usize), + round_function: Arc::new(Poseidon2Goldilocks), + expected_public_input: None, + }), + ZkSyncBaseLayerCircuit::L1MessagesHasher(ZkSyncUniformCircuitInstance { + witness: AtomicCell::new(None), + config: Arc::new(geometry.limit_for_l1_messages_pudata_hasher as usize), + round_function: Arc::new(Poseidon2Goldilocks), + expected_public_input: None, + }), + ZkSyncBaseLayerCircuit::TransientStorageSorter(ZkSyncUniformCircuitInstance { + witness: AtomicCell::new(None), + config: Arc::new(geometry.cycles_per_transient_storage_sorter as usize), + round_function: Arc::new(Poseidon2Goldilocks), + expected_public_input: None, + }), + ZkSyncBaseLayerCircuit::Secp256r1Verify(ZkSyncUniformCircuitInstance { + witness: AtomicCell::new(None), + config: Arc::new(geometry.cycles_per_secp256r1_verify_circuit as usize), + round_function: Arc::new(Poseidon2Goldilocks), + expected_public_input: None, + }), + ZkSyncBaseLayerCircuit::EIP4844Repack(ZkSyncUniformCircuitInstance { + witness: AtomicCell::new(None), + config: Arc::new(ELEMENTS_PER_4844_BLOCK as usize), + round_function: Arc::new(Poseidon2Goldilocks), + expected_public_input: None, + }), + ] +} + +/// Returns all the recursive circuits (including leaves, nodes and scheduler). +/// Source must contain the verification keys for basic layer, leaf and node. +fn get_all_recursive_circuits( + source: &mut dyn SetupDataSource, +) -> crate::data_source::SourceResult> { + let mut result = get_leaf_circuits(source)?; + + result.push(get_node_circuit(source)?); + result.push(get_recursion_tip_circuit(source)?); + result.push(get_scheduler_circuit(source)?); + return Ok(result); +} + +/// Returns all the leaf circuits. +fn get_leaf_circuits( + source: &mut dyn SetupDataSource, +) -> crate::data_source::SourceResult> { + let mut result = vec![]; + + for base_circuit_type in ((BaseLayerCircuitType::VM as u8) + ..=(BaseLayerCircuitType::Secp256r1Verify as u8)) + .chain(std::iter::once(BaseLayerCircuitType::EIP4844Repack as u8)) + { + let _recursive_circuit_type = base_circuit_type_into_recursive_leaf_circuit_type( + BaseLayerCircuitType::from_numeric_value(base_circuit_type), + ); + + use crate::zkevm_circuits::recursion::leaf_layer::input::*; + let input = RecursionLeafInput::placeholder_witness(); + let vk = source.get_base_layer_vk(base_circuit_type)?; + + use crate::boojum::gadgets::queue::full_state_queue::FullStateCircuitQueueRawWitness; + let witness = RecursionLeafInstanceWitness { + input, + vk_witness: vk.clone().into_inner(), + queue_witness: FullStateCircuitQueueRawWitness { + elements: VecDeque::new(), + }, + proof_witnesses: VecDeque::new(), + }; + + use crate::zkevm_circuits::recursion::leaf_layer::LeafLayerRecursionConfig; + let config = LeafLayerRecursionConfig { + proof_config: recursion_layer_proof_config(), + vk_fixed_parameters: vk.into_inner().fixed_parameters, + capacity: RECURSION_ARITY, + _marker: std::marker::PhantomData, + }; + + let circuit = ZkSyncLeafLayerRecursiveCircuit { + base_layer_circuit_type: BaseLayerCircuitType::from_numeric_value(base_circuit_type), + witness: witness, + config: config, + transcript_params: (), + _marker: std::marker::PhantomData, + }; + + let circuit = ZkSyncRecursiveLayerCircuit::leaf_circuit_from_base_type( + BaseLayerCircuitType::from_numeric_value(base_circuit_type), + circuit, + ); + result.push(circuit); + } + return Ok(result); +} + +/// Returns the node circuit. +fn get_node_circuit( + source: &mut dyn SetupDataSource, +) -> crate::data_source::SourceResult { + use crate::zkevm_circuits::recursion::node_layer::input::*; + let input = RecursionNodeInput::placeholder_witness(); + let vk = source + .get_recursion_layer_vk(ZkSyncRecursionLayerStorageType::LeafLayerCircuitForMainVM as u8)?; + + // the only thing to setup here is to have proper number of split points + use crate::boojum::gadgets::queue::QueueTailState; + let split_points = vec![ + QueueTailState::::placeholder_witness(); + RECURSION_ARITY - 1 + ]; + let witness = RecursionNodeInstanceWitness { + input, + vk_witness: vk.clone().into_inner(), + split_points: split_points.into(), + proof_witnesses: VecDeque::new(), + }; + + use crate::zkevm_circuits::recursion::node_layer::NodeLayerRecursionConfig; + use circuit_definitions::circuit_definitions::recursion_layer::node_layer::ZkSyncNodeLayerRecursiveCircuit; + let config = NodeLayerRecursionConfig { + proof_config: recursion_layer_proof_config(), + vk_fixed_parameters: vk.into_inner().fixed_parameters, + leaf_layer_capacity: RECURSION_ARITY, + node_layer_capacity: RECURSION_ARITY, + _marker: std::marker::PhantomData, + }; + let circuit = ZkSyncNodeLayerRecursiveCircuit { + witness, + config, + transcript_params: (), + _marker: std::marker::PhantomData, + }; + + Ok(ZkSyncRecursiveLayerCircuit::NodeLayerCircuit(circuit)) +} + +/// Returns the recursion tip circuit +fn get_recursion_tip_circuit( + source: &mut dyn SetupDataSource, +) -> crate::data_source::SourceResult { + use crate::zkevm_circuits::recursion::recursion_tip::input::*; + let input = RecursionTipInput::placeholder_witness(); + let vk = source.get_recursion_layer_node_vk()?.into_inner(); + + let witness = RecursionTipInstanceWitness { + input, + vk_witness: vk.clone(), + proof_witnesses: VecDeque::new(), + }; + + use crate::zkevm_circuits::recursion::recursion_tip::*; + use circuit_definitions::circuit_definitions::recursion_layer::recursion_tip::*; + + let config = RecursionTipConfig { + proof_config: recursion_layer_proof_config(), + vk_fixed_parameters: vk.fixed_parameters, + _marker: std::marker::PhantomData, + }; + + let circuit = RecursionTipCircuit { + witness, + config, + transcript_params: (), + _marker: std::marker::PhantomData, + }; + + Ok(ZkSyncRecursiveLayerCircuit::RecursionTipCircuit(circuit)) +} + +/// Returns the scheduler circuit. +/// Source must contain the leafs, node and tip verification keys. +fn get_scheduler_circuit( + source: &mut dyn SetupDataSource, +) -> crate::data_source::SourceResult { + use crate::zkevm_circuits::scheduler::SchedulerConfig; + use circuit_definitions::circuit_definitions::recursion_layer::scheduler::SchedulerCircuit; + + println!("Computing leaf params"); + let leaf_layer_params = compute_leaf_params(source)?; + println!("Obtaining node VK"); + let node_vk = source.get_recursion_layer_node_vk()?.into_inner(); + println!("Obtaining recursion tip VK"); + let recursion_tip_vk = source.get_recursion_tip_vk()?.into_inner(); + + let leaf_layer_params: [RecursionLeafParametersWitness; + NUM_BASE_LAYER_CIRCUITS] = leaf_layer_params + .into_iter() + .map(|el| el.1) + .collect::>() + .try_into() + .unwrap(); + + let config = SchedulerConfig { + proof_config: recursion_layer_proof_config(), + leaf_layer_parameters: leaf_layer_params, + node_layer_vk: node_vk, + recursion_tip_vk: recursion_tip_vk.clone(), + vk_fixed_parameters: recursion_tip_vk.fixed_parameters.clone(), + capacity: SCHEDULER_CAPACITY, + _marker: std::marker::PhantomData, + }; + + use crate::zkevm_circuits::scheduler::input::SchedulerCircuitInstanceWitness; + let scheduler_witness = SchedulerCircuitInstanceWitness::placeholder(); + + let scheduler_circuit = SchedulerCircuit { + witness: scheduler_witness, + config, + transcript_params: (), + _marker: std::marker::PhantomData, + }; + + Ok(ZkSyncRecursiveLayerCircuit::SchedulerCircuit( + scheduler_circuit, + )) +} diff --git a/crates/zkevm_test_harness/src/prover_utils.rs b/crates/zkevm_test_harness/src/prover_utils/full.rs similarity index 62% rename from crates/zkevm_test_harness/src/prover_utils.rs rename to crates/zkevm_test_harness/src/prover_utils/full.rs index 6cb4011..8da55d6 100644 --- a/crates/zkevm_test_harness/src/prover_utils.rs +++ b/crates/zkevm_test_harness/src/prover_utils/full.rs @@ -20,10 +20,10 @@ use circuit_definitions::circuit_definitions::verifier_builder::dyn_verifier_bui use circuit_definitions::ZkSyncDefaultRoundFunction; -use rescue_poseidon::poseidon2::transcript::Poseidon2Transcript; -use rescue_poseidon::poseidon2::Poseidon2Sponge; -use snark_wrapper::franklin_crypto::bellman::pairing::bn256::{Bn256, Fr}; -use snark_wrapper::implementations::poseidon2::tree_hasher::AbsorptionModeReplacement; +use crate::rescue_poseidon::poseidon2::transcript::Poseidon2Transcript; +use crate::rescue_poseidon::poseidon2::Poseidon2Sponge; +use crate::snark_wrapper::franklin_crypto::bellman::pairing::bn256::{Bn256, Fr}; +use crate::snark_wrapper::implementations::poseidon2::tree_hasher::AbsorptionModeReplacement; type F = GoldilocksField; type P = GoldilocksField; @@ -48,149 +48,7 @@ pub fn create_base_layer_setup_data( DenseWitnessCopyHint, FinalizationHintsForProver, ) { - use crate::boojum::cs::cs_builder::new_builder; - use crate::boojum::cs::cs_builder_reference::CsReferenceImplementationBuilder; - - let geometry = circuit.geometry(); - let (max_trace_len, num_vars) = circuit.size_hint(); - - let builder_impl = CsReferenceImplementationBuilder::::new( - geometry, - max_trace_len.unwrap(), - ); - - let builder = new_builder::<_, GoldilocksField>(builder_impl); - - let (cs, finalization_hint) = match circuit { - ZkSyncBaseLayerCircuit::MainVM(inner) => { - let builder = inner.configure_builder_proxy(builder); - let mut cs = builder.build(num_vars.unwrap()); - inner.add_tables_proxy(&mut cs); - inner.synthesize_proxy(&mut cs); - let (_, finalization_hint) = cs.pad_and_shrink(); - (cs.into_assembly::(), finalization_hint) - } - ZkSyncBaseLayerCircuit::CodeDecommittmentsSorter(inner) => { - let builder = inner.configure_builder_proxy(builder); - let mut cs = builder.build(num_vars.unwrap()); - inner.add_tables_proxy(&mut cs); - inner.synthesize_proxy(&mut cs); - let (_, finalization_hint) = cs.pad_and_shrink(); - (cs.into_assembly::(), finalization_hint) - } - ZkSyncBaseLayerCircuit::CodeDecommitter(inner) => { - let builder = inner.configure_builder_proxy(builder); - let mut cs = builder.build(num_vars.unwrap()); - inner.add_tables_proxy(&mut cs); - inner.synthesize_proxy(&mut cs); - let (_, finalization_hint) = cs.pad_and_shrink(); - (cs.into_assembly::(), finalization_hint) - } - ZkSyncBaseLayerCircuit::LogDemuxer(inner) => { - let builder = inner.configure_builder_proxy(builder); - let mut cs = builder.build(num_vars.unwrap()); - inner.add_tables_proxy(&mut cs); - inner.synthesize_proxy(&mut cs); - let (_, finalization_hint) = cs.pad_and_shrink(); - (cs.into_assembly::(), finalization_hint) - } - ZkSyncBaseLayerCircuit::KeccakRoundFunction(inner) => { - let builder = inner.configure_builder_proxy(builder); - let mut cs = builder.build(num_vars.unwrap()); - inner.add_tables_proxy(&mut cs); - inner.synthesize_proxy(&mut cs); - let (_, finalization_hint) = cs.pad_and_shrink(); - (cs.into_assembly::(), finalization_hint) - } - ZkSyncBaseLayerCircuit::Sha256RoundFunction(inner) => { - let builder = inner.configure_builder_proxy(builder); - let mut cs = builder.build(num_vars.unwrap()); - inner.add_tables_proxy(&mut cs); - inner.synthesize_proxy(&mut cs); - let (_, finalization_hint) = cs.pad_and_shrink(); - (cs.into_assembly::(), finalization_hint) - } - ZkSyncBaseLayerCircuit::ECRecover(inner) => { - let builder = inner.configure_builder_proxy(builder); - let mut cs = builder.build(num_vars.unwrap()); - inner.add_tables_proxy(&mut cs); - inner.synthesize_proxy(&mut cs); - let (_, finalization_hint) = cs.pad_and_shrink(); - (cs.into_assembly::(), finalization_hint) - } - ZkSyncBaseLayerCircuit::RAMPermutation(inner) => { - let builder = inner.configure_builder_proxy(builder); - let mut cs = builder.build(num_vars.unwrap()); - inner.add_tables_proxy(&mut cs); - inner.synthesize_proxy(&mut cs); - let (_, finalization_hint) = cs.pad_and_shrink(); - (cs.into_assembly::(), finalization_hint) - } - ZkSyncBaseLayerCircuit::StorageSorter(inner) => { - let builder = inner.configure_builder_proxy(builder); - let mut cs = builder.build(num_vars.unwrap()); - inner.add_tables_proxy(&mut cs); - inner.synthesize_proxy(&mut cs); - let (_, finalization_hint) = cs.pad_and_shrink(); - (cs.into_assembly::(), finalization_hint) - } - ZkSyncBaseLayerCircuit::StorageApplication(inner) => { - let builder = inner.configure_builder_proxy(builder); - let mut cs = builder.build(num_vars.unwrap()); - inner.add_tables_proxy(&mut cs); - inner.synthesize_proxy(&mut cs); - let (_, finalization_hint) = cs.pad_and_shrink(); - (cs.into_assembly::(), finalization_hint) - } - ZkSyncBaseLayerCircuit::EventsSorter(inner) => { - let builder = inner.configure_builder_proxy(builder); - let mut cs = builder.build(num_vars.unwrap()); - inner.add_tables_proxy(&mut cs); - inner.synthesize_proxy(&mut cs); - let (_, finalization_hint) = cs.pad_and_shrink(); - (cs.into_assembly::(), finalization_hint) - } - ZkSyncBaseLayerCircuit::L1MessagesSorter(inner) => { - let builder = inner.configure_builder_proxy(builder); - let mut cs = builder.build(num_vars.unwrap()); - inner.add_tables_proxy(&mut cs); - inner.synthesize_proxy(&mut cs); - let (_, finalization_hint) = cs.pad_and_shrink(); - (cs.into_assembly::(), finalization_hint) - } - ZkSyncBaseLayerCircuit::L1MessagesHasher(inner) => { - let builder = inner.configure_builder_proxy(builder); - let mut cs = builder.build(num_vars.unwrap()); - inner.add_tables_proxy(&mut cs); - inner.synthesize_proxy(&mut cs); - let (_, finalization_hint) = cs.pad_and_shrink(); - (cs.into_assembly::(), finalization_hint) - } - ZkSyncBaseLayerCircuit::TransientStorageSorter(inner) => { - let builder = inner.configure_builder_proxy(builder); - let mut cs = builder.build(num_vars.unwrap()); - inner.add_tables_proxy(&mut cs); - inner.synthesize_proxy(&mut cs); - let (_, finalization_hint) = cs.pad_and_shrink(); - (cs.into_assembly::(), finalization_hint) - } - ZkSyncBaseLayerCircuit::Secp256r1Verify(inner) => { - let builder = inner.configure_builder_proxy(builder); - let mut cs = builder.build(num_vars.unwrap()); - inner.add_tables_proxy(&mut cs); - inner.synthesize_proxy(&mut cs); - let (_, finalization_hint) = cs.pad_and_shrink(); - (cs.into_assembly::(), finalization_hint) - } - ZkSyncBaseLayerCircuit::EIP4844Repack(inner) => { - let builder = inner.configure_builder_proxy(builder); - let mut cs = builder.build(num_vars.unwrap()); - inner.add_tables_proxy(&mut cs); - inner.synthesize_proxy(&mut cs); - let (_, finalization_hint) = cs.pad_and_shrink(); - (cs.into_assembly::(), finalization_hint) - } - }; + let (cs, finalization_hint) = get_cs_finalization_hint_for_base_layer(circuit); let (setup_base, setup, vk, setup_tree, vars_hint, witness_hints) = cs.get_full_setup(worker, fri_lde_factor, merkle_tree_cap_size); @@ -414,69 +272,7 @@ pub fn create_recursive_layer_setup_data( DenseWitnessCopyHint, FinalizationHintsForProver, ) { - use crate::boojum::cs::cs_builder::new_builder; - use crate::boojum::cs::cs_builder_reference::CsReferenceImplementationBuilder; - - let round_function = ZkSyncDefaultRoundFunction::default(); - - let geometry = circuit.geometry(); - let (max_trace_len, num_vars) = circuit.size_hint(); - - let builder_impl = CsReferenceImplementationBuilder::::new( - geometry, - max_trace_len.unwrap(), - ); - let builder = new_builder::<_, GoldilocksField>(builder_impl); - - let (cs, finalization_hint) = match circuit { - ZkSyncRecursiveLayerCircuit::SchedulerCircuit(inner) => { - let builder = inner.configure_builder_proxy(builder); - let mut cs = builder.build(num_vars.unwrap()); - inner.add_tables(&mut cs); - inner.synthesize_into_cs(&mut cs, &round_function); - let (_, finalization_hint) = cs.pad_and_shrink(); - (cs.into_assembly::(), finalization_hint) - } - ZkSyncRecursiveLayerCircuit::NodeLayerCircuit(inner) => { - let builder = inner.configure_builder_proxy(builder); - let mut cs = builder.build(num_vars.unwrap()); - inner.add_tables(&mut cs); - inner.synthesize_into_cs(&mut cs, &round_function); - let (_, finalization_hint) = cs.pad_and_shrink(); - (cs.into_assembly::(), finalization_hint) - } - ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForMainVM(inner) - | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForCodeDecommittmentsSorter(inner) - | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForCodeDecommitter(inner) - | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForLogDemuxer(inner) - | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForKeccakRoundFunction(inner) - | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForSha256RoundFunction(inner) - | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForECRecover(inner) - | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForRAMPermutation(inner) - | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForStorageSorter(inner) - | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForStorageApplication(inner) - | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForEventsSorter(inner) - | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForL1MessagesSorter(inner) - | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForL1MessagesHasher(inner) - | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForTransientStorageSorter(inner) - | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForSecp256r1Verify(inner) - | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForEIP4844Repack(inner) => { - let builder = inner.configure_builder_proxy(builder); - let mut cs = builder.build(num_vars.unwrap()); - inner.add_tables(&mut cs); - inner.synthesize_into_cs(&mut cs, &round_function); - let (_, finalization_hint) = cs.pad_and_shrink(); - (cs.into_assembly::(), finalization_hint) - } - ZkSyncRecursiveLayerCircuit::RecursionTipCircuit(inner) => { - let builder = inner.configure_builder_proxy(builder); - let mut cs = builder.build(num_vars.unwrap()); - inner.add_tables(&mut cs); - inner.synthesize_into_cs(&mut cs, &round_function); - let (_, finalization_hint) = cs.pad_and_shrink(); - (cs.into_assembly::(), finalization_hint) - } - }; + let (cs, finalization_hint) = get_cs_finalization_hint_for_recursive_layer(circuit); let (setup_base, setup, vk, setup_tree, vars_hint, witness_hints) = cs.get_full_setup(worker, fri_lde_factor, merkle_tree_cap_size); @@ -621,41 +417,7 @@ pub fn create_compression_layer_setup_data( DenseWitnessCopyHint, FinalizationHintsForProver, ) { - use crate::boojum::cs::cs_builder::new_builder; - use crate::boojum::cs::cs_builder_reference::CsReferenceImplementationBuilder; - - fn synthesize_inner( - circuit: CompressionLayerCircuit, - ) -> ( - CSReferenceAssembly, - FinalizationHintsForProver, - ) { - let geometry = circuit.geometry(); - let (max_trace_len, num_vars) = circuit.size_hint(); - - let builder_impl = - CsReferenceImplementationBuilder::::new( - geometry, - max_trace_len.unwrap(), - ); - let builder = new_builder::<_, GoldilocksField>(builder_impl); - - let builder = circuit.configure_builder_proxy(builder); - let mut cs = builder.build(num_vars.unwrap()); - circuit.add_tables(&mut cs); - circuit.synthesize_into_cs(&mut cs); - let (_, finalization_hint) = cs.pad_and_shrink(); - (cs.into_assembly::(), finalization_hint) - } - - let (cs, finalization_hint) = match circuit { - ZkSyncCompressionLayerCircuit::CompressionMode1Circuit(inner) => synthesize_inner(inner), - ZkSyncCompressionLayerCircuit::CompressionMode2Circuit(inner) => synthesize_inner(inner), - ZkSyncCompressionLayerCircuit::CompressionMode3Circuit(inner) => synthesize_inner(inner), - ZkSyncCompressionLayerCircuit::CompressionMode4Circuit(inner) => synthesize_inner(inner), - ZkSyncCompressionLayerCircuit::CompressionMode5Circuit(inner) => synthesize_inner(inner), - }; - + let (cs, finalization_hint) = get_cs_finalization_hint_for_compression(circuit); let (setup_base, setup, vk, setup_tree, vars_hint, witness_hints) = cs.get_full_setup(worker, fri_lde_factor, merkle_tree_cap_size); @@ -765,50 +527,7 @@ pub fn create_compression_for_wrapper_setup_data( DenseWitnessCopyHint, FinalizationHintsForProver, ) { - use crate::boojum::cs::cs_builder::new_builder; - use crate::boojum::cs::cs_builder_reference::CsReferenceImplementationBuilder; - - fn synthesize_inner( - circuit: CompressionLayerCircuit, - ) -> ( - CSReferenceAssembly, - FinalizationHintsForProver, - ) { - let geometry = circuit.geometry(); - let (max_trace_len, num_vars) = circuit.size_hint(); - - let builder_impl = - CsReferenceImplementationBuilder::::new( - geometry, - max_trace_len.unwrap(), - ); - let builder = new_builder::<_, GoldilocksField>(builder_impl); - - let builder = circuit.configure_builder_proxy(builder); - let mut cs = builder.build(num_vars.unwrap()); - circuit.add_tables(&mut cs); - circuit.synthesize_into_cs(&mut cs); - let (_, finalization_hint) = cs.pad_and_shrink(); - (cs.into_assembly::(), finalization_hint) - } - - let (cs, finalization_hint) = match circuit { - ZkSyncCompressionForWrapperCircuit::CompressionMode1Circuit(inner) => { - synthesize_inner(inner) - } - ZkSyncCompressionForWrapperCircuit::CompressionMode2Circuit(inner) => { - synthesize_inner(inner) - } - ZkSyncCompressionForWrapperCircuit::CompressionMode3Circuit(inner) => { - synthesize_inner(inner) - } - ZkSyncCompressionForWrapperCircuit::CompressionMode4Circuit(inner) => { - synthesize_inner(inner) - } - ZkSyncCompressionForWrapperCircuit::CompressionMode5Circuit(inner) => { - synthesize_inner(inner) - } - }; + let (cs, finalization_hint) = get_cs_finalization_hint_for_compression_for_wrapper(circuit); let (setup_base, setup, vk, setup_tree, vars_hint, witness_hints) = cs.get_full_setup(worker, fri_lde_factor, merkle_tree_cap_size); diff --git a/crates/zkevm_test_harness/src/prover_utils/light.rs b/crates/zkevm_test_harness/src/prover_utils/light.rs new file mode 100644 index 0000000..821d514 --- /dev/null +++ b/crates/zkevm_test_harness/src/prover_utils/light.rs @@ -0,0 +1,136 @@ +use super::*; + +use crate::boojum::algebraic_props::round_function::AbsorptionModeOverwrite; +use crate::boojum::algebraic_props::sponge::GoldilocksPoseidon2Sponge; +use crate::boojum::config::*; +use crate::boojum::cs::implementations::hints::*; +use crate::boojum::cs::implementations::polynomial_storage::*; +use crate::boojum::cs::implementations::verifier::*; +use crate::boojum::cs::oracle::merkle_tree::*; +use crate::boojum::field::goldilocks::GoldilocksExt2; +use crate::boojum::worker::Worker; +use crate::GoldilocksField; + +use circuit_definitions::boojum::cs::implementations::reference_cs::CSReferenceAssembly; +use circuit_definitions::circuit_definitions::aux_layer::{compression::*, *}; +use circuit_definitions::circuit_definitions::base_layer::ZkSyncBaseLayerCircuit; +use circuit_definitions::circuit_definitions::recursion_layer::verifier_builder::dyn_verifier_builder_for_recursive_circuit_type; +use circuit_definitions::circuit_definitions::recursion_layer::*; +use circuit_definitions::circuit_definitions::verifier_builder::dyn_verifier_builder_for_circuit_type; + +use circuit_definitions::ZkSyncDefaultRoundFunction; + +use crate::rescue_poseidon::poseidon2::transcript::Poseidon2Transcript; +use crate::rescue_poseidon::poseidon2::Poseidon2Sponge; +use crate::snark_wrapper::franklin_crypto::bellman::pairing::bn256::{Bn256, Fr}; +use crate::snark_wrapper::implementations::poseidon2::tree_hasher::AbsorptionModeReplacement; + +type F = GoldilocksField; + +type EXT = GoldilocksExt2; +type H = GoldilocksPoseidon2Sponge; + +use crate::boojum::cs::implementations::setup::FinalizationHintsForProver; + +pub fn create_light_base_layer_setup_data( + circuit: ZkSyncBaseLayerCircuit, + worker: &Worker, + fri_lde_factor: usize, + merkle_tree_cap_size: usize, +) -> ( + SetupBaseStorage, + VerificationKeyCircuitGeometry, + DenseVariablesCopyHint, + DenseWitnessCopyHint, + FinalizationHintsForProver, +) { + let (cs, finalization_hint) = get_cs_finalization_hint_for_base_layer(circuit); + + let (setup_base, vk_geometry, vars_hint, witness_hint) = + cs.get_light_setup(worker, fri_lde_factor, merkle_tree_cap_size); + + ( + setup_base, + vk_geometry, + vars_hint, + witness_hint, + finalization_hint, + ) +} + +pub fn create_light_recursive_layer_setup_data( + circuit: ZkSyncRecursiveLayerCircuit, + worker: &Worker, + fri_lde_factor: usize, + merkle_tree_cap_size: usize, +) -> ( + SetupBaseStorage, + VerificationKeyCircuitGeometry, + DenseVariablesCopyHint, + DenseWitnessCopyHint, + FinalizationHintsForProver, +) { + let (cs, finalization_hint) = get_cs_finalization_hint_for_recursive_layer(circuit); + + let (setup_base, vk_geometry, vars_hint, witness_hint) = + cs.get_light_setup(worker, fri_lde_factor, merkle_tree_cap_size); + + ( + setup_base, + vk_geometry, + vars_hint, + witness_hint, + finalization_hint, + ) +} + +pub fn create_light_compression_layer_setup_data( + circuit: ZkSyncCompressionLayerCircuit, + worker: &Worker, + fri_lde_factor: usize, + merkle_tree_cap_size: usize, +) -> ( + SetupBaseStorage, + VerificationKeyCircuitGeometry, + DenseVariablesCopyHint, + DenseWitnessCopyHint, + FinalizationHintsForProver, +) { + let (cs, finalization_hint) = get_cs_finalization_hint_for_compression(circuit); + let (setup_base, vk_geometry, vars_hint, witness_hint) = + cs.get_light_setup(worker, fri_lde_factor, merkle_tree_cap_size); + + ( + setup_base, + vk_geometry, + vars_hint, + witness_hint, + finalization_hint, + ) +} + +pub fn create_light_compression_for_wrapper_setup_data( + circuit: ZkSyncCompressionForWrapperCircuit, + worker: &Worker, + fri_lde_factor: usize, + merkle_tree_cap_size: usize, +) -> ( + SetupBaseStorage, + VerificationKeyCircuitGeometry, + DenseVariablesCopyHint, + DenseWitnessCopyHint, + FinalizationHintsForProver, +) { + let (cs, finalization_hint) = get_cs_finalization_hint_for_compression_for_wrapper(circuit); + + let (setup_base, vk_geometry, vars_hint, witness_hint) = + cs.get_light_setup(worker, fri_lde_factor, merkle_tree_cap_size); + + ( + setup_base, + vk_geometry, + vars_hint, + witness_hint, + finalization_hint, + ) +} diff --git a/crates/zkevm_test_harness/src/prover_utils/mod.rs b/crates/zkevm_test_harness/src/prover_utils/mod.rs new file mode 100644 index 0000000..bbd6ca9 --- /dev/null +++ b/crates/zkevm_test_harness/src/prover_utils/mod.rs @@ -0,0 +1,337 @@ +mod full; +pub mod light; + +use crate::boojum::cs::implementations::reference_cs::CSReferenceAssembly; +use crate::boojum::cs::implementations::setup::FinalizationHintsForProver; +use circuit_definitions::boojum::config::SetupCSConfig; +use circuit_definitions::boojum::cs::cs_builder::new_builder; +use circuit_definitions::boojum::cs::cs_builder_reference::CsReferenceImplementationBuilder; +use circuit_definitions::boojum::field::goldilocks::GoldilocksField; +use circuit_definitions::circuit_definitions::aux_layer::compression::{ + CompressionLayerCircuit, ProofCompressionFunction, +}; +use circuit_definitions::circuit_definitions::aux_layer::{ + ZkSyncCompressionForWrapperCircuit, ZkSyncCompressionLayerCircuit, +}; +use circuit_definitions::circuit_definitions::base_layer::ZkSyncBaseLayerCircuit; +use circuit_definitions::circuit_definitions::recursion_layer::ZkSyncRecursiveLayerCircuit; +use circuit_definitions::ZkSyncDefaultRoundFunction; +pub use full::*; + +type P = GoldilocksField; + +fn get_cs_finalization_hint_for_base_layer( + circuit: ZkSyncBaseLayerCircuit, +) -> ( + CSReferenceAssembly, + FinalizationHintsForProver, +) { + use crate::boojum::cs::cs_builder::new_builder; + use crate::boojum::cs::cs_builder_reference::CsReferenceImplementationBuilder; + + let geometry = circuit.geometry(); + let (max_trace_len, num_vars) = circuit.size_hint(); + + let builder_impl = CsReferenceImplementationBuilder::::new( + geometry, + max_trace_len.unwrap(), + ); + + let builder = new_builder::<_, GoldilocksField>(builder_impl); + + match circuit { + ZkSyncBaseLayerCircuit::MainVM(inner) => { + let builder = inner.configure_builder_proxy(builder); + let mut cs = builder.build(num_vars.unwrap()); + inner.add_tables_proxy(&mut cs); + inner.synthesize_proxy(&mut cs); + let (_, finalization_hint) = cs.pad_and_shrink(); + (cs.into_assembly::(), finalization_hint) + } + ZkSyncBaseLayerCircuit::CodeDecommittmentsSorter(inner) => { + let builder = inner.configure_builder_proxy(builder); + let mut cs = builder.build(num_vars.unwrap()); + inner.add_tables_proxy(&mut cs); + inner.synthesize_proxy(&mut cs); + let (_, finalization_hint) = cs.pad_and_shrink(); + (cs.into_assembly::(), finalization_hint) + } + ZkSyncBaseLayerCircuit::CodeDecommitter(inner) => { + let builder = inner.configure_builder_proxy(builder); + let mut cs = builder.build(num_vars.unwrap()); + inner.add_tables_proxy(&mut cs); + inner.synthesize_proxy(&mut cs); + let (_, finalization_hint) = cs.pad_and_shrink(); + (cs.into_assembly::(), finalization_hint) + } + ZkSyncBaseLayerCircuit::LogDemuxer(inner) => { + let builder = inner.configure_builder_proxy(builder); + let mut cs = builder.build(num_vars.unwrap()); + inner.add_tables_proxy(&mut cs); + inner.synthesize_proxy(&mut cs); + let (_, finalization_hint) = cs.pad_and_shrink(); + (cs.into_assembly::(), finalization_hint) + } + ZkSyncBaseLayerCircuit::KeccakRoundFunction(inner) => { + let builder = inner.configure_builder_proxy(builder); + let mut cs = builder.build(num_vars.unwrap()); + inner.add_tables_proxy(&mut cs); + inner.synthesize_proxy(&mut cs); + let (_, finalization_hint) = cs.pad_and_shrink(); + (cs.into_assembly::(), finalization_hint) + } + ZkSyncBaseLayerCircuit::Sha256RoundFunction(inner) => { + let builder = inner.configure_builder_proxy(builder); + let mut cs = builder.build(num_vars.unwrap()); + inner.add_tables_proxy(&mut cs); + inner.synthesize_proxy(&mut cs); + let (_, finalization_hint) = cs.pad_and_shrink(); + (cs.into_assembly::(), finalization_hint) + } + ZkSyncBaseLayerCircuit::ECRecover(inner) => { + let builder = inner.configure_builder_proxy(builder); + let mut cs = builder.build(num_vars.unwrap()); + inner.add_tables_proxy(&mut cs); + inner.synthesize_proxy(&mut cs); + let (_, finalization_hint) = cs.pad_and_shrink(); + (cs.into_assembly::(), finalization_hint) + } + ZkSyncBaseLayerCircuit::RAMPermutation(inner) => { + let builder = inner.configure_builder_proxy(builder); + let mut cs = builder.build(num_vars.unwrap()); + inner.add_tables_proxy(&mut cs); + inner.synthesize_proxy(&mut cs); + let (_, finalization_hint) = cs.pad_and_shrink(); + (cs.into_assembly::(), finalization_hint) + } + ZkSyncBaseLayerCircuit::StorageSorter(inner) => { + let builder = inner.configure_builder_proxy(builder); + let mut cs = builder.build(num_vars.unwrap()); + inner.add_tables_proxy(&mut cs); + inner.synthesize_proxy(&mut cs); + let (_, finalization_hint) = cs.pad_and_shrink(); + (cs.into_assembly::(), finalization_hint) + } + ZkSyncBaseLayerCircuit::StorageApplication(inner) => { + let builder = inner.configure_builder_proxy(builder); + let mut cs = builder.build(num_vars.unwrap()); + inner.add_tables_proxy(&mut cs); + inner.synthesize_proxy(&mut cs); + let (_, finalization_hint) = cs.pad_and_shrink(); + (cs.into_assembly::(), finalization_hint) + } + ZkSyncBaseLayerCircuit::EventsSorter(inner) => { + let builder = inner.configure_builder_proxy(builder); + let mut cs = builder.build(num_vars.unwrap()); + inner.add_tables_proxy(&mut cs); + inner.synthesize_proxy(&mut cs); + let (_, finalization_hint) = cs.pad_and_shrink(); + (cs.into_assembly::(), finalization_hint) + } + ZkSyncBaseLayerCircuit::L1MessagesSorter(inner) => { + let builder = inner.configure_builder_proxy(builder); + let mut cs = builder.build(num_vars.unwrap()); + inner.add_tables_proxy(&mut cs); + inner.synthesize_proxy(&mut cs); + let (_, finalization_hint) = cs.pad_and_shrink(); + (cs.into_assembly::(), finalization_hint) + } + ZkSyncBaseLayerCircuit::L1MessagesHasher(inner) => { + let builder = inner.configure_builder_proxy(builder); + let mut cs = builder.build(num_vars.unwrap()); + inner.add_tables_proxy(&mut cs); + inner.synthesize_proxy(&mut cs); + let (_, finalization_hint) = cs.pad_and_shrink(); + (cs.into_assembly::(), finalization_hint) + } + ZkSyncBaseLayerCircuit::TransientStorageSorter(inner) => { + let builder = inner.configure_builder_proxy(builder); + let mut cs = builder.build(num_vars.unwrap()); + inner.add_tables_proxy(&mut cs); + inner.synthesize_proxy(&mut cs); + let (_, finalization_hint) = cs.pad_and_shrink(); + (cs.into_assembly::(), finalization_hint) + } + ZkSyncBaseLayerCircuit::Secp256r1Verify(inner) => { + let builder = inner.configure_builder_proxy(builder); + let mut cs = builder.build(num_vars.unwrap()); + inner.add_tables_proxy(&mut cs); + inner.synthesize_proxy(&mut cs); + let (_, finalization_hint) = cs.pad_and_shrink(); + (cs.into_assembly::(), finalization_hint) + } + ZkSyncBaseLayerCircuit::EIP4844Repack(inner) => { + let builder = inner.configure_builder_proxy(builder); + let mut cs = builder.build(num_vars.unwrap()); + inner.add_tables_proxy(&mut cs); + inner.synthesize_proxy(&mut cs); + let (_, finalization_hint) = cs.pad_and_shrink(); + (cs.into_assembly::(), finalization_hint) + } + } +} + +fn get_cs_finalization_hint_for_recursive_layer( + circuit: ZkSyncRecursiveLayerCircuit, +) -> ( + CSReferenceAssembly, + FinalizationHintsForProver, +) { + use crate::boojum::cs::cs_builder::new_builder; + use crate::boojum::cs::cs_builder_reference::CsReferenceImplementationBuilder; + + let round_function = ZkSyncDefaultRoundFunction::default(); + + let geometry = circuit.geometry(); + let (max_trace_len, num_vars) = circuit.size_hint(); + + let builder_impl = CsReferenceImplementationBuilder::::new( + geometry, + max_trace_len.unwrap(), + ); + let builder = new_builder::<_, GoldilocksField>(builder_impl); + + match circuit { + ZkSyncRecursiveLayerCircuit::SchedulerCircuit(inner) => { + let builder = inner.configure_builder_proxy(builder); + let mut cs = builder.build(num_vars.unwrap()); + inner.add_tables(&mut cs); + inner.synthesize_into_cs(&mut cs, &round_function); + let (_, finalization_hint) = cs.pad_and_shrink(); + (cs.into_assembly::(), finalization_hint) + } + ZkSyncRecursiveLayerCircuit::NodeLayerCircuit(inner) => { + let builder = inner.configure_builder_proxy(builder); + let mut cs = builder.build(num_vars.unwrap()); + inner.add_tables(&mut cs); + inner.synthesize_into_cs(&mut cs, &round_function); + let (_, finalization_hint) = cs.pad_and_shrink(); + (cs.into_assembly::(), finalization_hint) + } + ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForMainVM(inner) + | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForCodeDecommittmentsSorter(inner) + | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForCodeDecommitter(inner) + | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForLogDemuxer(inner) + | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForKeccakRoundFunction(inner) + | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForSha256RoundFunction(inner) + | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForECRecover(inner) + | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForRAMPermutation(inner) + | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForStorageSorter(inner) + | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForStorageApplication(inner) + | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForEventsSorter(inner) + | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForL1MessagesSorter(inner) + | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForL1MessagesHasher(inner) + | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForTransientStorageSorter(inner) + | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForSecp256r1Verify(inner) + | ZkSyncRecursiveLayerCircuit::LeafLayerCircuitForEIP4844Repack(inner) => { + let builder = inner.configure_builder_proxy(builder); + let mut cs = builder.build(num_vars.unwrap()); + inner.add_tables(&mut cs); + inner.synthesize_into_cs(&mut cs, &round_function); + let (_, finalization_hint) = cs.pad_and_shrink(); + (cs.into_assembly::(), finalization_hint) + } + ZkSyncRecursiveLayerCircuit::RecursionTipCircuit(inner) => { + let builder = inner.configure_builder_proxy(builder); + let mut cs = builder.build(num_vars.unwrap()); + inner.add_tables(&mut cs); + inner.synthesize_into_cs(&mut cs, &round_function); + let (_, finalization_hint) = cs.pad_and_shrink(); + (cs.into_assembly::(), finalization_hint) + } + } +} + +fn get_cs_finalization_hint_for_compression( + circuit: ZkSyncCompressionLayerCircuit, +) -> ( + CSReferenceAssembly, + FinalizationHintsForProver, +) { + use crate::boojum::cs::cs_builder::new_builder; + use crate::boojum::cs::cs_builder_reference::CsReferenceImplementationBuilder; + + fn synthesize_inner( + circuit: CompressionLayerCircuit, + ) -> ( + CSReferenceAssembly, + FinalizationHintsForProver, + ) { + let geometry = circuit.geometry(); + let (max_trace_len, num_vars) = circuit.size_hint(); + + let builder_impl = + CsReferenceImplementationBuilder::::new( + geometry, + max_trace_len.unwrap(), + ); + let builder = new_builder::<_, GoldilocksField>(builder_impl); + + let builder = circuit.configure_builder_proxy(builder); + let mut cs = builder.build(num_vars.unwrap()); + circuit.add_tables(&mut cs); + circuit.synthesize_into_cs(&mut cs); + let (_, finalization_hint) = cs.pad_and_shrink(); + (cs.into_assembly::(), finalization_hint) + } + + match circuit { + ZkSyncCompressionLayerCircuit::CompressionMode1Circuit(inner) => synthesize_inner(inner), + ZkSyncCompressionLayerCircuit::CompressionMode2Circuit(inner) => synthesize_inner(inner), + ZkSyncCompressionLayerCircuit::CompressionMode3Circuit(inner) => synthesize_inner(inner), + ZkSyncCompressionLayerCircuit::CompressionMode4Circuit(inner) => synthesize_inner(inner), + ZkSyncCompressionLayerCircuit::CompressionMode5Circuit(inner) => synthesize_inner(inner), + } +} + +fn get_cs_finalization_hint_for_compression_for_wrapper( + circuit: ZkSyncCompressionForWrapperCircuit, +) -> ( + CSReferenceAssembly, + FinalizationHintsForProver, +) { + use crate::boojum::cs::cs_builder::new_builder; + use crate::boojum::cs::cs_builder_reference::CsReferenceImplementationBuilder; + + fn synthesize_inner( + circuit: CompressionLayerCircuit, + ) -> ( + CSReferenceAssembly, + FinalizationHintsForProver, + ) { + let geometry = circuit.geometry(); + let (max_trace_len, num_vars) = circuit.size_hint(); + + let builder_impl = + CsReferenceImplementationBuilder::::new( + geometry, + max_trace_len.unwrap(), + ); + let builder = new_builder::<_, GoldilocksField>(builder_impl); + + let builder = circuit.configure_builder_proxy(builder); + let mut cs = builder.build(num_vars.unwrap()); + circuit.add_tables(&mut cs); + circuit.synthesize_into_cs(&mut cs); + let (_, finalization_hint) = cs.pad_and_shrink(); + (cs.into_assembly::(), finalization_hint) + } + + match circuit { + ZkSyncCompressionForWrapperCircuit::CompressionMode1Circuit(inner) => { + synthesize_inner(inner) + } + ZkSyncCompressionForWrapperCircuit::CompressionMode2Circuit(inner) => { + synthesize_inner(inner) + } + ZkSyncCompressionForWrapperCircuit::CompressionMode3Circuit(inner) => { + synthesize_inner(inner) + } + ZkSyncCompressionForWrapperCircuit::CompressionMode4Circuit(inner) => { + synthesize_inner(inner) + } + ZkSyncCompressionForWrapperCircuit::CompressionMode5Circuit(inner) => { + synthesize_inner(inner) + } + } +}