pub const KECCAK_ROUNDS: usize = 24;
pub(crate) const RC: [u64; KECCAK_ROUNDS] = [
0x0000_0000_0000_0001,
0x0000_0000_0000_8082,
0x8000_0000_0000_808A,
0x8000_0000_8000_8000,
0x0000_0000_0000_808B,
0x0000_0000_8000_0001,
0x8000_0000_8000_8081,
0x8000_0000_0000_8009,
0x0000_0000_0000_008A,
0x0000_0000_0000_0088,
0x0000_0000_8000_8009,
0x0000_0000_8000_000A,
0x0000_0000_8000_808B,
0x8000_0000_0000_008B,
0x8000_0000_0000_8089,
0x8000_0000_0000_8003,
0x8000_0000_0000_8002,
0x8000_0000_0000_0080,
0x0000_0000_0000_800A,
0x8000_0000_8000_000A,
0x8000_0000_8000_8081,
0x8000_0000_0000_8080,
0x0000_0000_8000_0001,
0x8000_0000_8000_8008,
];
pub(crate) const RHO: [u32; 25] = [
0, 1, 62, 28, 27, 36, 44, 6, 55, 20, 3, 10, 43, 25, 39, 41, 45, 15, 21, 8, 18, 2, 61, 56, 14,
];
pub(crate) const PI: [usize; 25] = [
0, 6, 12, 18, 24, 3, 9, 10, 16, 22, 1, 7, 13, 19, 20, 4, 5, 11, 17, 23, 2, 8, 14, 15, 21,
];
#[cfg(all(not(feature = "kernel"), target_arch = "x86_64"))]
#[inline]
pub(crate) const fn round_constants() -> [u64; KECCAK_ROUNDS] {
RC
}
#[inline]
pub fn keccak_f1600(state: &mut [u64; 25]) {
for round in RC.iter().take(KECCAK_ROUNDS) {
let c0 = state[0] ^ state[5] ^ state[10] ^ state[15] ^ state[20];
let c1 = state[1] ^ state[6] ^ state[11] ^ state[16] ^ state[21];
let c2 = state[2] ^ state[7] ^ state[12] ^ state[17] ^ state[22];
let c3 = state[3] ^ state[8] ^ state[13] ^ state[18] ^ state[23];
let c4 = state[4] ^ state[9] ^ state[14] ^ state[19] ^ state[24];
let d0 = c4 ^ c1.rotate_left(1);
let d1 = c0 ^ c2.rotate_left(1);
let d2 = c1 ^ c3.rotate_left(1);
let d3 = c2 ^ c4.rotate_left(1);
let d4 = c3 ^ c0.rotate_left(1);
state[0] ^= d0;
state[5] ^= d0;
state[10] ^= d0;
state[15] ^= d0;
state[20] ^= d0;
state[1] ^= d1;
state[6] ^= d1;
state[11] ^= d1;
state[16] ^= d1;
state[21] ^= d1;
state[2] ^= d2;
state[7] ^= d2;
state[12] ^= d2;
state[17] ^= d2;
state[22] ^= d2;
state[3] ^= d3;
state[8] ^= d3;
state[13] ^= d3;
state[18] ^= d3;
state[23] ^= d3;
state[4] ^= d4;
state[9] ^= d4;
state[14] ^= d4;
state[19] ^= d4;
state[24] ^= d4;
let mut b = [0u64; 25];
for i in 0..25 {
b[i] = state[PI[i]].rotate_left(RHO[PI[i]]);
}
for y in 0..5 {
let base = 5 * y;
state[base] = b[base] ^ ((!b[base + 1]) & b[base + 2]);
state[base + 1] = b[base + 1] ^ ((!b[base + 2]) & b[base + 3]);
state[base + 2] = b[base + 2] ^ ((!b[base + 3]) & b[base + 4]);
state[base + 3] = b[base + 3] ^ ((!b[base + 4]) & b[base]);
state[base + 4] = b[base + 4] ^ ((!b[base]) & b[base + 1]);
}
state[0] ^= *round;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_keccak_f1600_zero_state() {
let mut state = [0u64; 25];
keccak_f1600(&mut state);
assert_ne!(state[0], 0, "ι step must inject round constants");
assert_ne!(state, [0u64; 25], "Permutation must modify state");
}
#[test]
fn test_keccak_f1600_deterministic() {
let mut s1 = [0u64; 25];
let mut s2 = [0u64; 25];
for i in 0..25 {
s1[i] = i as u64 * 0x0123_4567_89AB_CDEF;
s2[i] = s1[i];
}
keccak_f1600(&mut s1);
keccak_f1600(&mut s2);
assert_eq!(s1, s2);
}
#[test]
fn test_keccak_f1600_different_inputs() {
let mut s1 = [0u64; 25];
let mut s2 = [0u64; 25];
s2[0] = 1;
keccak_f1600(&mut s1);
keccak_f1600(&mut s2);
assert_ne!(s1, s2, "Single-bit input difference must avalanche");
}
#[test]
fn test_keccak_f1600_known_answer() {
let mut state = [0u64; 25];
keccak_f1600(&mut state);
assert_eq!(
state[0], 0xF1258F7940E1DDE7,
"Lane 0 must match XKCP reference"
);
assert_eq!(
state[1], 0x84D5CCF933C0478A,
"Lane 1 must match XKCP reference"
);
assert_eq!(
state[2], 0xD598261EA65AA9EE,
"Lane 2 must match XKCP reference"
);
assert_eq!(
state[3], 0xBD1547306F80494D,
"Lane 3 must match XKCP reference"
);
assert_eq!(
state[4], 0x8B284E056253D057,
"Lane 4 must match XKCP reference"
);
}
}