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

Optimize bls12 381 pairing #923

Merged
merged 16 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
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
2 changes: 1 addition & 1 deletion math/benches/criterion_elliptic_curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ use elliptic_curves::{
criterion_group!(
name = elliptic_curve_benches;
config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None)));
targets = bn_254_elliptic_curve_benchmarks,bls12_377_elliptic_curve_benchmarks,bls12_381_elliptic_curve_benchmarks
targets = bn_254_elliptic_curve_benchmarks,bls12_381_elliptic_curve_benchmarks,bls12_377_elliptic_curve_benchmarks
);
criterion_main!(elliptic_curve_benches);
33 changes: 32 additions & 1 deletion math/benches/elliptic_curves/bls12_381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ use lambdaworks_math::{
elliptic_curve::{
short_weierstrass::{
curves::bls12_381::{
curve::BLS12381Curve, pairing::BLS12381AtePairing, twist::BLS12381TwistCurve,
curve::BLS12381Curve,
pairing::{
final_exponentiation, final_exponentiation_optimized, miller, miller_optimized,
BLS12381AtePairing,
},
twist::BLS12381TwistCurve,
},
traits::Compress,
},
Expand All @@ -24,6 +29,8 @@ pub fn bls12_381_elliptic_curve_benchmarks(c: &mut Criterion) {
let a_g2 = BLS12381TwistCurve::generator();
let b_g2 = BLS12381TwistCurve::generator();

let miller_loop_output = miller_optimized(&a_g2, &a_g1);

let mut group = c.benchmark_group("BLS12-381 Ops");
group.significance_level(0.1).sample_size(10000);
group.throughput(criterion::Throughput::Elements(1));
Expand Down Expand Up @@ -93,4 +100,28 @@ pub fn bls12_381_elliptic_curve_benchmarks(c: &mut Criterion) {
))
});
});

// Miller Naive
group.bench_function("Miller Naive", |bencher| {
bencher.iter(|| black_box(miller(black_box(&a_g2), black_box(&a_g1))))
});

// Miller Optimized
group.bench_function("Miller Optimized", |bencher| {
bencher.iter(|| black_box(miller_optimized(black_box(&a_g2), black_box(&a_g1))))
});

// Final Exponentiation Naive
group.bench_function("Final Exponentiation Naive", |bencher| {
bencher.iter(|| black_box(final_exponentiation(black_box(&miller_loop_output))))
});

// Final Exponentiation Optimized
group.bench_function("Final Exponentiation Optimized", |bencher| {
bencher.iter(|| {
black_box(final_exponentiation_optimized(black_box(
&miller_loop_output,
)))
})
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ impl IsModulus<U384> for BLS12381FieldModulus {
}

pub type BLS12381PrimeField = MontgomeryBackendPrimeField<BLS12381FieldModulus, 6>;
type Fp2E = FieldElement<Degree2ExtensionField>;

//////////////////
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -199,6 +200,16 @@ impl HasCubicNonResidue<Degree2ExtensionField> for LevelTwoResidue {
}
}

impl HasQuadraticNonResidue<Degree2ExtensionField> for LevelTwoResidue {
fn residue() -> FieldElement<Degree2ExtensionField> {
FieldElement::new([
FieldElement::new(U384::from("1")),
FieldElement::new(U384::from("1")),
])
}
}
pub type Degree4ExtensionField = QuadraticExtensionField<Degree2ExtensionField, LevelTwoResidue>;

pub type Degree6ExtensionField = CubicExtensionField<Degree2ExtensionField, LevelTwoResidue>;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -284,6 +295,14 @@ impl FieldElement<Degree12ExtensionField> {
}
}

/// Computes the multiplication of an element of fp2 by the level two non-residue 9+u.
pub fn mul_fp2_by_nonresidue(a: &Fp2E) -> Fp2E {
// (c0 + c1 * u) * (1 + u) = (c0 - c1) + (c1 + c0) * u
let c0 = &a.value()[0] - &a.value()[1]; // c0 - c1
let c1 = &a.value()[0] + &a.value()[1]; // c1 + c0

Fp2E::new([c0, c1])
}
#[cfg(test)]
mod tests {
use crate::elliptic_curve::{
Expand Down
Loading
Loading