Skip to content

Commit

Permalink
separate sector builder/non-sector builder FFI code + provide idiomat…
Browse files Browse the repository at this point in the history
…ic Rust API to "primitive proofs operations" (#684)

* reorganize crates in preparation for split

* split sector builder FFI from non-sector builder FFI

* add comments

* internal -> safe

* rust fmt

* remove api indirection
  • Loading branch information
laser committed Jun 4, 2019
1 parent 85c6301 commit 3da7cb9
Show file tree
Hide file tree
Showing 37 changed files with 1,016 additions and 865 deletions.
6 changes: 3 additions & 3 deletions drop-struct-macro-derive/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ $ cargo expand --lib api::responses
Finished dev [unoptimized + debuginfo] target(s) in 0.70s

pub mod responses {
use crate::api::sector_builder::errors::SectorBuilderErr;
use crate::api::sector_builder::SectorBuilder;
use crate::api::API_POREP_PROOF_BYTES;
use crate::sector_builder::errors::SectorBuilderErr;
use crate::sector_builder::SectorBuilder;
use crate::API_POREP_PROOF_BYTES;
use drop_struct_macro_derive::DropStructMacro;
use failure::Error;
Expand Down
2 changes: 1 addition & 1 deletion filecoin-proofs/examples/beacon-post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use paired::bls12_381::Bls12;
use rand::{Rng, SeedableRng, XorShiftRng};
use std::time::{Duration, Instant};

use filecoin_proofs::FCP_LOG;
use filecoin_proofs::singletons::FCP_LOG;
use storage_proofs::beacon_post::*;
use storage_proofs::drgraph::*;
use storage_proofs::example_helper::prettyb;
Expand Down
2 changes: 1 addition & 1 deletion filecoin-proofs/examples/drgporep-vanilla-disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use memmap::MmapOptions;
use std::fs::File;
use std::io::Write;

use filecoin_proofs::FCP_LOG;
use filecoin_proofs::singletons::FCP_LOG;

fn file_backed_mmap_from_random_bytes(n: usize) -> MmapMut {
let rng = &mut XorShiftRng::from_seed([0x3dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
Expand Down
2 changes: 1 addition & 1 deletion filecoin-proofs/examples/drgporep-vanilla.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use storage_proofs::hasher::{Blake2sHasher, Hasher, PedersenHasher, Sha256Hasher
use storage_proofs::porep::PoRep;
use storage_proofs::proof::ProofScheme;

use filecoin_proofs::FCP_LOG;
use filecoin_proofs::singletons::FCP_LOG;

#[cfg(feature = "cpu-profile")]
#[inline(always)]
Expand Down
2 changes: 1 addition & 1 deletion filecoin-proofs/examples/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use storage_proofs::proof::ProofScheme;
use storage_proofs::vde;
use storage_proofs::zigzag_drgporep::*;

use filecoin_proofs::FCP_LOG;
use filecoin_proofs::singletons::FCP_LOG;

#[cfg(feature = "cpu-profile")]
#[inline(always)]
Expand Down
2 changes: 1 addition & 1 deletion filecoin-proofs/examples/zigzag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use storage_proofs::porep::PoRep;
use storage_proofs::proof::ProofScheme;
use storage_proofs::zigzag_drgporep::*;

use filecoin_proofs::FCP_LOG;
use filecoin_proofs::singletons::FCP_LOG;

// We can only one of the profilers at a time, either CPU (`profile`)
// or memory (`heap-profile`), duplicating the function so they won't
Expand Down
19 changes: 10 additions & 9 deletions filecoin-proofs/src/bin/paramcache.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use clap::{App, Arg};
use paired::bls12_381::Bls12;
use slog::*;

use filecoin_proofs::api::internal;
use filecoin_proofs::FCP_LOG;
use paired::bls12_381::Bls12;
use filecoin_proofs::parameters::{post_public_params, public_params};
use filecoin_proofs::singletons::ENGINE_PARAMS;
use filecoin_proofs::singletons::FCP_LOG;
use sector_base::api::bytes_amount::PaddedBytesAmount;
use sector_base::api::disk_backed_storage::LIVE_SECTOR_SIZE;
use sector_base::api::disk_backed_storage::TEST_SECTOR_SIZE;
Expand All @@ -26,17 +27,17 @@ fn cache_porep_params(porep_config: PoRepConfig) {
let n = u64::from(PaddedBytesAmount::from(porep_config));
info!(FCP_LOG, "begin PoRep parameter-cache check/populate routine for {}-byte sectors", n; "target" => "paramcache");

let public_params = internal::public_params(
let public_params = public_params(
PaddedBytesAmount::from(porep_config),
usize::from(PoRepProofPartitions::from(porep_config)),
);
{
let circuit = ZigZagCompound::blank_circuit(&public_params, &internal::ENGINE_PARAMS);
let circuit = ZigZagCompound::blank_circuit(&public_params, &ENGINE_PARAMS);

let _ = ZigZagCompound::get_groth_params(circuit, &public_params);
}
{
let circuit = ZigZagCompound::blank_circuit(&public_params, &internal::ENGINE_PARAMS);
let circuit = ZigZagCompound::blank_circuit(&public_params, &ENGINE_PARAMS);
let _ = ZigZagCompound::get_verifying_key(circuit, &public_params);
}
}
Expand All @@ -45,14 +46,14 @@ fn cache_post_params(post_config: PoStConfig) {
let n = u64::from(PaddedBytesAmount::from(post_config));
info!(FCP_LOG, "begin PoSt parameter-cache check/populate routine for {}-byte sectors", n; "target" => "paramcache");

let post_public_params = internal::post_public_params(post_config);
let post_public_params = post_public_params(post_config);
{
let post_circuit: VDFPoStCircuit<Bls12> =
<VDFPostCompound as CompoundProof<
Bls12,
VDFPoSt<PedersenHasher, Sloth>,
VDFPoStCircuit<Bls12>,
>>::blank_circuit(&post_public_params, &internal::ENGINE_PARAMS);
>>::blank_circuit(&post_public_params, &ENGINE_PARAMS);
let _ = VDFPostCompound::get_groth_params(post_circuit, &post_public_params);
}
{
Expand All @@ -61,7 +62,7 @@ fn cache_post_params(post_config: PoStConfig) {
Bls12,
VDFPoSt<PedersenHasher, Sloth>,
VDFPoStCircuit<Bls12>,
>>::blank_circuit(&post_public_params, &internal::ENGINE_PARAMS);
>>::blank_circuit(&post_public_params, &ENGINE_PARAMS);

let _ = VDFPostCompound::get_verifying_key(post_circuit, &post_public_params);
}
Expand Down
12 changes: 7 additions & 5 deletions filecoin-proofs/src/bin/paramgen.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use rand::OsRng;
use std::env;
use std::fs::File;

use filecoin_proofs::api::internal;
use rand::OsRng;

use filecoin_proofs::parameters::public_params;
use filecoin_proofs::singletons::ENGINE_PARAMS;
use sector_base::api::bytes_amount::PaddedBytesAmount;
use sector_base::api::disk_backed_storage::TEST_SECTOR_SIZE;
use sector_base::api::porep_proof_partitions::PoRepProofPartitions;
Expand All @@ -15,19 +17,19 @@ pub fn main() {
let args: Vec<String> = env::args().collect();
let out_file = &args[1];

let public_params = internal::public_params(
let public_params = public_params(
PaddedBytesAmount::from(SectorSize(TEST_SECTOR_SIZE)),
usize::from(PoRepProofPartitions(2)),
);

let circuit = ZigZagCompound::blank_circuit(&public_params, &internal::ENGINE_PARAMS);
let circuit = ZigZagCompound::blank_circuit(&public_params, &ENGINE_PARAMS);
let mut params = phase21::MPCParameters::new(circuit).unwrap();

let rng = &mut OsRng::new().unwrap();
let hash = params.contribute(rng);

{
let circuit = ZigZagCompound::blank_circuit(&public_params, &internal::ENGINE_PARAMS);
let circuit = ZigZagCompound::blank_circuit(&public_params, &ENGINE_PARAMS);
let contributions = params.verify(circuit).expect("parameters should be valid!");

// We need to check the `contributions` to see if our `hash`
Expand Down
171 changes: 171 additions & 0 deletions filecoin-proofs/src/caches.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::Mutex;

use bellperson::groth16;
use paired::bls12_381::Bls12;

use sector_base::api::bytes_amount::PaddedBytesAmount;
use sector_base::api::porep_config::PoRepConfig;
use sector_base::api::porep_proof_partitions::PoRepProofPartitions;
use sector_base::api::post_config::PoStConfig;
use storage_proofs::circuit::vdf_post::VDFPoStCircuit;
use storage_proofs::circuit::vdf_post::VDFPostCompound;
use storage_proofs::circuit::zigzag::ZigZagCompound;
use storage_proofs::compound_proof::CompoundProof;
use storage_proofs::hasher::PedersenHasher;
use storage_proofs::vdf_post::VDFPoSt;
use storage_proofs::vdf_sloth::Sloth;

use crate::error;
use crate::parameters::{post_public_params, public_params};
use crate::singletons::ENGINE_PARAMS;
use crate::singletons::FCP_LOG;

type Bls12GrothParams = groth16::Parameters<Bls12>;
pub type Bls12VerifyingKey = groth16::VerifyingKey<Bls12>;

type Cache<G> = HashMap<String, Arc<G>>;
type GrothMemCache = Cache<Bls12GrothParams>;
type VerifyingKeyMemCache = Cache<Bls12VerifyingKey>;

lazy_static! {
static ref GROTH_PARAM_MEMORY_CACHE: Mutex<GrothMemCache> = Default::default();
static ref VERIFYING_KEY_MEMORY_CACHE: Mutex<VerifyingKeyMemCache> = Default::default();
}

pub fn cache_lookup<F, G>(
cache_ref: &Mutex<Cache<G>>,
identifier: String,
generator: F,
) -> error::Result<Arc<G>>
where
F: FnOnce() -> error::Result<G>,
G: Send + Sync,
{
info!(FCP_LOG, "trying parameters memory cache for: {}", &identifier; "target" => "params");
{
let cache = (*cache_ref).lock().unwrap();

if let Some(entry) = cache.get(&identifier) {
info!(FCP_LOG, "found params in memory cache for {}", &identifier; "target" => "params");
return Ok(entry.clone());
}
}

info!(FCP_LOG, "no params in memory cache for {}", &identifier; "target" => "params");

let new_entry = Arc::new(generator()?);
let res = new_entry.clone();
{
let cache = &mut (*cache_ref).lock().unwrap();
cache.insert(identifier, new_entry);
}

Ok(res)
}

#[inline]
pub fn lookup_groth_params<F>(
identifier: String,
generator: F,
) -> error::Result<Arc<Bls12GrothParams>>
where
F: FnOnce() -> error::Result<Bls12GrothParams>,
{
cache_lookup(&*GROTH_PARAM_MEMORY_CACHE, identifier, generator)
}

#[inline]
pub fn lookup_verifying_key<F>(
identifier: String,
generator: F,
) -> error::Result<Arc<Bls12VerifyingKey>>
where
F: FnOnce() -> error::Result<Bls12VerifyingKey>,
{
let vk_identifier = format!("{}-verifying-key", &identifier);
cache_lookup(&*VERIFYING_KEY_MEMORY_CACHE, vk_identifier, generator)
}

pub fn get_zigzag_params(
porep_config: PoRepConfig,
) -> error::Result<Arc<groth16::Parameters<Bls12>>> {
let public_params = public_params(
PaddedBytesAmount::from(porep_config),
usize::from(PoRepProofPartitions::from(porep_config)),
);

let parameters_generator =
|| ZigZagCompound::groth_params(&public_params, &ENGINE_PARAMS).map_err(Into::into);

Ok(lookup_groth_params(
format!(
"ZIGZAG[{}]",
usize::from(PaddedBytesAmount::from(porep_config))
),
parameters_generator,
)?)
}

pub fn get_post_params(post_config: PoStConfig) -> error::Result<Arc<groth16::Parameters<Bls12>>> {
let post_public_params = post_public_params(post_config);

let parameters_generator = || {
<VDFPostCompound as CompoundProof<
Bls12,
VDFPoSt<PedersenHasher, Sloth>,
VDFPoStCircuit<Bls12>,
>>::groth_params(&post_public_params, &ENGINE_PARAMS)
.map_err(Into::into)
};

Ok(lookup_groth_params(
format!(
"POST[{}]",
usize::from(PaddedBytesAmount::from(post_config))
),
parameters_generator,
)?)
}

pub fn get_zigzag_verifying_key(
porep_config: PoRepConfig,
) -> error::Result<Arc<Bls12VerifyingKey>> {
let public_params = public_params(
PaddedBytesAmount::from(porep_config),
usize::from(PoRepProofPartitions::from(porep_config)),
);

let vk_generator =
|| ZigZagCompound::verifying_key(&public_params, &ENGINE_PARAMS).map_err(Into::into);

Ok(lookup_verifying_key(
format!(
"ZIGZAG[{}]",
usize::from(PaddedBytesAmount::from(porep_config))
),
vk_generator,
)?)
}

pub fn get_post_verifying_key(post_config: PoStConfig) -> error::Result<Arc<Bls12VerifyingKey>> {
let post_public_params = post_public_params(post_config);

let vk_generator = || {
<VDFPostCompound as CompoundProof<
Bls12,
VDFPoSt<PedersenHasher, Sloth>,
VDFPoStCircuit<Bls12>,
>>::verifying_key(&post_public_params, &ENGINE_PARAMS)
.map_err(Into::into)
};

Ok(lookup_verifying_key(
format!(
"POST[{}]",
usize::from(PaddedBytesAmount::from(post_config))
),
vk_generator,
)?)
}
2 changes: 2 additions & 0 deletions filecoin-proofs/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub const POST_SECTORS_COUNT: usize = 2;
pub const POREP_MINIMUM_CHALLENGES: usize = 12; // FIXME: 8,000
2 changes: 1 addition & 1 deletion filecoin-proofs/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::FCP_LOG;
use crate::singletons::FCP_LOG;
use failure::{Backtrace, Error};
use slog::*;

Expand Down

0 comments on commit 3da7cb9

Please sign in to comment.