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