Skip to content

Commit

Permalink
add poly arithmetic bench and test
Browse files Browse the repository at this point in the history
  • Loading branch information
ashWhiteHat committed Apr 26, 2022
1 parent 66b2b3b commit 8544f14
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,13 @@ members = [
"halo2_gadgets",
"halo2_proofs",
]

[profile.bench]
opt-level = 3
debug = false
debug-assertions = false
overflow-checks = false
rpath = false
lto = "thin"
incremental = false
codegen-units = 1
2 changes: 1 addition & 1 deletion book/src/design/proving-system/vanishing.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ verifier samples $y$) linear combination of the circuit relations.

## Committing to $h(X)$

$h(X)$ has degree $(d - 1)n - d$ (because the divisor $t(X)$ has degree $n$). However, the
$h(X)$ has degree $d(n - 1) - n$ (because the divisor $t(X)$ has degree $n$). However, the
polynomial commitment scheme we use for Halo 2 only supports committing to polynomials of
degree $n - 1$ (which is the maximum degree that the rest of the protocol needs to commit
to). Instead of increasing the cost of the polynomial commitment scheme, the prover split
Expand Down
4 changes: 4 additions & 0 deletions halo2_proofs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ harness = false
name = "plonk"
harness = false

[[bench]]
name = "poly"
harness = false

[[bench]]
name = "dev_lookup"
harness = false
Expand Down
39 changes: 39 additions & 0 deletions halo2_proofs/benches/poly.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#[macro_use]
extern crate criterion;

use crate::arithmetic::{compute_inner_product, eval_polynomial};
use crate::pasta::Fp;
use group::ff::Field;
use halo2_proofs::*;

use criterion::{BenchmarkId, Criterion};
use rand_core::OsRng;

fn criterion_benchmark(c: &mut Criterion) {
let mut eval_polynomial_group = c.benchmark_group("poly-eval_polynomial");
for k in 3..19 {
eval_polynomial_group.bench_function(BenchmarkId::new("k", k), |b| {
b.iter(|| {
let poly = (0..(1 << k)).map(|_| Fp::random(OsRng)).collect::<Vec<_>>();
let point = Fp::random(OsRng);
eval_polynomial(&poly, point);
});
});
}
eval_polynomial_group.finish();

let mut compute_inner_product_group = c.benchmark_group("poly-compute_inner_product");
for k in 3..19 {
compute_inner_product_group.bench_function(BenchmarkId::new("k", k), |b| {
b.iter(|| {
let a = (0..(1 << k)).map(|_| Fp::random(OsRng)).collect::<Vec<_>>();
let b = (0..(1 << k)).map(|_| Fp::random(OsRng)).collect::<Vec<_>>();
compute_inner_product(&a, &b)
});
});
}
compute_inner_product_group.finish();
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
26 changes: 26 additions & 0 deletions halo2_proofs/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,32 @@ use rand_core::OsRng;
#[cfg(test)]
use crate::pasta::Fp;

#[test]
fn test_eval_polynomial() {
for k in 3..10 {
let mut eval = Fp::zero();
let mut exp = Fp::one();
let point = Fp::random(OsRng);
let poly = (0..(1 << k)).map(|_| Fp::random(OsRng)).collect::<Vec<_>>();
poly.iter().for_each(|a| {
eval += a * exp;
exp *= point;
});
assert_eq!(eval_polynomial(&poly, point), eval);
}
}

#[test]
fn test_compute_inner_product() {
for k in 3..10 {
let mut product = Fp::zero();
let a = (0..(1 << k)).map(|_| Fp::random(OsRng)).collect::<Vec<_>>();
let b = (0..(1 << k)).map(|_| Fp::random(OsRng)).collect::<Vec<_>>();
a.iter().zip(b.iter()).for_each(|(a, b)| product += a * b);
assert_eq!(compute_inner_product(&a, &b), product);
}
}

#[test]
fn test_lagrange_interpolate() {
let rng = OsRng;
Expand Down

0 comments on commit 8544f14

Please sign in to comment.