hash-chain-sonobe/src/utils.rs
src/utils.rsRaw
1#[cfg(test)]
2pub(crate) mod tests {
3 use ark_ff::{BigInteger, BigInteger256, PrimeField};
4 use folding_schemes::Error;
5
6 /// interprets the vector of finite field elements as a vector of bytes
7 pub(crate) fn f_vec_to_bytes<F: PrimeField>(b: Vec<F>) -> Vec<u8> {
8 b.iter()
9 .map(|e| {
10 let bytes: Vec<u8> = e.into_bigint().to_bytes_le();
11 bytes[0]
12 })
13 .collect()
14 }
15 /// for a given byte array, returns the bytes representation in finite field elements
16 pub(crate) fn bytes_to_f_vec<F: PrimeField>(b: Vec<u8>) -> Result<Vec<F>, Error> {
17 Ok(b.iter()
18 .map(|&e| F::from_le_bytes_mod_order(&[e]))
19 .collect::<Vec<F>>())
20 }
21 /// returns the bytes representation of the given vector of finite field elements that represent
22 /// bits
23 pub(crate) fn f_vec_bits_to_bytes<F: PrimeField>(v: Vec<F>) -> Vec<u8> {
24 let b = f_vec_to_bits(v);
25 BigInteger256::from_bits_le(&b).to_bytes_le()
26 }
27 /// for a given byte array, returns its bits representation in finite field elements
28 pub(crate) fn bytes_to_f_vec_bits<F: PrimeField>(b: Vec<u8>) -> Result<Vec<F>, Error> {
29 use num_bigint::BigUint;
30 let bi = BigUint::from_bytes_le(&b);
31 let bi = BigInteger256::try_from(bi).unwrap();
32 let bits = bi.to_bits_le();
33 Ok(bits
34 .iter()
35 .map(|&e| if e { F::one() } else { F::zero() })
36 .collect())
37 }
38 /// interprets the given vector of finite field elements as a vector of bits
39 pub(crate) fn f_vec_to_bits<F: PrimeField>(v: Vec<F>) -> Vec<bool> {
40 v.iter()
41 .map(|v_i| {
42 if v_i.is_one() {
43 return true;
44 }
45 false
46 })
47 .collect()
48 }
49}