From dab8b319ddc3c7410ec41515a970b98de13587a6 Mon Sep 17 00:00:00 2001 From: Volker Mische Date: Tue, 10 Oct 2023 14:25:57 +0200 Subject: [PATCH] refactor: StackedCircuit only needs the number of layers There's no need to store the full public parameters within the `StackedCircuit` struct, as it only needs the number of layers. --- filecoin-proofs/src/types/porep_config.rs | 2 +- .../src/stacked/circuit/proof.rs | 35 +++++++++---------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/filecoin-proofs/src/types/porep_config.rs b/filecoin-proofs/src/types/porep_config.rs index b86d5a912..39747fa8f 100644 --- a/filecoin-proofs/src/types/porep_config.rs +++ b/filecoin-proofs/src/types/porep_config.rs @@ -107,7 +107,7 @@ impl PoRepConfig { Ok( as CacheableParameters< - StackedCircuit<'_, Tree, DefaultPieceHasher>, + StackedCircuit, _, >>::cache_identifier(¶ms), ) diff --git a/storage-proofs-porep/src/stacked/circuit/proof.rs b/storage-proofs-porep/src/stacked/circuit/proof.rs index 247547fcc..25d270595 100644 --- a/storage-proofs-porep/src/stacked/circuit/proof.rs +++ b/storage-proofs-porep/src/stacked/circuit/proof.rs @@ -25,13 +25,13 @@ use crate::stacked::{circuit::params::Proof, StackedDrg}; /// /// * `params` - parameters for the curve /// -pub struct StackedCircuit<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher> { - public_params: as ProofScheme<'a>>::PublicParams, +pub struct StackedCircuit { replica_id: Option<::Domain>, comm_d: Option, comm_r: Option<::Domain>, comm_r_last: Option<::Domain>, comm_c: Option<::Domain>, + num_layers: usize, // one proof per challenge proofs: Vec>, @@ -41,25 +41,25 @@ pub struct StackedCircuit<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hash // #[derive(Clone)]) because derive(Clone) will only expand for MerkleTreeTrait types that also // implement Clone. Not every MerkleTreeTrait type is Clone-able because not all merkel Store's are // Clone-able, therefore deriving Clone would impl Clone for less than all possible Tree types. -impl<'a, Tree: MerkleTreeTrait, G: Hasher> Clone for StackedCircuit<'a, Tree, G> { +impl Clone for StackedCircuit { fn clone(&self) -> Self { StackedCircuit { - public_params: self.public_params.clone(), replica_id: self.replica_id, comm_d: self.comm_d, comm_r: self.comm_r, comm_r_last: self.comm_r_last, comm_c: self.comm_c, + num_layers: self.num_layers, proofs: self.proofs.clone(), } } } -impl<'a, Tree: MerkleTreeTrait, G: Hasher> CircuitComponent for StackedCircuit<'a, Tree, G> { +impl CircuitComponent for StackedCircuit { type ComponentPrivateInputs = (); } -impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher> StackedCircuit<'a, Tree, G> { +impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher> StackedCircuit { #[allow(clippy::too_many_arguments)] pub fn synthesize( mut cs: CS, @@ -74,13 +74,13 @@ impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher> StackedCircuit<'a where CS: ConstraintSystem, { - let circuit = StackedCircuit::<'a, Tree, G> { - public_params, + let circuit = StackedCircuit:: { replica_id, comm_d, comm_r, comm_r_last, comm_c, + num_layers: public_params.num_layers, proofs, }; @@ -88,17 +88,16 @@ impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher> StackedCircuit<'a } } -impl<'a, Tree: MerkleTreeTrait, G: Hasher> Circuit for StackedCircuit<'a, Tree, G> { +impl Circuit for StackedCircuit { fn synthesize>(self, cs: &mut CS) -> Result<(), SynthesisError> { let StackedCircuit { - public_params, proofs, replica_id, comm_r, comm_d, comm_r_last, comm_c, - .. + num_layers, } = self; // Allocate replica_id @@ -168,7 +167,7 @@ impl<'a, Tree: MerkleTreeTrait, G: Hasher> Circuit for StackedCircuit<'a, Tr for (i, proof) in proofs.into_iter().enumerate() { proof.synthesize( &mut cs.namespace(|| format!("challenge_{}", i)), - public_params.num_layers, + num_layers, &comm_d_num, &comm_c_num, &comm_r_last_num, @@ -200,7 +199,7 @@ impl, P: ParameterSetMetadata, Tree: MerkleTreeTrait, G: Hasher> } impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher> - CompoundProof<'a, StackedDrg<'a, Tree, G>, StackedCircuit<'a, Tree, G>> + CompoundProof<'a, StackedDrg<'a, Tree, G>, StackedCircuit> for StackedCompound { fn generate_public_inputs( @@ -292,11 +291,11 @@ impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher> fn circuit<'b>( public_inputs: &'b as ProofScheme<'_>>::PublicInputs, - _component_private_inputs: as CircuitComponent>::ComponentPrivateInputs, + _component_private_inputs: as CircuitComponent>::ComponentPrivateInputs, vanilla_proof: &'b as ProofScheme<'_>>::Proof, public_params: &'b as ProofScheme<'_>>::PublicParams, _partition_k: Option, - ) -> Result> { + ) -> Result> { ensure!( !vanilla_proof.is_empty(), "Cannot create a circuit with no vanilla proofs" @@ -316,26 +315,26 @@ impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher> ); Ok(StackedCircuit { - public_params: public_params.clone(), replica_id: Some(public_inputs.replica_id), comm_d: public_inputs.tau.as_ref().map(|t| t.comm_d), comm_r: public_inputs.tau.as_ref().map(|t| t.comm_r), comm_r_last: Some(comm_r_last), comm_c: Some(comm_c), + num_layers: public_params.num_layers, proofs: vanilla_proof.iter().cloned().map(|p| p.into()).collect(), }) } fn blank_circuit( public_params: & as ProofScheme<'_>>::PublicParams, - ) -> StackedCircuit<'a, Tree, G> { + ) -> StackedCircuit { StackedCircuit { - public_params: public_params.clone(), replica_id: None, comm_d: None, comm_r: None, comm_r_last: None, comm_c: None, + num_layers: public_params.num_layers, proofs: (0..public_params.challenges.challenges_count_all()) .map(|_challenge_index| Proof::empty(public_params)) .collect(),