Skip to content

Commit

Permalink
EC Add Bench with Starknet RS (#614)
Browse files Browse the repository at this point in the history
* Draft benches

* Refactor

* Update text

* Clippy

* Change point for generator to avoid falling in a small subgroup

* Format files

* Remove unused point in benchmark

* Remove unused imports

---------

Co-authored-by: Mariano A. Nicolini <[email protected]>
  • Loading branch information
MauroToscano and entropidelic authored Oct 23, 2023
1 parent d0e9788 commit f940e14
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
6 changes: 6 additions & 0 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ ark-test-curves = { git = "https://github.com/arkworks-rs/algebra", rev = "ef8f7
ark-std = "0.4.0"
rand = "0.8.5"
rand_chacha = "0.3.1"
starknet-curve = { git = "https://github.com/xJonathanLEI/starknet-rs" }
starknet-ff = { git = "https://github.com/xJonathanLEI/starknet-rs" }

[dependencies.lambdaworks-math]
path = "../math"
Expand Down Expand Up @@ -39,3 +41,7 @@ harness = false
[[bench]]
name = "pow"
harness = false

[[bench]]
name = "point"
harness = false
77 changes: 77 additions & 0 deletions benches/benches/point.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use lambdaworks_math::{
cyclic_group::IsGroup,
elliptic_curve::{short_weierstrass::curves::stark_curve::StarkCurve, traits::IsEllipticCurve},
};
use starknet_curve::{curve_params::GENERATOR, AffinePoint, ProjectivePoint};
use std::ops::AddAssign;

const BENCHMARK_NAME: &str = "point";

pub fn criterion_benchmark(c: &mut Criterion) {
let initial_projective_point = ProjectivePoint::from(&GENERATOR);
let second_project_point = initial_projective_point;

// This is the code we are going to bench
// We test it once outside the bench to check the result matches with Lambdaworks
let mut projective_point = initial_projective_point;
for _i in 0..10000 {
projective_point.add_assign(&second_project_point);
}

let starknet_rs_x = AffinePoint::from(&projective_point).x.to_string();
println!("Starknet RS result X: {} ", starknet_rs_x);
let starknet_rs_y = AffinePoint::from(&projective_point).y.to_string();
print!("Starknet RS result Y: {} ", starknet_rs_y);

{
c.bench_function(
&format!("{} 10k Operations | Starknet RS ", BENCHMARK_NAME),
|b| {
b.iter(|| {
let mut projective_point = initial_projective_point;
// We loop to have a higher variance of numbers, and make the time of the clones not relevant
for _i in 0..10000 {
projective_point.add_assign(&second_project_point);
}
projective_point
});
},
);
}

let initial_projective_point = StarkCurve::generator();
let second_projective_point = initial_projective_point.clone();

// This is the code we are going to bench
// We test it once outside the bench to check the result matches with Starknet RS
let mut projective_point = initial_projective_point.clone();
for _i in 0..10000 {
projective_point =
black_box(projective_point.operate_with(black_box(&second_projective_point)));
}
let lambdaworks_x = projective_point.to_affine().x().to_string();
let lambdaworks_y = projective_point.to_affine().y().to_string();
println!("Lambdaworks result, X: {}", lambdaworks_x);
println!("Lambdaworks result, Y: {}", lambdaworks_y);

{
c.bench_function(
&format!("{} 10k Operations | Lambdaworks", BENCHMARK_NAME),
|b| {
b.iter(|| {
let mut projective_point = initial_projective_point.clone();
for _i in 0..10000 {
projective_point = black_box(
projective_point.operate_with(black_box(&second_projective_point)),
);
}
projective_point
});
},
);
}
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

0 comments on commit f940e14

Please sign in to comment.