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