use crate::crypto::sha256_avx2;
use sha2::{Digest, Sha256};
#[cfg(target_arch = "x86_64")]
pub fn batch_sha256_avx2(inputs: &[&[u8]]) -> Vec<[u8; 32]> {
if inputs.is_empty() {
return Vec::new();
}
if !is_x86_feature_detected!("avx2") {
return inputs
.iter()
.map(|input| {
let hash = Sha256::digest(input);
let mut result = [0u8; 32];
result.copy_from_slice(&hash);
result
})
.collect();
}
let mut results = Vec::with_capacity(inputs.len());
let chunks = inputs.chunks_exact(8);
let remainder = chunks.remainder();
for chunk in chunks {
let chunk_array: [&[u8]; 8] = [
chunk[0], chunk[1], chunk[2], chunk[3], chunk[4], chunk[5], chunk[6], chunk[7],
];
unsafe {
let avx2_results = sha256_avx2::sha256_8way_avx2(&chunk_array);
results.extend_from_slice(&avx2_results);
}
}
for input in remainder {
let hash = Sha256::digest(input);
let mut result = [0u8; 32];
result.copy_from_slice(&hash);
results.push(result);
}
results
}
#[cfg(not(target_arch = "x86_64"))]
pub fn batch_sha256_avx2(inputs: &[&[u8]]) -> Vec<[u8; 32]> {
inputs
.iter()
.map(|input| {
let hash = Sha256::digest(input);
let mut result = [0u8; 32];
result.copy_from_slice(&hash);
result
})
.collect()
}