2020-10-13 14:06:21 -07:00
|
|
|
#[macro_use]
|
|
|
|
extern crate criterion;
|
|
|
|
|
|
|
|
extern crate halo2;
|
2020-11-12 16:08:08 -08:00
|
|
|
use halo2::arithmetic::FieldExt;
|
2021-06-21 11:10:59 -07:00
|
|
|
use halo2::circuit::{Cell, Layouter, SimpleFloorPlanner};
|
2021-01-27 02:30:39 -08:00
|
|
|
use halo2::pasta::{EqAffine, Fp};
|
2020-10-13 14:06:21 -07:00
|
|
|
use halo2::plonk::*;
|
2020-12-23 08:45:16 -08:00
|
|
|
use halo2::poly::{commitment::Params, Rotation};
|
2021-04-30 18:28:50 -07:00
|
|
|
use halo2::transcript::{Blake2bRead, Blake2bWrite, Challenge255};
|
2020-10-13 14:06:21 -07:00
|
|
|
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
|
|
|
use criterion::Criterion;
|
|
|
|
|
|
|
|
fn bench_with_k(name: &str, k: u32, c: &mut Criterion) {
|
|
|
|
/// This represents an advice column at a certain row in the ConstraintSystem
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct Variable(Column<Advice>, usize);
|
|
|
|
|
|
|
|
// Initialize the polynomial commitment parameters
|
2020-12-23 15:09:17 -08:00
|
|
|
let params: Params<EqAffine> = Params::new(k);
|
2020-10-13 14:06:21 -07:00
|
|
|
|
2021-02-18 23:24:21 -08:00
|
|
|
#[derive(Clone)]
|
2021-06-23 05:33:04 -07:00
|
|
|
struct PlonkConfig {
|
2020-10-13 14:06:21 -07:00
|
|
|
a: Column<Advice>,
|
|
|
|
b: Column<Advice>,
|
|
|
|
c: Column<Advice>,
|
|
|
|
|
|
|
|
sa: Column<Fixed>,
|
|
|
|
sb: Column<Fixed>,
|
|
|
|
sc: Column<Fixed>,
|
|
|
|
sm: Column<Fixed>,
|
|
|
|
}
|
|
|
|
|
2021-06-23 05:33:04 -07:00
|
|
|
trait StandardCs<FF: FieldExt> {
|
2021-06-21 11:51:32 -07:00
|
|
|
fn raw_multiply<F>(
|
|
|
|
&self,
|
|
|
|
layouter: &mut impl Layouter<FF>,
|
|
|
|
f: F,
|
|
|
|
) -> Result<(Cell, Cell, Cell), Error>
|
2020-10-13 14:06:21 -07:00
|
|
|
where
|
2021-06-21 11:51:32 -07:00
|
|
|
F: FnMut() -> Result<(FF, FF, FF), Error>;
|
|
|
|
fn raw_add<F>(
|
|
|
|
&self,
|
|
|
|
layouter: &mut impl Layouter<FF>,
|
|
|
|
f: F,
|
|
|
|
) -> Result<(Cell, Cell, Cell), Error>
|
2020-10-13 14:06:21 -07:00
|
|
|
where
|
2021-06-21 11:51:32 -07:00
|
|
|
F: FnMut() -> Result<(FF, FF, FF), Error>;
|
|
|
|
fn copy(&self, layouter: &mut impl Layouter<FF>, a: Cell, b: Cell) -> Result<(), Error>;
|
2020-10-13 14:06:21 -07:00
|
|
|
}
|
|
|
|
|
2021-01-20 05:48:51 -08:00
|
|
|
#[derive(Clone)]
|
2020-11-12 16:08:08 -08:00
|
|
|
struct MyCircuit<F: FieldExt> {
|
2020-10-13 14:06:21 -07:00
|
|
|
a: Option<F>,
|
|
|
|
k: u32,
|
|
|
|
}
|
|
|
|
|
2021-06-23 05:33:04 -07:00
|
|
|
struct StandardPlonk<F: FieldExt> {
|
|
|
|
config: PlonkConfig,
|
2020-10-13 14:06:21 -07:00
|
|
|
_marker: PhantomData<F>,
|
|
|
|
}
|
|
|
|
|
2021-06-23 05:33:04 -07:00
|
|
|
impl<FF: FieldExt> StandardPlonk<FF> {
|
|
|
|
fn new(config: PlonkConfig) -> Self {
|
|
|
|
StandardPlonk {
|
2020-10-13 14:06:21 -07:00
|
|
|
config,
|
|
|
|
_marker: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-23 05:33:04 -07:00
|
|
|
impl<FF: FieldExt> StandardCs<FF> for StandardPlonk<FF> {
|
2021-06-21 11:51:32 -07:00
|
|
|
fn raw_multiply<F>(
|
|
|
|
&self,
|
|
|
|
layouter: &mut impl Layouter<FF>,
|
|
|
|
mut f: F,
|
|
|
|
) -> Result<(Cell, Cell, Cell), Error>
|
2020-10-13 14:06:21 -07:00
|
|
|
where
|
2021-06-21 11:51:32 -07:00
|
|
|
F: FnMut() -> Result<(FF, FF, FF), Error>,
|
2020-10-13 14:06:21 -07:00
|
|
|
{
|
2021-06-21 11:51:32 -07:00
|
|
|
layouter.assign_region(
|
|
|
|
|| "raw_multiply",
|
|
|
|
|mut region| {
|
|
|
|
let mut value = None;
|
|
|
|
let lhs = region.assign_advice(
|
|
|
|
|| "lhs",
|
|
|
|
self.config.a,
|
|
|
|
0,
|
|
|
|
|| {
|
|
|
|
value = Some(f()?);
|
2021-05-26 15:55:40 -07:00
|
|
|
Ok(value.ok_or(Error::Synthesis)?.0)
|
2021-06-21 11:51:32 -07:00
|
|
|
},
|
|
|
|
)?;
|
|
|
|
let rhs = region.assign_advice(
|
|
|
|
|| "rhs",
|
|
|
|
self.config.b,
|
|
|
|
0,
|
2021-05-26 15:55:40 -07:00
|
|
|
|| Ok(value.ok_or(Error::Synthesis)?.1),
|
2021-06-21 11:51:32 -07:00
|
|
|
)?;
|
|
|
|
let out = region.assign_advice(
|
|
|
|
|| "out",
|
|
|
|
self.config.c,
|
|
|
|
0,
|
2021-05-26 15:55:40 -07:00
|
|
|
|| Ok(value.ok_or(Error::Synthesis)?.2),
|
2021-06-21 11:51:32 -07:00
|
|
|
)?;
|
|
|
|
|
|
|
|
region.assign_fixed(|| "a", self.config.sa, 0, || Ok(FF::zero()))?;
|
|
|
|
region.assign_fixed(|| "b", self.config.sb, 0, || Ok(FF::zero()))?;
|
|
|
|
region.assign_fixed(|| "c", self.config.sc, 0, || Ok(FF::one()))?;
|
|
|
|
region.assign_fixed(|| "a * b", self.config.sm, 0, || Ok(FF::one()))?;
|
2021-11-27 07:08:01 -08:00
|
|
|
Ok((lhs.cell(), rhs.cell(), out.cell()))
|
2021-01-22 08:57:38 -08:00
|
|
|
},
|
2021-06-21 11:51:32 -07:00
|
|
|
)
|
2020-10-13 14:06:21 -07:00
|
|
|
}
|
2021-06-21 11:51:32 -07:00
|
|
|
fn raw_add<F>(
|
|
|
|
&self,
|
|
|
|
layouter: &mut impl Layouter<FF>,
|
|
|
|
mut f: F,
|
|
|
|
) -> Result<(Cell, Cell, Cell), Error>
|
2020-10-13 14:06:21 -07:00
|
|
|
where
|
2021-06-21 11:51:32 -07:00
|
|
|
F: FnMut() -> Result<(FF, FF, FF), Error>,
|
2020-10-13 14:06:21 -07:00
|
|
|
{
|
2021-06-21 11:51:32 -07:00
|
|
|
layouter.assign_region(
|
|
|
|
|| "raw_add",
|
|
|
|
|mut region| {
|
|
|
|
let mut value = None;
|
|
|
|
let lhs = region.assign_advice(
|
|
|
|
|| "lhs",
|
|
|
|
self.config.a,
|
|
|
|
0,
|
|
|
|
|| {
|
|
|
|
value = Some(f()?);
|
2021-05-26 15:55:40 -07:00
|
|
|
Ok(value.ok_or(Error::Synthesis)?.0)
|
2021-06-21 11:51:32 -07:00
|
|
|
},
|
|
|
|
)?;
|
|
|
|
let rhs = region.assign_advice(
|
|
|
|
|| "rhs",
|
|
|
|
self.config.b,
|
|
|
|
0,
|
2021-05-26 15:55:40 -07:00
|
|
|
|| Ok(value.ok_or(Error::Synthesis)?.1),
|
2021-06-21 11:51:32 -07:00
|
|
|
)?;
|
|
|
|
let out = region.assign_advice(
|
|
|
|
|| "out",
|
|
|
|
self.config.c,
|
|
|
|
0,
|
2021-05-26 15:55:40 -07:00
|
|
|
|| Ok(value.ok_or(Error::Synthesis)?.2),
|
2021-06-21 11:51:32 -07:00
|
|
|
)?;
|
|
|
|
|
|
|
|
region.assign_fixed(|| "a", self.config.sa, 0, || Ok(FF::one()))?;
|
|
|
|
region.assign_fixed(|| "b", self.config.sb, 0, || Ok(FF::one()))?;
|
|
|
|
region.assign_fixed(|| "c", self.config.sc, 0, || Ok(FF::one()))?;
|
|
|
|
region.assign_fixed(|| "a * b", self.config.sm, 0, || Ok(FF::zero()))?;
|
2021-11-27 07:08:01 -08:00
|
|
|
Ok((lhs.cell(), rhs.cell(), out.cell()))
|
2021-01-22 08:57:38 -08:00
|
|
|
},
|
2021-06-21 11:51:32 -07:00
|
|
|
)
|
2020-10-13 14:06:21 -07:00
|
|
|
}
|
2021-06-21 11:51:32 -07:00
|
|
|
fn copy(
|
|
|
|
&self,
|
|
|
|
layouter: &mut impl Layouter<FF>,
|
|
|
|
left: Cell,
|
|
|
|
right: Cell,
|
|
|
|
) -> Result<(), Error> {
|
2021-07-08 11:47:51 -07:00
|
|
|
layouter.assign_region(|| "copy", |mut region| region.constrain_equal(left, right))
|
2020-10-13 14:06:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-12 16:08:08 -08:00
|
|
|
impl<F: FieldExt> Circuit<F> for MyCircuit<F> {
|
2021-06-23 05:33:04 -07:00
|
|
|
type Config = PlonkConfig;
|
2021-06-21 11:10:59 -07:00
|
|
|
type FloorPlanner = SimpleFloorPlanner;
|
|
|
|
|
|
|
|
fn without_witnesses(&self) -> Self {
|
|
|
|
Self { a: None, k: self.k }
|
|
|
|
}
|
2020-10-13 14:06:21 -07:00
|
|
|
|
2021-06-23 05:33:04 -07:00
|
|
|
fn configure(meta: &mut ConstraintSystem<F>) -> PlonkConfig {
|
2021-07-12 14:28:16 -07:00
|
|
|
meta.set_minimum_degree(5);
|
|
|
|
|
2020-10-13 14:06:21 -07:00
|
|
|
let a = meta.advice_column();
|
|
|
|
let b = meta.advice_column();
|
|
|
|
let c = meta.advice_column();
|
|
|
|
|
2021-07-08 11:47:51 -07:00
|
|
|
meta.enable_equality(a.into());
|
|
|
|
meta.enable_equality(b.into());
|
|
|
|
meta.enable_equality(c.into());
|
2020-10-13 14:06:21 -07:00
|
|
|
|
|
|
|
let sm = meta.fixed_column();
|
|
|
|
let sa = meta.fixed_column();
|
|
|
|
let sb = meta.fixed_column();
|
|
|
|
let sc = meta.fixed_column();
|
|
|
|
|
2021-01-22 11:46:06 -08:00
|
|
|
meta.create_gate("Combined add-mult", |meta| {
|
2020-12-23 08:45:16 -08:00
|
|
|
let a = meta.query_advice(a, Rotation::cur());
|
|
|
|
let b = meta.query_advice(b, Rotation::cur());
|
|
|
|
let c = meta.query_advice(c, Rotation::cur());
|
|
|
|
|
|
|
|
let sa = meta.query_fixed(sa, Rotation::cur());
|
|
|
|
let sb = meta.query_fixed(sb, Rotation::cur());
|
|
|
|
let sc = meta.query_fixed(sc, Rotation::cur());
|
|
|
|
let sm = meta.query_fixed(sm, Rotation::cur());
|
2020-10-13 14:06:21 -07:00
|
|
|
|
2021-07-25 13:32:11 -07:00
|
|
|
vec![a.clone() * sa + b.clone() * sb + a * b * sm - (c * sc)]
|
2020-10-13 14:06:21 -07:00
|
|
|
});
|
|
|
|
|
2021-06-23 05:33:04 -07:00
|
|
|
PlonkConfig {
|
2020-10-13 14:06:21 -07:00
|
|
|
a,
|
|
|
|
b,
|
|
|
|
c,
|
|
|
|
sa,
|
|
|
|
sb,
|
|
|
|
sc,
|
|
|
|
sm,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn synthesize(
|
|
|
|
&self,
|
2021-06-23 05:33:04 -07:00
|
|
|
config: PlonkConfig,
|
2021-06-21 11:10:59 -07:00
|
|
|
mut layouter: impl Layouter<F>,
|
2020-10-13 14:06:21 -07:00
|
|
|
) -> Result<(), Error> {
|
2021-06-23 05:33:04 -07:00
|
|
|
let cs = StandardPlonk::new(config);
|
2020-10-13 14:06:21 -07:00
|
|
|
|
2021-07-13 08:15:30 -07:00
|
|
|
for _ in 0..((1 << (self.k - 1)) - 3) {
|
2020-10-13 14:06:21 -07:00
|
|
|
let mut a_squared = None;
|
2021-06-21 11:51:32 -07:00
|
|
|
let (a0, _, c0) = cs.raw_multiply(&mut layouter, || {
|
2020-10-13 14:06:21 -07:00
|
|
|
a_squared = self.a.map(|a| a.square());
|
|
|
|
Ok((
|
2021-05-26 15:55:40 -07:00
|
|
|
self.a.ok_or(Error::Synthesis)?,
|
|
|
|
self.a.ok_or(Error::Synthesis)?,
|
|
|
|
a_squared.ok_or(Error::Synthesis)?,
|
2020-10-13 14:06:21 -07:00
|
|
|
))
|
|
|
|
})?;
|
2021-06-21 11:51:32 -07:00
|
|
|
let (a1, b1, _) = cs.raw_add(&mut layouter, || {
|
2020-10-13 14:06:21 -07:00
|
|
|
let fin = a_squared.and_then(|a2| self.a.map(|a| a + a2));
|
|
|
|
Ok((
|
2021-05-26 15:55:40 -07:00
|
|
|
self.a.ok_or(Error::Synthesis)?,
|
|
|
|
a_squared.ok_or(Error::Synthesis)?,
|
|
|
|
fin.ok_or(Error::Synthesis)?,
|
2020-10-13 14:06:21 -07:00
|
|
|
))
|
|
|
|
})?;
|
2021-06-21 11:51:32 -07:00
|
|
|
cs.copy(&mut layouter, a0, a1)?;
|
|
|
|
cs.copy(&mut layouter, b1, c0)?;
|
2020-10-13 14:06:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let empty_circuit: MyCircuit<Fp> = MyCircuit { a: None, k };
|
|
|
|
|
|
|
|
// Initialize the proving key
|
2021-01-13 09:23:01 -08:00
|
|
|
let vk = keygen_vk(¶ms, &empty_circuit).expect("keygen_vk should not fail");
|
|
|
|
let pk = keygen_pk(¶ms, vk, &empty_circuit).expect("keygen_pk should not fail");
|
2020-10-13 14:06:21 -07:00
|
|
|
|
|
|
|
let prover_name = name.to_string() + "-prover";
|
|
|
|
let verifier_name = name.to_string() + "-verifier";
|
|
|
|
|
|
|
|
c.bench_function(&prover_name, |b| {
|
|
|
|
b.iter(|| {
|
|
|
|
let circuit: MyCircuit<Fp> = MyCircuit {
|
2020-11-12 16:08:08 -08:00
|
|
|
a: Some(Fp::rand()),
|
2020-10-13 14:06:21 -07:00
|
|
|
k,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create a proof
|
2021-05-24 09:55:34 -07:00
|
|
|
let mut transcript = Blake2bWrite::<_, _, Challenge255<_>>::init(vec![]);
|
2021-02-18 23:46:32 -08:00
|
|
|
create_proof(¶ms, &pk, &[circuit], &[&[]], &mut transcript)
|
2020-11-23 07:35:50 -08:00
|
|
|
.expect("proof generation should not fail")
|
2020-10-13 14:06:21 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
let circuit: MyCircuit<Fp> = MyCircuit {
|
2020-11-12 16:08:08 -08:00
|
|
|
a: Some(Fp::rand()),
|
2020-10-13 14:06:21 -07:00
|
|
|
k,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create a proof
|
2021-05-24 09:55:34 -07:00
|
|
|
let mut transcript = Blake2bWrite::<_, _, Challenge255<_>>::init(vec![]);
|
2021-02-18 23:46:32 -08:00
|
|
|
create_proof(¶ms, &pk, &[circuit], &[&[]], &mut transcript)
|
2020-10-13 14:06:21 -07:00
|
|
|
.expect("proof generation should not fail");
|
2020-12-23 12:03:31 -08:00
|
|
|
let proof = transcript.finalize();
|
2020-10-13 14:06:21 -07:00
|
|
|
|
|
|
|
c.bench_function(&verifier_name, |b| {
|
|
|
|
b.iter(|| {
|
|
|
|
let msm = params.empty_msm();
|
2021-05-24 09:55:34 -07:00
|
|
|
let mut transcript = Blake2bRead::<_, _, Challenge255<_>>::init(&proof[..]);
|
2021-02-18 23:46:32 -08:00
|
|
|
let guard = verify_proof(¶ms, pk.get_vk(), msm, &[&[]], &mut transcript).unwrap();
|
2020-10-13 14:06:21 -07:00
|
|
|
let msm = guard.clone().use_challenges();
|
|
|
|
assert!(msm.eval());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn criterion_benchmark(c: &mut Criterion) {
|
|
|
|
bench_with_k("plonk-k=8", 8, c);
|
|
|
|
bench_with_k("plonk-k=9", 9, c);
|
|
|
|
bench_with_k("plonk-k=10", 10, c);
|
|
|
|
bench_with_k("plonk-k=11", 11, c);
|
|
|
|
bench_with_k("plonk-k=12", 12, c);
|
|
|
|
bench_with_k("plonk-k=13", 13, c);
|
|
|
|
bench_with_k("plonk-k=14", 14, c);
|
|
|
|
bench_with_k("plonk-k=15", 15, c);
|
|
|
|
bench_with_k("plonk-k=16", 16, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, criterion_benchmark);
|
|
|
|
criterion_main!(benches);
|