Skip to content

Commit

Permalink
refactor: get number of layers from private inputs (#1735)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vmx committed Dec 1, 2023
1 parent 44333b0 commit 259c992
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
10 changes: 7 additions & 3 deletions storage-proofs-porep/src/stacked/circuit/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CS: ConstraintSystem<Fr>>(
&self,
Expand All @@ -77,4 +77,8 @@ impl AllocatedColumn {
);
&self.rows[layer - 1]
}

pub(crate) fn len(&self) -> usize {
self.rows.len()
}
}
4 changes: 4 additions & 0 deletions storage-proofs-porep/src/stacked/circuit/column_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ impl<

Ok((column, inclusion_path))
}

pub(crate) fn len(&self) -> usize {
self.column.len()
}
}

impl<Proof: MerkleProofTrait> From<VanillaColumnProof<Proof>>
Expand Down
10 changes: 5 additions & 5 deletions storage-proofs-porep/src/stacked/circuit/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ impl<Tree: MerkleTreeTrait, G: 'static + Hasher> Proof<Tree, G> {
pub fn synthesize<CS: ConstraintSystem<Fr>>(
self,
mut cs: CS,
layers: usize,
comm_d: &AllocatedNum<Fr>,
comm_c: &AllocatedNum<Fr>,
comm_r_last: &AllocatedNum<Fr>,
Expand Down Expand Up @@ -137,12 +136,13 @@ impl<Tree: MerkleTreeTrait, G: 'static + Hasher> Proof<Tree, G> {
// -- 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)))?;
Expand All @@ -162,7 +162,7 @@ impl<Tree: MerkleTreeTrait, G: 'static + Hasher> Proof<Tree, G> {
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)))?;
Expand All @@ -185,7 +185,7 @@ impl<Tree: MerkleTreeTrait, G: 'static + Hasher> Proof<Tree, G> {
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));
Expand Down
13 changes: 2 additions & 11 deletions storage-proofs-porep/src/stacked/circuit/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pub struct StackedCircuit<Tree: 'static + MerkleTreeTrait, G: 'static + Hasher>
comm_r: Option<<Tree::Hasher as Hasher>::Domain>,
comm_r_last: Option<<Tree::Hasher as Hasher>::Domain>,
comm_c: Option<<Tree::Hasher as Hasher>::Domain>,
num_layers: usize,

// one proof per challenge
proofs: Vec<Proof<Tree, G>>,
Expand All @@ -49,7 +48,6 @@ impl<Tree: MerkleTreeTrait, G: Hasher> Clone for StackedCircuit<Tree, G> {
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(),
}
}
Expand All @@ -59,11 +57,9 @@ impl<Tree: MerkleTreeTrait, G: Hasher> CircuitComponent for StackedCircuit<Tree,
type ComponentPrivateInputs = ();
}

impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher> StackedCircuit<Tree, G> {
#[allow(clippy::too_many_arguments)]
impl<Tree: 'static + MerkleTreeTrait, G: 'static + Hasher> StackedCircuit<Tree, G> {
pub fn synthesize<CS>(
mut cs: CS,
public_params: <StackedDrg<'a, Tree, G> as ProofScheme<'a>>::PublicParams,
replica_id: Option<<Tree::Hasher as Hasher>::Domain>,
comm_d: Option<G::Domain>,
comm_r: Option<<Tree::Hasher as Hasher>::Domain>,
Expand All @@ -80,7 +76,6 @@ impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher> StackedCircuit<Tr
comm_r,
comm_r_last,
comm_c,
num_layers: public_params.num_layers,
proofs,
};

Expand All @@ -97,7 +92,6 @@ impl<Tree: MerkleTreeTrait, G: Hasher> Circuit<Fr> for StackedCircuit<Tree, G> {
comm_d,
comm_r_last,
comm_c,
num_layers,
} = self;

// Allocate replica_id
Expand Down Expand Up @@ -167,7 +161,6 @@ impl<Tree: MerkleTreeTrait, G: Hasher> Circuit<Fr> for StackedCircuit<Tree, G> {
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,
Expand Down Expand Up @@ -293,7 +286,7 @@ impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher>
public_inputs: &'b <StackedDrg<'_, Tree, G> as ProofScheme<'_>>::PublicInputs,
_component_private_inputs: <StackedCircuit<Tree, G> as CircuitComponent>::ComponentPrivateInputs,
vanilla_proof: &'b <StackedDrg<'_, Tree, G> as ProofScheme<'_>>::Proof,
public_params: &'b <StackedDrg<'_, Tree, G> as ProofScheme<'_>>::PublicParams,
_public_params: &'b <StackedDrg<'_, Tree, G> as ProofScheme<'_>>::PublicParams,
_partition_k: Option<usize>,
) -> Result<StackedCircuit<Tree, G>> {
ensure!(
Expand All @@ -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(),
})
}
Expand All @@ -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(),
Expand Down

0 comments on commit 259c992

Please sign in to comment.