mirror of
https://github.com/saymrwulf/pasta_curves-source.git
synced 2026-09-07 20:30:34 +00:00
Update PLONK test_proving() example
This commit is contained in:
parent
0bdcbb6c67
commit
9482202a98
1 changed files with 114 additions and 20 deletions
134
src/plonk.rs
134
src/plonk.rs
|
|
@ -92,8 +92,8 @@ fn hash_point<C: CurveAffine, H: Hasher<C::Base>>(
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_proving() {
|
fn test_proving() {
|
||||||
use crate::arithmetic::{EqAffine, Field, Fp, Fq};
|
use crate::arithmetic::{Curve, EqAffine, Field, Fp, Fq};
|
||||||
use crate::poly::commitment::Params;
|
use crate::poly::commitment::{Blind, Params};
|
||||||
use crate::transcript::DummyHash;
|
use crate::transcript::DummyHash;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
const K: u32 = 5;
|
const K: u32 = 5;
|
||||||
|
|
@ -102,6 +102,14 @@ fn test_proving() {
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
pub struct Variable(AdviceWire, usize);
|
pub struct Variable(AdviceWire, usize);
|
||||||
|
|
||||||
|
/// This represents an auxiliary wire at a certain row in the ConstraintSystem
|
||||||
|
#[derive(Copy, Clone, Debug)]
|
||||||
|
pub struct AuxVariable(AuxWire, usize);
|
||||||
|
|
||||||
|
/// This represents a wire at a certain row in the ConstraintSystem
|
||||||
|
#[derive(Copy, Clone, Debug)]
|
||||||
|
pub struct PermVariable(Wire, usize);
|
||||||
|
|
||||||
// Initialize the polynomial commitment parameters
|
// Initialize the polynomial commitment parameters
|
||||||
let params: Params<EqAffine> = Params::new::<DummyHash<Fq>>(K);
|
let params: Params<EqAffine> = Params::new::<DummyHash<Fq>>(K);
|
||||||
|
|
||||||
|
|
@ -112,10 +120,13 @@ fn test_proving() {
|
||||||
d: AdviceWire,
|
d: AdviceWire,
|
||||||
e: AdviceWire,
|
e: AdviceWire,
|
||||||
|
|
||||||
|
x: AuxWire,
|
||||||
|
|
||||||
sa: FixedWire,
|
sa: FixedWire,
|
||||||
sb: FixedWire,
|
sb: FixedWire,
|
||||||
sc: FixedWire,
|
sc: FixedWire,
|
||||||
sm: FixedWire,
|
sm: FixedWire,
|
||||||
|
sx: FixedWire,
|
||||||
|
|
||||||
perm: usize,
|
perm: usize,
|
||||||
perm2: usize,
|
perm2: usize,
|
||||||
|
|
@ -128,11 +139,15 @@ fn test_proving() {
|
||||||
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>
|
||||||
where
|
where
|
||||||
F: FnOnce() -> Result<(FF, FF, FF), Error>;
|
F: FnOnce() -> Result<(FF, FF, FF), Error>;
|
||||||
fn copy(&mut self, a: Variable, b: Variable) -> Result<(), Error>;
|
fn copy(&mut self, a: PermVariable, b: PermVariable) -> Result<(), Error>;
|
||||||
|
fn raw_aux<F>(&mut self, f: F) -> Result<(Variable, AuxVariable), Error>
|
||||||
|
where
|
||||||
|
F: FnOnce() -> Result<(FF, FF), Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct MyCircuit<F: Field> {
|
struct MyCircuit<F: Field> {
|
||||||
a: Option<F>,
|
a: Option<F>,
|
||||||
|
x: Option<F>,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct StandardPLONK<'a, F: Field, CS: Assignment<F> + 'a> {
|
struct StandardPLONK<'a, F: Field, CS: Assignment<F> + 'a> {
|
||||||
|
|
@ -230,17 +245,31 @@ fn test_proving() {
|
||||||
Variable(self.config.c, index),
|
Variable(self.config.c, index),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
fn copy(&mut self, left: Variable, right: Variable) -> Result<(), Error> {
|
fn copy(&mut self, left: PermVariable, right: PermVariable) -> Result<(), Error> {
|
||||||
let left_wire = match left.0 {
|
let left_wire = match left.0 {
|
||||||
x if x == self.config.a => 0,
|
Wire::Advice(wire) => match wire {
|
||||||
x if x == self.config.b => 1,
|
x if x == self.config.a => 0,
|
||||||
x if x == self.config.c => 2,
|
x if x == self.config.b => 1,
|
||||||
|
x if x == self.config.c => 2,
|
||||||
|
_ => unreachable!(),
|
||||||
|
},
|
||||||
|
Wire::Aux(wire) => match wire {
|
||||||
|
x if x == self.config.x => 3,
|
||||||
|
_ => unreachable!(),
|
||||||
|
},
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
let right_wire = match right.0 {
|
let right_wire = match right.0 {
|
||||||
x if x == self.config.a => 0,
|
Wire::Advice(wire) => match wire {
|
||||||
x if x == self.config.b => 1,
|
x if x == self.config.a => 0,
|
||||||
x if x == self.config.c => 2,
|
x if x == self.config.b => 1,
|
||||||
|
x if x == self.config.c => 2,
|
||||||
|
_ => unreachable!(),
|
||||||
|
},
|
||||||
|
Wire::Aux(wire) => match wire {
|
||||||
|
x if x == self.config.x => 3,
|
||||||
|
_ => unreachable!(),
|
||||||
|
},
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -249,6 +278,24 @@ fn test_proving() {
|
||||||
self.cs
|
self.cs
|
||||||
.copy(self.config.perm2, left_wire, left.1, right_wire, right.1)
|
.copy(self.config.perm2, left_wire, left.1, right_wire, right.1)
|
||||||
}
|
}
|
||||||
|
fn raw_aux<F>(&mut self, f: F) -> Result<(Variable, AuxVariable), Error>
|
||||||
|
where
|
||||||
|
F: FnOnce() -> Result<(FF, FF), Error>,
|
||||||
|
{
|
||||||
|
let index = self.current_gate;
|
||||||
|
self.current_gate += 1;
|
||||||
|
let mut value = None;
|
||||||
|
self.cs.assign_advice(self.config.a, index, || {
|
||||||
|
value = Some(f()?);
|
||||||
|
Ok(value.ok_or(Error::SynthesisError)?.0)
|
||||||
|
})?;
|
||||||
|
self.cs
|
||||||
|
.assign_fixed(self.config.sx, index, || Ok(FF::zero()))?;
|
||||||
|
Ok((
|
||||||
|
Variable(self.config.a, index),
|
||||||
|
AuxVariable(self.config.x, index),
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Field> Circuit<F> for MyCircuit<F> {
|
impl<F: Field> Circuit<F> for MyCircuit<F> {
|
||||||
|
|
@ -264,8 +311,18 @@ fn test_proving() {
|
||||||
|
|
||||||
let x = meta.aux_wire();
|
let x = meta.aux_wire();
|
||||||
|
|
||||||
let perm = meta.permutation(&[a, b, c]);
|
let perm = meta.permutation(&[
|
||||||
let perm2 = meta.permutation(&[a, b, c]);
|
Wire::Advice(a),
|
||||||
|
Wire::Advice(b),
|
||||||
|
Wire::Advice(c),
|
||||||
|
Wire::Aux(x),
|
||||||
|
]);
|
||||||
|
let perm2 = meta.permutation(&[
|
||||||
|
Wire::Advice(a),
|
||||||
|
Wire::Advice(b),
|
||||||
|
Wire::Advice(c),
|
||||||
|
Wire::Aux(x),
|
||||||
|
]);
|
||||||
|
|
||||||
let sm = meta.fixed_wire();
|
let sm = meta.fixed_wire();
|
||||||
let sa = meta.fixed_wire();
|
let sa = meta.fixed_wire();
|
||||||
|
|
@ -281,12 +338,13 @@ 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 x = meta.query_aux(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);
|
||||||
|
let sx = meta.query_fixed(sx, 0);
|
||||||
|
|
||||||
a.clone() * sa
|
a.clone() * sa
|
||||||
+ b.clone() * sb
|
+ b.clone() * sb
|
||||||
|
|
@ -302,10 +360,12 @@ fn test_proving() {
|
||||||
c,
|
c,
|
||||||
d,
|
d,
|
||||||
e,
|
e,
|
||||||
|
x,
|
||||||
sa,
|
sa,
|
||||||
sb,
|
sb,
|
||||||
sc,
|
sc,
|
||||||
sm,
|
sm,
|
||||||
|
sx,
|
||||||
perm,
|
perm,
|
||||||
perm2,
|
perm2,
|
||||||
}
|
}
|
||||||
|
|
@ -336,9 +396,25 @@ fn test_proving() {
|
||||||
fin.ok_or(Error::SynthesisError)?,
|
fin.ok_or(Error::SynthesisError)?,
|
||||||
))
|
))
|
||||||
})?;
|
})?;
|
||||||
cs.copy(a0, a1)?;
|
cs.copy(
|
||||||
cs.copy(b1, c0)?;
|
PermVariable(Wire::Advice(a0.0), a0.1),
|
||||||
|
PermVariable(Wire::Advice(a1.0), a1.1),
|
||||||
|
)?;
|
||||||
|
cs.copy(
|
||||||
|
PermVariable(Wire::Advice(b1.0), b1.1),
|
||||||
|
PermVariable(Wire::Advice(c0.0), c0.1),
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
|
let (_, x) = cs.raw_aux(|| {
|
||||||
|
Ok((
|
||||||
|
self.x.ok_or(Error::SynthesisError)?,
|
||||||
|
self.x.ok_or(Error::SynthesisError)?,
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
cs.copy(
|
||||||
|
PermVariable(Wire::Aux(x.0), x.1),
|
||||||
|
PermVariable(Wire::Aux(x.0), x.1),
|
||||||
|
)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
@ -346,21 +422,39 @@ fn test_proving() {
|
||||||
|
|
||||||
let circuit: MyCircuit<Fp> = MyCircuit {
|
let circuit: MyCircuit<Fp> = MyCircuit {
|
||||||
a: Some(Fp::random()),
|
a: Some(Fp::random()),
|
||||||
|
|
||||||
|
// TODO: use meaningful value from recursion
|
||||||
|
x: Some(Fp::random()),
|
||||||
};
|
};
|
||||||
|
|
||||||
let empty_circuit: MyCircuit<Fp> = MyCircuit { a: None };
|
let empty_circuit: MyCircuit<Fp> = MyCircuit { a: None, x: None };
|
||||||
|
|
||||||
// Initialize the SRS
|
// Initialize the SRS
|
||||||
let srs = SRS::generate(¶ms, &empty_circuit).expect("SRS generation should not fail");
|
let srs = SRS::generate(¶ms, &empty_circuit).expect("SRS generation should not fail");
|
||||||
|
|
||||||
|
// TODO: use meaningful value from recursion
|
||||||
|
let aux_lagrange_polys = vec![srs.domain.empty_lagrange(); srs.cs.num_aux_wires];
|
||||||
|
|
||||||
|
// TODO: use meaningful value from recursion
|
||||||
|
let mut aux_commitments: Vec<EqAffine> = vec![];
|
||||||
|
for poly in &aux_lagrange_polys {
|
||||||
|
let commitment = params.commit_lagrange(poly, Blind::default());
|
||||||
|
aux_commitments.push(commitment.to_affine());
|
||||||
|
}
|
||||||
|
|
||||||
for _ in 0..100 {
|
for _ in 0..100 {
|
||||||
// Create a proof
|
// Create a proof
|
||||||
let proof = Proof::create::<DummyHash<Fq>, DummyHash<Fp>, _>(¶ms, &srs, &circuit)
|
let proof = Proof::create::<DummyHash<Fq>, DummyHash<Fp>, _>(
|
||||||
.expect("proof generation should not fail");
|
¶ms,
|
||||||
|
&srs,
|
||||||
|
&circuit,
|
||||||
|
aux_lagrange_polys.clone(),
|
||||||
|
)
|
||||||
|
.expect("proof generation should not fail");
|
||||||
|
|
||||||
let msm = params.empty_msm();
|
let msm = params.empty_msm();
|
||||||
let guard = proof
|
let guard = proof
|
||||||
.verify::<DummyHash<Fq>, DummyHash<Fp>>(¶ms, &srs, msm)
|
.verify::<DummyHash<Fq>, DummyHash<Fp>>(¶ms, &srs, msm, aux_commitments.clone())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
{
|
{
|
||||||
let msm = guard.clone().use_challenges();
|
let msm = guard.clone().use_challenges();
|
||||||
|
|
@ -374,7 +468,7 @@ fn test_proving() {
|
||||||
let msm = guard.clone().use_challenges();
|
let msm = guard.clone().use_challenges();
|
||||||
assert!(msm.clone().is_zero());
|
assert!(msm.clone().is_zero());
|
||||||
let guard = proof
|
let guard = proof
|
||||||
.verify::<DummyHash<Fq>, DummyHash<Fp>>(¶ms, &srs, msm)
|
.verify::<DummyHash<Fq>, DummyHash<Fp>>(¶ms, &srs, msm, aux_commitments.clone())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
{
|
{
|
||||||
let msm = guard.clone().use_challenges();
|
let msm = guard.clone().use_challenges();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue