diff --git a/src/witness/recursive_aggregation.rs b/src/witness/recursive_aggregation.rs index ece7ef83..cd7c3e7b 100644 --- a/src/witness/recursive_aggregation.rs +++ b/src/witness/recursive_aggregation.rs @@ -67,6 +67,11 @@ pub(crate) fn compute_encodable_item_from_witness< commitment } +pub fn split_recursion_queue(queue: RecursionQueueSimulator) -> Vec> { + let round_function = ZkSyncDefaultRoundFunction::default(); + queue.split_by(RECURSION_ARITY, &round_function) +} + /// Creates leaf witnesses: each leaf aggregates RECURSION_ARITY (32) basic circuits of a given type. pub fn create_leaf_witnesses( subset: ( @@ -85,76 +90,102 @@ pub fn create_leaf_witnesses( Vec, // proofs for chunks Vec>, ) { - let round_function = ZkSyncDefaultRoundFunction::default(); - let (circuit_type, queue, closed_form_inputs) = subset; assert_eq!(queue.num_items as usize, proofs.len()); assert_eq!(circuit_type, vk.numeric_circuit_type() as u64); - let (t, params) = leaf_params; - assert_eq!(t, circuit_type as u8); + assert_eq!(leaf_params.0, circuit_type as u8); - let queue_splits = queue.split_by(RECURSION_ARITY, &round_function); + let queue_splits = split_recursion_queue(queue); let mut proofs_iter = proofs.into_iter(); let mut results = Vec::with_capacity(queue_splits.len()); let mut recursive_circuits = Vec::with_capacity(queue_splits.len()); - for el in queue_splits.iter().cloned() { - let mut proof_witnesses = VecDeque::new(); + for el in queue_splits.into_iter() { + let mut proofs = vec![]; for _ in 0..el.num_items { let t = proofs_iter.next().expect("proof"); - proof_witnesses.push_back(t.into_inner()); + proofs.push(t); } - let leaf_input = RecursionLeafInputWitness:: { - params: params.clone(), - queue_state: take_sponge_like_queue_state_from_simulator(&el), - }; - let elements: VecDeque<_> = el - .witness - .iter() - .map(|(_, old_tail, element)| (element.reflect(), *old_tail)) - .collect(); + let (circuit_type, circuit) = + create_leaf_witness(circuit_type, el.clone(), proofs, &vk, &leaf_params); - let witness = RecursionLeafInstanceWitness:: { - input: leaf_input, - vk_witness: vk.clone().into_inner(), - queue_witness: FullStateCircuitQueueRawWitness { elements: elements }, - proof_witnesses: proof_witnesses, - }; + results.push((circuit_type, el)); + recursive_circuits.push(circuit); + } - let config = LeafLayerRecursionConfig::< - F, - >>::NonCircuitSimulator, - EXT, - > { - proof_config: recursion_layer_proof_config(), - vk_fixed_parameters: vk.clone().into_inner().fixed_parameters, - capacity: RECURSION_ARITY, - _marker: std::marker::PhantomData, - }; + (results, recursive_circuits, closed_form_inputs) +} - let base_layer_circuit_type = - BaseLayerCircuitType::from_numeric_value(vk.numeric_circuit_type()); - let circuit = ZkSyncLeafLayerRecursiveCircuit { - witness, - config, - transcript_params: (), - base_layer_circuit_type, - _marker: std::marker::PhantomData, - }; +pub fn create_leaf_witness( + circuit_type: u64, // circuit type + queue: RecursionQueueSimulator, + proofs: Vec, // proofs coming from the base layer + vk: &ZkSyncBaseLayerVerificationKey, + leaf_params: &(u8, RecursionLeafParametersWitness), // (cirtuit_type, and ??) +) -> ( + u64, // type of the basic circuit + ZkSyncRecursiveLayerCircuit, // proofs for chunks +) { + assert_eq!(queue.num_items as usize, proofs.len()); + assert_eq!(circuit_type, vk.numeric_circuit_type() as u64); - let circuit = ZkSyncRecursiveLayerCircuit::leaf_circuit_from_base_type( - BaseLayerCircuitType::from_numeric_value(vk.numeric_circuit_type()), - circuit, - ); + let (t, params) = leaf_params; + assert_eq!(*t, circuit_type as u8); - results.push((circuit_type, el)); - recursive_circuits.push(circuit); + let mut proofs_iter = proofs.into_iter(); + let mut proof_witnesses = VecDeque::new(); + for _ in 0..queue.num_items { + let t = proofs_iter.next().expect("proof"); + proof_witnesses.push_back(t.into_inner()); } + let leaf_input = RecursionLeafInputWitness:: { + params: params.clone(), + queue_state: take_sponge_like_queue_state_from_simulator(&queue), + }; - (results, recursive_circuits, closed_form_inputs) + let elements: VecDeque<_> = queue + .witness + .iter() + .map(|(_, old_tail, element)| (element.reflect(), *old_tail)) + .collect(); + + let witness = RecursionLeafInstanceWitness:: { + input: leaf_input, + vk_witness: vk.clone().into_inner(), + queue_witness: FullStateCircuitQueueRawWitness { elements }, + proof_witnesses, + }; + + let config = LeafLayerRecursionConfig::< + F, + >>::NonCircuitSimulator, + EXT, + > { + proof_config: recursion_layer_proof_config(), + vk_fixed_parameters: vk.clone().into_inner().fixed_parameters, + capacity: RECURSION_ARITY, + _marker: std::marker::PhantomData, + }; + + let base_layer_circuit_type = + BaseLayerCircuitType::from_numeric_value(vk.numeric_circuit_type()); + let circuit = ZkSyncLeafLayerRecursiveCircuit { + witness, + config, + transcript_params: (), + base_layer_circuit_type, + _marker: std::marker::PhantomData, + }; + + let circuit = ZkSyncRecursiveLayerCircuit::leaf_circuit_from_base_type( + BaseLayerCircuitType::from_numeric_value(vk.numeric_circuit_type()), + circuit, + ); + + (circuit_type, circuit) } pub fn compute_leaf_params( @@ -398,7 +429,7 @@ pub fn create_node_witnesses( }; let circuit = ZkSyncNodeLayerRecursiveCircuit { - witness: witness, + witness, config: config.clone(), transcript_params: (), _marker: std::marker::PhantomData,