Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pcs benchmark #26

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions plonkish_backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,8 @@ sanity-check = []
name = "zero_check"
harness = false
required-features = ["benchmark"]

[[bench]]
name = "pcs"
harness = false
required-features = ["benchmark"]
127 changes: 127 additions & 0 deletions plonkish_backend/benches/pcs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
use criterion::{
black_box, criterion_group, criterion_main, measurement::Measurement, BenchmarkGroup,
BenchmarkId, Criterion,
};
use halo2_curves::bn256::{Bn256, Fr, G1Affine};
use plonkish_backend::{
pcs::{
multilinear::{
Gemini, MultilinearBrakedown, MultilinearHyrax, MultilinearIpa, MultilinearKzg,
Zeromorph,
},
univariate::UnivariateKzg,
Point, PolynomialCommitmentScheme,
},
poly::Polynomial,
util::{
arithmetic::PrimeField,
code::BrakedownSpec6,
hash::Keccak256,
test::std_rng,
transcript::{InMemoryTranscript, Keccak256Transcript, TranscriptWrite},
},
};
use std::{any::type_name, io::Cursor, ops::Range};

const NUM_VARS_RANGE: Range<usize> = 16..21;

fn pcs_name<Pcs>() -> &'static str {
type_name::<Pcs>()
.split("::")
.find(|s| s.chars().next().unwrap().is_uppercase())
.unwrap()
.split('<')
.next()
.unwrap()
}

#[allow(clippy::type_complexity)]
fn prepare<F, Pcs>(
k: usize,
) -> (
Pcs::ProverParam,
Pcs::Polynomial,
Pcs::Commitment,
Point<F, Pcs::Polynomial>,
F,
)
where
F: PrimeField,
Pcs: PolynomialCommitmentScheme<F>,
{
let n = 1 << k;
let mut rng = std_rng();
let param = Pcs::setup(n, 1, &mut rng).unwrap();
let (pp, _) = Pcs::trim(&param, n, 1).unwrap();
let poly = Pcs::Polynomial::rand(n, &mut rng);
let comm = Pcs::commit(&pp, &poly).unwrap();
let point = Pcs::Polynomial::rand_point(k, &mut rng);
let eval = poly.evaluate(&point);
(pp, poly, comm, point, eval)
}

fn commit<F, Pcs>(group: &mut BenchmarkGroup<impl Measurement>)
where
F: PrimeField,
Pcs: PolynomialCommitmentScheme<F>,
{
let name = pcs_name::<Pcs>();
for k in NUM_VARS_RANGE {
let (pp, poly, _, _, _) = prepare::<F, Pcs>(k);
group.bench_with_input(BenchmarkId::new(name, k), &k, |b, _| {
b.iter(|| Pcs::commit(black_box(&pp), black_box(&poly)).unwrap())
});
}
}

fn open<F, Pcs>(group: &mut BenchmarkGroup<impl Measurement>)
where
F: PrimeField,
Pcs: PolynomialCommitmentScheme<F>,
Keccak256Transcript<Cursor<Vec<u8>>>: TranscriptWrite<Pcs::CommitmentChunk, F>,
{
let name = pcs_name::<Pcs>();
for k in NUM_VARS_RANGE {
let (pp, poly, comm, point, eval) = prepare::<F, Pcs>(k);
group.bench_with_input(BenchmarkId::new(name, k), &k, |b, _| {
b.iter(|| {
Pcs::open(
black_box(&pp),
black_box(&poly),
black_box(&comm),
black_box(&point),
black_box(&eval),
black_box(&mut Keccak256Transcript::new(())),
)
.unwrap()
})
});
}
}

fn bench_commit(c: &mut Criterion) {
let mut group = c.benchmark_group("commit");
group.sample_size(10);

commit::<_, MultilinearBrakedown<Fr, Keccak256, BrakedownSpec6>>(&mut group);
commit::<_, MultilinearKzg<Bn256>>(&mut group);
commit::<_, MultilinearIpa<G1Affine>>(&mut group);
commit::<_, MultilinearHyrax<G1Affine>>(&mut group);
commit::<_, Gemini<UnivariateKzg<Bn256>>>(&mut group);
commit::<_, Zeromorph<UnivariateKzg<Bn256>>>(&mut group);
}

fn bench_open(c: &mut Criterion) {
let mut group = c.benchmark_group("open");
group.sample_size(10);

open::<_, MultilinearBrakedown<Fr, Keccak256, BrakedownSpec6>>(&mut group);
open::<_, MultilinearKzg<Bn256>>(&mut group);
open::<_, MultilinearIpa<G1Affine>>(&mut group);
open::<_, MultilinearHyrax<G1Affine>>(&mut group);
open::<_, Gemini<UnivariateKzg<Bn256>>>(&mut group);
open::<_, Zeromorph<UnivariateKzg<Bn256>>>(&mut group);
}

criterion_group!(benches, bench_commit, bench_open);
criterion_main!(benches);
8 changes: 8 additions & 0 deletions plonkish_backend/src/poly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ pub trait Polynomial<F: Field>: Clone + Debug + for<'a> AddAssign<(&'a F, &'a Se
fn evals(&self) -> &[F];

fn evaluate(&self, point: &Self::Point) -> F;

#[cfg(any(test, feature = "benchmark"))]
fn rand(n: usize, rng: &mut impl rand::RngCore) -> Self {
Self::from_evals(crate::util::test::rand_vec(n, rng))
}

#[cfg(any(test, feature = "benchmark"))]
fn rand_point(k: usize, rng: &mut impl rand::RngCore) -> Self::Point;
}
5 changes: 5 additions & 0 deletions plonkish_backend/src/poly/multilinear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ impl<F: Field> Polynomial<F> for MultilinearPolynomial<F> {
fn evaluate(&self, point: &Self::Point) -> F {
MultilinearPolynomial::evaluate(self, point.as_slice())
}

#[cfg(any(test, feature = "benchmark"))]
fn rand_point(k: usize, rng: &mut impl rand::RngCore) -> Self::Point {
crate::util::test::rand_vec(k, rng)
}
}

impl<F: Field> MultilinearPolynomial<F> {
Expand Down
5 changes: 5 additions & 0 deletions plonkish_backend/src/poly/univariate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ impl<F: Field> Polynomial<F> for UnivariatePolynomial<F, CoefficientBasis> {
fn evaluate(&self, point: &Self::Point) -> F {
UnivariatePolynomial::evaluate(self, point)
}

#[cfg(any(test, feature = "benchmark"))]
fn rand_point(_: usize, rng: &mut impl rand::RngCore) -> F {
F::random(rng)
}
}

impl<F: Field> UnivariatePolynomial<F, CoefficientBasis> {
Expand Down
Loading