Skip to content

Commit

Permalink
Merge pull request zcash#83 from filecoin-project/example-circuit-size
Browse files Browse the repository at this point in the history
feat(example): print number of constraints on bench
  • Loading branch information
nicola authored Jul 14, 2018
2 parents 1409d9a + bda64ce commit da2697d
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 48 deletions.
108 changes: 78 additions & 30 deletions examples/drgporep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,52 @@ struct DrgPoRepApp {}

const SLOTH_ROUNDS: usize = 1;

impl DrgPoRepApp {
fn create_bench_circuit<R: Rng>(
&mut self,
rng: &mut R,
engine_params: &JubjubBls12,
tree_depth: usize,
_challenge_count: usize,
_leaves: usize,
lambda: usize,
m: usize,
) -> BenchCS<Bls12> {
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");
cs
}
}

impl Example<Bls12> for DrgPoRepApp {
fn name() -> String {
"DrgPoRep".to_string()
Expand Down Expand Up @@ -150,47 +196,49 @@ 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,
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);
self.create_bench_circuit(
rng,
engine_params,
tree_depth,
challenge_count,
leaves,
lambda,
m,
);
}

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()),
fn get_num_constraints<R: Rng>(
&mut self,
rng: &mut R,
engine_params: &JubjubBls12,
tree_depth: usize,
challenge_count: usize,
leaves: usize,
lambda: usize,
m: usize,
) -> usize {
let cs = self.create_bench_circuit(
rng,
engine_params,
tree_depth,
challenge_count,
leaves,
lambda,
m,
};
);

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

Expand Down
82 changes: 64 additions & 18 deletions examples/merklepor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,37 @@ impl Default for MerklePorApp {
}
}

impl MerklePorApp {
fn create_bench_circuit<R: Rng>(
&mut self,
rng: &mut R,
engine_params: &JubjubBls12,
tree_depth: usize,
challenge_count: usize,
_leaves: usize,
_lambda: usize,
_m: usize,
) -> BenchCS<Bls12> {
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");
cs
}
}

impl Example<Bls12> for MerklePorApp {
fn name() -> String {
"Multi-Challenge MerklePor".to_string()
Expand Down Expand Up @@ -139,26 +170,41 @@ impl Example<Bls12> for MerklePorApp {
engine_params: &JubjubBls12,
tree_depth: usize,
challenge_count: usize,
_leaves: usize,
_lambda: usize,
_m: 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),
};
self.create_bench_circuit(
rng,
engine_params,
tree_depth,
challenge_count,
leaves,
lambda,
m,
);
}

let mut cs = BenchCS::<Bls12>::new();
c.synthesize(&mut cs).expect("failed to synthesize circuit");
fn get_num_constraints<R: Rng>(
&mut self,
rng: &mut R,
engine_params: &JubjubBls12,
tree_depth: usize,
challenge_count: usize,
leaves: usize,
lambda: usize,
m: usize,
) -> usize {
let cs = self.create_bench_circuit(
rng,
engine_params,
tree_depth,
challenge_count,
leaves,
lambda,
m,
);
cs.num_constraints()
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/circuit/bench/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ impl<E: Engine> BenchCS<E> {
c: vec![],
}
}

pub fn num_constraints(&self) -> usize {
self.a.len()
}
}

impl<E: Engine> ConstraintSystem<E> for BenchCS<E> {
Expand Down
26 changes: 26 additions & 0 deletions src/example_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,20 @@ pub trait Example<E: JubjubEngine>: Default {
.progress_chars("#>-"),
);

info!(
target: "stats",
"Number of constraints: {}",
self.get_num_constraints(
rng,
&engine_params,
tree_depth,
challenge_count,
leaves,
lambda,
m
)
);

for _ in 0..samples {
// -- create proof

Expand Down Expand Up @@ -349,4 +363,16 @@ pub trait Example<E: JubjubEngine>: Default {

/// Create a new bench
fn create_bench<R: Rng>(&mut self, &mut R, &E::Params, usize, usize, usize, usize, usize);

/// Get the number of constraitns of the circuit
fn get_num_constraints<R: Rng>(
&mut self,
&mut R,
&E::Params,
usize,
usize,
usize,
usize,
usize,
) -> usize;
}

0 comments on commit da2697d

Please sign in to comment.