From a121f2e77f60b41a81dd131cad3117e14576a642 Mon Sep 17 00:00:00 2001 From: Robin Salen Date: Wed, 24 Apr 2024 11:37:09 -0400 Subject: [PATCH 1/3] Unify types --- .../src/fixed_recursive_verifier.rs | 125 ++++++------------ 1 file changed, 38 insertions(+), 87 deletions(-) diff --git a/evm_arithmetization/src/fixed_recursive_verifier.rs b/evm_arithmetization/src/fixed_recursive_verifier.rs index 6df4335a7..b47c06ff3 100644 --- a/evm_arithmetization/src/fixed_recursive_verifier.rs +++ b/evm_arithmetization/src/fixed_recursive_verifier.rs @@ -186,8 +186,8 @@ where C: GenericConfig, { pub circuit: CircuitData, - lhs: SegmentAggregationChildTarget, - rhs: SegmentAggregationChildTarget, + lhs: AggregationChildTarget, + rhs: AggregationChildTarget, public_values: PublicValuesTarget, cyclic_vk: VerifierCircuitTarget, } @@ -219,8 +219,8 @@ where let circuit = buffer.read_circuit_data(gate_serializer, generator_serializer)?; let cyclic_vk = buffer.read_target_verifier_circuit()?; let public_values = PublicValuesTarget::from_buffer(buffer)?; - let lhs = SegmentAggregationChildTarget::from_buffer(buffer)?; - let rhs = SegmentAggregationChildTarget::from_buffer(buffer)?; + let lhs = AggregationChildTarget::from_buffer(buffer)?; + let rhs = AggregationChildTarget::from_buffer(buffer)?; Ok(Self { circuit, lhs, @@ -232,28 +232,28 @@ where } #[derive(Eq, PartialEq, Debug)] -struct SegmentAggregationChildTarget { +struct AggregationChildTarget { is_agg: BoolTarget, agg_proof: ProofWithPublicInputsTarget, - segment_proof: ProofWithPublicInputsTarget, + proof: ProofWithPublicInputsTarget, } -impl SegmentAggregationChildTarget { +impl AggregationChildTarget { fn to_buffer(&self, buffer: &mut Vec) -> IoResult<()> { buffer.write_target_bool(self.is_agg)?; buffer.write_target_proof_with_public_inputs(&self.agg_proof)?; - buffer.write_target_proof_with_public_inputs(&self.segment_proof)?; + buffer.write_target_proof_with_public_inputs(&self.proof)?; Ok(()) } fn from_buffer(buffer: &mut Buffer) -> IoResult { let is_agg = buffer.read_target_bool()?; let agg_proof = buffer.read_target_proof_with_public_inputs()?; - let segment_proof = buffer.read_target_proof_with_public_inputs()?; + let proof = buffer.read_target_proof_with_public_inputs()?; Ok(Self { is_agg, agg_proof, - segment_proof, + proof, }) } @@ -267,7 +267,7 @@ impl SegmentAggregationChildTarget { let agg_pv = PublicValuesTarget::from_public_inputs(&self.agg_proof.public_inputs, len_mem_cap); let segment_pv = - PublicValuesTarget::from_public_inputs(&self.segment_proof.public_inputs, len_mem_cap); + PublicValuesTarget::from_public_inputs(&self.proof.public_inputs, len_mem_cap); PublicValuesTarget::select(builder, self.is_agg, agg_pv, segment_pv) } } @@ -282,8 +282,8 @@ where C: GenericConfig, { pub circuit: CircuitData, - lhs: TxnAggregationChildTarget, - rhs: TxnAggregationChildTarget, + lhs: AggregationChildTarget, + rhs: AggregationChildTarget, public_values: PublicValuesTarget, cyclic_vk: VerifierCircuitTarget, } @@ -315,8 +315,8 @@ where let circuit = buffer.read_circuit_data(gate_serializer, generator_serializer)?; let cyclic_vk = buffer.read_target_verifier_circuit()?; let public_values = PublicValuesTarget::from_buffer(buffer)?; - let lhs = TxnAggregationChildTarget::from_buffer(buffer)?; - let rhs = TxnAggregationChildTarget::from_buffer(buffer)?; + let lhs = AggregationChildTarget::from_buffer(buffer)?; + let rhs = AggregationChildTarget::from_buffer(buffer)?; Ok(Self { circuit, lhs, @@ -327,49 +327,6 @@ where } } -#[derive(Eq, PartialEq, Debug)] -struct TxnAggregationChildTarget { - is_agg: BoolTarget, - txn_agg_proof: ProofWithPublicInputsTarget, - segment_agg_proof: ProofWithPublicInputsTarget, -} - -impl TxnAggregationChildTarget { - fn to_buffer(&self, buffer: &mut Vec) -> IoResult<()> { - buffer.write_target_bool(self.is_agg)?; - buffer.write_target_proof_with_public_inputs(&self.txn_agg_proof)?; - buffer.write_target_proof_with_public_inputs(&self.segment_agg_proof)?; - Ok(()) - } - - fn from_buffer(buffer: &mut Buffer) -> IoResult { - let is_agg = buffer.read_target_bool()?; - let txn_agg_proof = buffer.read_target_proof_with_public_inputs()?; - let segment_agg_proof = buffer.read_target_proof_with_public_inputs()?; - Ok(Self { - is_agg, - txn_agg_proof, - segment_agg_proof, - }) - } - - // `len_mem_cap` is the length of the Merkle - // caps for `MemBefore` and `MemAfter`. - fn public_values>( - &self, - builder: &mut CircuitBuilder, - len_mem_cap: usize, - ) -> PublicValuesTarget { - let txn_agg_pv = - PublicValuesTarget::from_public_inputs(&self.txn_agg_proof.public_inputs, len_mem_cap); - let segment_agg_pv = PublicValuesTarget::from_public_inputs( - &self.segment_agg_proof.public_inputs, - len_mem_cap, - ); - PublicValuesTarget::select(builder, self.is_agg, txn_agg_pv, segment_agg_pv) - } -} - /// Data for the block circuit, which is used to generate a final block proof, /// and compress it with an optional parent proof if present. #[derive(Eq, PartialEq, Debug)] @@ -1220,51 +1177,45 @@ where fn add_segment_agg_child( builder: &mut CircuitBuilder, root: &RootCircuitData, - ) -> SegmentAggregationChildTarget { + ) -> AggregationChildTarget { let common = &root.circuit.common; let root_vk = builder.constant_verifier_data(&root.circuit.verifier_only); let is_agg = builder.add_virtual_bool_target_safe(); let agg_proof = builder.add_virtual_proof_with_pis(common); - let segment_proof = builder.add_virtual_proof_with_pis(common); + let proof = builder.add_virtual_proof_with_pis(common); builder - .conditionally_verify_cyclic_proof::( - is_agg, - &agg_proof, - &segment_proof, - &root_vk, - common, - ) + .conditionally_verify_cyclic_proof::(is_agg, &agg_proof, &proof, &root_vk, common) .expect("Failed to build cyclic recursion circuit"); - SegmentAggregationChildTarget { + AggregationChildTarget { is_agg, agg_proof, - segment_proof, + proof, } } fn add_txn_agg_child( builder: &mut CircuitBuilder, segment_agg: &SegmentAggregationCircuitData, - ) -> TxnAggregationChildTarget { + ) -> AggregationChildTarget { let common = &segment_agg.circuit.common; let inner_segment_agg_vk = builder.constant_verifier_data(&segment_agg.circuit.verifier_only); let is_agg = builder.add_virtual_bool_target_safe(); - let txn_agg_proof = builder.add_virtual_proof_with_pis(common); - let segment_agg_proof = builder.add_virtual_proof_with_pis(common); + let agg_proof = builder.add_virtual_proof_with_pis(common); + let proof = builder.add_virtual_proof_with_pis(common); builder .conditionally_verify_cyclic_proof::( is_agg, - &txn_agg_proof, - &segment_agg_proof, + &agg_proof, + &proof, &inner_segment_agg_vk, common, ) .expect("Failed to build cyclic recursion circuit"); - TxnAggregationChildTarget { + AggregationChildTarget { is_agg, - txn_agg_proof, - segment_agg_proof, + agg_proof, + proof, } } @@ -1662,7 +1613,7 @@ where lhs_is_agg, &self.segment_aggregation.circuit, &mut agg_inputs, - &self.segment_aggregation.lhs.segment_proof, + &self.segment_aggregation.lhs.proof, &self.segment_aggregation.lhs.agg_proof, lhs_proof, ); @@ -1672,7 +1623,7 @@ where rhs_is_agg, &self.segment_aggregation.circuit, &mut agg_inputs, - &self.segment_aggregation.rhs.segment_proof, + &self.segment_aggregation.rhs.proof, &self.segment_aggregation.rhs.agg_proof, rhs_proof, ); @@ -1741,8 +1692,8 @@ where /// one will generate a proof of /// validity for both the transaction range covered by the previous proof /// and the current transaction. - /// - `agg_segment_proof`: the final aggregation proof containing all - /// segments within the current transaction. + /// - `agg_proof`: the final aggregation proof containing all segments + /// within the current transaction. /// - `public_values`: the public values associated to the aggregation /// proof. /// @@ -1767,8 +1718,8 @@ where lhs_is_agg, &self.txn_aggregation.circuit, &mut txn_inputs, - &self.txn_aggregation.lhs.segment_agg_proof, - &self.txn_aggregation.lhs.txn_agg_proof, + &self.txn_aggregation.lhs.proof, + &self.txn_aggregation.lhs.agg_proof, lhs_proof, ); @@ -1777,8 +1728,8 @@ where rhs_is_agg, &self.txn_aggregation.circuit, &mut txn_inputs, - &self.txn_aggregation.rhs.segment_agg_proof, - &self.txn_aggregation.rhs.txn_agg_proof, + &self.txn_aggregation.rhs.proof, + &self.txn_aggregation.rhs.agg_proof, rhs_proof, ); @@ -1869,7 +1820,7 @@ where is_agg: bool, circuit: &CircuitData, agg_inputs: &mut PartialWitness, - segment_proof_target: &ProofWithPublicInputsTarget, + proof_target: &ProofWithPublicInputsTarget, agg_proof_target: &ProofWithPublicInputsTarget, proof: &ProofWithPublicInputs, ) { @@ -1879,7 +1830,7 @@ where } else { Self::set_dummy_proof_with_cyclic_vk_pis(circuit, agg_inputs, agg_proof_target, proof) } - agg_inputs.set_proof_with_pis_target(segment_proof_target, proof); + agg_inputs.set_proof_with_pis_target(proof_target, proof); } /// Create a final block proof, once all transactions of a given block have From 5330b2b2c1b429bd4bbaa846a76fc8de6b37e2d7 Mon Sep 17 00:00:00 2001 From: Robin Salen Date: Wed, 24 Apr 2024 11:40:09 -0400 Subject: [PATCH 2/3] Reduce arguments --- .../src/fixed_recursive_verifier.rs | 33 ++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/evm_arithmetization/src/fixed_recursive_verifier.rs b/evm_arithmetization/src/fixed_recursive_verifier.rs index b47c06ff3..500bc171a 100644 --- a/evm_arithmetization/src/fixed_recursive_verifier.rs +++ b/evm_arithmetization/src/fixed_recursive_verifier.rs @@ -1609,22 +1609,18 @@ where let mut agg_inputs = PartialWitness::new(); Self::set_dummy_if_necessary( - self.segment_aggregation.lhs.is_agg, lhs_is_agg, + &self.segment_aggregation.lhs, &self.segment_aggregation.circuit, &mut agg_inputs, - &self.segment_aggregation.lhs.proof, - &self.segment_aggregation.lhs.agg_proof, lhs_proof, ); Self::set_dummy_if_necessary( - self.segment_aggregation.rhs.is_agg, rhs_is_agg, + &self.segment_aggregation.rhs, &self.segment_aggregation.circuit, &mut agg_inputs, - &self.segment_aggregation.rhs.proof, - &self.segment_aggregation.rhs.agg_proof, rhs_proof, ); @@ -1714,22 +1710,18 @@ where let mut txn_inputs = PartialWitness::new(); Self::set_dummy_if_necessary( - self.txn_aggregation.lhs.is_agg, lhs_is_agg, + &self.txn_aggregation.lhs, &self.txn_aggregation.circuit, &mut txn_inputs, - &self.txn_aggregation.lhs.proof, - &self.txn_aggregation.lhs.agg_proof, lhs_proof, ); Self::set_dummy_if_necessary( - self.txn_aggregation.rhs.is_agg, rhs_is_agg, + &self.txn_aggregation.rhs, &self.txn_aggregation.circuit, &mut txn_inputs, - &self.txn_aggregation.rhs.proof, - &self.txn_aggregation.rhs.agg_proof, rhs_proof, ); @@ -1816,21 +1808,24 @@ where /// If the lhs is not an aggregation, we set the cyclic vk to a dummy value, /// so that it corresponds to the aggregation cyclic vk. fn set_dummy_if_necessary( - is_agg_target: BoolTarget, is_agg: bool, + agg_child: &AggregationChildTarget, circuit: &CircuitData, agg_inputs: &mut PartialWitness, - proof_target: &ProofWithPublicInputsTarget, - agg_proof_target: &ProofWithPublicInputsTarget, proof: &ProofWithPublicInputs, ) { - agg_inputs.set_bool_target(is_agg_target, is_agg); + agg_inputs.set_bool_target(agg_child.is_agg, is_agg); if is_agg { - agg_inputs.set_proof_with_pis_target(agg_proof_target, proof); + agg_inputs.set_proof_with_pis_target(&agg_child.agg_proof, proof); } else { - Self::set_dummy_proof_with_cyclic_vk_pis(circuit, agg_inputs, agg_proof_target, proof) + Self::set_dummy_proof_with_cyclic_vk_pis( + circuit, + agg_inputs, + &agg_child.agg_proof, + proof, + ) } - agg_inputs.set_proof_with_pis_target(proof_target, proof); + agg_inputs.set_proof_with_pis_target(&agg_child.proof, proof); } /// Create a final block proof, once all transactions of a given block have From b14198477ed55449161eab2f1c8306267879c0ae Mon Sep 17 00:00:00 2001 From: Robin Salen Date: Wed, 24 Apr 2024 12:00:31 -0400 Subject: [PATCH 3/3] Reorder args for consistency --- evm_arithmetization/src/fixed_recursive_verifier.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/evm_arithmetization/src/fixed_recursive_verifier.rs b/evm_arithmetization/src/fixed_recursive_verifier.rs index 500bc171a..4e0065109 100644 --- a/evm_arithmetization/src/fixed_recursive_verifier.rs +++ b/evm_arithmetization/src/fixed_recursive_verifier.rs @@ -1609,16 +1609,16 @@ where let mut agg_inputs = PartialWitness::new(); Self::set_dummy_if_necessary( - lhs_is_agg, &self.segment_aggregation.lhs, + lhs_is_agg, &self.segment_aggregation.circuit, &mut agg_inputs, lhs_proof, ); Self::set_dummy_if_necessary( - rhs_is_agg, &self.segment_aggregation.rhs, + rhs_is_agg, &self.segment_aggregation.circuit, &mut agg_inputs, rhs_proof, @@ -1710,16 +1710,16 @@ where let mut txn_inputs = PartialWitness::new(); Self::set_dummy_if_necessary( - lhs_is_agg, &self.txn_aggregation.lhs, + lhs_is_agg, &self.txn_aggregation.circuit, &mut txn_inputs, lhs_proof, ); Self::set_dummy_if_necessary( - rhs_is_agg, &self.txn_aggregation.rhs, + rhs_is_agg, &self.txn_aggregation.circuit, &mut txn_inputs, rhs_proof, @@ -1808,8 +1808,8 @@ where /// If the lhs is not an aggregation, we set the cyclic vk to a dummy value, /// so that it corresponds to the aggregation cyclic vk. fn set_dummy_if_necessary( - is_agg: bool, agg_child: &AggregationChildTarget, + is_agg: bool, circuit: &CircuitData, agg_inputs: &mut PartialWitness, proof: &ProofWithPublicInputs,