aurora_engine_precompiles/bls12_381/
map_fp_to_g1.rs1use crate::prelude::{Borrowed, Vec};
2use crate::{EvmPrecompileResult, Precompile, PrecompileOutput};
3use aurora_engine_sdk::bls12_381::{self, PADDED_FP_LENGTH};
4use aurora_engine_types::types::{make_address, Address, EthGas};
5use aurora_evm::{Context, ExitError};
6
7const MAP_FP_TO_G1_BASE: u64 = 5500;
9
10pub struct BlsMapFpToG1;
12
13impl BlsMapFpToG1 {
14 pub const ADDRESS: Address = make_address(0, 0x10);
15
16 fn execute(input: &[u8]) -> Result<Vec<u8>, ExitError> {
17 bls12_381::map_fp_to_g1(input).map_err(|e| ExitError::Other(Borrowed(e.as_ref())))
18 }
19}
20
21impl Precompile for BlsMapFpToG1 {
22 fn required_gas(_input: &[u8]) -> Result<EthGas, ExitError>
23 where
24 Self: Sized,
25 {
26 Ok(EthGas::new(MAP_FP_TO_G1_BASE))
27 }
28
29 fn run(
33 &self,
34 input: &[u8],
35 target_gas: Option<EthGas>,
36 _context: &Context,
37 _is_static: bool,
38 ) -> EvmPrecompileResult {
39 let cost = Self::required_gas(input)?;
40 if let Some(target_gas) = target_gas {
41 if cost > target_gas {
42 return Err(ExitError::OutOfGas);
43 }
44 }
45 if input.len() != PADDED_FP_LENGTH {
46 return Err(ExitError::Other(Borrowed("ERR_BLS_MAP_FP_TO_G1_LEN")));
47 }
48
49 let output = Self::execute(input)?;
50 Ok(PrecompileOutput::without_logs(cost, output))
51 }
52}
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57 use aurora_engine_types::H160;
58
59 #[test]
60 fn bls12_381_fp_to_g1() {
61 let precompile = BlsMapFpToG1;
62 let ctx = Context {
63 address: H160::zero(),
64 caller: H160::zero(),
65 apparent_value: 0.into(),
66 };
67 let input = hex::decode("0000000000000000000000000000000017f66b472b36717ee0902d685c808bb5f190bbcb2c51d067f1cbec64669f10199a5868d7181dcec0498fcc71f5acaf79").expect("hex decoding failed");
68
69 let res = precompile
70 .run(&input, None, &ctx, false)
71 .expect("precompile run should not fail");
72 let expected = hex::decode("\
73 00000000000000000000000000000000188dc9e5ddf48977f33aeb6e505518269bf67fb624fa86b79741d842e75a6fa1be0911c2caa9e55571b6e55a3c0c0b9e\
74 00000000000000000000000000000000193e8b7c7e78daf104a59d7b39401a65355fa874bd34e91688580941e99a863367efc68fe871e38e07423090e93919c9")
75 .expect("hex decoding failed");
76
77 assert_eq!(res.output, expected);
78 }
79}