Profiler API WIP

This commit is contained in:
Sean Bowe 2018-06-22 20:02:07 -06:00
parent 10c5010fd9
commit b1c1c17fc7
6 changed files with 73 additions and 21 deletions

View File

@ -22,7 +22,8 @@ use ::{
ConstraintSystem,
LinearCombination,
Variable,
Index
Index,
Profiler
};
use ::domain::{
@ -36,7 +37,8 @@ use ::multicore::{
/// Generates a random common reference string for
/// a circuit.
pub fn generate_random_parameters<E, C, R>(
pub fn generate_random_parameters<'a, P: Profiler, E, C, R>(
profiler: &'a mut P,
circuit: C,
rng: &mut R
) -> Result<Parameters<E>, SynthesisError>
@ -50,7 +52,8 @@ pub fn generate_random_parameters<E, C, R>(
let delta = rng.gen();
let tau = rng.gen();
generate_parameters::<E, C>(
generate_parameters::<P, E, C>(
profiler,
circuit,
g1,
g2,
@ -64,7 +67,7 @@ pub fn generate_random_parameters<E, C, R>(
/// This is our assembly structure that we'll use to synthesize the
/// circuit into a QAP.
struct KeypairAssembly<E: Engine> {
struct KeypairAssembly<'a, E: Engine, P: Profiler + 'a> {
num_inputs: usize,
num_aux: usize,
num_constraints: usize,
@ -73,10 +76,17 @@ struct KeypairAssembly<E: Engine> {
ct_inputs: Vec<Vec<(E::Fr, usize)>>,
at_aux: Vec<Vec<(E::Fr, usize)>>,
bt_aux: Vec<Vec<(E::Fr, usize)>>,
ct_aux: Vec<Vec<(E::Fr, usize)>>
ct_aux: Vec<Vec<(E::Fr, usize)>>,
profiler: &'a mut P
}
impl<E: Engine> ConstraintSystem<E> for KeypairAssembly<E> {
impl<'a, E: Engine, P: Profiler> ConstraintSystem<E> for KeypairAssembly<'a, E, P> {
type Profiler = P;
fn profiler(&mut self) -> &mut Self::Profiler {
self.profiler
}
type Root = Self;
fn alloc<F, A, AR>(
@ -170,7 +180,8 @@ impl<E: Engine> ConstraintSystem<E> for KeypairAssembly<E> {
}
/// Create parameters for a circuit, given some toxic waste.
pub fn generate_parameters<E, C>(
pub fn generate_parameters<'a, P: Profiler, E, C>(
profiler: &'a mut P,
circuit: C,
g1: E::G1,
g2: E::G2,
@ -191,7 +202,8 @@ pub fn generate_parameters<E, C>(
ct_inputs: vec![],
at_aux: vec![],
bt_aux: vec![],
ct_aux: vec![]
ct_aux: vec![],
profiler: profiler
};
// Allocate the "one" input variable

View File

@ -526,7 +526,8 @@ mod test_with_bls12_381 {
let rng = &mut thread_rng();
let params = generate_random_parameters::<Bls12, _, _>(
let params = generate_random_parameters::<_, Bls12, _, _>(
&mut (),
MySillyCircuit { a: None, b: None },
rng
).unwrap();
@ -553,6 +554,7 @@ mod test_with_bls12_381 {
c.mul_assign(&b);
let proof = create_random_proof(
&mut (),
MySillyCircuit {
a: Some(a),
b: Some(b)

View File

@ -23,7 +23,8 @@ use ::{
ConstraintSystem,
LinearCombination,
Variable,
Index
Index,
Profiler
};
use ::domain::{
@ -80,7 +81,7 @@ fn eval<E: Engine>(
acc
}
struct ProvingAssignment<E: Engine> {
struct ProvingAssignment<'a, E: Engine, P: Profiler + 'a> {
// Density of queries
a_aux_density: DensityTracker,
b_input_density: DensityTracker,
@ -93,10 +94,18 @@ struct ProvingAssignment<E: Engine> {
// Assignments of variables
input_assignment: Vec<E::Fr>,
aux_assignment: Vec<E::Fr>
aux_assignment: Vec<E::Fr>,
profiler: &'a mut P
}
impl<E: Engine> ConstraintSystem<E> for ProvingAssignment<E> {
impl<'a, E: Engine, P: Profiler> ConstraintSystem<E> for ProvingAssignment<'a, E, P> {
type Profiler = P;
fn profiler(&mut self) -> &mut Self::Profiler {
self.profiler
}
type Root = Self;
fn alloc<F, A, AR>(
@ -188,9 +197,10 @@ impl<E: Engine> ConstraintSystem<E> for ProvingAssignment<E> {
}
}
pub fn create_random_proof<E, C, R, P: ParameterSource<E>>(
pub fn create_random_proof<'a, P: Profiler, E, C, R, Params: ParameterSource<E>>(
profiler: &'a mut P,
circuit: C,
params: P,
params: Params,
rng: &mut R
) -> Result<Proof<E>, SynthesisError>
where E: Engine, C: Circuit<E>, R: Rng
@ -198,12 +208,13 @@ pub fn create_random_proof<E, C, R, P: ParameterSource<E>>(
let r = rng.gen();
let s = rng.gen();
create_proof::<E, C, P>(circuit, params, r, s)
create_proof::<P, E, C, Params>(profiler, circuit, params, r, s)
}
pub fn create_proof<E, C, P: ParameterSource<E>>(
pub fn create_proof<'a, P: Profiler, E, C, Params: ParameterSource<E>>(
profiler: &'a mut P,
circuit: C,
mut params: P,
mut params: Params,
r: E::Fr,
s: E::Fr
) -> Result<Proof<E>, SynthesisError>
@ -217,7 +228,8 @@ pub fn create_proof<E, C, P: ParameterSource<E>>(
b: vec![],
c: vec![],
input_assignment: vec![],
aux_assignment: vec![]
aux_assignment: vec![],
profiler: profiler
};
prover.alloc_input(|| "", || Ok(E::Fr::one()))?;

View File

@ -113,6 +113,7 @@ fn test_xordemo() {
};
generate_parameters(
&mut (),
c,
g1,
g2,
@ -303,6 +304,7 @@ fn test_xordemo() {
};
create_proof(
&mut (),
c,
&params,
r,

View File

@ -217,9 +217,21 @@ impl fmt::Display for SynthesisError {
}
}
pub trait Profiler {
}
impl Profiler for () {
}
/// Represents a constraint system which can have new variables
/// allocated and constrains between them formed.
pub trait ConstraintSystem<E: Engine>: Sized {
type Profiler: Profiler;
fn profiler(&mut self) -> &mut Self::Profiler;
/// Represents the type of the "root" of this constraint system
/// so that nested namespaces can minimize indirection.
type Root: ConstraintSystem<E>;
@ -294,6 +306,12 @@ pub trait ConstraintSystem<E: Engine>: Sized {
pub struct Namespace<'a, E: Engine, CS: ConstraintSystem<E> + 'a>(&'a mut CS, PhantomData<E>);
impl<'cs, E: Engine, CS: ConstraintSystem<E>> ConstraintSystem<E> for Namespace<'cs, E, CS> {
type Profiler = CS::Profiler;
fn profiler(&mut self) -> &mut Self::Profiler {
self.0.profiler()
}
type Root = CS::Root;
fn one() -> Variable {
@ -365,6 +383,12 @@ impl<'a, E: Engine, CS: ConstraintSystem<E>> Drop for Namespace<'a, E, CS> {
/// Convenience implementation of ConstraintSystem<E> for mutable references to
/// constraint systems.
impl<'cs, E: Engine, CS: ConstraintSystem<E>> ConstraintSystem<E> for &'cs mut CS {
type Profiler = CS::Profiler;
fn profiler(&mut self) -> &mut Self::Profiler {
(**self).profiler()
}
type Root = CS::Root;
fn one() -> Variable {

View File

@ -185,7 +185,7 @@ fn test_mimc() {
constants: &constants
};
generate_random_parameters(c, rng).unwrap()
generate_random_parameters(&mut (), c, rng).unwrap()
};
// Prepare the verification key (for proof verification)
@ -221,7 +221,7 @@ fn test_mimc() {
};
// Create a groth16 proof with our parameters.
let proof = create_random_proof(c, &params, rng).unwrap();
let proof = create_random_proof(&mut (), c, &params, rng).unwrap();
proof.write(&mut proof_vec).unwrap();
}