mirror of
https://github.com/saymrwulf/pasta_curves-source.git
synced 2026-09-06 20:20:34 +00:00
WIP implement copy() on Variables
This commit is contained in:
parent
4f8570db95
commit
85fd924b15
4 changed files with 45 additions and 15 deletions
20
src/plonk.rs
20
src/plonk.rs
|
|
@ -101,9 +101,6 @@ fn test_proving() {
|
||||||
sm: FixedWire,
|
sm: FixedWire,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
|
||||||
struct Variable(AdviceWire, usize);
|
|
||||||
|
|
||||||
trait StandardCS<FF: Field> {
|
trait StandardCS<FF: Field> {
|
||||||
fn raw_multiply<F>(&mut self, f: F) -> Result<(Variable, Variable, Variable), Error>
|
fn raw_multiply<F>(&mut self, f: F) -> Result<(Variable, Variable, Variable), Error>
|
||||||
where
|
where
|
||||||
|
|
@ -163,9 +160,9 @@ fn test_proving() {
|
||||||
self.cs
|
self.cs
|
||||||
.assign_fixed(self.config.sm, index, || Ok(FF::one()))?;
|
.assign_fixed(self.config.sm, index, || Ok(FF::one()))?;
|
||||||
Ok((
|
Ok((
|
||||||
Variable(self.config.a, index),
|
Variable::new(self.config.a, index),
|
||||||
Variable(self.config.b, index),
|
Variable::new(self.config.b, index),
|
||||||
Variable(self.config.c, index),
|
Variable::new(self.config.c, index),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
fn raw_add<F>(&mut self, f: F) -> Result<(Variable, Variable, Variable), Error>
|
fn raw_add<F>(&mut self, f: F) -> Result<(Variable, Variable, Variable), Error>
|
||||||
|
|
@ -195,9 +192,9 @@ fn test_proving() {
|
||||||
self.cs
|
self.cs
|
||||||
.assign_fixed(self.config.sm, index, || Ok(FF::zero()))?;
|
.assign_fixed(self.config.sm, index, || Ok(FF::zero()))?;
|
||||||
Ok((
|
Ok((
|
||||||
Variable(self.config.a, index),
|
Variable::new(self.config.a, index),
|
||||||
Variable(self.config.b, index),
|
Variable::new(self.config.b, index),
|
||||||
Variable(self.config.c, index),
|
Variable::new(self.config.c, index),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -248,7 +245,7 @@ fn test_proving() {
|
||||||
|
|
||||||
for _ in 0..10 {
|
for _ in 0..10 {
|
||||||
let mut a_squared = None;
|
let mut a_squared = None;
|
||||||
let (_, _, _) = cs.raw_multiply(|| {
|
let (_, _, c0) = cs.raw_multiply(|| {
|
||||||
a_squared = self.a.map(|a| a.square());
|
a_squared = self.a.map(|a| a.square());
|
||||||
Ok((
|
Ok((
|
||||||
self.a.ok_or(Error::SynthesisError)?,
|
self.a.ok_or(Error::SynthesisError)?,
|
||||||
|
|
@ -256,7 +253,7 @@ fn test_proving() {
|
||||||
a_squared.ok_or(Error::SynthesisError)?,
|
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));
|
let fin = a_squared.and_then(|a2| self.a.map(|a| a + a2));
|
||||||
Ok((
|
Ok((
|
||||||
self.a.ok_or(Error::SynthesisError)?,
|
self.a.ok_or(Error::SynthesisError)?,
|
||||||
|
|
@ -264,6 +261,7 @@ fn test_proving() {
|
||||||
fin.ok_or(Error::SynthesisError)?,
|
fin.ok_or(Error::SynthesisError)?,
|
||||||
))
|
))
|
||||||
})?;
|
})?;
|
||||||
|
cs.cs.assign_copy(a1, c0)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,17 @@ pub struct FixedWire(pub usize);
|
||||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
||||||
pub struct AdviceWire(pub usize);
|
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
|
/// 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> {
|
||||||
|
|
@ -33,7 +44,8 @@ pub trait ConstraintSystem<F: Field> {
|
||||||
to: impl FnOnce() -> Result<F, Error>,
|
to: impl FnOnce() -> Result<F, Error>,
|
||||||
) -> Result<(), 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
|
/// 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 struct MetaCircuit<F> {
|
||||||
pub(crate) num_fixed_wires: usize,
|
pub(crate) num_fixed_wires: usize,
|
||||||
pub(crate) num_advice_wires: usize,
|
pub(crate) num_advice_wires: usize,
|
||||||
// permutations: Vec<Vec<Wire>>,
|
|
||||||
pub(crate) gates: Vec<Polynomial<F>>,
|
pub(crate) gates: Vec<Polynomial<F>>,
|
||||||
pub(crate) advice_queries: Vec<(AdviceWire, Rotation)>,
|
pub(crate) advice_queries: Vec<(AdviceWire, Rotation)>,
|
||||||
pub(crate) fixed_queries: Vec<(FixedWire, Rotation)>,
|
pub(crate) fixed_queries: Vec<(FixedWire, Rotation)>,
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use super::{
|
use super::{
|
||||||
circuit::{AdviceWire, Circuit, ConstraintSystem, FixedWire, MetaCircuit},
|
circuit::{AdviceWire, Circuit, ConstraintSystem, FixedWire, MetaCircuit, Variable},
|
||||||
domain::Rotation,
|
domain::Rotation,
|
||||||
hash_point, Error, Proof, SRS,
|
hash_point, Error, Proof, SRS,
|
||||||
};
|
};
|
||||||
|
|
@ -53,6 +53,12 @@ impl<C: CurveAffine> Proof<C> {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn assign_copy(&mut self, _: Variable, _: Variable) -> Result<(), Error> {
|
||||||
|
// We only care about advice wires here
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut meta = MetaCircuit::default();
|
let mut meta = MetaCircuit::default();
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use super::{
|
use super::{
|
||||||
circuit::{AdviceWire, Circuit, ConstraintSystem, FixedWire, MetaCircuit},
|
circuit::{AdviceWire, Circuit, ConstraintSystem, FixedWire, MetaCircuit, Variable},
|
||||||
domain::EvaluationDomain,
|
domain::EvaluationDomain,
|
||||||
Error, SRS,
|
Error, SRS,
|
||||||
};
|
};
|
||||||
|
|
@ -15,6 +15,7 @@ impl<C: CurveAffine> SRS<C> {
|
||||||
) -> Result<Self, Error> {
|
) -> Result<Self, Error> {
|
||||||
struct Assembly<F: Field> {
|
struct Assembly<F: Field> {
|
||||||
fixed: Vec<Vec<F>>,
|
fixed: Vec<Vec<F>>,
|
||||||
|
copy: Vec<Vec<Variable>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field> ConstraintSystem<F> for Assembly<F> {
|
impl<F: Field> ConstraintSystem<F> for Assembly<F> {
|
||||||
|
|
@ -42,6 +43,16 @@ impl<C: CurveAffine> SRS<C> {
|
||||||
|
|
||||||
Ok(())
|
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();
|
let mut meta = MetaCircuit::default();
|
||||||
|
|
@ -49,6 +60,10 @@ impl<C: CurveAffine> SRS<C> {
|
||||||
|
|
||||||
let mut assembly: Assembly<C::Scalar> = Assembly {
|
let mut assembly: Assembly<C::Scalar> = Assembly {
|
||||||
fixed: vec![vec![C::Scalar::zero(); params.n as usize]; meta.num_fixed_wires],
|
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
|
// Synthesize the circuit to obtain SRS
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue