WIP implement copy() on Variables

This commit is contained in:
therealyingtong 2020-08-31 13:58:00 +08:00
parent 4f8570db95
commit 85fd924b15
No known key found for this signature in database
GPG key ID: 179F32A1503D607E
4 changed files with 45 additions and 15 deletions

View file

@ -101,9 +101,6 @@ fn test_proving() {
sm: FixedWire,
}
#[derive(Copy, Clone)]
struct Variable(AdviceWire, usize);
trait StandardCS<FF: Field> {
fn raw_multiply<F>(&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<F>(&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(())

View file

@ -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<F: Field> {
@ -33,7 +44,8 @@ pub trait ConstraintSystem<F: Field> {
to: impl FnOnce() -> Result<F, Error>,
) -> 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<F> {
pub(crate) num_fixed_wires: usize,
pub(crate) num_advice_wires: usize,
// permutations: Vec<Vec<Wire>>,
pub(crate) gates: Vec<Polynomial<F>>,
pub(crate) advice_queries: Vec<(AdviceWire, Rotation)>,
pub(crate) fixed_queries: Vec<(FixedWire, Rotation)>,

View file

@ -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<C: CurveAffine> Proof<C> {
Ok(())
}
fn assign_copy(&mut self, _: Variable, _: Variable) -> Result<(), Error> {
// We only care about advice wires here
Ok(())
}
}
let mut meta = MetaCircuit::default();

View file

@ -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<C: CurveAffine> SRS<C> {
) -> Result<Self, Error> {
struct Assembly<F: Field> {
fixed: Vec<Vec<F>>,
copy: Vec<Vec<Variable>>,
}
impl<F: Field> ConstraintSystem<F> for Assembly<F> {
@ -42,6 +43,16 @@ impl<C: CurveAffine> SRS<C> {
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<C: CurveAffine> SRS<C> {
let mut assembly: Assembly<C::Scalar> = 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