From 259c992b973db6894e81be623bc69d861000123d Mon Sep 17 00:00:00 2001 From: Volker Mische Date: Sat, 2 Dec 2023 00:51:20 +0100 Subject: [PATCH] refactor: get number of layers from private inputs (#1735) Instead of passing in the number of layers through the public params, get the number of layers from the private inputs that are required anyway. The number of layers equals the number of columns of a columns proof. --- storage-proofs-porep/src/stacked/circuit/column.rs | 10 +++++++--- .../src/stacked/circuit/column_proof.rs | 4 ++++ storage-proofs-porep/src/stacked/circuit/params.rs | 10 +++++----- storage-proofs-porep/src/stacked/circuit/proof.rs | 13 ++----------- 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/storage-proofs-porep/src/stacked/circuit/column.rs b/storage-proofs-porep/src/stacked/circuit/column.rs index 4bce19fca..421550d83 100644 --- a/storage-proofs-porep/src/stacked/circuit/column.rs +++ b/storage-proofs-porep/src/stacked/circuit/column.rs @@ -52,13 +52,13 @@ impl Column { Ok(AllocatedColumn { rows }) } -} -impl AllocatedColumn { - pub fn len(&self) -> usize { + pub(crate) fn len(&self) -> usize { self.rows.len() } +} +impl AllocatedColumn { /// Creates the column hash of this column. pub fn hash>( &self, @@ -77,4 +77,8 @@ impl AllocatedColumn { ); &self.rows[layer - 1] } + + pub(crate) fn len(&self) -> usize { + self.rows.len() + } } diff --git a/storage-proofs-porep/src/stacked/circuit/column_proof.rs b/storage-proofs-porep/src/stacked/circuit/column_proof.rs index 327d41e4b..214de65a8 100644 --- a/storage-proofs-porep/src/stacked/circuit/column_proof.rs +++ b/storage-proofs-porep/src/stacked/circuit/column_proof.rs @@ -57,6 +57,10 @@ impl< Ok((column, inclusion_path)) } + + pub(crate) fn len(&self) -> usize { + self.column.len() + } } impl From> diff --git a/storage-proofs-porep/src/stacked/circuit/params.rs b/storage-proofs-porep/src/stacked/circuit/params.rs index 696a48454..4540a9e1c 100644 --- a/storage-proofs-porep/src/stacked/circuit/params.rs +++ b/storage-proofs-porep/src/stacked/circuit/params.rs @@ -99,7 +99,6 @@ impl Proof { pub fn synthesize>( self, mut cs: CS, - layers: usize, comm_d: &AllocatedNum, comm_c: &AllocatedNum, comm_r_last: &AllocatedNum, @@ -137,12 +136,13 @@ impl Proof { // -- verify replica column openings // Private Inputs for the DRG parent nodes. - let mut drg_parents = Vec::with_capacity(layers); + let num_layers = drg_parents_proofs[0].len(); + let mut drg_parents = Vec::with_capacity(num_layers); for (i, parent) in drg_parents_proofs.into_iter().enumerate() { let (parent_col, inclusion_path) = parent.alloc(cs.namespace(|| format!("drg_parent_{}_num", i)))?; - assert_eq!(layers, parent_col.len()); + assert_eq!(num_layers, parent_col.len()); // calculate column hash let val = parent_col.hash(cs.namespace(|| format!("drg_parent_{}_constraint", i)))?; @@ -162,7 +162,7 @@ impl Proof { for (i, parent) in exp_parents_proofs.into_iter().enumerate() { let (parent_col, inclusion_path) = parent.alloc(cs.namespace(|| format!("exp_parent_{}_num", i)))?; - assert_eq!(layers, parent_col.len()); + assert_eq!(num_layers, parent_col.len()); // calculate column hash let val = parent_col.hash(cs.namespace(|| format!("exp_parent_{}_constraint", i)))?; @@ -185,7 +185,7 @@ impl Proof { let challenge_num = UInt64::alloc(cs.namespace(|| "challenge"), challenge)?; challenge_num.pack_into_input(cs.namespace(|| "challenge input"))?; - for layer in 1..=layers { + for layer in 1..=num_layers { let layer_num = UInt32::constant(layer as u32); let mut cs = cs.namespace(|| format!("labeling_{}", layer)); diff --git a/storage-proofs-porep/src/stacked/circuit/proof.rs b/storage-proofs-porep/src/stacked/circuit/proof.rs index 25d270595..14c4b610e 100644 --- a/storage-proofs-porep/src/stacked/circuit/proof.rs +++ b/storage-proofs-porep/src/stacked/circuit/proof.rs @@ -31,7 +31,6 @@ pub struct StackedCircuit comm_r: Option<::Domain>, comm_r_last: Option<::Domain>, comm_c: Option<::Domain>, - num_layers: usize, // one proof per challenge proofs: Vec>, @@ -49,7 +48,6 @@ impl Clone for StackedCircuit { 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(), } } @@ -59,11 +57,9 @@ impl CircuitComponent for StackedCircuit StackedCircuit { - #[allow(clippy::too_many_arguments)] +impl StackedCircuit { pub fn synthesize( mut cs: CS, - public_params: as ProofScheme<'a>>::PublicParams, replica_id: Option<::Domain>, comm_d: Option, comm_r: Option<::Domain>, @@ -80,7 +76,6 @@ impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher> StackedCircuit Circuit for StackedCircuit { comm_d, comm_r_last, comm_c, - num_layers, } = self; // Allocate replica_id @@ -167,7 +161,6 @@ impl Circuit for StackedCircuit { for (i, proof) in proofs.into_iter().enumerate() { proof.synthesize( &mut cs.namespace(|| format!("challenge_{}", i)), - num_layers, &comm_d_num, &comm_c_num, &comm_r_last_num, @@ -293,7 +286,7 @@ impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher> public_inputs: &'b as ProofScheme<'_>>::PublicInputs, _component_private_inputs: as CircuitComponent>::ComponentPrivateInputs, vanilla_proof: &'b as ProofScheme<'_>>::Proof, - public_params: &'b as ProofScheme<'_>>::PublicParams, + _public_params: &'b as ProofScheme<'_>>::PublicParams, _partition_k: Option, ) -> Result> { ensure!( @@ -320,7 +313,6 @@ impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher> 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(), }) } @@ -334,7 +326,6 @@ impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher> 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(),