Skip to content

Commit

Permalink
Make the namespacing not introduce too much indirection.
Browse files Browse the repository at this point in the history
  • Loading branch information
ebfull committed Nov 20, 2017
1 parent a1e1aa3 commit 945d86f
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 22 deletions.
16 changes: 15 additions & 1 deletion src/groth16/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ use ::{
Circuit,
Variable,
ConstraintSystem,
PublicConstraintSystem
PublicConstraintSystem,
Namespace
};
use std::marker::PhantomData;
use super::{VerifyingKey, Parameters};
use domain::{Scalar, EvaluationDomain};
use rand::Rng;
Expand Down Expand Up @@ -91,6 +93,8 @@ pub fn generate_parameters<E, C>(
}

impl<E: Engine> ConstraintSystem<E> for KeypairAssembly<E> {
type Root = Self;

fn alloc<NR, N, F>(
&mut self,
_: N,
Expand Down Expand Up @@ -140,6 +144,16 @@ pub fn generate_parameters<E, C>(

self.num_constraints += 1;
}

/// Begin a namespace for the constraint system
fn namespace<'a, NR, N>(
&'a mut self,
_: N
) -> Namespace<'a, E, Self::Root>
where NR: Into<String>, N: FnOnce() -> NR
{
Namespace(self, PhantomData)
}
}

let mut assembly = KeypairAssembly {
Expand Down
16 changes: 15 additions & 1 deletion src/groth16/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use ::{
Index,
Error,
Variable,
LinearCombination
LinearCombination,
Namespace
};
use std::marker::PhantomData;
use multiexp::*;
use super::{ParameterSource, Proof};
use rand::Rng;
Expand Down Expand Up @@ -70,6 +72,8 @@ pub fn create_proof<E, C, P: ParameterSource<E>>(
}

impl<E: Engine> ConstraintSystem<E> for ProvingAssignment<E> {
type Root = Self;

fn alloc<NR, N, F>(
&mut self,
_: N,
Expand All @@ -96,6 +100,16 @@ pub fn create_proof<E, C, P: ParameterSource<E>>(
self.b.push(Scalar(b.eval(Some(&mut self.b_input_density), Some(&mut self.b_aux_density), &self.input_assignment, &self.aux_assignment)));
self.c.push(Scalar(c.eval(None, None, &self.input_assignment, &self.aux_assignment)));
}

/// Begin a namespace for the constraint system
fn namespace<'a, NR, N>(
&'a mut self,
_: N
) -> Namespace<'a, E, Self::Root>
where NR: Into<String>, N: FnOnce() -> NR
{
Namespace(self, PhantomData)
}
}

let mut prover = ProvingAssignment {
Expand Down
32 changes: 29 additions & 3 deletions src/groth16/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ use ::{
Index,
Variable,
ConstraintSystem,
PublicConstraintSystem
PublicConstraintSystem,
Namespace
};
use super::{Proof, VerifyingKey, PreparedVerifyingKey};
use std::marker::PhantomData;

/// This is the constraint system synthesizer that is made available to
/// callers of the verification function when they wish to perform
Expand All @@ -21,7 +23,9 @@ pub struct VerifierInput<'a, E: Engine> {
num_aux: usize
}

impl<'a, E: Engine> ConstraintSystem<E> for VerifierInput<'a, E> {
impl<'cs, E: Engine> ConstraintSystem<E> for VerifierInput<'cs, E> {
type Root = Self;

fn alloc<NR, N, F>(
&mut self,
_: N,
Expand Down Expand Up @@ -51,13 +55,25 @@ impl<'a, E: Engine> ConstraintSystem<E> for VerifierInput<'a, E> {
// Do nothing; we don't care about the constraint system
// in this context.
}

/// Begin a namespace for the constraint system
fn namespace<'a, NR, N>(
&'a mut self,
_: N
) -> Namespace<'a, E, Self::Root>
where NR: Into<String>, N: FnOnce() -> NR
{
Namespace(self, PhantomData)
}
}

/// This is intended to be a wrapper around VerifierInput that is kept
/// private and used for input allocation.
struct InputAllocator<T>(T);

impl<'a, 'b, E: Engine> ConstraintSystem<E> for InputAllocator<&'a mut VerifierInput<'b, E>> {
impl<'cs, 'b, E: Engine> ConstraintSystem<E> for InputAllocator<&'cs mut VerifierInput<'b, E>> {
type Root = Self;

fn alloc<NR, N, F>(
&mut self,
name_fn: N,
Expand All @@ -79,6 +95,16 @@ impl<'a, 'b, E: Engine> ConstraintSystem<E> for InputAllocator<&'a mut VerifierI
// Do nothing; we don't care about the constraint system
// in this context.
}

/// Begin a namespace for the constraint system
fn namespace<'a, NR, N>(
&'a mut self,
_: N
) -> Namespace<'a, E, Self::Root>
where NR: Into<String>, N: FnOnce() -> NR
{
Namespace(self, PhantomData)
}
}

impl<'a, 'b, E: Engine> PublicConstraintSystem<E> for InputAllocator<&'a mut VerifierInput<'b, E>> {
Expand Down
159 changes: 142 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ pub trait PublicConstraintSystem<E: Engine>: ConstraintSystem<E> {
where NR: Into<String>, N: FnOnce() -> NR, F: FnOnce() -> Result<E::Fr, Error>;
}

pub trait ConstraintSystem<E: Engine> {
pub trait ConstraintSystem<E: Engine>: Sized {
type Root: ConstraintSystem<E>;

/// Return the "one" input variable
fn one() -> Variable {
Variable(Index::Input(0))
Expand All @@ -214,15 +216,129 @@ pub trait ConstraintSystem<E: Engine> {
c: LinearCombination<E>
);

fn push_namespace<NR, N>(&mut self, _: N)
where NR: Into<String>, N: FnOnce() -> NR
{
// Default is to do nothing.
}

fn pop_namespace(&mut self)
{
// Default is to do nothing.
}

/// Begin a namespace for the constraint system
fn namespace<NR, N, R, F>(
fn namespace<'a, NR, N>(
&'a mut self,
name_fn: N
) -> Namespace<'a, E, Self::Root>
where NR: Into<String>, N: FnOnce() -> NR;
}

impl<'cs, E: Engine, CS: ConstraintSystem<E>> ConstraintSystem<E> for &'cs mut CS {
type Root = CS::Root;

/// Allocate a private variable in the constraint system. The provided function is used to
/// determine the assignment of the variable.
fn alloc<NR, N, F>(
&mut self,
name_fn: N,
f: F
) -> Result<Variable, Error>
where NR: Into<String>, N: FnOnce() -> NR, F: FnOnce() -> Result<E::Fr, Error>
{
(*self).alloc(name_fn, f)
}

/// Enforce that `A` * `B` = `C`.
fn enforce<NR: Into<String>, N: FnOnce() -> NR>(
&mut self,
_: N,
space_fn: F
) -> Result<R, Error>
where NR: Into<String>, N: FnOnce() -> NR, F: FnOnce(&mut Self) -> Result<R, Error>
name_fn: N,
a: LinearCombination<E>,
b: LinearCombination<E>,
c: LinearCombination<E>
)
{
(*self).enforce(name_fn, a, b, c)
}

fn push_namespace<NR, N>(&mut self, name_fn: N)
where NR: Into<String>, N: FnOnce() -> NR
{
(*self).push_namespace(name_fn)
}

fn pop_namespace(&mut self)
{
(*self).pop_namespace()
}

/// Begin a namespace for the constraint system
fn namespace<'a, NR, N>(
&'a mut self,
name_fn: N
) -> Namespace<'a, E, Self::Root>
where NR: Into<String>, N: FnOnce() -> NR
{
space_fn(self)
(*self).namespace(name_fn)
}
}

use std::marker::PhantomData;

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 Root = CS;

fn alloc<NR, N, F>(
&mut self,
name_fn: N,
f: F
) -> Result<Variable, Error>
where NR: Into<String>, N: FnOnce() -> NR, F: FnOnce() -> Result<E::Fr, Error>
{
self.0.alloc(name_fn, f)
}

fn enforce<NR: Into<String>, N: FnOnce() -> NR>(
&mut self,
name_fn: N,
a: LinearCombination<E>,
b: LinearCombination<E>,
c: LinearCombination<E>
)
{
self.0.enforce(name_fn, a, b, c)
}

fn push_namespace<NR, N>(&mut self, name_fn: N)
where NR: Into<String>, N: FnOnce() -> NR
{
self.0.push_namespace(name_fn);
}

fn pop_namespace(&mut self)
{
self.0.pop_namespace();
}

/// Begin a namespace for the constraint system
fn namespace<'a, NR, N>(
&'a mut self,
name_fn: N
) -> Namespace<'a, E, Self::Root>
where NR: Into<String>, N: FnOnce() -> NR
{
self.0.push_namespace(name_fn);

Namespace(self.0, PhantomData)
}
}

impl<'a, E: Engine, CS: ConstraintSystem<E>> Drop for Namespace<'a, E, CS> {
fn drop(&mut self) {
self.0.pop_namespace()
}
}

Expand Down Expand Up @@ -353,6 +469,8 @@ impl<E: Engine> PublicConstraintSystem<E> for TestConstraintSystem<E> {
}

impl<E: Engine> ConstraintSystem<E> for TestConstraintSystem<E> {
type Root = Self;

fn alloc<NR, N, F>(
&mut self,
name_fn: N,
Expand Down Expand Up @@ -386,24 +504,31 @@ impl<E: Engine> ConstraintSystem<E> for TestConstraintSystem<E> {
self.constraints.push((a, b, c, this_path));
}

fn namespace<NR, N, R, F>(
&mut self,
name_fn: N,
space_fn: F
) -> Result<R, Error>
where NR: Into<String>, N: FnOnce() -> NR, F: FnOnce(&mut Self) -> Result<R, Error>
fn push_namespace<NR, N>(&mut self, name_fn: N)
where NR: Into<String>, N: FnOnce() -> NR
{
let name = name_fn().into();

let this_path = compute_path(&self.current_namespace, name.clone());

self.set_named_obj(this_path, NamedObject::Namespace);

self.current_namespace.push(name);
}

let r = space_fn(self)?;

fn pop_namespace(&mut self)
{
self.current_namespace.pop();
}

/// Begin a namespace for the constraint system
fn namespace<'a, NR, N>(
&'a mut self,
name_fn: N
) -> Namespace<'a, E, Self::Root>
where NR: Into<String>, N: FnOnce() -> NR
{
self.push_namespace(name_fn);

Ok(r)
Namespace(self, PhantomData)
}
}

0 comments on commit 945d86f

Please sign in to comment.