Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/groth16/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ use ::{
ConstraintSystem,
LinearCombination,
Variable,
Index
Index,
Profiler
};

use ::domain::{
Expand All @@ -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>
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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>(
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/groth16/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)
Expand Down
32 changes: 22 additions & 10 deletions src/groth16/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ use ::{
ConstraintSystem,
LinearCombination,
Variable,
Index
Index,
Profiler
};

use ::domain::{
Expand Down Expand Up @@ -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,
Expand All @@ -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>(
Expand Down Expand Up @@ -188,22 +197,24 @@ 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
{
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>
Expand All @@ -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()))?;
Expand Down
2 changes: 2 additions & 0 deletions src/groth16/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ fn test_xordemo() {
};

generate_parameters(
&mut (),
c,
g1,
g2,
Expand Down Expand Up @@ -303,6 +304,7 @@ fn test_xordemo() {
};

create_proof(
&mut (),
c,
&params,
r,
Expand Down
24 changes: 24 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions tests/mimc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
}
Expand Down