revm_precompile/bls12_381/
g2_mul.rsuse super::{
g2::{encode_g2_point, extract_g2_input, G2_INPUT_ITEM_LENGTH},
utils::{extract_scalar_input, NBITS},
};
use crate::{u64_to_address, PrecompileWithAddress};
use crate::{Precompile, PrecompileError, PrecompileOutput, PrecompileResult};
use blst::{blst_p2, blst_p2_affine, blst_p2_from_affine, blst_p2_mult, blst_p2_to_affine};
use primitives::Bytes;
pub const PRECOMPILE: PrecompileWithAddress =
PrecompileWithAddress(u64_to_address(ADDRESS), Precompile::Standard(g2_mul));
pub const ADDRESS: u64 = 0x0f;
pub(super) const BASE_GAS_FEE: u64 = 45000;
pub(super) const INPUT_LENGTH: usize = 288;
pub(super) fn g2_mul(input: &Bytes, gas_limit: u64) -> PrecompileResult {
if BASE_GAS_FEE > gas_limit {
return Err(PrecompileError::OutOfGas.into());
}
if input.len() != INPUT_LENGTH {
return Err(PrecompileError::Other(format!(
"G2MUL input should be {INPUT_LENGTH} bytes, was {}",
input.len()
))
.into());
}
let p0_aff = &extract_g2_input(&input[..G2_INPUT_ITEM_LENGTH], true)?;
let mut p0 = blst_p2::default();
unsafe { blst_p2_from_affine(&mut p0, p0_aff) };
let input_scalar0 = extract_scalar_input(&input[G2_INPUT_ITEM_LENGTH..])?;
let mut p = blst_p2::default();
unsafe { blst_p2_mult(&mut p, &p0, input_scalar0.b.as_ptr(), NBITS) };
let mut p_aff = blst_p2_affine::default();
unsafe { blst_p2_to_affine(&mut p_aff, &p) };
let out = encode_g2_point(&p_aff);
Ok(PrecompileOutput::new(BASE_GAS_FEE, out))
}