From 9dfc6ac3798fc56fa090f81b9fbee0bec9be74e0 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Sat, 22 Aug 2020 15:09:47 -0600 Subject: [PATCH] Add first pieces of the API. --- src/plonk.rs | 13 ++++++- src/plonk/circuit.rs | 82 ++++++++++++++++++++++++++++++++++++++++++-- src/plonk/prover.rs | 8 +++-- src/plonk/srs.rs | 8 +++-- 4 files changed, 103 insertions(+), 8 deletions(-) diff --git a/src/plonk.rs b/src/plonk.rs index 66f3e32..97eba66 100644 --- a/src/plonk.rs +++ b/src/plonk.rs @@ -103,12 +103,23 @@ fn test_proving() { // Initialize the polynomial commitment parameters let params: Params = Params::new::>(K); + struct MyConfig {} struct MyCircuit { a: Option, } impl Circuit for MyCircuit { - fn synthesize(&self, cs: &mut impl ConstraintSystem) -> Result<(), Error> { + type Config = MyConfig; + + fn configure(meta: &mut MetaCircuit) -> MyConfig { + MyConfig {} + } + + fn synthesize( + &self, + cs: &mut impl ConstraintSystem, + config: MyConfig, + ) -> Result<(), Error> { for _ in 0..10 { let (_, _, _, _) = cs.multiply(|| { let a = self.a.ok_or(Error::SynthesisError)?; diff --git a/src/plonk/circuit.rs b/src/plonk/circuit.rs index c0168d2..48da226 100644 --- a/src/plonk/circuit.rs +++ b/src/plonk/circuit.rs @@ -1,10 +1,12 @@ -use super::Error; +use core::cmp::max; +use core::ops::{Add, Mul}; +use super::Error; use crate::arithmetic::Field; /// This represents a PLONK wire, which could be a fixed (selector) wire or an /// advice wire. -#[derive(Debug)] +#[derive(Clone, Debug)] pub enum Wire { /// A wires A(usize), @@ -59,8 +61,82 @@ pub trait ConstraintSystem { /// backend prover can ask the circuit to synthesize using some given /// [`ConstraintSystem`] implementation. pub trait Circuit { + /// This is a configuration object that stores things like wires. + type Config; + + /// The circuit is given an opportunity to describe the exact gate + /// arrangement, wire arrangement, etc. + fn configure(meta: &mut MetaCircuit) -> Self::Config; + /// Given the provided `cs`, synthesize the circuit. The concrete type of /// the caller will be different depending on the context, and they may or /// may not expect to have a witness present. - fn synthesize(&self, cs: &mut impl ConstraintSystem) -> Result<(), Error>; + fn synthesize( + &self, + cs: &mut impl ConstraintSystem, + config: Self::Config, + ) -> Result<(), Error>; +} + +/// Low-degree polynomial representing an identity that must hold over the committed wires. +#[derive(Clone, Debug)] +pub enum Polynomial { + /// This is a wire queried at a certain relative location + Wire(Wire, isize), + /// This is the sum of two polynomials + Sum(Box>, Box>), + /// This is the product of two polynomials + Product(Box>, Box>), + /// This is a scaled polynomial + Scaled(Box>, F), +} + +impl Polynomial { + fn degree(&self) -> usize { + match self { + Polynomial::Wire(_, _) => 1, + Polynomial::Sum(ref a, ref b) => max(a.degree(), b.degree()), + Polynomial::Product(ref a, ref b) => a.degree() + b.degree(), + Polynomial::Scaled(ref poly, _) => poly.degree(), + } + } +} + +impl Add for Polynomial { + type Output = Polynomial; + fn add(self, rhs: Polynomial) -> Polynomial { + Polynomial::Sum(Box::new(self), Box::new(rhs)) + } +} + +impl Mul for Polynomial { + type Output = Polynomial; + fn mul(self, rhs: Polynomial) -> Polynomial { + Polynomial::Product(Box::new(self), Box::new(rhs)) + } +} + +impl Mul for Polynomial { + type Output = Polynomial; + fn mul(self, rhs: F) -> Polynomial { + Polynomial::Scaled(Box::new(self), rhs) + } +} + +/// This is a description of the circuit environment, such as the gate, wire and +/// permutation arrangements. +#[derive(Debug, Clone)] +pub struct MetaCircuit { + // num_fixed_wires: usize, +// num_advice_wires: usize, +// permutations: Vec>, +// gates: Vec, +// queries: HashSet<(Wire, usize)>, +// num_queries: usize, +} + +impl Default for MetaCircuit { + fn default() -> MetaCircuit { + MetaCircuit {} + } } diff --git a/src/plonk/prover.rs b/src/plonk/prover.rs index d06119d..b0b66bf 100644 --- a/src/plonk/prover.rs +++ b/src/plonk/prover.rs @@ -1,5 +1,5 @@ use super::{ - circuit::{Circuit, ConstraintSystem, Wire}, + circuit::{Circuit, ConstraintSystem, MetaCircuit, Wire}, hash_point, Error, Proof, SRS, }; use crate::arithmetic::{ @@ -78,8 +78,12 @@ impl Proof { sm: vec![], }; + let mut meta = MetaCircuit::default(); + + let config = ConcreteCircuit::configure(&mut meta); + // Synthesize the circuit to obtain the witness and other information. - circuit.synthesize(&mut witness)?; + circuit.synthesize(&mut witness, config)?; // Create a transcript for obtaining Fiat-Shamir challenges. let mut transcript = HBase::init(C::Base::one()); diff --git a/src/plonk/srs.rs b/src/plonk/srs.rs index 0a9acdb..7d0487d 100644 --- a/src/plonk/srs.rs +++ b/src/plonk/srs.rs @@ -1,5 +1,5 @@ use super::{ - circuit::{Circuit, ConstraintSystem, Wire}, + circuit::{Circuit, ConstraintSystem, MetaCircuit, Wire}, domain::EvaluationDomain, Error, GATE_DEGREE, SRS, }; @@ -54,8 +54,12 @@ impl SRS { sm: vec![], }; + let mut meta = MetaCircuit::default(); + + let config = ConcreteCircuit::configure(&mut meta); + // Synthesize the circuit to obtain SRS - circuit.synthesize(&mut assembly)?; + circuit.synthesize(&mut assembly, config)?; assembly.sa.resize(params.n as usize, C::Scalar::zero()); assembly.sb.resize(params.n as usize, C::Scalar::zero());