Make the namespacing not introduce too much indirection.

This commit is contained in:
Sean Bowe 2017-11-20 15:50:41 -07:00
parent a1e1aa30b5
commit 945d86fe7d
4 changed files with 203 additions and 24 deletions

View File

@ -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;
@ -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,
@ -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 {

View File

@ -8,8 +8,10 @@ use ::{
Index,
Error,
Variable,
LinearCombination
LinearCombination,
Namespace
};
use std::marker::PhantomData;
use multiexp::*;
use super::{ParameterSource, Proof};
use rand::Rng;
@ -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,
@ -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 {

View File

@ -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
@ -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,
@ -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,
@ -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>> {

View File

@ -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))
@ -214,15 +216,129 @@ pub trait ConstraintSystem<E: Engine> {
c: LinearCombination<E>
);
/// Begin a namespace for the constraint system
fn namespace<NR, N, R, F>(
&mut self,
_: 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, _: N)
where NR: Into<String>, N: FnOnce() -> NR
{
space_fn(self)
// Default is to do nothing.
}
fn pop_namespace(&mut self)
{
// Default is to do nothing.
}
/// 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;
}
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,
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
{
(*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()
}
}
@ -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,
@ -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();
}
Ok(r)
/// 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);
Namespace(self, PhantomData)
}
}