use crate::engines::EngineBLS;
use ark_ff::PrimeField;
use ark_serialize::CanonicalSerialize;
use ark_std::vec::Vec;
use sha2::Digest;
pub fn sha256(b: &[u8]) -> Vec<u8> {
let mut hasher = sha2::Sha256::new();
hasher.update(b);
hasher.finalize().to_vec()
}
#[inline(always)]
pub fn cross_product_const<const N: usize>(a: &[u8], b: &[u8]) -> [u8; N] {
let mut result = [0u8; N];
let chunks = N / 8;
let remainder = N % 8;
for i in 0..chunks {
let start_idx = i * 8;
let end_idx = start_idx + 8;
let a_chunk = u64::from_ne_bytes(a[start_idx..end_idx].try_into().unwrap());
let b_chunk = u64::from_ne_bytes(b[start_idx..end_idx].try_into().unwrap());
let result_chunk = (a_chunk ^ b_chunk).to_ne_bytes();
result[start_idx..end_idx].copy_from_slice(&result_chunk);
}
let remainder_start = chunks * 8;
for j in 0..remainder {
result[remainder_start + j] = a[remainder_start + j] ^ b[remainder_start + j];
}
result
}
pub fn h2<G: CanonicalSerialize>(g: G) -> Vec<u8> {
let mut out = Vec::new();
g.serialize_compressed(&mut out)
.expect("Enough space has been allocated in the buffer");
sha256(&out)
}
pub fn h3<E: EngineBLS>(a: &[u8], b: &[u8]) -> E::Scalar {
let mut input = Vec::new();
input.extend_from_slice(a);
input.extend_from_slice(b);
let hash = sha256(&input);
E::Scalar::from_be_bytes_mod_order(&hash)
}
pub fn h4(a: &[u8]) -> Vec<u8> {
let o = sha256(a);
o[..a.len()].to_vec()
}
#[cfg(test)]
mod test {
use alloc::vec;
#[test]
fn utils_can_calc_sha256() {
let actual = crate::ibe::utils::sha256(b"test");
let expected = vec![
159, 134, 208, 129, 136, 76, 125, 101, 154, 47, 234, 160, 197, 90, 208, 21, 163, 191,
79, 27, 43, 11, 130, 44, 209, 93, 108, 21, 176, 240, 10, 8,
];
assert_eq!(actual, expected);
}
}