Skip to content

Commit

Permalink
Improve clarity on n vs. k
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed May 27, 2024
1 parent 14f4552 commit 776f341
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
14 changes: 8 additions & 6 deletions packages/crypto/benches/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,14 @@ where
.map(|(secret_key, message)| *message * secret_key)
.collect();

for i in 1..=two_pow_max {
let num_points = 2_usize.pow(i);
let messages = &messages[..num_points];
let keys = &public_keys[..num_points];
for i in 0..=two_pow_max {
let n = 2_usize.pow(i); // the number of pairings on the left hand side
let k = n + 1; // the number of pairings in total
let messages: &[ark_ec::short_weierstrass::Affine<ark_bls12_381::g2::Config>] =
&messages[..n];
let keys = &public_keys[..n];
let aggregated_signature: G2Affine =
signatures[..num_points].iter().sum::<G2Projective>().into();
signatures[..n].iter().sum::<G2Projective>().into();

let serialized_pubkeys: Vec<u8> = keys
.iter()
Expand All @@ -187,7 +189,7 @@ where
.serialize_compressed(&mut serialized_signature[..])
.unwrap();

group.bench_function(format!("bls12_381_pairing_equality_{num_points}"), |b| {
group.bench_function(format!("bls12_381_pairing_equality_k={k}"), |b| {
b.iter(|| {
let is_valid = black_box(bls12_381_pairing_equality(
&serialized_pubkeys,
Expand Down
9 changes: 7 additions & 2 deletions packages/vm/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,14 +372,19 @@ pub fn do_bls12_381_pairing_equality<
let r = read_region(&memory, r_ptr, BLS12_381_G1_POINT_LEN)?;
let s = read_region(&memory, s_ptr, BLS12_381_G2_POINT_LEN)?;

let estimated_point_count = (ps.len() / BLS12_381_G1_POINT_LEN) as u64;
// The values here are only correct if ps and qs can be divided by the point size.
// They are good enough for gas since we error in `bls12_381_pairing_equality` if the inputs are
// not properly formatted.
let estimated_n = (ps.len() / BLS12_381_G1_POINT_LEN) as u64;
// The number of parings to compute (`n` on the left hand side and `k = n + 1` in total)
let estimated_k = estimated_n + 1;

let gas_info = GasInfo::with_cost(
// Add one to the `estimated_point_count` since we do not include any pairs in the base
// benchmark, and we always need to add one for the `r` and `s` pair.
data.gas_config
.bls12_381_pairing_equality_cost
.total_cost(estimated_point_count + 1),
.total_cost(estimated_k),
);
process_gas_info(data, &mut store, gas_info)?;

Expand Down

0 comments on commit 776f341

Please sign in to comment.