|
|
@ -1,3 +1,6 @@ |
|
|
|
extern crate ff; |
|
|
|
extern crate group; |
|
|
|
#[cfg(feature = "pairing")] |
|
|
|
extern crate pairing; |
|
|
|
extern crate rand; |
|
|
|
extern crate num_cpus; |
|
|
@ -10,9 +13,10 @@ extern crate byteorder; |
|
|
|
pub mod multicore; |
|
|
|
mod multiexp; |
|
|
|
pub mod domain; |
|
|
|
#[cfg(feature = "groth16")] |
|
|
|
pub mod groth16; |
|
|
|
|
|
|
|
use pairing::{Engine, Field}; |
|
|
|
use ff::{Field, ScalarEngine}; |
|
|
|
|
|
|
|
use std::ops::{Add, Sub}; |
|
|
|
use std::fmt; |
|
|
@ -24,7 +28,7 @@ use std::marker::PhantomData; |
|
|
|
/// rank-1 quadratic constraint systems. The `Circuit` trait represents a
|
|
|
|
/// circuit that can be synthesized. The `synthesize` method is called during
|
|
|
|
/// CRS generation and during proving.
|
|
|
|
pub trait Circuit<E: Engine> { |
|
|
|
pub trait Circuit<E: ScalarEngine> { |
|
|
|
/// Synthesize the circuit into a rank-1 quadratic constraint system
|
|
|
|
fn synthesize<CS: ConstraintSystem<E>>( |
|
|
|
self, |
|
|
@ -61,21 +65,21 @@ pub enum Index { |
|
|
|
/// This represents a linear combination of some variables, with coefficients
|
|
|
|
/// in the scalar field of a pairing-friendly elliptic curve group.
|
|
|
|
#[derive(Clone)] |
|
|
|
pub struct LinearCombination<E: Engine>(Vec<(Variable, E::Fr)>); |
|
|
|
pub struct LinearCombination<E: ScalarEngine>(Vec<(Variable, E::Fr)>); |
|
|
|
|
|
|
|
impl<E: Engine> AsRef<[(Variable, E::Fr)]> for LinearCombination<E> { |
|
|
|
impl<E: ScalarEngine> AsRef<[(Variable, E::Fr)]> for LinearCombination<E> { |
|
|
|
fn as_ref(&self) -> &[(Variable, E::Fr)] { |
|
|
|
&self.0 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl<E: Engine> LinearCombination<E> { |
|
|
|
impl<E: ScalarEngine> LinearCombination<E> { |
|
|
|
pub fn zero() -> LinearCombination<E> { |
|
|
|
LinearCombination(vec![]) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl<E: Engine> Add<(E::Fr, Variable)> for LinearCombination<E> { |
|
|
|
impl<E: ScalarEngine> Add<(E::Fr, Variable)> for LinearCombination<E> { |
|
|
|
type Output = LinearCombination<E>; |
|
|
|
|
|
|
|
fn add(mut self, (coeff, var): (E::Fr, Variable)) -> LinearCombination<E> { |
|
|
@ -85,7 +89,7 @@ impl<E: Engine> Add<(E::Fr, Variable)> for LinearCombination<E> { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl<E: Engine> Sub<(E::Fr, Variable)> for LinearCombination<E> { |
|
|
|
impl<E: ScalarEngine> Sub<(E::Fr, Variable)> for LinearCombination<E> { |
|
|
|
type Output = LinearCombination<E>; |
|
|
|
|
|
|
|
fn sub(self, (mut coeff, var): (E::Fr, Variable)) -> LinearCombination<E> { |
|
|
@ -95,7 +99,7 @@ impl<E: Engine> Sub<(E::Fr, Variable)> for LinearCombination<E> { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl<E: Engine> Add<Variable> for LinearCombination<E> { |
|
|
|
impl<E: ScalarEngine> Add<Variable> for LinearCombination<E> { |
|
|
|
type Output = LinearCombination<E>; |
|
|
|
|
|
|
|
fn add(self, other: Variable) -> LinearCombination<E> { |
|
|
@ -103,7 +107,7 @@ impl<E: Engine> Add<Variable> for LinearCombination<E> { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl<E: Engine> Sub<Variable> for LinearCombination<E> { |
|
|
|
impl<E: ScalarEngine> Sub<Variable> for LinearCombination<E> { |
|
|
|
type Output = LinearCombination<E>; |
|
|
|
|
|
|
|
fn sub(self, other: Variable) -> LinearCombination<E> { |
|
|
@ -111,7 +115,7 @@ impl<E: Engine> Sub<Variable> for LinearCombination<E> { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl<'a, E: Engine> Add<&'a LinearCombination<E>> for LinearCombination<E> { |
|
|
|
impl<'a, E: ScalarEngine> Add<&'a LinearCombination<E>> for LinearCombination<E> { |
|
|
|
type Output = LinearCombination<E>; |
|
|
|
|
|
|
|
fn add(mut self, other: &'a LinearCombination<E>) -> LinearCombination<E> { |
|
|
@ -123,7 +127,7 @@ impl<'a, E: Engine> Add<&'a LinearCombination<E>> for LinearCombination<E> { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl<'a, E: Engine> Sub<&'a LinearCombination<E>> for LinearCombination<E> { |
|
|
|
impl<'a, E: ScalarEngine> Sub<&'a LinearCombination<E>> for LinearCombination<E> { |
|
|
|
type Output = LinearCombination<E>; |
|
|
|
|
|
|
|
fn sub(mut self, other: &'a LinearCombination<E>) -> LinearCombination<E> { |
|
|
@ -135,7 +139,7 @@ impl<'a, E: Engine> Sub<&'a LinearCombination<E>> for LinearCombination<E> { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl<'a, E: Engine> Add<(E::Fr, &'a LinearCombination<E>)> for LinearCombination<E> { |
|
|
|
impl<'a, E: ScalarEngine> Add<(E::Fr, &'a LinearCombination<E>)> for LinearCombination<E> { |
|
|
|
type Output = LinearCombination<E>; |
|
|
|
|
|
|
|
fn add(mut self, (coeff, other): (E::Fr, &'a LinearCombination<E>)) -> LinearCombination<E> { |
|
|
@ -149,7 +153,7 @@ impl<'a, E: Engine> Add<(E::Fr, &'a LinearCombination<E>)> for LinearCombination |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl<'a, E: Engine> Sub<(E::Fr, &'a LinearCombination<E>)> for LinearCombination<E> { |
|
|
|
impl<'a, E: ScalarEngine> Sub<(E::Fr, &'a LinearCombination<E>)> for LinearCombination<E> { |
|
|
|
type Output = LinearCombination<E>; |
|
|
|
|
|
|
|
fn sub(mut self, (coeff, other): (E::Fr, &'a LinearCombination<E>)) -> LinearCombination<E> { |
|
|
@ -219,7 +223,7 @@ impl fmt::Display for SynthesisError { |
|
|
|
|
|
|
|
/// Represents a constraint system which can have new variables
|
|
|
|
/// allocated and constrains between them formed.
|
|
|
|
pub trait ConstraintSystem<E: Engine>: Sized { |
|
|
|
pub trait ConstraintSystem<E: ScalarEngine>: Sized { |
|
|
|
/// Represents the type of the "root" of this constraint system
|
|
|
|
/// so that nested namespaces can minimize indirection.
|
|
|
|
type Root: ConstraintSystem<E>; |
|
|
@ -291,9 +295,9 @@ pub trait ConstraintSystem<E: Engine>: Sized { |
|
|
|
|
|
|
|
/// This is a "namespaced" constraint system which borrows a constraint system (pushing
|
|
|
|
/// a namespace context) and, when dropped, pops out of the namespace context.
|
|
|
|
pub struct Namespace<'a, E: Engine, CS: ConstraintSystem<E> + 'a>(&'a mut CS, PhantomData<E>); |
|
|
|
pub struct Namespace<'a, E: ScalarEngine, CS: ConstraintSystem<E> + 'a>(&'a mut CS, PhantomData<E>); |
|
|
|
|
|
|
|
impl<'cs, E: Engine, CS: ConstraintSystem<E>> ConstraintSystem<E> for Namespace<'cs, E, CS> { |
|
|
|
impl<'cs, E: ScalarEngine, CS: ConstraintSystem<E>> ConstraintSystem<E> for Namespace<'cs, E, CS> { |
|
|
|
type Root = CS::Root; |
|
|
|
|
|
|
|
fn one() -> Variable { |
|
|
@ -356,7 +360,7 @@ impl<'cs, E: Engine, CS: ConstraintSystem<E>> ConstraintSystem<E> for Namespace< |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl<'a, E: Engine, CS: ConstraintSystem<E>> Drop for Namespace<'a, E, CS> { |
|
|
|
impl<'a, E: ScalarEngine, CS: ConstraintSystem<E>> Drop for Namespace<'a, E, CS> { |
|
|
|
fn drop(&mut self) { |
|
|
|
self.get_root().pop_namespace() |
|
|
|
} |
|
|
@ -364,7 +368,7 @@ 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 { |
|
|
|
impl<'cs, E: ScalarEngine, CS: ConstraintSystem<E>> ConstraintSystem<E> for &'cs mut CS { |
|
|
|
type Root = CS::Root; |
|
|
|
|
|
|
|
fn one() -> Variable { |
|
|
|