revm_precompile/bls12_381/
g2_msm.rs1use super::crypto_backend::{encode_g2_point, p2_msm, read_g2, read_scalar};
3use super::utils::remove_g2_padding;
4use crate::bls12_381_const::{
5 DISCOUNT_TABLE_G2_MSM, G2_MSM_ADDRESS, G2_MSM_BASE_GAS_FEE, G2_MSM_INPUT_LENGTH,
6 PADDED_G2_LENGTH, SCALAR_LENGTH,
7};
8use crate::bls12_381_utils::msm_required_gas;
9use crate::{PrecompileError, PrecompileOutput, PrecompileResult, PrecompileWithAddress};
10use std::vec::Vec;
11
12pub const PRECOMPILE: PrecompileWithAddress = PrecompileWithAddress(G2_MSM_ADDRESS, g2_msm);
14
15pub fn g2_msm(input: &[u8], gas_limit: u64) -> PrecompileResult {
24 let input_len = input.len();
25 if input_len == 0 || input_len % G2_MSM_INPUT_LENGTH != 0 {
26 return Err(PrecompileError::Other(format!(
27 "G2MSM input length should be multiple of {G2_MSM_INPUT_LENGTH}, was {input_len}"
28 )));
29 }
30
31 let k = input_len / G2_MSM_INPUT_LENGTH;
32 let required_gas = msm_required_gas(k, &DISCOUNT_TABLE_G2_MSM, G2_MSM_BASE_GAS_FEE);
33 if required_gas > gas_limit {
34 return Err(PrecompileError::OutOfGas);
35 }
36
37 let mut g2_points: Vec<_> = Vec::with_capacity(k);
38 let mut scalars = Vec::with_capacity(k);
39 for i in 0..k {
40 let encoded_g2_element =
41 &input[i * G2_MSM_INPUT_LENGTH..i * G2_MSM_INPUT_LENGTH + PADDED_G2_LENGTH];
42 let encoded_scalar = &input[i * G2_MSM_INPUT_LENGTH + PADDED_G2_LENGTH
43 ..i * G2_MSM_INPUT_LENGTH + PADDED_G2_LENGTH + SCALAR_LENGTH];
44
45 if encoded_g2_element.iter().all(|i| *i == 0) {
49 continue;
50 }
51
52 let [a_x_0, a_x_1, a_y_0, a_y_1] = remove_g2_padding(encoded_g2_element)?;
53
54 let p0_aff = read_g2(a_x_0, a_x_1, a_y_0, a_y_1)?;
58
59 if encoded_scalar.iter().all(|i| *i == 0) {
65 continue;
66 }
67
68 g2_points.push(p0_aff);
70 scalars.push(read_scalar(encoded_scalar)?);
71 }
72
73 if g2_points.is_empty() {
75 return Ok(PrecompileOutput::new(
76 required_gas,
77 [0; PADDED_G2_LENGTH].into(),
78 ));
79 }
80
81 let multiexp_aff = p2_msm(g2_points, scalars);
83
84 let out = encode_g2_point(&multiexp_aff);
85 Ok(PrecompileOutput::new(required_gas, out.into()))
86}