Skip to content

Commit 7e8aaf9

Browse files
webmaster128chipshort
authored andcommitted
Improve clarity on n vs. k
1 parent c989cc5 commit 7e8aaf9

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

packages/crypto/benches/main.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,14 @@ where
157157
.map(|(secret_key, message)| *message * secret_key)
158158
.collect();
159159

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

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

190-
group.bench_function(format!("bls12_381_pairing_equality_{num_points}"), |b| {
192+
group.bench_function(format!("bls12_381_pairing_equality_k={k}"), |b| {
191193
b.iter(|| {
192194
let is_valid = black_box(bls12_381_pairing_equality(
193195
&serialized_pubkeys,

packages/vm/src/imports.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,14 +372,19 @@ pub fn do_bls12_381_pairing_equality<
372372
let r = read_region(&memory, r_ptr, BLS12_381_G1_POINT_LEN)?;
373373
let s = read_region(&memory, s_ptr, BLS12_381_G2_POINT_LEN)?;
374374

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

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

0 commit comments

Comments
 (0)