From b193cd790d01778246dda8c7dc346f3ff834a92f Mon Sep 17 00:00:00 2001 From: PatStiles Date: Tue, 16 Apr 2024 09:14:53 -0700 Subject: [PATCH 1/4] add iai boilerplate --- jolt-core/Cargo.toml | 7 +++++++ jolt-core/src/benches/iai.rs | 33 +++++++++++++++++++++++++++++++++ jolt-core/src/benches/mod.rs | 1 + 3 files changed, 41 insertions(+) create mode 100644 jolt-core/src/benches/iai.rs diff --git a/jolt-core/Cargo.toml b/jolt-core/Cargo.toml index 03dd41112..aabaf0e19 100644 --- a/jolt-core/Cargo.toml +++ b/jolt-core/Cargo.toml @@ -64,6 +64,9 @@ bincode = "1.3.3" [build-dependencies] common = { path = "../common" } +[dev-dependencies] +iai-callgrind = "0.10.2" + [lib] name = "jolt_core" path = "src/lib.rs" @@ -77,3 +80,7 @@ default = [ "multicore", ] multicore = ["rayon"] + +[[bench]] +name = "iai" +harness = false \ No newline at end of file diff --git a/jolt-core/src/benches/iai.rs b/jolt-core/src/benches/iai.rs new file mode 100644 index 000000000..54d9bc90e --- /dev/null +++ b/jolt-core/src/benches/iai.rs @@ -0,0 +1,33 @@ +use iai_callgrind::{main, library_benchmark, library_benchmark_group}; + +fn msm_setup(value: u64) -> () { + value +} + +fn poly_setup(value: u64) -> () { + value +} + +#[library_benchmark] +#[bench::long(msm_setup(30))] +fn bench_msm(scalars: Vec, points: Vec>) -> u64 { + black_box(msm(value)) +} + +// Poly benches fix vars of polys of size 12-32 +#[library_benchmark] +#[bench::long(poly_setup(30))] +fn bench_polynomial_binding(j: F, poly: DensePolynomial) -> u64 { + black_box(poly.bound_poly_var_top(&j)) +} + +#[library_benchmark] +#[bench::long(poly_setup(30))] +fn bench_polynomial_evaluation(j: F, value: DensePolynomial) -> u64 { + black_box(poly.bound_poly_var_top(&j)) +} + +main!( + callgrind_args = "toggle-collect=util::*"; + functions = bench_msm, bench_polynomial_binding, bench_polynomial_evaluation +); \ No newline at end of file diff --git a/jolt-core/src/benches/mod.rs b/jolt-core/src/benches/mod.rs index 5f65a2f7c..204dca619 100644 --- a/jolt-core/src/benches/mod.rs +++ b/jolt-core/src/benches/mod.rs @@ -1,2 +1,3 @@ pub mod bench; pub mod sum_timer; +pub mod iai; \ No newline at end of file From e37c5207d2f86aa89c4869246d2b57c61f7f2a80 Mon Sep 17 00:00:00 2001 From: PatStiles Date: Tue, 16 Apr 2024 18:53:04 -0700 Subject: [PATCH 2/4] setup --- jolt-core/Cargo.toml | 10 +++--- jolt-core/src/benches/iai.rs | 64 ++++++++++++++++++++++++++++++++---- 2 files changed, 64 insertions(+), 10 deletions(-) diff --git a/jolt-core/Cargo.toml b/jolt-core/Cargo.toml index aabaf0e19..3ec731ae1 100644 --- a/jolt-core/Cargo.toml +++ b/jolt-core/Cargo.toml @@ -56,6 +56,7 @@ tracing-chrome = "0.7.1" tracing-flame = "0.2.0" tracing-subscriber = "0.3.18" tracing-texray = "0.2.0" +iai-callgrind = "0.10.2" common = { path = "../common" } tracer = { path = "../tracer" } @@ -67,6 +68,11 @@ common = { path = "../common" } [dev-dependencies] iai-callgrind = "0.10.2" +[[bench]] +name = "iai" +path = "./src/benches/iai.rs" +harness = false + [lib] name = "jolt_core" path = "src/lib.rs" @@ -80,7 +86,3 @@ default = [ "multicore", ] multicore = ["rayon"] - -[[bench]] -name = "iai" -harness = false \ No newline at end of file diff --git a/jolt-core/src/benches/iai.rs b/jolt-core/src/benches/iai.rs index 54d9bc90e..93bd44b86 100644 --- a/jolt-core/src/benches/iai.rs +++ b/jolt-core/src/benches/iai.rs @@ -1,17 +1,33 @@ +/* +use core::num; + +use ark_ec::AffineRepr; use iai_callgrind::{main, library_benchmark, library_benchmark_group}; +use jolt_core::{msm::, poly::dense_mlpoly::}; -fn msm_setup(value: u64) -> () { - value +/* + value = length of MSM + */ +fn msm_setup(num_points: u64) -> (Vec, Vec) { + // Define the elliptic curve you want to work with. + // For example, let's use the BN254 curve. + + // Initialize a random number generator. + let mut rng = rand::thread_rng(); + + // Generate a vector of random affine points on the curve. + (vec![C::ScalarField::rand(&mut rng); num_points], vec![C::rand(&mut rng); num_points]) } +// length of poly fn poly_setup(value: u64) -> () { - value + } #[library_benchmark] #[bench::long(msm_setup(30))] -fn bench_msm(scalars: Vec, points: Vec>) -> u64 { - black_box(msm(value)) +fn bench_msm(input: (Vec, Vec)) -> u64 { + black_box(VariableBaseMSM::msm(&G::normalize_batch(&input.0), &input.1).unwrap()); } // Poly benches fix vars of polys of size 12-32 @@ -30,4 +46,40 @@ fn bench_polynomial_evaluation(j: F, value: DensePolynomial) -> u64 { main!( callgrind_args = "toggle-collect=util::*"; functions = bench_msm, bench_polynomial_binding, bench_polynomial_evaluation -); \ No newline at end of file +); + +fn some_setup_func(value: u64) -> u64 { + value +} + +#[library_benchmark] +#[bench::long(some_setup_func(30))] +fn bench_fibonacci(value: u64) -> u64 { + black_box(fibonacci(value)) +} +*/ + +use iai_callgrind::{main, library_benchmark_group, library_benchmark}; +use std::hint::black_box; + +fn fibonacci(n: u64) -> u64 { + match n { + 0 => 1, + 1 => 1, + n => fibonacci(n - 1) + fibonacci(n - 2), + } +} + +#[library_benchmark] +#[bench::short(10)] +#[bench::long(30)] +fn bench_fibonacci(value: u64) -> u64 { + black_box(fibonacci(value)) +} + +library_benchmark_group!( + name = bench_fibonacci_group; + benchmarks = bench_fibonacci +); + +main!(library_benchmark_groups = bench_fibonacci_group); \ No newline at end of file From 56063bfc3a6b11ccb66dedb987e8692a9bad31c5 Mon Sep 17 00:00:00 2001 From: PatStiles Date: Wed, 17 Apr 2024 03:20:54 +0000 Subject: [PATCH 3/4] add benches --- jolt-core/Cargo.toml | 4 -- jolt-core/benches/iai.rs | 55 +++++++++++++++++++++++ jolt-core/src/benches/iai.rs | 85 ------------------------------------ jolt-core/src/benches/mod.rs | 3 +- jolt-core/src/lib.rs | 4 +- 5 files changed, 58 insertions(+), 93 deletions(-) create mode 100644 jolt-core/benches/iai.rs delete mode 100644 jolt-core/src/benches/iai.rs diff --git a/jolt-core/Cargo.toml b/jolt-core/Cargo.toml index 3ec731ae1..a3b4dcb2c 100644 --- a/jolt-core/Cargo.toml +++ b/jolt-core/Cargo.toml @@ -65,12 +65,8 @@ bincode = "1.3.3" [build-dependencies] common = { path = "../common" } -[dev-dependencies] -iai-callgrind = "0.10.2" - [[bench]] name = "iai" -path = "./src/benches/iai.rs" harness = false [lib] diff --git a/jolt-core/benches/iai.rs b/jolt-core/benches/iai.rs new file mode 100644 index 000000000..1d2f9d200 --- /dev/null +++ b/jolt-core/benches/iai.rs @@ -0,0 +1,55 @@ +use ark_ec::CurveGroup; +use ark_ff::PrimeField; +use ark_std::{UniformRand, test_rng}; +use ark_bn254::{G1Projective, Fr}; +use iai_callgrind::{main, library_benchmark, library_benchmark_group}; +use std::hint::black_box; +use jolt_core::{poly::dense_mlpoly::DensePolynomial, msm::VariableBaseMSM}; + +fn msm_setup(num_points: usize) -> (Vec, Vec) { + let mut rng = test_rng(); + + // Generate a vector of random affine points on the curve. + (vec![G::rand(&mut rng); num_points], vec![G::ScalarField::rand(&mut rng); num_points]) +} + +fn bound_poly_setup(size: usize) -> (DensePolynomial, F) { + let mut rng = test_rng(); + + (DensePolynomial::new(vec![F::rand(&mut rng); size]), F::rand(&mut rng)) +} + +fn eval_poly_setup(size: usize) -> (DensePolynomial, Vec) { + let mut rng = test_rng(); + + let poly = DensePolynomial::new(vec![F::rand(&mut rng); size]); + let points = vec![F::rand(&mut rng); poly.get_num_vars()]; + (poly, points) +} + +#[library_benchmark] +#[bench::long(msm_setup::(4096))] +fn bench_msm(input: (Vec, Vec)) -> G { + black_box(VariableBaseMSM::msm(&G::normalize_batch(&input.0), &input.1).unwrap()) +} + +#[library_benchmark] +#[bench::long(bound_poly_setup::(4096))] +fn bench_polynomial_binding(input: (DensePolynomial, F)) { + let (mut poly, val) = input; + black_box(poly.bound_poly_var_top(&val)); +} + +#[library_benchmark] +#[bench::long(eval_poly_setup::(4096))] +fn bench_polynomial_evaluate(input: (DensePolynomial, Vec)) -> F { + let (poly, points) = input; + black_box(poly.evaluate(&points)) +} + +library_benchmark_group!( + name = jolt_core_ops; + benchmarks = bench_msm, bench_polynomial_binding, bench_polynomial_evaluate +); + +main!(library_benchmark_groups = jolt_core_ops); \ No newline at end of file diff --git a/jolt-core/src/benches/iai.rs b/jolt-core/src/benches/iai.rs deleted file mode 100644 index 93bd44b86..000000000 --- a/jolt-core/src/benches/iai.rs +++ /dev/null @@ -1,85 +0,0 @@ -/* -use core::num; - -use ark_ec::AffineRepr; -use iai_callgrind::{main, library_benchmark, library_benchmark_group}; -use jolt_core::{msm::, poly::dense_mlpoly::}; - -/* - value = length of MSM - */ -fn msm_setup(num_points: u64) -> (Vec, Vec) { - // Define the elliptic curve you want to work with. - // For example, let's use the BN254 curve. - - // Initialize a random number generator. - let mut rng = rand::thread_rng(); - - // Generate a vector of random affine points on the curve. - (vec![C::ScalarField::rand(&mut rng); num_points], vec![C::rand(&mut rng); num_points]) -} - -// length of poly -fn poly_setup(value: u64) -> () { - -} - -#[library_benchmark] -#[bench::long(msm_setup(30))] -fn bench_msm(input: (Vec, Vec)) -> u64 { - black_box(VariableBaseMSM::msm(&G::normalize_batch(&input.0), &input.1).unwrap()); -} - -// Poly benches fix vars of polys of size 12-32 -#[library_benchmark] -#[bench::long(poly_setup(30))] -fn bench_polynomial_binding(j: F, poly: DensePolynomial) -> u64 { - black_box(poly.bound_poly_var_top(&j)) -} - -#[library_benchmark] -#[bench::long(poly_setup(30))] -fn bench_polynomial_evaluation(j: F, value: DensePolynomial) -> u64 { - black_box(poly.bound_poly_var_top(&j)) -} - -main!( - callgrind_args = "toggle-collect=util::*"; - functions = bench_msm, bench_polynomial_binding, bench_polynomial_evaluation -); - -fn some_setup_func(value: u64) -> u64 { - value -} - -#[library_benchmark] -#[bench::long(some_setup_func(30))] -fn bench_fibonacci(value: u64) -> u64 { - black_box(fibonacci(value)) -} -*/ - -use iai_callgrind::{main, library_benchmark_group, library_benchmark}; -use std::hint::black_box; - -fn fibonacci(n: u64) -> u64 { - match n { - 0 => 1, - 1 => 1, - n => fibonacci(n - 1) + fibonacci(n - 2), - } -} - -#[library_benchmark] -#[bench::short(10)] -#[bench::long(30)] -fn bench_fibonacci(value: u64) -> u64 { - black_box(fibonacci(value)) -} - -library_benchmark_group!( - name = bench_fibonacci_group; - benchmarks = bench_fibonacci -); - -main!(library_benchmark_groups = bench_fibonacci_group); \ No newline at end of file diff --git a/jolt-core/src/benches/mod.rs b/jolt-core/src/benches/mod.rs index 204dca619..0282a07ec 100644 --- a/jolt-core/src/benches/mod.rs +++ b/jolt-core/src/benches/mod.rs @@ -1,3 +1,2 @@ pub mod bench; -pub mod sum_timer; -pub mod iai; \ No newline at end of file +pub mod sum_timer; \ No newline at end of file diff --git a/jolt-core/src/lib.rs b/jolt-core/src/lib.rs index aee692dc9..e18cd2e1e 100644 --- a/jolt-core/src/lib.rs +++ b/jolt-core/src/lib.rs @@ -12,8 +12,8 @@ pub mod benches; pub mod host; pub mod jolt; pub mod lasso; -mod msm; -mod poly; +pub mod msm; +pub mod poly; pub mod r1cs; mod subprotocols; mod utils; From 7ef04bdc7e9a0dd3bbfcc4b74183508d915612e3 Mon Sep 17 00:00:00 2001 From: PatStiles Date: Wed, 17 Apr 2024 16:01:58 +0000 Subject: [PATCH 4/4] fmt --- jolt-core/benches/iai.rs | 24 +++++++++++++++--------- jolt-core/src/benches/mod.rs | 2 +- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/jolt-core/benches/iai.rs b/jolt-core/benches/iai.rs index 1d2f9d200..3322fc1d0 100644 --- a/jolt-core/benches/iai.rs +++ b/jolt-core/benches/iai.rs @@ -1,23 +1,29 @@ +use ark_bn254::{Fr, G1Projective}; use ark_ec::CurveGroup; use ark_ff::PrimeField; -use ark_std::{UniformRand, test_rng}; -use ark_bn254::{G1Projective, Fr}; -use iai_callgrind::{main, library_benchmark, library_benchmark_group}; +use ark_std::{test_rng, UniformRand}; +use iai_callgrind::{library_benchmark, library_benchmark_group, main}; +use jolt_core::{msm::VariableBaseMSM, poly::dense_mlpoly::DensePolynomial}; use std::hint::black_box; -use jolt_core::{poly::dense_mlpoly::DensePolynomial, msm::VariableBaseMSM}; fn msm_setup(num_points: usize) -> (Vec, Vec) { let mut rng = test_rng(); // Generate a vector of random affine points on the curve. - (vec![G::rand(&mut rng); num_points], vec![G::ScalarField::rand(&mut rng); num_points]) + ( + vec![G::rand(&mut rng); num_points], + vec![G::ScalarField::rand(&mut rng); num_points], + ) } fn bound_poly_setup(size: usize) -> (DensePolynomial, F) { let mut rng = test_rng(); - (DensePolynomial::new(vec![F::rand(&mut rng); size]), F::rand(&mut rng)) -} + ( + DensePolynomial::new(vec![F::rand(&mut rng); size]), + F::rand(&mut rng), + ) +} fn eval_poly_setup(size: usize) -> (DensePolynomial, Vec) { let mut rng = test_rng(); @@ -25,7 +31,7 @@ fn eval_poly_setup(size: usize) -> (DensePolynomial, Vec) { let poly = DensePolynomial::new(vec![F::rand(&mut rng); size]); let points = vec![F::rand(&mut rng); poly.get_num_vars()]; (poly, points) -} +} #[library_benchmark] #[bench::long(msm_setup::(4096))] @@ -52,4 +58,4 @@ library_benchmark_group!( benchmarks = bench_msm, bench_polynomial_binding, bench_polynomial_evaluate ); -main!(library_benchmark_groups = jolt_core_ops); \ No newline at end of file +main!(library_benchmark_groups = jolt_core_ops); diff --git a/jolt-core/src/benches/mod.rs b/jolt-core/src/benches/mod.rs index 0282a07ec..5f65a2f7c 100644 --- a/jolt-core/src/benches/mod.rs +++ b/jolt-core/src/benches/mod.rs @@ -1,2 +1,2 @@ pub mod bench; -pub mod sum_timer; \ No newline at end of file +pub mod sum_timer;