diff --git a/src/groth16/generator.rs b/src/groth16/generator.rs index 1eed62db0..bc433c72c 100644 --- a/src/groth16/generator.rs +++ b/src/groth16/generator.rs @@ -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( +pub fn generate_random_parameters<'a, P: Profiler, E, C, R>( + profiler: &'a mut P, circuit: C, rng: &mut R ) -> Result, SynthesisError> @@ -50,7 +52,8 @@ pub fn generate_random_parameters( let delta = rng.gen(); let tau = rng.gen(); - generate_parameters::( + generate_parameters::( + profiler, circuit, g1, g2, @@ -64,7 +67,7 @@ pub fn generate_random_parameters( /// This is our assembly structure that we'll use to synthesize the /// circuit into a QAP. -struct KeypairAssembly { +struct KeypairAssembly<'a, E: Engine, P: Profiler + 'a> { num_inputs: usize, num_aux: usize, num_constraints: usize, @@ -73,10 +76,17 @@ struct KeypairAssembly { ct_inputs: Vec>, at_aux: Vec>, bt_aux: Vec>, - ct_aux: Vec> + ct_aux: Vec>, + profiler: &'a mut P } -impl ConstraintSystem for KeypairAssembly { +impl<'a, E: Engine, P: Profiler> ConstraintSystem for KeypairAssembly<'a, E, P> { + type Profiler = P; + + fn profiler(&mut self) -> &mut Self::Profiler { + self.profiler + } + type Root = Self; fn alloc( @@ -170,7 +180,8 @@ impl ConstraintSystem for KeypairAssembly { } /// Create parameters for a circuit, given some toxic waste. -pub fn generate_parameters( +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( ct_inputs: vec![], at_aux: vec![], bt_aux: vec![], - ct_aux: vec![] + ct_aux: vec![], + profiler: profiler }; // Allocate the "one" input variable diff --git a/src/groth16/mod.rs b/src/groth16/mod.rs index 3b8d67148..a04047fd6 100644 --- a/src/groth16/mod.rs +++ b/src/groth16/mod.rs @@ -526,7 +526,8 @@ mod test_with_bls12_381 { let rng = &mut thread_rng(); - let params = generate_random_parameters::( + 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) diff --git a/src/groth16/prover.rs b/src/groth16/prover.rs index f21fcce90..cd21e2cc9 100644 --- a/src/groth16/prover.rs +++ b/src/groth16/prover.rs @@ -23,7 +23,8 @@ use ::{ ConstraintSystem, LinearCombination, Variable, - Index + Index, + Profiler }; use ::domain::{ @@ -80,7 +81,7 @@ fn eval( acc } -struct ProvingAssignment { +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 { // Assignments of variables input_assignment: Vec, - aux_assignment: Vec + aux_assignment: Vec, + + profiler: &'a mut P } -impl ConstraintSystem for ProvingAssignment { +impl<'a, E: Engine, P: Profiler> ConstraintSystem for ProvingAssignment<'a, E, P> { + type Profiler = P; + + fn profiler(&mut self) -> &mut Self::Profiler { + self.profiler + } + type Root = Self; fn alloc( @@ -188,9 +197,10 @@ impl ConstraintSystem for ProvingAssignment { } } -pub fn create_random_proof>( +pub fn create_random_proof<'a, P: Profiler, E, C, R, Params: ParameterSource>( + profiler: &'a mut P, circuit: C, - params: P, + params: Params, rng: &mut R ) -> Result, SynthesisError> where E: Engine, C: Circuit, R: Rng @@ -198,12 +208,13 @@ pub fn create_random_proof>( let r = rng.gen(); let s = rng.gen(); - create_proof::(circuit, params, r, s) + create_proof::(profiler, circuit, params, r, s) } -pub fn create_proof>( +pub fn create_proof<'a, P: Profiler, E, C, Params: ParameterSource>( + profiler: &'a mut P, circuit: C, - mut params: P, + mut params: Params, r: E::Fr, s: E::Fr ) -> Result, SynthesisError> @@ -217,7 +228,8 @@ pub fn create_proof>( b: vec![], c: vec![], input_assignment: vec![], - aux_assignment: vec![] + aux_assignment: vec![], + profiler: profiler }; prover.alloc_input(|| "", || Ok(E::Fr::one()))?; diff --git a/src/groth16/tests/mod.rs b/src/groth16/tests/mod.rs index a8e291477..cf44e50e3 100644 --- a/src/groth16/tests/mod.rs +++ b/src/groth16/tests/mod.rs @@ -113,6 +113,7 @@ fn test_xordemo() { }; generate_parameters( + &mut (), c, g1, g2, @@ -303,6 +304,7 @@ fn test_xordemo() { }; create_proof( + &mut (), c, ¶ms, r, diff --git a/src/lib.rs b/src/lib.rs index fb8d0431b..0c2dd75b4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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: 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; @@ -294,6 +306,12 @@ pub trait ConstraintSystem: Sized { pub struct Namespace<'a, E: Engine, CS: ConstraintSystem + 'a>(&'a mut CS, PhantomData); impl<'cs, E: Engine, CS: ConstraintSystem> ConstraintSystem 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> Drop for Namespace<'a, E, CS> { /// Convenience implementation of ConstraintSystem for mutable references to /// constraint systems. impl<'cs, E: Engine, CS: ConstraintSystem> ConstraintSystem for &'cs mut CS { + type Profiler = CS::Profiler; + + fn profiler(&mut self) -> &mut Self::Profiler { + (**self).profiler() + } + type Root = CS::Root; fn one() -> Variable { diff --git a/tests/mimc.rs b/tests/mimc.rs index d6ff72b7b..c36cb7850 100644 --- a/tests/mimc.rs +++ b/tests/mimc.rs @@ -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, ¶ms, rng).unwrap(); + let proof = create_random_proof(&mut (), c, ¶ms, rng).unwrap(); proof.write(&mut proof_vec).unwrap(); }