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

Test and bench for eval_polynomial and compute_inner_product #559

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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
ashWhiteHat marked this conversation as resolved.
Show resolved Hide resolved
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");
ashWhiteHat marked this conversation as resolved.
Show resolved Hide resolved
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);
});
ashWhiteHat marked this conversation as resolved.
Show resolved Hide resolved
});
}
eval_polynomial_group.finish();

let mut compute_inner_product_group = c.benchmark_group("poly-compute_inner_product");
ashWhiteHat marked this conversation as resolved.
Show resolved Hide resolved
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)
});
ashWhiteHat marked this conversation as resolved.
Show resolved Hide resolved
});
}
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<_>>();
ashWhiteHat marked this conversation as resolved.
Show resolved Hide resolved
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<_>>();
ashWhiteHat marked this conversation as resolved.
Show resolved Hide resolved
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