mirror of
https://github.com/Qortal/pirate-librustzcash.git
synced 2025-01-31 07:42:15 +00:00
Make the namespacing not introduce too much indirection.
This commit is contained in:
parent
a1e1aa30b5
commit
945d86fe7d
@ -7,8 +7,10 @@ use ::{
|
|||||||
Circuit,
|
Circuit,
|
||||||
Variable,
|
Variable,
|
||||||
ConstraintSystem,
|
ConstraintSystem,
|
||||||
PublicConstraintSystem
|
PublicConstraintSystem,
|
||||||
|
Namespace
|
||||||
};
|
};
|
||||||
|
use std::marker::PhantomData;
|
||||||
use super::{VerifyingKey, Parameters};
|
use super::{VerifyingKey, Parameters};
|
||||||
use domain::{Scalar, EvaluationDomain};
|
use domain::{Scalar, EvaluationDomain};
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
@ -91,6 +93,8 @@ pub fn generate_parameters<E, C>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Engine> ConstraintSystem<E> for KeypairAssembly<E> {
|
impl<E: Engine> ConstraintSystem<E> for KeypairAssembly<E> {
|
||||||
|
type Root = Self;
|
||||||
|
|
||||||
fn alloc<NR, N, F>(
|
fn alloc<NR, N, F>(
|
||||||
&mut self,
|
&mut self,
|
||||||
_: N,
|
_: N,
|
||||||
@ -140,6 +144,16 @@ pub fn generate_parameters<E, C>(
|
|||||||
|
|
||||||
self.num_constraints += 1;
|
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 {
|
let mut assembly = KeypairAssembly {
|
||||||
|
@ -8,8 +8,10 @@ use ::{
|
|||||||
Index,
|
Index,
|
||||||
Error,
|
Error,
|
||||||
Variable,
|
Variable,
|
||||||
LinearCombination
|
LinearCombination,
|
||||||
|
Namespace
|
||||||
};
|
};
|
||||||
|
use std::marker::PhantomData;
|
||||||
use multiexp::*;
|
use multiexp::*;
|
||||||
use super::{ParameterSource, Proof};
|
use super::{ParameterSource, Proof};
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
@ -70,6 +72,8 @@ pub fn create_proof<E, C, P: ParameterSource<E>>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Engine> ConstraintSystem<E> for ProvingAssignment<E> {
|
impl<E: Engine> ConstraintSystem<E> for ProvingAssignment<E> {
|
||||||
|
type Root = Self;
|
||||||
|
|
||||||
fn alloc<NR, N, F>(
|
fn alloc<NR, N, F>(
|
||||||
&mut self,
|
&mut self,
|
||||||
_: N,
|
_: 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.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)));
|
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 {
|
let mut prover = ProvingAssignment {
|
||||||
|
@ -6,9 +6,11 @@ use ::{
|
|||||||
Index,
|
Index,
|
||||||
Variable,
|
Variable,
|
||||||
ConstraintSystem,
|
ConstraintSystem,
|
||||||
PublicConstraintSystem
|
PublicConstraintSystem,
|
||||||
|
Namespace
|
||||||
};
|
};
|
||||||
use super::{Proof, VerifyingKey, PreparedVerifyingKey};
|
use super::{Proof, VerifyingKey, PreparedVerifyingKey};
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
/// This is the constraint system synthesizer that is made available to
|
/// This is the constraint system synthesizer that is made available to
|
||||||
/// callers of the verification function when they wish to perform
|
/// callers of the verification function when they wish to perform
|
||||||
@ -21,7 +23,9 @@ pub struct VerifierInput<'a, E: Engine> {
|
|||||||
num_aux: usize
|
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>(
|
fn alloc<NR, N, F>(
|
||||||
&mut self,
|
&mut self,
|
||||||
_: N,
|
_: 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
|
// Do nothing; we don't care about the constraint system
|
||||||
// in this context.
|
// 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
|
/// This is intended to be a wrapper around VerifierInput that is kept
|
||||||
/// private and used for input allocation.
|
/// private and used for input allocation.
|
||||||
struct InputAllocator<T>(T);
|
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>(
|
fn alloc<NR, N, F>(
|
||||||
&mut self,
|
&mut self,
|
||||||
name_fn: N,
|
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
|
// Do nothing; we don't care about the constraint system
|
||||||
// in this context.
|
// 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>> {
|
impl<'a, 'b, E: Engine> PublicConstraintSystem<E> for InputAllocator<&'a mut VerifierInput<'b, E>> {
|
||||||
|
163
src/lib.rs
163
src/lib.rs
@ -190,7 +190,9 @@ pub trait PublicConstraintSystem<E: Engine>: ConstraintSystem<E> {
|
|||||||
where NR: Into<String>, N: FnOnce() -> NR, F: FnOnce() -> Result<E::Fr, Error>;
|
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
|
/// Return the "one" input variable
|
||||||
fn one() -> Variable {
|
fn one() -> Variable {
|
||||||
Variable(Index::Input(0))
|
Variable(Index::Input(0))
|
||||||
@ -214,15 +216,129 @@ pub trait ConstraintSystem<E: Engine> {
|
|||||||
c: LinearCombination<E>
|
c: LinearCombination<E>
|
||||||
);
|
);
|
||||||
|
|
||||||
/// Begin a namespace for the constraint system
|
fn push_namespace<NR, N>(&mut self, _: N)
|
||||||
fn namespace<NR, N, R, F>(
|
where 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>
|
|
||||||
{
|
{
|
||||||
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> {
|
impl<E: Engine> ConstraintSystem<E> for TestConstraintSystem<E> {
|
||||||
|
type Root = Self;
|
||||||
|
|
||||||
fn alloc<NR, N, F>(
|
fn alloc<NR, N, F>(
|
||||||
&mut self,
|
&mut self,
|
||||||
name_fn: N,
|
name_fn: N,
|
||||||
@ -386,24 +504,31 @@ impl<E: Engine> ConstraintSystem<E> for TestConstraintSystem<E> {
|
|||||||
self.constraints.push((a, b, c, this_path));
|
self.constraints.push((a, b, c, this_path));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn namespace<NR, N, R, F>(
|
fn push_namespace<NR, N>(&mut self, name_fn: N)
|
||||||
&mut self,
|
where NR: Into<String>, N: FnOnce() -> NR
|
||||||
name_fn: N,
|
|
||||||
space_fn: F
|
|
||||||
) -> Result<R, Error>
|
|
||||||
where NR: Into<String>, N: FnOnce() -> NR, F: FnOnce(&mut Self) -> Result<R, Error>
|
|
||||||
{
|
{
|
||||||
let name = name_fn().into();
|
let name = name_fn().into();
|
||||||
|
|
||||||
let this_path = compute_path(&self.current_namespace, name.clone());
|
let this_path = compute_path(&self.current_namespace, name.clone());
|
||||||
|
|
||||||
self.set_named_obj(this_path, NamedObject::Namespace);
|
self.set_named_obj(this_path, NamedObject::Namespace);
|
||||||
|
|
||||||
self.current_namespace.push(name);
|
self.current_namespace.push(name);
|
||||||
|
}
|
||||||
|
|
||||||
let r = space_fn(self)?;
|
fn pop_namespace(&mut self)
|
||||||
|
{
|
||||||
self.current_namespace.pop();
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user