Introduce Wire enum for use in permutations

This commit is contained in:
therealyingtong 2020-09-18 01:33:31 +08:00 committed by Sean Bowe
parent a257308ba2
commit 0bdcbb6c67
No known key found for this signature in database
GPG key ID: 95684257D8F8B031
2 changed files with 77 additions and 22 deletions

View file

@ -18,6 +18,17 @@ pub struct AdviceWire(pub usize);
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct AuxWire(pub usize);
/// An enum over all wire types, to be used in permutations
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum Wire {
/// Fixed wire
Fixed(FixedWire),
/// Advice wire
Advice(AdviceWire),
/// Auxiliary wire
Aux(AuxWire),
}
/// This trait allows a [`Circuit`] to direct some backend to assign a witness
/// for a constraint system.
pub trait Assignment<F: Field> {
@ -176,7 +187,7 @@ pub struct ConstraintSystem<F> {
// enforced between advice wire values in A, B and C, and another
// permutation between wires (B, C, D) which allows the same with D instead
// of A.
pub(crate) permutations: Vec<Vec<(AdviceWire, usize)>>,
pub(crate) permutations: Vec<Vec<(Wire, usize)>>,
}
impl<F: Field> Default for ConstraintSystem<F> {
@ -200,7 +211,7 @@ impl<F: Field> Default for ConstraintSystem<F> {
impl<F: Field> ConstraintSystem<F> {
/// Add a permutation argument for some advice wires
pub fn permutation(&mut self, wires: &[AdviceWire]) -> usize {
pub fn permutation(&mut self, wires: &[Wire]) -> usize {
let index = self.permutations.len();
if index == 0 {
let at = Rotation(-1);
@ -209,7 +220,11 @@ impl<F: Field> ConstraintSystem<F> {
}
let wires = wires
.iter()
.map(|&wire| (wire, self.query_advice_index(wire, 0)))
.map(|&wire| match wire {
Wire::Advice(wire) => (Wire::Advice(wire), self.query_advice_index(wire, 0)),
Wire::Aux(wire) => (Wire::Aux(wire), self.query_aux_index(wire, 0)),
Wire::Fixed(wire) => (Wire::Fixed(wire), self.query_fixed_index(wire, 0)),
})
.collect();
self.permutations.push(wires);

View file

@ -1,5 +1,5 @@
use super::{
circuit::{AdviceWire, Assignment, Circuit, ConstraintSystem, FixedWire},
circuit::{AdviceWire, Assignment, Circuit, ConstraintSystem, FixedWire, Wire},
hash_point, Error, Proof, SRS,
};
use crate::arithmetic::{
@ -182,15 +182,34 @@ impl<C: CurveAffine> Proof<C> {
// Iterate over each wire of the permutation
for (&(wire, _), permuted_wire_values) in wires.iter().zip(permuted_values.iter()) {
parallelize(&mut modified_advice, |modified_advice, start| {
for ((modified_advice, advice_value), permuted_advice_value) in modified_advice
.iter_mut()
.zip(witness.advice[wire.0][start..].iter())
.zip(permuted_wire_values[start..].iter())
{
*modified_advice *= &(x_0 * permuted_advice_value + &x_1 + advice_value);
match wire {
Wire::Advice(wire) => {
parallelize(&mut modified_advice, |modified_advice, start| {
for ((modified_advice, advice_value), permuted_advice_value) in
modified_advice
.iter_mut()
.zip(witness.advice[wire.0][start..].iter())
.zip(permuted_wire_values[start..].iter())
{
*modified_advice *=
&(x_0 * permuted_advice_value + &x_1 + advice_value);
}
});
}
});
Wire::Aux(wire) => {
parallelize(&mut modified_advice, |modified_aux, start| {
for ((modified_aux, aux_value), permuted_aux_value) in modified_aux
.iter_mut()
.zip(aux_lagrange_polys[wire.0][start..].iter())
.zip(permuted_wire_values[start..].iter())
{
*modified_aux *= &(x_0 * permuted_aux_value + &x_1 + aux_value);
}
});
}
// TODO: implement for fixed wires
_ => unreachable!(),
}
}
permutation_modified_advice.push(modified_advice);
@ -214,17 +233,38 @@ impl<C: CurveAffine> Proof<C> {
let mut deltaomega = C::Scalar::one();
for &(wire, _) in wires.iter() {
let omega = domain.get_omega();
parallelize(&mut modified_advice, |modified_advice, start| {
let mut deltaomega = deltaomega * &omega.pow_vartime(&[start as u64, 0, 0, 0]);
for (modified_advice, advice_value) in modified_advice
.iter_mut()
.zip(witness.advice[wire.0][start..].iter())
{
// Multiply by p_j(\omega^i) + \delta^j \omega^i \beta
*modified_advice *= &(deltaomega * &x_0 + &x_1 + advice_value);
deltaomega *= &omega;
match wire {
Wire::Advice(wire) => {
parallelize(&mut modified_advice, |modified_advice, start| {
let mut deltaomega =
deltaomega * &omega.pow_vartime(&[start as u64, 0, 0, 0]);
for (modified_advice, advice_value) in modified_advice
.iter_mut()
.zip(witness.advice[wire.0][start..].iter())
{
// Multiply by p_j(\omega^i) + \delta^j \omega^i \beta
*modified_advice *= &(deltaomega * &x_0 + &x_1 + advice_value);
deltaomega *= &omega;
}
});
}
});
Wire::Aux(wire) => {
parallelize(&mut modified_advice, |modified_advice, start| {
let mut deltaomega =
deltaomega * &omega.pow_vartime(&[start as u64, 0, 0, 0]);
for (modified_advice, advice_value) in modified_advice
.iter_mut()
.zip(aux_lagrange_polys[wire.0][start..].iter())
{
// Multiply by p_j(\omega^i) + \delta^j \omega^i \beta
*modified_advice *= &(deltaomega * &x_0 + &x_1 + advice_value);
deltaomega *= &omega;
}
});
}
// TODO: implement for fixed wires
_ => unreachable!(),
}
deltaomega *= &C::Scalar::DELTA;
}