use crate::primitives::{AesBlock, Hash, HashKey};
use aes::cipher::{generic_array::GenericArray, BlockEncrypt, KeyInit};
use aes::Aes128Enc;
use zeroize::ZeroizeOnDrop;
#[derive(ZeroizeOnDrop)]
pub struct Aes128Z2Hash {
cipher: Aes128Enc,
}
impl Hash for Aes128Z2Hash {
fn new(key: &HashKey) -> Self {
let key_array = GenericArray::from_slice(key);
let cipher = Aes128Enc::new(key_array);
Self { cipher }
}
fn hash(&self, data: &[u8; 16]) -> u8 {
let mut block = AesBlock::from(*data);
self.cipher.encrypt_block(&mut block);
block[0] & 1u8
}
fn hash_all_into<const D: usize, const B: usize>(
&self,
data: &mut [AesBlock; D],
out: &mut [u8; B],
) {
self.cipher.encrypt_blocks(data);
crate::primitives::simd::lsb_mask(data, out);
}
}
pub struct FixedPiZ2Hash {
nonce: AesBlock,
}
pub const PI_KEY: [u8; 16] = *b"ORE-rs.v2.H-pi.1";
fn pi() -> &'static Aes128Enc {
use std::sync::OnceLock;
static PI: OnceLock<Aes128Enc> = OnceLock::new();
PI.get_or_init(|| Aes128Enc::new(GenericArray::from_slice(&PI_KEY)))
}
#[inline]
pub(crate) fn gf128_double_u128(x: u128) -> u128 {
(x << 1) ^ ((x >> 127) * 0x87)
}
#[inline]
fn gf128_double(b: &mut [u8; 16]) {
*b = gf128_double_u128(u128::from_be_bytes(*b)).to_be_bytes();
}
impl Hash for FixedPiZ2Hash {
fn new(nonce: &HashKey) -> Self {
Self { nonce: *nonce }
}
fn hash(&self, data: &[u8; 16]) -> u8 {
let mut block = *data;
gf128_double(&mut block); for (slot, &r) in block.iter_mut().zip(self.nonce.iter()) {
*slot ^= r; }
let m_lsb = block[0] & 1u8;
let mut block = AesBlock::from(block);
pi().encrypt_block(&mut block);
(block[0] & 1u8) ^ m_lsb
}
fn hash_all_into<const D: usize, const B: usize>(
&self,
data: &mut [AesBlock; D],
out: &mut [u8; B],
) {
let nonce = u128::from_be_bytes(self.nonce.into());
for block in data.iter_mut() {
let m = gf128_double_u128(u128::from_be_bytes((*block).into())) ^ nonce;
*block = m.to_be_bytes().into();
}
crate::primitives::simd::lsb_mask(data, out);
pi().encrypt_blocks(data);
let mut pi_mask = [0u8; B];
crate::primitives::simd::lsb_mask(data, &mut pi_mask);
for (slot, &m) in out.iter_mut().zip(pi_mask.iter()) {
*slot ^= m;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use hex_literal::hex;
fn init_hash() -> Aes128Z2Hash {
let key: [u8; 16] = hex!("00010203 04050607 08090a0b 0c0d0e0f");
let key_array = GenericArray::from_slice(&key);
Hash::new(key_array)
}
#[test]
fn hash_test_1() {
let hash = init_hash();
let input: [u8; 16] = hex!("00010203 04050607 08090a0b 0c0d0eaa");
assert_eq!(1u8, hash.hash(&input));
}
#[test]
fn hash_test_2() {
let hash = init_hash();
let input: [u8; 16] = hex!("00010203 04050607 08090a0b 0c0d0e0f");
assert_eq!(0u8, hash.hash(&input));
}
#[test]
fn fixed_pi_scalar_matches_bulk() {
let nonce: [u8; 16] = hex!("0f0e0d0c 0b0a0908 07060504 03020100");
let h: FixedPiZ2Hash = Hash::new(GenericArray::from_slice(&nonce));
check::<64, 8>(&h);
check::<256, 32>(&h);
fn check<const D: usize, const B: usize>(h: &FixedPiZ2Hash) {
let mut blocks = [AesBlock::default(); D];
for (i, block) in blocks.iter_mut().enumerate() {
for (j, slot) in block.iter_mut().enumerate() {
*slot = (i.wrapping_mul(31).wrapping_add(j)) as u8;
}
}
let mut expected = [0u8; B];
for (i, b) in blocks.iter().enumerate() {
expected[i / 8] |= h.hash(&(*b).into()) << (i % 8);
}
let mut out = [0u8; B];
h.hash_all_into(&mut blocks, &mut out);
assert_eq!(out, expected, "scalar vs bulk mismatch for n={D}");
}
}
#[test]
fn gf128_double_reduction() {
let mut b = [0u8; 16];
b[15] = 0x01;
gf128_double(&mut b);
let mut want = [0u8; 16];
want[15] = 0x02;
assert_eq!(b, want);
let mut b = [0u8; 16];
b[0] = 0x80;
gf128_double(&mut b);
let mut want = [0u8; 16];
want[15] = 0x87;
assert_eq!(b, want);
}
}
#[cfg(kani)]
mod kani_proofs {
use super::*;
fn dbl_reference(b: [u8; 16]) -> [u8; 16] {
let mut out = [0u8; 16];
for k in 0..16 {
let next_msb = if k + 1 < 16 { b[k + 1] >> 7 } else { 0 };
out[k] = (b[k] << 1) | next_msb;
}
if b[0] & 0x80 != 0 {
out[15] ^= 0x87;
}
out
}
#[kani::proof]
#[kani::unwind(17)]
fn gf128_double_matches_reference() {
let b: [u8; 16] = kani::any();
let expected = dbl_reference(b);
assert_eq!(
gf128_double_u128(u128::from_be_bytes(b)).to_be_bytes(),
expected
);
let mut in_place = b;
gf128_double(&mut in_place);
assert_eq!(in_place, expected);
}
}