1use crate::types::Fr;
2use crate::Error;
3
4pub fn buffer_as_fields(input: &[u8], target_length: usize) -> Result<Vec<Fr>, Error> {
10 let mut encoded = vec![Fr::from(input.len() as u64)];
11 for chunk in input.chunks(31) {
12 let mut padded = [0u8; 32];
13 padded[1..1 + chunk.len()].copy_from_slice(chunk);
14 encoded.push(Fr::from(padded));
15 }
16 if encoded.len() > target_length {
17 return Err(Error::Abi(format!(
18 "buffer exceeds maximum field count: got {} but max is {}",
19 encoded.len(),
20 target_length
21 )));
22 }
23 encoded.resize(target_length, Fr::zero());
24 Ok(encoded)
25}
26
27pub fn buffer_from_fields(fields: &[Fr]) -> Result<Vec<u8>, Error> {
32 if fields.is_empty() {
33 return Err(Error::Abi("empty field array".to_owned()));
34 }
35 let length = fields[0].to_usize();
36 let mut result = Vec::with_capacity(length);
37 for field in &fields[1..] {
38 let bytes = field.to_be_bytes();
39 result.extend_from_slice(&bytes[1..]);
40 }
41 result.truncate(length);
42 Ok(result)
43}