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

Optimize bls12 381 pairing #923

merged 16 commits into from
Oct 4, 2024

Conversation

jotabulacios
Copy link
Contributor

BLS 12-381 pairing optimization

Description

This PR aims to improve the pairing for the bls 12-381 curve by using optimized operations

Type of change

  • Optimization

Benches

  • Actual
    Ate pairing : 12.169 ms
    Final exponentiation : 11.499 ms

  • New version
    Ate pairing : 2.0644 ms
    Final exponentiation : 1.0674 ms

@codecov-commenter
Copy link

codecov-commenter commented Oct 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 71.08%. Comparing base (e25a464) to head (7ac1473).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #923      +/-   ##
==========================================
+ Coverage   70.96%   71.08%   +0.11%     
==========================================
  Files         144      144              
  Lines       31672    31824     +152     
==========================================
+ Hits        22477    22622     +145     
- Misses       9195     9202       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jotabulacios jotabulacios marked this pull request as ready for review October 3, 2024 19:55
@jotabulacios jotabulacios requested a review from a team as a code owner October 3, 2024 19:55
Comment on lines 120 to 157
pub fn miller(
q: &ShortWeierstrassProjectivePoint<BLS12381TwistCurve>,
p: &ShortWeierstrassProjectivePoint<BLS12381Curve>,
) -> FieldElement<Degree12ExtensionField> {
let mut r = q.clone();
let mut f = FieldElement::<Degree12ExtensionField>::one();
let mut miller_loop_constant = MILLER_LOOP_CONSTANT;
let mut miller_loop_constant_bits: alloc::vec::Vec<bool> = alloc::vec![];

while miller_loop_constant > 0 {
miller_loop_constant_bits.insert(0, (miller_loop_constant & 1) == 1);
miller_loop_constant >>= 1;
}

for bit in miller_loop_constant_bits[1..].iter() {
double_accumulate_line(&mut r, p, &mut f);
if *bit {
add_accumulate_line(&mut r, q, p, &mut f);
}
}
f.conjugate()
}
#[allow(unused)]
pub fn miller_optimized(
q: &ShortWeierstrassProjectivePoint<BLS12381TwistCurve>,
p: &ShortWeierstrassProjectivePoint<BLS12381Curve>,
) -> FieldElement<Degree12ExtensionField> {
let mut r = q.clone();
let mut f = FieldElement::<Degree12ExtensionField>::one();
X_BINARY.iter().skip(1).for_each(|bit| {
double_accumulate_line(&mut r, p, &mut f);
if *bit {
add_accumulate_line(&mut r, q, p, &mut f);
}
});

f.conjugate()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can just keep miller_optimized as miller and, if we need the old one for validation, call it miller_slow.

fn miller(
q: &ShortWeierstrassProjectivePoint<BLS12381TwistCurve>,
p: &ShortWeierstrassProjectivePoint<BLS12381Curve>,
pub fn final_exponentiation(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, if both implementations are essentially the same in intent, reserve the plain name for the fast version.

Comment on lines 101 to 104
// Miller
group.bench_function("Miller Naive", |bencher| {
bencher.iter(|| black_box(miller(black_box(&a_g2), black_box(&a_g1))))
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is no longer the naive version.

}

#[allow(clippy::needless_range_loop)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem necessary.

@diegokingston diegokingston added this pull request to the merge queue Oct 4, 2024
Merged via the queue into main with commit bec0b1a Oct 4, 2024
8 checks passed
@diegokingston diegokingston deleted the optimize_bls12_381_pairing branch October 4, 2024 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants