revm_precompile/bls12_381/
g1_mul.rsuse super::{
g1::{encode_g1_point, extract_g1_input, G1_INPUT_ITEM_LENGTH},
utils::{extract_scalar_input, NBITS},
};
use crate::{u64_to_address, PrecompileWithAddress};
use crate::{PrecompileError, PrecompileOutput, PrecompileResult};
use blst::{blst_p1, blst_p1_affine, blst_p1_from_affine, blst_p1_mult, blst_p1_to_affine};
use primitives::Bytes;
pub const PRECOMPILE: PrecompileWithAddress =
PrecompileWithAddress(u64_to_address(ADDRESS), g1_mul);
pub const ADDRESS: u64 = 0x0c;
pub(super) const BASE_GAS_FEE: u64 = 12000;
pub(super) const INPUT_LENGTH: usize = 160;
pub(super) fn g1_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!(
"G1MUL input should be {INPUT_LENGTH} bytes, was {}",
input.len()
))
.into());
}
let slice = &input[..G1_INPUT_ITEM_LENGTH];
let p0_aff = &extract_g1_input(slice, true)?;
let mut p0 = blst_p1::default();
unsafe { blst_p1_from_affine(&mut p0, p0_aff) };
let input_scalar0 = extract_scalar_input(&input[G1_INPUT_ITEM_LENGTH..])?;
let mut p = blst_p1::default();
unsafe { blst_p1_mult(&mut p, &p0, input_scalar0.b.as_ptr(), NBITS) };
let mut p_aff = blst_p1_affine::default();
unsafe { blst_p1_to_affine(&mut p_aff, &p) };
let out = encode_g1_point(&p_aff);
Ok(PrecompileOutput::new(BASE_GAS_FEE, out))
}