Introduce Variable type

This commit is contained in:
Sean Bowe 2020-08-22 15:15:39 -06:00
parent 9dfc6ac379
commit c16141be9a
No known key found for this signature in database
GPG key ID: 95684257D8F8B031
3 changed files with 28 additions and 24 deletions

View file

@ -9,15 +9,19 @@ use crate::arithmetic::Field;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Wire { pub enum Wire {
/// A wires /// A wires
A(usize), A,
/// B wires /// B wires
B(usize), B,
/// C wires /// C wires
C(usize), C,
/// D wires /// D wires
D(usize), D,
} }
/// Represents a pointer to a value in the constraint system.
#[derive(Clone, Debug)]
pub struct Variable(pub(crate) Wire, pub(crate) usize);
/// This trait allows a [`Circuit`] to direct some backend to assign a witness /// This trait allows a [`Circuit`] to direct some backend to assign a witness
/// for a constraint system. /// for a constraint system.
pub trait ConstraintSystem<F: Field> { pub trait ConstraintSystem<F: Field> {
@ -30,13 +34,13 @@ pub trait ConstraintSystem<F: Field> {
sd: F, sd: F,
sm: F, sm: F,
f: impl Fn() -> Result<(F, F, F, F), Error>, f: impl Fn() -> Result<(F, F, F, F), Error>,
) -> Result<(Wire, Wire, Wire, Wire), Error>; ) -> Result<(Variable, Variable, Variable, Variable), Error>;
/// a * b - c = 0 /// a * b - c = 0
fn multiply( fn multiply(
&mut self, &mut self,
f: impl Fn() -> Result<(F, F, F), Error>, f: impl Fn() -> Result<(F, F, F), Error>,
) -> Result<(Wire, Wire, Wire, Wire), Error> { ) -> Result<(Variable, Variable, Variable, Variable), Error> {
self.create_gate(F::zero(), F::zero(), F::one(), F::zero(), F::one(), || { self.create_gate(F::zero(), F::zero(), F::one(), F::zero(), F::one(), || {
let (a, b, c) = f()?; let (a, b, c) = f()?;
Ok((a, b, c, F::zero())) Ok((a, b, c, F::zero()))
@ -47,7 +51,7 @@ pub trait ConstraintSystem<F: Field> {
fn add( fn add(
&mut self, &mut self,
f: impl Fn() -> Result<(F, F, F), Error>, f: impl Fn() -> Result<(F, F, F), Error>,
) -> Result<(Wire, Wire, Wire, Wire), Error> { ) -> Result<(Variable, Variable, Variable, Variable), Error> {
self.create_gate(F::one(), F::one(), F::one(), F::zero(), F::zero(), || { self.create_gate(F::one(), F::one(), F::one(), F::zero(), F::zero(), || {
let (a, b, c) = f()?; let (a, b, c) = f()?;
Ok((a, b, c, F::zero())) Ok((a, b, c, F::zero()))
@ -128,11 +132,11 @@ impl<F> Mul<F> for Polynomial<F> {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct MetaCircuit { pub struct MetaCircuit {
// num_fixed_wires: usize, // num_fixed_wires: usize,
// num_advice_wires: usize, // num_advice_wires: usize,
// permutations: Vec<Vec<Wire>>, // permutations: Vec<Vec<Wire>>,
// gates: Vec<Polynomial>, // gates: Vec<Polynomial>,
// queries: HashSet<(Wire, usize)>, // queries: HashSet<(Wire, usize)>,
// num_queries: usize, // num_queries: usize,
} }
impl Default for MetaCircuit { impl Default for MetaCircuit {

View file

@ -1,5 +1,5 @@
use super::{ use super::{
circuit::{Circuit, ConstraintSystem, MetaCircuit, Wire}, circuit::{Circuit, ConstraintSystem, MetaCircuit, Wire, Variable},
hash_point, Error, Proof, SRS, hash_point, Error, Proof, SRS,
}; };
use crate::arithmetic::{ use crate::arithmetic::{
@ -42,13 +42,13 @@ impl<C: CurveAffine> Proof<C> {
sd: F, sd: F,
sm: F, sm: F,
f: impl Fn() -> Result<(F, F, F, F), Error>, f: impl Fn() -> Result<(F, F, F, F), Error>,
) -> Result<(Wire, Wire, Wire, Wire), Error> { ) -> Result<(Variable, Variable, Variable, Variable), Error> {
let (a, b, c, d) = f()?; let (a, b, c, d) = f()?;
let tmp = Ok(( let tmp = Ok((
Wire::A(self.a.len()), Variable(Wire::A, self.a.len()),
Wire::B(self.a.len()), Variable(Wire::B, self.a.len()),
Wire::C(self.a.len()), Variable(Wire::C, self.a.len()),
Wire::D(self.a.len()), Variable(Wire::D, self.a.len()),
)); ));
self.a.push(a); self.a.push(a);
self.b.push(b); self.b.push(b);

View file

@ -1,5 +1,5 @@
use super::{ use super::{
circuit::{Circuit, ConstraintSystem, MetaCircuit, Wire}, circuit::{Circuit, ConstraintSystem, MetaCircuit, Wire, Variable},
domain::EvaluationDomain, domain::EvaluationDomain,
Error, GATE_DEGREE, SRS, Error, GATE_DEGREE, SRS,
}; };
@ -30,12 +30,12 @@ impl<C: CurveAffine> SRS<C> {
sd: F, sd: F,
sm: F, sm: F,
_: impl Fn() -> Result<(F, F, F, F), Error>, _: impl Fn() -> Result<(F, F, F, F), Error>,
) -> Result<(Wire, Wire, Wire, Wire), Error> { ) -> Result<(Variable, Variable, Variable, Variable), Error> {
let tmp = Ok(( let tmp = Ok((
Wire::A(self.sa.len()), Variable(Wire::A, self.sa.len()),
Wire::B(self.sa.len()), Variable(Wire::B, self.sa.len()),
Wire::C(self.sa.len()), Variable(Wire::C, self.sa.len()),
Wire::D(self.sa.len()), Variable(Wire::D, self.sa.len()),
)); ));
self.sa.push(sa); self.sa.push(sa);
self.sb.push(sb); self.sb.push(sb);