revm_precompile/bls12_381/
map_fp2_to_g2.rs

1//! BLS12-381 map fp2 to g2 precompile. More details in [`map_fp2_to_g2`]
2use super::utils::{pad_g2_point, remove_fp_padding};
3use crate::bls12_381_const::{
4    MAP_FP2_TO_G2_ADDRESS, MAP_FP2_TO_G2_BASE_GAS_FEE, PADDED_FP2_LENGTH, PADDED_FP_LENGTH,
5};
6use crate::{
7    crypto, Precompile, PrecompileError, PrecompileId, PrecompileOutput, PrecompileResult,
8};
9
10/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_MAP_FP2_TO_G2 precompile.
11pub const PRECOMPILE: Precompile = Precompile::new(
12    PrecompileId::Bls12MapFp2ToGp2,
13    MAP_FP2_TO_G2_ADDRESS,
14    map_fp2_to_g2,
15);
16
17/// Field-to-curve call expects 128 bytes as an input that is interpreted as
18/// an element of Fp2. Output of this call is 256 bytes and is an encoded G2
19/// point.
20/// See also: <https://eips.ethereum.org/EIPS/eip-2537#abi-for-mapping-fp2-element-to-g2-point>
21pub fn map_fp2_to_g2(input: &[u8], gas_limit: u64) -> PrecompileResult {
22    if MAP_FP2_TO_G2_BASE_GAS_FEE > gas_limit {
23        return Err(PrecompileError::OutOfGas);
24    }
25
26    if input.len() != PADDED_FP2_LENGTH {
27        return Err(PrecompileError::Other(format!(
28            "MAP_FP2_TO_G2 input should be {PADDED_FP2_LENGTH} bytes, was {}",
29            input.len()
30        )));
31    }
32
33    let input_p0_x = remove_fp_padding(&input[..PADDED_FP_LENGTH])?;
34    let input_p0_y = remove_fp_padding(&input[PADDED_FP_LENGTH..PADDED_FP2_LENGTH])?;
35
36    let unpadded_result = crypto().bls12_381_fp2_to_g2((*input_p0_x, *input_p0_y))?;
37
38    // Pad the result for EVM compatibility
39    let padded_result = pad_g2_point(&unpadded_result);
40
41    Ok(PrecompileOutput::new(
42        MAP_FP2_TO_G2_BASE_GAS_FEE,
43        padded_result.into(),
44    ))
45}