revm_precompile/bls12_381/
utils.rs1use crate::bls12_381_const::{
3 FP_LENGTH, FP_PAD_BY, PADDED_FP_LENGTH, PADDED_G1_LENGTH, PADDED_G2_LENGTH,
4};
5use crate::PrecompileError;
6
7pub(super) fn remove_fp_padding(input: &[u8]) -> Result<&[u8; FP_LENGTH], PrecompileError> {
9 if input.len() != PADDED_FP_LENGTH {
10 return Err(PrecompileError::Other(format!(
11 "Padded input should be {PADDED_FP_LENGTH} bytes, was {}",
12 input.len()
13 )));
14 }
15 let (padding, unpadded) = input.split_at(FP_PAD_BY);
16 if !padding.iter().all(|&x| x == 0) {
17 return Err(PrecompileError::Other(format!(
18 "{FP_PAD_BY} top bytes of input are not zero",
19 )));
20 }
21 Ok(unpadded.try_into().unwrap())
22}
23pub(super) fn remove_g1_padding(input: &[u8]) -> Result<[&[u8; FP_LENGTH]; 2], PrecompileError> {
26 if input.len() != PADDED_G1_LENGTH {
27 return Err(PrecompileError::Other(format!(
28 "Input should be {PADDED_G1_LENGTH} bytes, was {}",
29 input.len()
30 )));
31 }
32
33 let x = remove_fp_padding(&input[..PADDED_FP_LENGTH])?;
34 let y = remove_fp_padding(&input[PADDED_FP_LENGTH..PADDED_G1_LENGTH])?;
35 Ok([x, y])
36}
37
38pub(super) fn remove_g2_padding(input: &[u8]) -> Result<[&[u8; FP_LENGTH]; 4], PrecompileError> {
41 if input.len() != PADDED_G2_LENGTH {
42 return Err(PrecompileError::Other(format!(
43 "Input should be {PADDED_G2_LENGTH} bytes, was {}",
44 input.len()
45 )));
46 }
47
48 let mut input_fps = [&[0; FP_LENGTH]; 4];
49 for i in 0..4 {
50 input_fps[i] = remove_fp_padding(&input[i * PADDED_FP_LENGTH..(i + 1) * PADDED_FP_LENGTH])?;
51 }
52 Ok(input_fps)
53}