Skip to content

Commit c11c595

Browse files
authored
Merge pull request #427 from filecoin-project/feat/post-compound-proof
[WIP] Add compound proof to PoSt
2 parents c3feac8 + ecc1330 commit c11c595

File tree

13 files changed

+736
-280
lines changed

13 files changed

+736
-280
lines changed

filecoin-proofs/examples/encoding.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,14 @@ use pairing::bls12_381::Bls12;
2323
use rand::{Rng, SeedableRng, XorShiftRng};
2424
use std::fs::File;
2525
use std::io::Write;
26-
use std::time::{Duration, Instant};
26+
use std::time::Instant;
2727

28-
use bellman::Circuit;
29-
use sapling_crypto::jubjub::JubjubBls12;
30-
31-
use storage_proofs::circuit::test::*;
32-
use storage_proofs::circuit::zigzag::{ZigZagCircuit, ZigZagCompound};
33-
use storage_proofs::compound_proof::{self, CircuitComponent, CompoundProof};
3428
use storage_proofs::drgporep;
3529
use storage_proofs::drgraph::*;
3630
use storage_proofs::example_helper::prettyb;
3731
use storage_proofs::fr32::fr_into_bytes;
38-
use storage_proofs::hasher::{Blake2sHasher, Hasher, PedersenHasher, Sha256Hasher};
32+
use storage_proofs::hasher::{Hasher, PedersenHasher};
3933
use storage_proofs::layered_drgporep;
40-
use storage_proofs::porep::PoRep;
4134
use storage_proofs::proof::ProofScheme;
4235
use storage_proofs::vde;
4336
use storage_proofs::zigzag_drgporep::*;
@@ -105,7 +98,6 @@ where
10598
let mut data = file_backed_mmap_from_random_bytes(nodes);
10699

107100
let replica_id: H::Domain = rng.gen();
108-
let mut data_copy = file_backed_mmap_from(&data);
109101

110102
let sp = layered_drgporep::SetupParams {
111103
drg_porep_setup_params: drgporep::SetupParams {
@@ -128,7 +120,6 @@ where
128120
stop_profile();
129121

130122
let start = Instant::now();
131-
let mut encode_duration = Duration::new(0, 0);
132123

133124
info!(FCP_LOG, "encoding");
134125

storage-proofs/src/bacon_post.rs renamed to storage-proofs/src/beacon_post.rs

+44-36
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::error::{Error, Result};
77
use crate::hasher::{Domain, HashFunction, Hasher};
88
use crate::hvh_post;
99
use crate::merkle::MerkleTree;
10+
use crate::parameter_cache::ParameterSetIdentifier;
1011
use crate::proof::ProofScheme;
1112
use crate::vdf::Vdf;
1213

@@ -22,6 +23,16 @@ pub struct PublicParams<T: Domain, V: Vdf<T>> {
2223
pub post_periods_count: usize,
2324
}
2425

26+
impl<T: Domain, V: Vdf<T>> ParameterSetIdentifier for PublicParams<T, V> {
27+
fn parameter_set_identifier(&self) -> String {
28+
format!(
29+
"beacon_post::PublicParams{{pub_params_hvh_post: {}, post_periods_count: {}",
30+
self.pub_params_hvh_post.parameter_set_identifier(),
31+
self.post_periods_count
32+
)
33+
}
34+
}
35+
2536
#[derive(Clone, Debug)]
2637
pub struct PublicInputs<T: Domain> {
2738
/// The root hashes of the merkle trees of the sealed sectors.
@@ -48,7 +59,7 @@ impl<'a, H: 'a + Hasher> PrivateInputs<'a, H> {
4859
}
4960
}
5061

51-
/// Bacon-PoSt
62+
/// Beacon-PoSt
5263
/// This is one construction of a Proof-of-Spacetime.
5364
/// It currently only supports proving over a single sector.
5465
#[derive(Clone, Debug)]
@@ -60,33 +71,23 @@ impl<'a, H: Hasher + 'a, V: Vdf<H::Domain>> Proof<'a, H, V> {
6071
}
6172
}
6273

63-
#[derive(Clone, Debug)]
64-
pub struct BaconPost<H: Hasher, V: Vdf<H::Domain>> {
74+
#[derive(Clone, Debug, Default)]
75+
pub struct BeaconPoSt<H: Hasher, V: Vdf<H::Domain>> {
6576
_t: PhantomData<H>,
6677
_v: PhantomData<V>,
67-
beacon: Beacon,
6878
}
6979

7080
#[derive(Clone, Debug, Default)]
7181
struct Beacon {
7282
count: usize,
7383
}
74-
impl<H: Hasher, V: Vdf<H::Domain>> Default for BaconPost<H, V> {
75-
fn default() -> Self {
76-
BaconPost {
77-
_t: PhantomData,
78-
_v: PhantomData,
79-
beacon: Default::default(),
80-
}
81-
}
82-
}
8384

8485
impl Beacon {
8586
pub fn get<T: Domain>(&mut self, t: usize) -> T {
8687
// TODO: actual beacon
8788

8889
if self.count < t {
89-
// sleep a bit, to simulate dely
90+
// sleep a bit, to simulate delay
9091
thread::sleep(time::Duration::from_millis(10));
9192
self.count += 1;
9293
}
@@ -97,16 +98,24 @@ impl Beacon {
9798
}
9899
}
99100

100-
impl<'a, H: Hasher + 'a, V: Vdf<H::Domain>> BaconPost<H, V> {
101-
pub fn setup(&self, sp: &SetupParams<H::Domain, V>) -> Result<PublicParams<H::Domain, V>> {
101+
impl<'a, H: Hasher, V: Vdf<H::Domain>> ProofScheme<'a> for BeaconPoSt<H, V>
102+
where
103+
H: 'a,
104+
{
105+
type PublicParams = PublicParams<H::Domain, V>;
106+
type SetupParams = SetupParams<H::Domain, V>;
107+
type PublicInputs = PublicInputs<H::Domain>;
108+
type PrivateInputs = PrivateInputs<'a, H>;
109+
type Proof = Proof<'a, H, V>;
110+
111+
fn setup(sp: &SetupParams<H::Domain, V>) -> Result<PublicParams<H::Domain, V>> {
102112
Ok(PublicParams {
103113
pub_params_hvh_post: hvh_post::HvhPost::<H, V>::setup(&sp.setup_params_hvh_post)?,
104114
post_periods_count: sp.post_periods_count,
105115
})
106116
}
107117

108-
pub fn prove<'b>(
109-
&mut self,
118+
fn prove<'b>(
110119
pub_params: &'b PublicParams<H::Domain, V>,
111120
pub_inputs: &'b PublicInputs<H::Domain>,
112121
priv_inputs: &'b PrivateInputs<'a, H>,
@@ -125,10 +134,12 @@ impl<'a, H: Hasher + 'a, V: Vdf<H::Domain>> BaconPost<H, V> {
125134

126135
let mut proofs_hvh_post = Vec::with_capacity(post_periods_count);
127136

137+
let mut beacon = Beacon::default();
138+
128139
// First (t = 0)
129140
{
130-
// Run Bacon
131-
let r = self.beacon.get::<H::Domain>(0);
141+
// Run Beacon
142+
let r = beacon.get::<H::Domain>(0);
132143

133144
// Generate challenges
134145
let challenges = derive_challenges::<H>(challenge_count, 0, &[], r.as_ref());
@@ -151,8 +162,8 @@ impl<'a, H: Hasher + 'a, V: Vdf<H::Domain>> BaconPost<H, V> {
151162

152163
// The rest (t = 1..post_periods_count)
153164
for t in 1..post_periods_count {
154-
// Run Bacon
155-
let r = self.beacon.get::<H::Domain>(t);
165+
// Run Beacon
166+
let r = beacon.get::<H::Domain>(t);
156167
let x = extract_post_input::<H, V>(&proofs_hvh_post[t - 1]);
157168

158169
// Generate challenges
@@ -178,8 +189,7 @@ impl<'a, H: Hasher + 'a, V: Vdf<H::Domain>> BaconPost<H, V> {
178189
Ok(Proof(proofs_hvh_post))
179190
}
180191

181-
pub fn verify(
182-
&mut self,
192+
fn verify(
183193
pub_params: &PublicParams<H::Domain, V>,
184194
pub_inputs: &PublicInputs<H::Domain>,
185195
proof: &Proof<H, V>,
@@ -189,9 +199,11 @@ impl<'a, H: Hasher + 'a, V: Vdf<H::Domain>> BaconPost<H, V> {
189199

190200
// HVH Post Verification
191201

202+
let mut beacon = Beacon::default();
203+
192204
// First (t = 0)
193205
{
194-
let r = self.beacon.get::<H::Domain>(0);
206+
let r = beacon.get::<H::Domain>(0);
195207
// Generate challenges
196208
let challenges = derive_challenges::<H>(challenge_count, 0, &[], r.as_ref());
197209

@@ -213,7 +225,7 @@ impl<'a, H: Hasher + 'a, V: Vdf<H::Domain>> BaconPost<H, V> {
213225
// The rest (t = 1..post_periods_count)
214226
for t in 1..post_periods_count {
215227
// Generate challenges
216-
let r = self.beacon.get::<H::Domain>(t);
228+
let r = beacon.get::<H::Domain>(t);
217229
let x = extract_post_input::<H, V>(&proof.0[t - 1]);
218230

219231
let challenges = derive_challenges::<H>(challenge_count, t, x.as_ref(), r.as_ref());
@@ -238,7 +250,7 @@ impl<'a, H: Hasher + 'a, V: Vdf<H::Domain>> BaconPost<H, V> {
238250
}
239251

240252
fn extract_post_input<H: Hasher, V: Vdf<H::Domain>>(proof: &hvh_post::Proof<H, V>) -> H::Domain {
241-
let leafs: Vec<u8> = proof.proofs_porep.iter().fold(Vec::new(), |mut acc, p| {
253+
let leafs: Vec<u8> = proof.porep_proofs.iter().fold(Vec::new(), |mut acc, p| {
242254
acc.extend(p.leafs().into_iter().fold(
243255
Vec::new(),
244256
|mut inner_acc: Vec<u8>, leaf: &H::Domain| {
@@ -277,7 +289,7 @@ mod tests {
277289
use crate::vdf_sloth;
278290

279291
#[test]
280-
fn test_bacon_post_basics() {
292+
fn test_beacon_post_basics() {
281293
let rng = &mut XorShiftRng::from_seed([0x3dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
282294

283295
let sp = SetupParams::<PedersenDomain, vdf_sloth::Sloth> {
@@ -294,9 +306,7 @@ mod tests {
294306
post_periods_count: 3,
295307
};
296308

297-
let mut bacon_post = BaconPost::<PedersenHasher, vdf_sloth::Sloth>::default();
298-
299-
let pub_params = bacon_post.setup(&sp).unwrap();
309+
let pub_params = BeaconPoSt::<PedersenHasher, vdf_sloth::Sloth>::setup(&sp).unwrap();
300310

301311
let data0: Vec<u8> = (0..1024)
302312
.flat_map(|_| fr_into_bytes::<Bls12>(&rng.gen()))
@@ -314,16 +324,14 @@ mod tests {
314324
commitments: vec![tree0.root(), tree1.root()],
315325
};
316326

317-
let priv_inputs = PrivateInputs {
327+
let priv_inputs = PrivateInputs::<PedersenHasher> {
318328
trees: &[&tree0, &tree1],
319329
replicas: &[&data0, &data1],
320330
_h: PhantomData,
321331
};
322332

323-
let proof = bacon_post
324-
.prove(&pub_params, &pub_inputs, &priv_inputs)
325-
.unwrap();
333+
let proof = BeaconPoSt::prove(&pub_params, &pub_inputs, &priv_inputs).unwrap();
326334

327-
assert!(bacon_post.verify(&pub_params, &pub_inputs, &proof).unwrap());
335+
assert!(BeaconPoSt::verify(&pub_params, &pub_inputs, &proof).unwrap());
328336
}
329337
}

0 commit comments

Comments
 (0)