Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Allow processig LWG in batches #170

Merged
merged 6 commits into from
Jul 30, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 81 additions & 50 deletions src/witness/recursive_aggregation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ pub(crate) fn compute_encodable_item_from_witness<
commitment
}

pub fn split_reqursion_queue(queue: RecursionQueueSimulator<F>) -> Vec<RecursionQueueSimulator<F>> {
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: (
Expand All @@ -85,76 +90,102 @@ pub fn create_leaf_witnesses(
Vec<ZkSyncRecursiveLayerCircuit>, // proofs for chunks
Vec<ZkSyncBaseLayerClosedFormInput<F>>,
) {
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_reqursion_queue(queue);
0xVolosnikov marked this conversation as resolved.
Show resolved Hide resolved
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::<F> {
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::<F, H, EXT> {
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,
<H as RecursiveTreeHasher<F, Num<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<F>,
proofs: Vec<ZkSyncBaseLayerProof>, // proofs coming from the base layer
vk: &ZkSyncBaseLayerVerificationKey,
leaf_params: &(u8, RecursionLeafParametersWitness<F>), // (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::<F> {
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::<F, H, EXT> {
input: leaf_input,
vk_witness: vk.clone().into_inner(),
queue_witness: FullStateCircuitQueueRawWitness { elements },
proof_witnesses,
};

let config = LeafLayerRecursionConfig::<
F,
<H as RecursiveTreeHasher<F, Num<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(
Expand Down Expand Up @@ -398,7 +429,7 @@ pub fn create_node_witnesses(
};

let circuit = ZkSyncNodeLayerRecursiveCircuit {
witness: witness,
witness,
config: config.clone(),
transcript_params: (),
_marker: std::marker::PhantomData,
Expand Down
Loading