diff --git a/src/plonk.rs b/src/plonk.rs index e62409f..2910b65 100644 --- a/src/plonk.rs +++ b/src/plonk.rs @@ -36,7 +36,7 @@ pub struct SRS { permutations: Vec>>, permutation_polys: Vec>>, permutation_cosets: Vec>>, - meta: MetaCircuit, + cs: ConstraintSystem, } /// This is an object which represents a (Turbo)PLONK proof. @@ -95,7 +95,7 @@ fn test_proving() { use std::marker::PhantomData; const K: u32 = 5; - /// This represents an advice wire at a certain row in the MetaCircuit + /// This represents an advice wire at a certain row in the ConstraintSystem #[derive(Copy, Clone, Debug)] pub struct Variable(AdviceWire, usize); @@ -132,14 +132,14 @@ fn test_proving() { a: Option, } - struct StandardPLONK<'a, F: Field, CS: ConstraintSystem + 'a> { + struct StandardPLONK<'a, F: Field, CS: Assignment + 'a> { cs: &'a mut CS, config: PLONKConfig, current_gate: usize, _marker: PhantomData, } - impl<'a, FF: Field, CS: ConstraintSystem> StandardPLONK<'a, FF, CS> { + impl<'a, FF: Field, CS: Assignment> StandardPLONK<'a, FF, CS> { fn new(cs: &'a mut CS, config: PLONKConfig) -> Self { StandardPLONK { cs, @@ -150,7 +150,7 @@ fn test_proving() { } } - impl<'a, FF: Field, CS: ConstraintSystem> StandardCS for StandardPLONK<'a, FF, CS> { + impl<'a, FF: Field, CS: Assignment> StandardCS for StandardPLONK<'a, FF, CS> { fn raw_multiply(&mut self, f: F) -> Result<(Variable, Variable, Variable), Error> where F: FnOnce() -> Result<(FF, FF, FF), Error>, @@ -251,7 +251,7 @@ fn test_proving() { impl Circuit for MyCircuit { type Config = PLONKConfig; - fn configure(meta: &mut MetaCircuit) -> PLONKConfig { + fn configure(meta: &mut ConstraintSystem) -> PLONKConfig { let e = meta.advice_wire(); let a = meta.advice_wire(); let b = meta.advice_wire(); @@ -300,7 +300,7 @@ fn test_proving() { fn synthesize( &self, - cs: &mut impl ConstraintSystem, + cs: &mut impl Assignment, config: PLONKConfig, ) -> Result<(), Error> { let mut cs = StandardPLONK::new(cs, config); diff --git a/src/plonk/circuit.rs b/src/plonk/circuit.rs index 5537e79..8ec8e3d 100644 --- a/src/plonk/circuit.rs +++ b/src/plonk/circuit.rs @@ -16,7 +16,7 @@ pub struct AdviceWire(pub usize); /// This trait allows a [`Circuit`] to direct some backend to assign a witness /// for a constraint system. -pub trait ConstraintSystem { +pub trait Assignment { /// Assign an advice wire value (witness) fn assign_advice( &mut self, @@ -53,16 +53,12 @@ pub trait Circuit { /// The circuit is given an opportunity to describe the exact gate /// arrangement, wire arrangement, etc. - fn configure(meta: &mut MetaCircuit) -> Self::Config; + fn configure(meta: &mut ConstraintSystem) -> 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, - config: Self::Config, - ) -> Result<(), Error>; + fn synthesize(&self, cs: &mut impl Assignment, config: Self::Config) -> Result<(), Error>; } /// Low-degree expression representing an identity that must hold over the committed wires. @@ -152,7 +148,7 @@ pub(crate) struct PointIndex(pub usize); /// This is a description of the circuit environment, such as the gate, wire and /// permutation arrangements. #[derive(Debug, Clone)] -pub struct MetaCircuit { +pub struct ConstraintSystem { pub(crate) num_fixed_wires: usize, pub(crate) num_advice_wires: usize, pub(crate) gates: Vec>, @@ -172,12 +168,12 @@ pub struct MetaCircuit { pub(crate) permutations: Vec>, } -impl Default for MetaCircuit { - fn default() -> MetaCircuit { +impl Default for ConstraintSystem { + fn default() -> ConstraintSystem { let mut rotations = BTreeMap::new(); rotations.insert(Rotation::default(), PointIndex(0)); - MetaCircuit { + ConstraintSystem { num_fixed_wires: 0, num_advice_wires: 0, gates: vec![], @@ -189,7 +185,7 @@ impl Default for MetaCircuit { } } -impl MetaCircuit { +impl ConstraintSystem { /// Add a permutation argument for some advice wires pub fn permutation(&mut self, wires: &[AdviceWire]) -> usize { let index = self.permutations.len(); diff --git a/src/plonk/prover.rs b/src/plonk/prover.rs index a2d9301..b42ea0d 100644 --- a/src/plonk/prover.rs +++ b/src/plonk/prover.rs @@ -1,5 +1,5 @@ use super::{ - circuit::{AdviceWire, Circuit, ConstraintSystem, FixedWire, MetaCircuit}, + circuit::{AdviceWire, Assignment, Circuit, ConstraintSystem, FixedWire}, hash_point, Error, Proof, SRS, }; use crate::arithmetic::{ @@ -30,7 +30,7 @@ impl Proof { _marker: std::marker::PhantomData, } - impl ConstraintSystem for WitnessCollection { + impl Assignment for WitnessCollection { fn assign_advice( &mut self, wire: AdviceWire, @@ -72,7 +72,7 @@ impl Proof { } let domain = &srs.domain; - let mut meta = MetaCircuit::default(); + let mut meta = ConstraintSystem::default(); let config = ConcreteCircuit::configure(&mut meta); let mut witness = WitnessCollection { @@ -140,7 +140,7 @@ impl Proof { // Iterate over each permutation let mut permutation_modified_advice = vec![]; - for (wires, permuted_values) in srs.meta.permutations.iter().zip(srs.permutations.iter()) { + for (wires, permuted_values) in srs.cs.permutations.iter().zip(srs.permutations.iter()) { // Goal is to compute the products of fractions // // (p_j(\omega^i) + \delta^j \omega^i \beta + \gamma) / @@ -174,7 +174,7 @@ impl Proof { .batch_invert(); for (wires, mut modified_advice) in srs - .meta + .cs .permutations .iter() .zip(permutation_modified_advice.into_iter()) @@ -276,7 +276,7 @@ impl Proof { } // z(X) \prod (p(X) + \beta s_i(X) + \gamma) - z(omega^{-1} X) \prod (p(X) + \delta^i \beta X + \gamma) - for (permutation_index, wires) in srs.meta.permutations.iter().enumerate() { + for (permutation_index, wires) in srs.cs.permutations.iter().enumerate() { h_poly = h_poly * x_2; let mut left = permutation_product_cosets[permutation_index].clone(); @@ -473,7 +473,7 @@ impl Proof { } // Handle permutation arguments, if any exist - if !srs.meta.permutations.is_empty() { + if !srs.cs.permutations.is_empty() { // Open permutation product commitments at x_3 for ((poly, blind), eval) in permutation_product_polys .iter() @@ -493,7 +493,7 @@ impl Proof { accumulate(current_index, poly, Blind::default(), *eval); } - let current_index = (*srs.meta.rotations.get(&Rotation(-1)).unwrap()).0; + let current_index = (*srs.cs.rotations.get(&Rotation(-1)).unwrap()).0; // Open permutation product commitments at \omega^{-1} x_3 for ((poly, blind), eval) in permutation_product_polys .iter() diff --git a/src/plonk/srs.rs b/src/plonk/srs.rs index ce545ef..14a7be3 100644 --- a/src/plonk/srs.rs +++ b/src/plonk/srs.rs @@ -1,5 +1,5 @@ use super::{ - circuit::{AdviceWire, Circuit, ConstraintSystem, FixedWire, MetaCircuit}, + circuit::{AdviceWire, Assignment, Circuit, ConstraintSystem, FixedWire}, Error, SRS, }; use crate::arithmetic::{Curve, CurveAffine, Field}; @@ -23,7 +23,7 @@ impl SRS { _marker: std::marker::PhantomData, } - impl ConstraintSystem for Assembly { + impl Assignment for Assembly { fn assign_advice( &mut self, _: AdviceWire, @@ -100,13 +100,13 @@ impl SRS { } } - let mut meta = MetaCircuit::default(); - let config = ConcreteCircuit::configure(&mut meta); + let mut cs = ConstraintSystem::default(); + let config = ConcreteCircuit::configure(&mut cs); // Get the largest permutation argument length in terms of the number of // advice wires involved. let mut largest_permutation_length = 0; - for permutation in &meta.permutations { + for permutation in &cs.permutations { largest_permutation_length = std::cmp::max(permutation.len(), largest_permutation_length); } @@ -117,7 +117,7 @@ impl SRS { // Account for each gate to ensure our quotient polynomial is the // correct degree and that our extended domain is the right size. - for poly in meta.gates.iter() { + for poly in cs.gates.iter() { degree = std::cmp::max(degree, poly.degree()); } @@ -150,7 +150,7 @@ impl SRS { } let mut assembly: Assembly = Assembly { - fixed: vec![domain.empty_lagrange(); meta.num_fixed_wires], + fixed: vec![domain.empty_lagrange(); cs.num_fixed_wires], mapping: vec![], aux: vec![], sizes: vec![], @@ -159,7 +159,7 @@ impl SRS { // Initialize the copy vector to keep track of copy constraints in all // the permutation arguments. - for permutation in &meta.permutations { + for permutation in &cs.permutations { let mut wires = vec![]; for i in 0..permutation.len() { // Computes [(i, 0), (i, 1), ..., (i, n - 1)] @@ -181,7 +181,7 @@ impl SRS { let mut permutations = vec![]; let mut permutation_polys = vec![]; let mut permutation_cosets = vec![]; - for (permutation_index, permutation) in meta.permutations.iter().enumerate() { + for (permutation_index, permutation) in cs.permutations.iter().enumerate() { let mut commitments = vec![]; let mut inner_permutations = vec![]; let mut polys = vec![]; @@ -225,7 +225,7 @@ impl SRS { .map(|poly| domain.lagrange_to_coeff(poly)) .collect(); - let fixed_cosets = meta + let fixed_cosets = cs .fixed_queries .iter() .map(|&(wire, at)| { @@ -251,7 +251,7 @@ impl SRS { permutations, permutation_polys, permutation_cosets, - meta, + cs, }) } } diff --git a/src/plonk/verifier.rs b/src/plonk/verifier.rs index 5197acd..7af545a 100644 --- a/src/plonk/verifier.rs +++ b/src/plonk/verifier.rs @@ -69,7 +69,7 @@ impl<'a, C: CurveAffine> Proof { // Evaluate the circuit using the custom gates provided let mut h_eval = C::Scalar::zero(); - for poly in srs.meta.gates.iter() { + for poly in srs.cs.gates.iter() { h_eval *= &x_2; let evaluation: C::Scalar = poly.evaluate( @@ -102,7 +102,7 @@ impl<'a, C: CurveAffine> Proof { } // z(X) \prod (p(X) + \beta s_i(X) + \gamma) - z(omega^{-1} X) \prod (p(X) + \delta^i \beta X + \gamma) - for (permutation_index, wires) in srs.meta.permutations.iter().enumerate() { + for (permutation_index, wires) in srs.cs.permutations.iter().enumerate() { h_eval *= &x_2; let mut left = self.permutation_product_evals[permutation_index]; @@ -148,8 +148,8 @@ impl<'a, C: CurveAffine> Proof { // Compress the commitments and expected evaluations at x_3 together // using the challenge x_4 - let mut q_commitments: Vec> = vec![None; srs.meta.rotations.len()]; - let mut q_evals: Vec<_> = vec![C::Scalar::zero(); srs.meta.rotations.len()]; + let mut q_commitments: Vec> = vec![None; srs.cs.rotations.len()]; + let mut q_evals: Vec<_> = vec![C::Scalar::zero(); srs.cs.rotations.len()]; { let mut accumulate = |point_index: usize, new_commitment, eval| { q_commitments[point_index] = q_commitments[point_index] @@ -163,8 +163,8 @@ impl<'a, C: CurveAffine> Proof { q_evals[point_index] += &eval; }; - for (query_index, &(wire, ref at)) in srs.meta.advice_queries.iter().enumerate() { - let point_index = (*srs.meta.rotations.get(at).unwrap()).0; + for (query_index, &(wire, ref at)) in srs.cs.advice_queries.iter().enumerate() { + let point_index = (*srs.cs.rotations.get(at).unwrap()).0; accumulate( point_index, self.advice_commitments[wire.0], @@ -172,8 +172,8 @@ impl<'a, C: CurveAffine> Proof { ); } - for (query_index, &(wire, ref at)) in srs.meta.fixed_queries.iter().enumerate() { - let point_index = (*srs.meta.rotations.get(at).unwrap()).0; + for (query_index, &(wire, ref at)) in srs.cs.fixed_queries.iter().enumerate() { + let point_index = (*srs.cs.rotations.get(at).unwrap()).0; accumulate( point_index, srs.fixed_commitments[wire.0], @@ -181,13 +181,13 @@ impl<'a, C: CurveAffine> Proof { ); } - let current_index = (*srs.meta.rotations.get(&Rotation::default()).unwrap()).0; + let current_index = (*srs.cs.rotations.get(&Rotation::default()).unwrap()).0; for (commitment, eval) in self.h_commitments.iter().zip(self.h_evals.iter()) { accumulate(current_index, *commitment, *eval); } // Handle permutation arguments, if any exist - if !srs.meta.permutations.is_empty() { + if !srs.cs.permutations.is_empty() { // Open permutation product commitments at x_3 for (commitment, eval) in self .permutation_product_commitments @@ -205,7 +205,7 @@ impl<'a, C: CurveAffine> Proof { { accumulate(current_index, *commitment, *eval); } - let current_index = (*srs.meta.rotations.get(&Rotation(-1)).unwrap()).0; + let current_index = (*srs.cs.rotations.get(&Rotation(-1)).unwrap()).0; // Open permutation product commitments at \omega^{-1} x_3 for (commitment, eval) in self .permutation_product_commitments @@ -240,7 +240,7 @@ impl<'a, C: CurveAffine> Proof { // We can compute the expected f_eval at x_6 using the q_evals provided // by the prover and from x_5 let mut f_eval = C::Scalar::zero(); - for (&row, point_index) in srs.meta.rotations.iter() { + for (&row, point_index) in srs.cs.rotations.iter() { let mut eval = self.q_evals[point_index.0]; let point = srs.domain.rotate_omega(x_3, row); @@ -257,7 +257,7 @@ impl<'a, C: CurveAffine> Proof { // Compute the final commitment that has to be opened let mut f_commitment: C::Projective = self.f_commitment.to_projective(); - for (_, &point_index) in srs.meta.rotations.iter() { + for (_, &point_index) in srs.cs.rotations.iter() { f_commitment *= x_7; f_commitment = f_commitment + &q_commitments[point_index.0].as_ref().unwrap(); f_eval *= &x_7;