revm_precompile/bls12_381/
g2_msm.rs

1//! BLS12-381 G2 msm precompile. More details in [`g2_msm`]
2use super::utils::{pad_g2_point, remove_g2_padding};
3use crate::bls12_381_const::{
4    DISCOUNT_TABLE_G2_MSM, G2_MSM_ADDRESS, G2_MSM_BASE_GAS_FEE, G2_MSM_INPUT_LENGTH,
5    PADDED_G2_LENGTH, SCALAR_LENGTH,
6};
7use crate::bls12_381_utils::msm_required_gas;
8use crate::{
9    crypto, Precompile, PrecompileError, PrecompileId, PrecompileOutput, PrecompileResult,
10};
11
12/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G2MSM precompile.
13pub const PRECOMPILE: Precompile =
14    Precompile::new(PrecompileId::Bls12G2Msm, G2_MSM_ADDRESS, g2_msm);
15
16/// Implements EIP-2537 G2MSM precompile.
17/// G2 multi-scalar-multiplication call expects `288*k` bytes as an input that is interpreted
18/// as byte concatenation of `k` slices each of them being a byte concatenation
19/// of encoding of G2 point (`256` bytes) and encoding of a scalar value (`32`
20/// bytes).
21/// Output is an encoding of multi-scalar-multiplication operation result - single G2
22/// point (`256` bytes).
23/// See also: <https://eips.ethereum.org/EIPS/eip-2537#abi-for-g2-multiexponentiation>
24pub fn g2_msm(input: &[u8], gas_limit: u64) -> PrecompileResult {
25    let input_len = input.len();
26    if input_len == 0 || !input_len.is_multiple_of(G2_MSM_INPUT_LENGTH) {
27        return Err(PrecompileError::Other(format!(
28            "G2MSM input length should be multiple of {G2_MSM_INPUT_LENGTH}, was {input_len}",
29        )));
30    }
31
32    let k = input_len / G2_MSM_INPUT_LENGTH;
33    let required_gas = msm_required_gas(k, &DISCOUNT_TABLE_G2_MSM, G2_MSM_BASE_GAS_FEE);
34    if required_gas > gas_limit {
35        return Err(PrecompileError::OutOfGas);
36    }
37
38    let mut valid_pairs_iter = (0..k).map(|i| {
39        let start = i * G2_MSM_INPUT_LENGTH;
40        let padded_g2 = &input[start..start + PADDED_G2_LENGTH];
41        let scalar_bytes = &input[start + PADDED_G2_LENGTH..start + G2_MSM_INPUT_LENGTH];
42
43        // Remove padding from G2 point - this validates padding format
44        let [x_0, x_1, y_0, y_1] = remove_g2_padding(padded_g2)?;
45        let scalar_array: [u8; SCALAR_LENGTH] = scalar_bytes.try_into().unwrap();
46
47        Ok(((*x_0, *x_1, *y_0, *y_1), scalar_array))
48    });
49
50    let unpadded_result = crypto().bls12_381_g2_msm(&mut valid_pairs_iter)?;
51
52    // Pad the result for EVM compatibility
53    let padded_result = pad_g2_point(&unpadded_result);
54
55    Ok(PrecompileOutput::new(required_gas, padded_result.into()))
56}