| 1 |
/// Backend implementation using the portable SIMD API. |
| 2 |
use super::soft::{LaneSize, keccak_p}; |
| 3 |
use crate::types::{Fn1600, ParFn1600}; |
| 4 |
use core::array; |
| 5 |
use hybrid_array::{Array, typenum}; |
| 6 |
|
| 7 |
#[cfg(keccak_backend = "simd128")] |
| 8 |
use core::simd::u64x2 as u64xN; |
| 9 |
#[cfg(keccak_backend = "simd256")] |
| 10 |
use core::simd::u64x4 as u64xN; |
| 11 |
#[cfg(keccak_backend = "simd512")] |
| 12 |
use core::simd::u64x8 as u64xN; |
| 13 |
|
| 14 |
impl LaneSize for u64xN { |
| 15 |
const RC: &[Self] = &{ |
| 16 |
use crate::consts::{F1600_ROUNDS, RC}; |
| 17 |
|
| 18 |
let mut res = [Self::splat(0); F1600_ROUNDS]; |
| 19 |
let mut i = 0; |
| 20 |
#[allow(clippy::cast_possible_truncation, trivial_numeric_casts)] |
| 21 |
while i < res.len() { |
| 22 |
res[i] = Self::splat(RC[i]); |
| 23 |
i += 1; |
| 24 |
} |
| 25 |
res |
| 26 |
}; |
| 27 |
|
| 28 |
fn rotate_left(self, n: u32) -> Self { |
| 29 |
self << Self::splat(n.into()) | self >> Self::splat((64 - n).into()) |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
/// Portable SIMD backend |
| 34 |
pub(crate) struct Backend; |
| 35 |
|
| 36 |
impl super::Backend for Backend { |
| 37 |
#[cfg(all(feature = "parallel", keccak_backend = "simd128"))] |
| 38 |
type ParSize1600 = typenum::U2; |
| 39 |
#[cfg(all(feature = "parallel", keccak_backend = "simd256"))] |
| 40 |
type ParSize1600 = typenum::U4; |
| 41 |
#[cfg(all(feature = "parallel", keccak_backend = "simd512"))] |
| 42 |
type ParSize1600 = typenum::U8; |
| 43 |
|
| 44 |
#[inline] |
| 45 |
fn get_p1600<const ROUNDS: usize>() -> Fn1600 { |
| 46 |
keccak_p::<u64, ROUNDS> |
| 47 |
} |
| 48 |
|
| 49 |
#[cfg(feature = "parallel")] |
| 50 |
#[inline] |
| 51 |
fn get_par_p1600<const ROUNDS: usize>() -> ParFn1600<Self> { |
| 52 |
|Array(state)| { |
| 53 |
let mut simd_state = array::from_fn(|i| { |
| 54 |
let t = array::from_fn(|j| state[j][i]); |
| 55 |
u64xN::from_array(t) |
| 56 |
}); |
| 57 |
keccak_p::<_, ROUNDS>(&mut simd_state); |
| 58 |
for i in 0..simd_state.len() { |
| 59 |
let s = simd_state[i].to_array(); |
| 60 |
for j in 0..s.len() { |
| 61 |
state[j][i] = s[j]; |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
} |