Skip to content

Commit

Permalink
Merge pull request zcash#73 from filecoin-project/example-less-data
Browse files Browse the repository at this point in the history
Drgporep test with fake data
  • Loading branch information
porcuquine authored Jul 10, 2018
2 parents 17d3542 + 4ec01de commit 0850449
Show file tree
Hide file tree
Showing 5 changed files with 353 additions and 104 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Configure to use nightly:
## Build

```
> cargo build
> cargo build --release --features u128-support
```

## Test
Expand All @@ -28,7 +28,7 @@ Configure to use nightly:
Build

```
> cargo build --examples --release
> cargo build --examples --release --features u128-support
```

Running them
Expand Down
151 changes: 70 additions & 81 deletions examples/drgporep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ extern crate sapling_crypto;

use bellman::groth16::*;
use bellman::{Circuit, ConstraintSystem, SynthesisError};
use pairing::bls12_381::{Bls12, Fr};
use pairing::bls12_381::Bls12;
use proofs::example_helper::Example;
use rand::Rng;
use sapling_crypto::jubjub::{JubjubBls12, JubjubEngine};

use proofs::fr32::{bytes_into_fr, fr_into_bytes};
use proofs::porep::PoRep;
use proofs::proof::ProofScheme;
use proofs::util::data_at_node;
use proofs::{circuit, drgporep};
use proofs::circuit;
use proofs::circuit::bench::BenchCS;
use proofs::fr32::fr_into_bytes;
use proofs::test_helper::fake_drgpoprep_proof;

struct DrgPoRepExample<'a, E: JubjubEngine> {
params: &'a E::Params,
Expand Down Expand Up @@ -56,6 +55,8 @@ impl<'a, E: JubjubEngine> Circuit<E> for DrgPoRepExample<'a, E> {
#[derive(Default)]
struct DrgPoRepApp {}

const SLOTH_ROUNDS: usize = 1;

impl Example<Bls12> for DrgPoRepApp {
fn name() -> String {
"DrgPoRep".to_string()
Expand Down Expand Up @@ -102,97 +103,43 @@ impl Example<Bls12> for DrgPoRepApp {
rng: &mut R,
engine_params: &JubjubBls12,
groth_params: &Parameters<Bls12>,
_tree_depth: usize,
tree_depth: usize,
_challenge_count: usize,
leaves: usize,
_leaves: usize,
lambda: usize,
m: usize,
) -> Proof<Bls12> {
let prover_id: Fr = rng.gen();
let prover_id_bytes = fr_into_bytes::<Bls12>(&prover_id);
let mut data: Vec<u8> = (0..leaves)
.flat_map(|_| fr_into_bytes::<Bls12>(&rng.gen()))
.collect();
let original_data = data.clone();
let challenge = 2;

let sp = drgporep::SetupParams {
lambda,
drg: drgporep::DrgParams { n: leaves, m },
};

let pp = drgporep::DrgPoRep::setup(&sp).expect("failed to create drgporep setup");

let (tau, aux) =
drgporep::DrgPoRep::replicate(&pp, prover_id_bytes.as_slice(), data.as_mut_slice())
.expect("failed to replicate");
let (
prover_id,
replica_node,
replica_node_path,
replica_root,
replica_parents,
replica_parents_paths,
data_node,
data_node_path,
data_root,
) = fake_drgpoprep_proof(rng, tree_depth, m, SLOTH_ROUNDS);

let pub_inputs = drgporep::PublicInputs {
prover_id: &prover_id,
challenge,
tau: &tau,
};
let priv_inputs = drgporep::PrivateInputs {
replica: data.as_slice(),
aux: &aux,
};
let prover_bytes = fr_into_bytes::<Bls12>(&prover_id);

let proof_nc =
drgporep::DrgPoRep::prove(&pp, &pub_inputs, &priv_inputs).expect("failed to prove");

assert!(
drgporep::DrgPoRep::verify(&pp, &pub_inputs, &proof_nc).expect("failed to verify"),
"failed to verify (non circuit)"
);

let replica_node = Some(&proof_nc.replica_node.data);

let replica_node_path = proof_nc.replica_node.proof.as_options();
let replica_root = Some(proof_nc.replica_node.proof.root().into());
let replica_parents: Vec<_> = proof_nc
.replica_parents
.iter()
.map(|(_, parent)| Some(&parent.data))
.collect();
let replica_parents_paths: Vec<_> = proof_nc
.replica_parents
.iter()
.map(|(_, parent)| parent.proof.as_options())
.collect();
let data_node = bytes_into_fr::<Bls12>(
data_at_node(&original_data, challenge, lambda).expect("failed to read original data"),
).unwrap();

let data_node_path = proof_nc.node.as_options();
let data_root = Some(proof_nc.node.root().into());
let prover_id = Some(prover_id_bytes.as_slice());

assert!(
proof_nc.node.validate(challenge),
"failed to verify data commitment"
);
assert!(
proof_nc.node.validate_data(&data_node),
"failed to verify data commitment with data"
);
// create an instance of our circut (with the witness)
let c = DrgPoRepExample {
params: engine_params,
lambda: lambda * 8,
replica_node,
replica_node: Some(&replica_node),
replica_node_path: &replica_node_path,
replica_root,
replica_parents,
replica_root: Some(replica_root),
replica_parents: replica_parents.iter().map(|parent| Some(parent)).collect(),
replica_parents_paths: &replica_parents_paths,
data_node: Some(&data_node),
data_node_path,
data_root,
prover_id,
data_node_path: data_node_path.clone(),
data_root: Some(data_root),
prover_id: Some(prover_bytes.as_slice()),
m,
};

// create groth16 proof
create_random_proof(c, groth_params, rng).expect("failed to create random proof")
create_random_proof(c, groth_params, rng).expect("failed to create proof")
}

fn verify_proof(
Expand All @@ -203,6 +150,48 @@ impl Example<Bls12> for DrgPoRepApp {
// not implemented yet
None
}
fn create_bench<R: Rng>(
&mut self,
rng: &mut R,
engine_params: &JubjubBls12,
tree_depth: usize,
_challenge_count: usize,
_leaves: usize,
lambda: usize,
m: usize,
) {
let (
prover_id,
replica_node,
replica_node_path,
replica_root,
replica_parents,
replica_parents_paths,
data_node,
data_node_path,
data_root,
) = fake_drgpoprep_proof(rng, tree_depth, m, SLOTH_ROUNDS);

let prover_bytes = fr_into_bytes::<Bls12>(&prover_id);
// create an instance of our circut (with the witness)
let c = DrgPoRepExample {
params: engine_params,
lambda: lambda * 8,
replica_node: Some(&replica_node),
replica_node_path: &replica_node_path,
replica_root: Some(replica_root),
replica_parents: replica_parents.iter().map(|parent| Some(parent)).collect(),
replica_parents_paths: &replica_parents_paths,
data_node: Some(&data_node),
data_node_path: data_node_path.clone(),
data_root: Some(data_root),
prover_id: Some(prover_bytes.as_slice()),
m,
};

let mut cs = BenchCS::<Bls12>::new();
c.synthesize(&mut cs).expect("failed to synthesize circuit");
}
}

fn main() {
Expand Down
30 changes: 30 additions & 0 deletions examples/merklepor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ extern crate rand;
extern crate sapling_crypto;

use bellman::groth16::*;
use bellman::Circuit;
use pairing::bls12_381::{Bls12, Fr};
use pairing::Field;
use rand::Rng;
use sapling_crypto::circuit::multipack;
use sapling_crypto::jubjub::JubjubBls12;

use proofs::circuit;
use proofs::circuit::bench::BenchCS;
use proofs::example_helper::Example;
use proofs::test_helper::random_merkle_path;

Expand Down Expand Up @@ -130,6 +132,34 @@ impl Example<Bls12> for MerklePorApp {
// -- verify proof with public inputs
Some(verify_proof(pvk, proof, &expected_inputs).expect("failed to verify proof"))
}

fn create_bench<R: Rng>(
&mut self,
rng: &mut R,
engine_params: &JubjubBls12,
tree_depth: usize,
challenge_count: usize,
_leaves: usize,
_lambda: usize,
_m: usize,
) {
let (auth_path, leaf, root) = random_merkle_path(rng, tree_depth);
self.root = root;
self.leaf = leaf;
self.auth_paths = (0..challenge_count).map(|_| auth_path.clone()).collect();
let values = (0..challenge_count).map(|_| Some(&self.leaf)).collect();

// create an instance of our circut (with the witness)
let c = circuit::ppor::ParallelProofOfRetrievability {
params: engine_params,
values,
auth_paths: &self.auth_paths,
root: Some(self.root),
};

let mut cs = BenchCS::<Bls12>::new();
c.synthesize(&mut cs).expect("failed to synthesize circuit");
}
}

fn main() {
Expand Down
Loading

0 comments on commit 0850449

Please sign in to comment.