Skip to content

Commit c771815

Browse files
Merge pull request #449 from filecoin-project/perf/vanilla-zigzag
feat(storage-proofs): improve partition proving
2 parents 51df01e + e075758 commit c771815

File tree

1 file changed

+44
-49
lines changed

1 file changed

+44
-49
lines changed

storage-proofs/src/layered_drgporep.rs

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::sync::mpsc::channel;
22

3-
use crate::merkle::MerkleTree;
43
use crossbeam_utils::thread;
4+
use rayon::prelude::*;
55
use serde::de::Deserialize;
66
use serde::ser::Serialize;
77
use slog::*;
@@ -11,6 +11,7 @@ use crate::drgporep::{self, DrgPoRep};
1111
use crate::drgraph::Graph;
1212
use crate::error::{Error, Result};
1313
use crate::hasher::{Domain, HashFunction, Hasher};
14+
use crate::merkle::MerkleTree;
1415
use crate::parameter_cache::ParameterSetIdentifier;
1516
use crate::porep::{self, PoRep};
1617
use crate::proof::ProofScheme;
@@ -172,53 +173,50 @@ pub trait Layers {
172173
>],
173174
layers: usize,
174175
total_layers: usize,
175-
proofs: &'a mut Vec<Vec<EncodingProof<Self::Hasher>>>,
176176
partition_count: usize,
177-
) -> Result<&'a Vec<Vec<EncodingProof<Self::Hasher>>>> {
177+
) -> Result<Vec<Vec<EncodingProof<Self::Hasher>>>> {
178178
assert!(layers > 0);
179179

180-
let new_priv_inputs = drgporep::PrivateInputs {
181-
aux: &porep::ProverAux {
182-
tree_d: aux[0].clone(),
183-
tree_r: aux[1].clone(),
184-
},
185-
};
180+
let mut new_pp = None;
186181

187-
let mut partition_proofs = Vec::with_capacity(partition_count);
188-
189-
for k in 0..partition_count {
190-
let drgporep_pub_inputs = drgporep::PublicInputs {
191-
replica_id: pub_inputs.replica_id,
192-
challenges: pub_inputs.challenges(
193-
pp.graph.size(),
194-
(total_layers - layers) as u8,
195-
Some(k),
196-
),
197-
tau: Some(tau[0]),
198-
};
199-
let drg_proof = DrgPoRep::prove(&pp, &drgporep_pub_inputs, &new_priv_inputs)?;
182+
(0..layers)
183+
.map(|layer| {
184+
let pp = match new_pp {
185+
Some(ref new_pp) => new_pp,
186+
None => pp,
187+
};
188+
let inner_layers = layers - layer;
200189

201-
partition_proofs.push(drg_proof);
202-
}
190+
let new_priv_inputs = drgporep::PrivateInputs {
191+
aux: &porep::ProverAux {
192+
tree_d: aux[layer].clone(),
193+
tree_r: aux[layer + 1].clone(),
194+
},
195+
};
196+
let layer_diff = total_layers - inner_layers;
197+
198+
let partition_proofs: Vec<_> = (0..partition_count)
199+
.into_par_iter()
200+
.map(|k| {
201+
let drgporep_pub_inputs = drgporep::PublicInputs {
202+
replica_id: pub_inputs.replica_id,
203+
challenges: pub_inputs.challenges(
204+
pp.graph.size(),
205+
layer_diff as u8,
206+
Some(k),
207+
),
208+
tau: Some(tau[layer]),
209+
};
203210

204-
proofs.push(partition_proofs);
205-
206-
let pp = &Self::transform(pp, total_layers - layers, total_layers);
207-
208-
if layers != 1 {
209-
Self::prove_layers(
210-
pp,
211-
pub_inputs,
212-
&tau[1..],
213-
&aux[1..],
214-
layers - 1,
215-
total_layers,
216-
proofs,
217-
partition_count,
218-
)?;
219-
}
211+
DrgPoRep::prove(pp, &drgporep_pub_inputs, &new_priv_inputs)
212+
})
213+
.collect::<Result<Vec<_>>>()?;
220214

221-
Ok(proofs)
215+
new_pp = Some(Self::transform(pp, layer_diff, total_layers));
216+
217+
Ok(partition_proofs)
218+
})
219+
.collect::<Result<Vec<_>>>()
222220
}
223221

224222
fn extract_and_invert_transform_layers<'a>(
@@ -418,26 +416,23 @@ impl<'a, L: Layers> ProofScheme<'a> for L {
418416
priv_inputs: &'b Self::PrivateInputs,
419417
partition_count: usize,
420418
) -> Result<Vec<Self::Proof>> {
421-
let mut proofs = Vec::with_capacity(pub_params.layers);
419+
assert!(partition_count > 0);
422420

423-
Self::prove_layers(
421+
let proofs = Self::prove_layers(
424422
&pub_params.drg_porep_public_params,
425423
pub_inputs,
426424
&priv_inputs.tau,
427425
&priv_inputs.aux,
428426
pub_params.layers,
429427
pub_params.layers,
430-
&mut proofs,
431428
partition_count,
432429
)?;
433430

434-
assert!(partition_count > 0);
435431
let mut proof_columns = vec![Vec::new(); partition_count];
436432

437-
// TODO: Avoid all the cloning
438-
for partition_proofs in &proofs {
439-
for (j, proof) in partition_proofs.iter().enumerate() {
440-
proof_columns[j].push(proof.clone());
433+
for partition_proofs in proofs.into_iter() {
434+
for (j, proof) in partition_proofs.into_iter().enumerate() {
435+
proof_columns[j].push(proof);
441436
}
442437
}
443438

0 commit comments

Comments
 (0)