revm_precompile/bls12_381_utils.rs
1//! Utility functions for the BLS12-381 precompiles
2use crate::bls12_381_const::MSM_MULTIPLIER;
3
4/// Implements the gas schedule for G1/G2 Multiscalar-multiplication assuming 30
5/// MGas/second, see also: <https://eips.ethereum.org/EIPS/eip-2537#g1g2-multiexponentiation>
6#[inline]
7pub fn msm_required_gas(k: usize, discount_table: &[u16], multiplication_cost: u64) -> u64 {
8 if k == 0 {
9 return 0;
10 }
11
12 let index = core::cmp::min(k - 1, discount_table.len() - 1);
13 let discount = discount_table[index] as u64;
14
15 (k as u64 * discount * multiplication_cost) / MSM_MULTIPLIER
16}