Skip to content

Commit

Permalink
Merge pull request #427 from filecoin-project/feat/post-compound-proof
Browse files Browse the repository at this point in the history
[WIP] Add compound proof to PoSt
  • Loading branch information
porcuquine authored Jan 9, 2019
2 parents c3feac8 + ecc1330 commit c11c595
Show file tree
Hide file tree
Showing 13 changed files with 736 additions and 280 deletions.
13 changes: 2 additions & 11 deletions filecoin-proofs/examples/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,14 @@ use pairing::bls12_381::Bls12;
use rand::{Rng, SeedableRng, XorShiftRng};
use std::fs::File;
use std::io::Write;
use std::time::{Duration, Instant};
use std::time::Instant;

use bellman::Circuit;
use sapling_crypto::jubjub::JubjubBls12;

use storage_proofs::circuit::test::*;
use storage_proofs::circuit::zigzag::{ZigZagCircuit, ZigZagCompound};
use storage_proofs::compound_proof::{self, CircuitComponent, CompoundProof};
use storage_proofs::drgporep;
use storage_proofs::drgraph::*;
use storage_proofs::example_helper::prettyb;
use storage_proofs::fr32::fr_into_bytes;
use storage_proofs::hasher::{Blake2sHasher, Hasher, PedersenHasher, Sha256Hasher};
use storage_proofs::hasher::{Hasher, PedersenHasher};
use storage_proofs::layered_drgporep;
use storage_proofs::porep::PoRep;
use storage_proofs::proof::ProofScheme;
use storage_proofs::vde;
use storage_proofs::zigzag_drgporep::*;
Expand Down Expand Up @@ -105,7 +98,6 @@ where
let mut data = file_backed_mmap_from_random_bytes(nodes);

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

let sp = layered_drgporep::SetupParams {
drg_porep_setup_params: drgporep::SetupParams {
Expand All @@ -128,7 +120,6 @@ where
stop_profile();

let start = Instant::now();
let mut encode_duration = Duration::new(0, 0);

info!(FCP_LOG, "encoding");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::error::{Error, Result};
use crate::hasher::{Domain, HashFunction, Hasher};
use crate::hvh_post;
use crate::merkle::MerkleTree;
use crate::parameter_cache::ParameterSetIdentifier;
use crate::proof::ProofScheme;
use crate::vdf::Vdf;

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

impl<T: Domain, V: Vdf<T>> ParameterSetIdentifier for PublicParams<T, V> {
fn parameter_set_identifier(&self) -> String {
format!(
"beacon_post::PublicParams{{pub_params_hvh_post: {}, post_periods_count: {}",
self.pub_params_hvh_post.parameter_set_identifier(),
self.post_periods_count
)
}
}

#[derive(Clone, Debug)]
pub struct PublicInputs<T: Domain> {
/// The root hashes of the merkle trees of the sealed sectors.
Expand All @@ -48,7 +59,7 @@ impl<'a, H: 'a + Hasher> PrivateInputs<'a, H> {
}
}

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

#[derive(Clone, Debug)]
pub struct BaconPost<H: Hasher, V: Vdf<H::Domain>> {
#[derive(Clone, Debug, Default)]
pub struct BeaconPoSt<H: Hasher, V: Vdf<H::Domain>> {
_t: PhantomData<H>,
_v: PhantomData<V>,
beacon: Beacon,
}

#[derive(Clone, Debug, Default)]
struct Beacon {
count: usize,
}
impl<H: Hasher, V: Vdf<H::Domain>> Default for BaconPost<H, V> {
fn default() -> Self {
BaconPost {
_t: PhantomData,
_v: PhantomData,
beacon: Default::default(),
}
}
}

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

if self.count < t {
// sleep a bit, to simulate dely
// sleep a bit, to simulate delay
thread::sleep(time::Duration::from_millis(10));
self.count += 1;
}
Expand All @@ -97,16 +98,24 @@ impl Beacon {
}
}

impl<'a, H: Hasher + 'a, V: Vdf<H::Domain>> BaconPost<H, V> {
pub fn setup(&self, sp: &SetupParams<H::Domain, V>) -> Result<PublicParams<H::Domain, V>> {
impl<'a, H: Hasher, V: Vdf<H::Domain>> ProofScheme<'a> for BeaconPoSt<H, V>
where
H: 'a,
{
type PublicParams = PublicParams<H::Domain, V>;
type SetupParams = SetupParams<H::Domain, V>;
type PublicInputs = PublicInputs<H::Domain>;
type PrivateInputs = PrivateInputs<'a, H>;
type Proof = Proof<'a, H, V>;

fn setup(sp: &SetupParams<H::Domain, V>) -> Result<PublicParams<H::Domain, V>> {
Ok(PublicParams {
pub_params_hvh_post: hvh_post::HvhPost::<H, V>::setup(&sp.setup_params_hvh_post)?,
post_periods_count: sp.post_periods_count,
})
}

pub fn prove<'b>(
&mut self,
fn prove<'b>(
pub_params: &'b PublicParams<H::Domain, V>,
pub_inputs: &'b PublicInputs<H::Domain>,
priv_inputs: &'b PrivateInputs<'a, H>,
Expand All @@ -125,10 +134,12 @@ impl<'a, H: Hasher + 'a, V: Vdf<H::Domain>> BaconPost<H, V> {

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

let mut beacon = Beacon::default();

// First (t = 0)
{
// Run Bacon
let r = self.beacon.get::<H::Domain>(0);
// Run Beacon
let r = beacon.get::<H::Domain>(0);

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

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

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

pub fn verify(
&mut self,
fn verify(
pub_params: &PublicParams<H::Domain, V>,
pub_inputs: &PublicInputs<H::Domain>,
proof: &Proof<H, V>,
Expand All @@ -189,9 +199,11 @@ impl<'a, H: Hasher + 'a, V: Vdf<H::Domain>> BaconPost<H, V> {

// HVH Post Verification

let mut beacon = Beacon::default();

// First (t = 0)
{
let r = self.beacon.get::<H::Domain>(0);
let r = beacon.get::<H::Domain>(0);
// Generate challenges
let challenges = derive_challenges::<H>(challenge_count, 0, &[], r.as_ref());

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

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

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

#[test]
fn test_bacon_post_basics() {
fn test_beacon_post_basics() {
let rng = &mut XorShiftRng::from_seed([0x3dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);

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

let mut bacon_post = BaconPost::<PedersenHasher, vdf_sloth::Sloth>::default();

let pub_params = bacon_post.setup(&sp).unwrap();
let pub_params = BeaconPoSt::<PedersenHasher, vdf_sloth::Sloth>::setup(&sp).unwrap();

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

let priv_inputs = PrivateInputs {
let priv_inputs = PrivateInputs::<PedersenHasher> {
trees: &[&tree0, &tree1],
replicas: &[&data0, &data1],
_h: PhantomData,
};

let proof = bacon_post
.prove(&pub_params, &pub_inputs, &priv_inputs)
.unwrap();
let proof = BeaconPoSt::prove(&pub_params, &pub_inputs, &priv_inputs).unwrap();

assert!(bacon_post.verify(&pub_params, &pub_inputs, &proof).unwrap());
assert!(BeaconPoSt::verify(&pub_params, &pub_inputs, &proof).unwrap());
}
}
Loading

0 comments on commit c11c595

Please sign in to comment.