diff --git a/src/plonk.rs b/src/plonk.rs index 7de9c61..345d39e 100644 --- a/src/plonk.rs +++ b/src/plonk.rs @@ -101,9 +101,6 @@ fn test_proving() { sm: FixedWire, } - #[derive(Copy, Clone)] - struct Variable(AdviceWire, usize); - trait StandardCS { fn raw_multiply(&mut self, f: F) -> Result<(Variable, Variable, Variable), Error> where @@ -163,9 +160,9 @@ fn test_proving() { self.cs .assign_fixed(self.config.sm, index, || Ok(FF::one()))?; Ok(( - Variable(self.config.a, index), - Variable(self.config.b, index), - Variable(self.config.c, index), + Variable::new(self.config.a, index), + Variable::new(self.config.b, index), + Variable::new(self.config.c, index), )) } fn raw_add(&mut self, f: F) -> Result<(Variable, Variable, Variable), Error> @@ -195,9 +192,9 @@ fn test_proving() { self.cs .assign_fixed(self.config.sm, index, || Ok(FF::zero()))?; Ok(( - Variable(self.config.a, index), - Variable(self.config.b, index), - Variable(self.config.c, index), + Variable::new(self.config.a, index), + Variable::new(self.config.b, index), + Variable::new(self.config.c, index), )) } } @@ -248,7 +245,7 @@ fn test_proving() { for _ in 0..10 { let mut a_squared = None; - let (_, _, _) = cs.raw_multiply(|| { + let (_, _, c0) = cs.raw_multiply(|| { a_squared = self.a.map(|a| a.square()); Ok(( self.a.ok_or(Error::SynthesisError)?, @@ -256,7 +253,7 @@ fn test_proving() { a_squared.ok_or(Error::SynthesisError)?, )) })?; - let (_, _, _) = cs.raw_add(|| { + let (a1, _, _) = cs.raw_add(|| { let fin = a_squared.and_then(|a2| self.a.map(|a| a + a2)); Ok(( self.a.ok_or(Error::SynthesisError)?, @@ -264,6 +261,7 @@ fn test_proving() { fin.ok_or(Error::SynthesisError)?, )) })?; + cs.cs.assign_copy(a1, c0)?; } Ok(()) diff --git a/src/plonk/circuit.rs b/src/plonk/circuit.rs index d1923b6..b190d29 100644 --- a/src/plonk/circuit.rs +++ b/src/plonk/circuit.rs @@ -14,6 +14,17 @@ pub struct FixedWire(pub usize); #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub struct AdviceWire(pub usize); +/// This represents an advice wire at a certain row in the MetaCircuit +#[derive(Copy, Clone, Debug)] +pub struct Variable(pub AdviceWire, pub usize); + +impl Variable { + /// Construct a Variable + pub fn new(wire: AdviceWire, index: usize) -> Variable { + Variable(wire, index) + } +} + /// This trait allows a [`Circuit`] to direct some backend to assign a witness /// for a constraint system. pub trait ConstraintSystem { @@ -33,7 +44,8 @@ pub trait ConstraintSystem { to: impl FnOnce() -> Result, ) -> Result<(), Error>; - // fn copy(&mut self, left: Wire, right: Wire); + /// Assign two advice wires to have the same value + fn assign_copy(&mut self, left: Variable, right: Variable) -> Result<(), Error>; } /// This is a trait that circuits provide implementations for so that the @@ -147,7 +159,6 @@ pub struct PointIndex(pub usize); pub struct MetaCircuit { pub(crate) num_fixed_wires: usize, pub(crate) num_advice_wires: usize, - // permutations: Vec>, pub(crate) gates: Vec>, pub(crate) advice_queries: Vec<(AdviceWire, Rotation)>, pub(crate) fixed_queries: Vec<(FixedWire, Rotation)>, diff --git a/src/plonk/prover.rs b/src/plonk/prover.rs index 70f006c..8cc1e72 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, Circuit, ConstraintSystem, FixedWire, MetaCircuit, Variable}, domain::Rotation, hash_point, Error, Proof, SRS, }; @@ -53,6 +53,12 @@ impl Proof { Ok(()) } + + fn assign_copy(&mut self, _: Variable, _: Variable) -> Result<(), Error> { + // We only care about advice wires here + + Ok(()) + } } let mut meta = MetaCircuit::default(); diff --git a/src/plonk/srs.rs b/src/plonk/srs.rs index 98bdc13..82ca2c9 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, Circuit, ConstraintSystem, FixedWire, MetaCircuit, Variable}, domain::EvaluationDomain, Error, SRS, }; @@ -15,6 +15,7 @@ impl SRS { ) -> Result { struct Assembly { fixed: Vec>, + copy: Vec>, } impl ConstraintSystem for Assembly { @@ -42,6 +43,16 @@ impl SRS { Ok(()) } + + fn assign_copy(&mut self, left: Variable, right: Variable) -> Result<(), Error> { + *self + .copy + .get_mut((left.0).0) + .and_then(|v| v.get_mut(left.1)) + .ok_or(Error::BoundsFailure)? = right; + + Ok(()) + } } let mut meta = MetaCircuit::default(); @@ -49,6 +60,10 @@ impl SRS { let mut assembly: Assembly = Assembly { fixed: vec![vec![C::Scalar::zero(); params.n as usize]; meta.num_fixed_wires], + copy: vec![ + vec![Variable::new(AdviceWire(0), 0); params.n as usize]; + meta.num_advice_wires + ], }; // Synthesize the circuit to obtain SRS