mirror of
https://github.com/saymrwulf/pasta_curves-source.git
synced 2026-09-05 20:10:32 +00:00
Add aux wires to ConstraintSystem
This commit is contained in:
parent
0caf1d2087
commit
a257308ba2
3 changed files with 51 additions and 1 deletions
12
src/plonk.rs
12
src/plonk.rs
|
|
@ -262,6 +262,8 @@ fn test_proving() {
|
||||||
let c = meta.advice_wire();
|
let c = meta.advice_wire();
|
||||||
let d = meta.advice_wire();
|
let d = meta.advice_wire();
|
||||||
|
|
||||||
|
let x = meta.aux_wire();
|
||||||
|
|
||||||
let perm = meta.permutation(&[a, b, c]);
|
let perm = meta.permutation(&[a, b, c]);
|
||||||
let perm2 = meta.permutation(&[a, b, c]);
|
let perm2 = meta.permutation(&[a, b, c]);
|
||||||
|
|
||||||
|
|
@ -269,6 +271,7 @@ fn test_proving() {
|
||||||
let sa = meta.fixed_wire();
|
let sa = meta.fixed_wire();
|
||||||
let sb = meta.fixed_wire();
|
let sb = meta.fixed_wire();
|
||||||
let sc = meta.fixed_wire();
|
let sc = meta.fixed_wire();
|
||||||
|
let sx = meta.fixed_wire();
|
||||||
|
|
||||||
meta.create_gate(|meta| {
|
meta.create_gate(|meta| {
|
||||||
let d = meta.query_advice(d, 1);
|
let d = meta.query_advice(d, 1);
|
||||||
|
|
@ -278,12 +281,19 @@ fn test_proving() {
|
||||||
let b = meta.query_advice(b, 0);
|
let b = meta.query_advice(b, 0);
|
||||||
let c = meta.query_advice(c, 0);
|
let c = meta.query_advice(c, 0);
|
||||||
|
|
||||||
|
let x = meta.query_advice(x, 0);
|
||||||
|
|
||||||
let sa = meta.query_fixed(sa, 0);
|
let sa = meta.query_fixed(sa, 0);
|
||||||
let sb = meta.query_fixed(sb, 0);
|
let sb = meta.query_fixed(sb, 0);
|
||||||
let sc = meta.query_fixed(sc, 0);
|
let sc = meta.query_fixed(sc, 0);
|
||||||
let sm = meta.query_fixed(sm, 0);
|
let sm = meta.query_fixed(sm, 0);
|
||||||
|
|
||||||
a.clone() * sa + b.clone() * sb + a * b * sm + (c * sc * (-F::one())) + sf * (d * e)
|
a.clone() * sa
|
||||||
|
+ b.clone() * sb
|
||||||
|
+ a * b * sm
|
||||||
|
+ (c * sc * (-F::one()))
|
||||||
|
+ sf * (d * e)
|
||||||
|
+ (x * sx * (-F::one()))
|
||||||
});
|
});
|
||||||
|
|
||||||
PLONKConfig {
|
PLONKConfig {
|
||||||
|
|
|
||||||
|
|
@ -160,6 +160,7 @@ pub(crate) struct PointIndex(pub usize);
|
||||||
pub struct ConstraintSystem<F> {
|
pub struct ConstraintSystem<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,
|
||||||
|
pub(crate) num_aux_wires: usize,
|
||||||
pub(crate) gates: Vec<Expression<F>>,
|
pub(crate) gates: Vec<Expression<F>>,
|
||||||
pub(crate) advice_queries: Vec<(AdviceWire, Rotation)>,
|
pub(crate) advice_queries: Vec<(AdviceWire, Rotation)>,
|
||||||
pub(crate) aux_queries: Vec<(AuxWire, Rotation)>,
|
pub(crate) aux_queries: Vec<(AuxWire, Rotation)>,
|
||||||
|
|
@ -186,6 +187,7 @@ impl<F: Field> Default for ConstraintSystem<F> {
|
||||||
ConstraintSystem {
|
ConstraintSystem {
|
||||||
num_fixed_wires: 0,
|
num_fixed_wires: 0,
|
||||||
num_advice_wires: 0,
|
num_advice_wires: 0,
|
||||||
|
num_aux_wires: 0,
|
||||||
gates: vec![],
|
gates: vec![],
|
||||||
fixed_queries: Vec::new(),
|
fixed_queries: Vec::new(),
|
||||||
advice_queries: Vec::new(),
|
advice_queries: Vec::new(),
|
||||||
|
|
@ -266,6 +268,32 @@ impl<F: Field> ConstraintSystem<F> {
|
||||||
Expression::Advice(self.query_advice_index(wire, at))
|
Expression::Advice(self.query_advice_index(wire, at))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn query_aux_index(&mut self, wire: AuxWire, at: i32) -> usize {
|
||||||
|
let at = Rotation(at);
|
||||||
|
{
|
||||||
|
let len = self.rotations.len();
|
||||||
|
self.rotations.entry(at).or_insert(PointIndex(len));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return existing query, if it exists
|
||||||
|
for (index, aux_query) in self.aux_queries.iter().enumerate() {
|
||||||
|
if aux_query == &(wire, at) {
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make a new query
|
||||||
|
let index = self.aux_queries.len();
|
||||||
|
self.aux_queries.push((wire, at));
|
||||||
|
|
||||||
|
index
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Query an auxiliary wire at a relative position
|
||||||
|
pub fn query_aux(&mut self, wire: AuxWire, at: i32) -> Expression<F> {
|
||||||
|
Expression::Aux(self.query_aux_index(wire, at))
|
||||||
|
}
|
||||||
|
|
||||||
/// Create a new gate
|
/// Create a new gate
|
||||||
pub fn create_gate(&mut self, f: impl FnOnce(&mut Self) -> Expression<F>) {
|
pub fn create_gate(&mut self, f: impl FnOnce(&mut Self) -> Expression<F>) {
|
||||||
let poly = f(self);
|
let poly = f(self);
|
||||||
|
|
@ -285,4 +313,11 @@ impl<F: Field> ConstraintSystem<F> {
|
||||||
self.num_advice_wires += 1;
|
self.num_advice_wires += 1;
|
||||||
tmp
|
tmp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Allocate a new auxiliary wire
|
||||||
|
pub fn aux_wire(&mut self) -> AuxWire {
|
||||||
|
let tmp = AuxWire(self.num_aux_wires);
|
||||||
|
self.num_aux_wires += 1;
|
||||||
|
tmp
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,11 @@ impl<'a, C: CurveAffine> Proof<C> {
|
||||||
mut msm: MSM<'a, C>,
|
mut msm: MSM<'a, C>,
|
||||||
aux_commitments: Vec<C>,
|
aux_commitments: Vec<C>,
|
||||||
) -> Result<Guard<'a, C>, Error> {
|
) -> Result<Guard<'a, C>, Error> {
|
||||||
|
// Check that aux_commitments matches the expected number of aux_wires
|
||||||
|
if aux_commitments.len() != srs.cs.num_aux_wires {
|
||||||
|
return Err(Error::IncompatibleParams);
|
||||||
|
}
|
||||||
|
|
||||||
// Scale the MSM by a random factor to ensure that if the existing MSM
|
// Scale the MSM by a random factor to ensure that if the existing MSM
|
||||||
// has is_zero() == false then this argument won't be able to interfere
|
// has is_zero() == false then this argument won't be able to interfere
|
||||||
// with it to make it true, with high probability.
|
// with it to make it true, with high probability.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue