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

Reduce benchmark startup time and StarkNet field #654

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
44 changes: 25 additions & 19 deletions bench-templates/src/macros/ec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,24 @@ macro_rules! ec_bench {
let mut rng = ark_std::test_rng();
let mut arithmetic =
c.benchmark_group(format!("Arithmetic for {name}"));
// Efficient sampling of inputs for benchmarking
let mut g_left = <$Group>::rand(&mut rng);
let mut g_right = <$Group>::rand(&mut rng);
let group_elements_left = (0..SAMPLES)
.map(|_| <$Group>::rand(&mut rng))
.map(|_| g_left.double())
.collect::<Vec<_>>();
let group_elements_right = (0..SAMPLES)
.map(|_| <$Group>::rand(&mut rng))
let mut group_elements_right = (0..SAMPLES)
.map(|_| g_right.double())
.collect::<Vec<_>>();
group_elements_right.reverse();
let group_elements_right_affine = <$Group>::normalize_batch(&group_elements_right);

// Sample scalars
let scalars = (0..SAMPLES)
.map(|_| Scalar::rand(&mut rng))
.collect::<Vec<_>>();

// Conduct benchmarks
arithmetic.bench_function("Addition", |b| {
let mut i = 0;
b.iter(|| {
Expand Down Expand Up @@ -99,7 +107,7 @@ macro_rules! ec_bench {
}

fn serialization(c: &mut $crate::criterion::Criterion) {
use ark_ec::CurveGroup;
use ark_ec::{AdditiveGroup, CurveGroup};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::UniformRand;

Expand All @@ -108,9 +116,9 @@ macro_rules! ec_bench {
let name = format!("{}::{}", $curve_name, stringify!($Group));
let mut rng = ark_std::test_rng();

let v: Vec<_> = (0..SAMPLES)
.map(|_| <$Group>::rand(&mut rng))
.collect();
// Efficient sampling of inputs for benchmarking
let g = <$Group>::rand(&mut rng);
let v: Vec<_> = (0..SAMPLES).map(|_| g.double()).collect();
let v = <$Group>::normalize_batch(&v);
let mut bytes = Vec::with_capacity(1000);
let v_compressed = v
Expand All @@ -129,6 +137,7 @@ macro_rules! ec_bench {
bytes
})
.collect::<Vec<_>>();
// Start benchmarks
let mut serialization =
c.benchmark_group(format!("Serialization for {name}"));
serialization.bench_function(
Expand Down Expand Up @@ -205,7 +214,7 @@ macro_rules! ec_bench {
}

fn msm_131072(c: &mut $crate::criterion::Criterion) {
use ark_ec::{scalar_mul::variable_base::VariableBaseMSM, CurveGroup};
use ark_ec::{scalar_mul::variable_base::VariableBaseMSM, CurveGroup, AdditiveGroup};
use ark_ff::PrimeField;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::UniformRand;
Expand All @@ -215,18 +224,15 @@ macro_rules! ec_bench {
let name = format!("{}::{}", $curve_name, stringify!($Group));
let mut rng = ark_std::test_rng();

let v: Vec<_> = (0..SAMPLES)
.map(|_| <$Group>::rand(&mut rng))
.collect();
let v = <$Group>::normalize_batch(&v);
let scalars: Vec<_> = (0..SAMPLES)
.map(|_| Scalar::rand(&mut rng).into_bigint())
.collect();
c.bench_function(&format!("MSM for {name}"), |b| {
b.iter(|| {
let result: $Group = VariableBaseMSM::msm_bigint(&v, &scalars);
result
})
let g = <$Group>::rand(&mut rng);
let v: Vec<_> = (0..SAMPLES).map(|_| g.double()).collect();
let v = <$Group>::normalize_batch(&v);

let scalars: Vec<_> = (0..SAMPLES)
.map(|_| Scalar::rand(&mut rng).into_bigint())
.collect();
b.iter(|| <$Group>::msm_bigint(&v, &scalars))
});
}

Expand Down
18 changes: 10 additions & 8 deletions bench-templates/src/macros/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,22 +340,24 @@ macro_rules! prime_field {
v2[i].num_bits()
})
});
let v_bits_le = v2
.iter()
.map(|s| ark_ff::BitIteratorLE::new(s).collect::<Vec<_>>())
.collect::<Vec<_>>();

bits.bench_function("From Little-Endian bits", |b| {
let v_bits_le = v2
.iter()
.map(|s| ark_ff::BitIteratorLE::new(s).collect::<Vec<_>>())
.collect::<Vec<_>>();
let mut i = 0;
b.iter(|| {
i = (i + 1) % SAMPLES;
BigInt::from_bits_be(&v_bits_le[i]);
})
});
let v_bits_be = v1
.iter()
.map(|s| ark_ff::BitIteratorBE::new(s).collect::<Vec<_>>())
.collect::<Vec<_>>();

bits.bench_function("From Big-Endian bits", |b| {
let v_bits_be = v1
.iter()
.map(|s| ark_ff::BitIteratorBE::new(s).collect::<Vec<_>>())
.collect::<Vec<_>>();
let mut i = 0;
b.iter(|| {
i = (i + 1) % SAMPLES;
Expand Down
24 changes: 18 additions & 6 deletions bench-templates/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ macro_rules! bench {

paste! {
criterion_main!(
[<$G1:lower>]::benches,
[<$G2:lower>]::benches,
[<$Fr:lower>]::benches,
[<$Fq:lower>]::benches,
[<$FqExt:lower>]::benches,
[<$FqTarget:lower>]::benches,
[<$G1:lower>]::benches,
[<$G2:lower>]::benches,
pairing::benches
);
}
Expand All @@ -45,15 +45,15 @@ macro_rules! bench {
ScalarField = $Fr:ident,
BaseField = $Fq:ident,
) => {
$crate::ec_bench!($name, $G);
$crate::f_bench!(prime, $name, $Fr);
$crate::f_bench!(extension, $name, $Fq);
$crate::ec_bench!($name, $G);

paste! {
criterion_main!(
[<$G:lower>]::benches,
[<$Fr:lower>]::benches,
[<$Fq:lower>]::benches,
[<$G:lower>]::benches,
);
}
};
Expand All @@ -63,15 +63,27 @@ macro_rules! bench {
ScalarField = $Fr:ident,
PrimeBaseField = $Fq:ident,
) => {
$crate::ec_bench!($name, $G);
$crate::f_bench!(prime, $name, $Fr);
$crate::f_bench!(prime, $name, $Fq);
$crate::ec_bench!($name, $G);

paste! {
criterion_main!(
[<$G:lower>]::benches,
[<$Fr:lower>]::benches,
[<$Fq:lower>]::benches,
[<$G:lower>]::benches,
);
}
};
(
Name = $name:expr,
PrimeField = $Fp:ident,
) => {
$crate::f_bench!(prime, $name, $Fp);

paste! {
criterion_main!(
[<$Fp:lower>]::benches,
);
}
};
Expand Down
5 changes: 5 additions & 0 deletions test-curves/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,8 @@ harness = false
name = "mnt6_753"
path = "benches/mnt6_753.rs"
harness = false

[[bench]]
name = "starknet_fp"
path = "benches/starknet_fp.rs"
harness = false
4 changes: 4 additions & 0 deletions test-curves/benches/starknet_fp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use ark_algebra_bench_templates::*;
use ark_test_curves::starknet_fp::Fq;

bench!(Name = "StarkNetFp", PrimeField = Fq,);
2 changes: 2 additions & 0 deletions test-curves/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ pub mod bn384_small_two_adicity;
pub mod secp256k1;

pub mod fp128;

pub mod starknet_fp;
15 changes: 15 additions & 0 deletions test-curves/src/starknet_fp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! Prime field `Fq` where `q` is the StarkNet prime.
Pratyush marked this conversation as resolved.
Show resolved Hide resolved
use ark_ff::fields::{Fp256, MontBackend};

#[derive(ark_ff::MontConfig)]
#[modulus = "3618502788666131213697322783095070105623107215331596699973092056135872020481"]
#[generator = "3"]
pub struct FqConfig;
pub type Fq = Fp256<MontBackend<FqConfig, 4>>;

#[cfg(test)]
mod tests {
use super::*;
use ark_algebra_test_templates::*;
test_field!(fp; Fq; mont_prime_field);
}
Loading