const SIPHASH_BLOCK_BITS: u64 = 6;
const SIPHASH_BLOCK_SIZE: u64 = 1 << SIPHASH_BLOCK_BITS;
const SIPHASH_BLOCK_MASK: u64 = SIPHASH_BLOCK_SIZE - 1;
macro_rules! rotl {
($num:expr, $shift:expr) => {
$num = ($num << $shift) | ($num >> (64 - $shift));
};
}
pub fn siphash24(v: &[u64; 4], nonce: u64) -> u64 {
let mut siphash = SipHash24::new(v);
siphash.hash(nonce, 21); siphash.digest()
}
pub fn siphash_block(v: &[u64; 4], nonce: u64, rot_e: u8, xor_all: bool) -> u64 {
let nonce0 = nonce & !SIPHASH_BLOCK_MASK;
let nonce_i = nonce & SIPHASH_BLOCK_MASK;
let mut nonce_hash = vec![0u64; SIPHASH_BLOCK_SIZE as usize];
let mut siphash = SipHash24::new(v);
for i in 0..SIPHASH_BLOCK_SIZE {
siphash.hash(nonce0 + i, rot_e);
nonce_hash[i as usize] = siphash.digest();
}
let mut xor: u64 = nonce_hash[nonce_i as usize];
let xor_from = if xor_all || nonce_i == SIPHASH_BLOCK_MASK {
nonce_i + 1
} else {
SIPHASH_BLOCK_MASK
};
for i in xor_from..SIPHASH_BLOCK_SIZE {
xor ^= nonce_hash[i as usize];
}
xor
}
pub struct SipHash24(u64, u64, u64, u64);
impl SipHash24 {
pub fn new(v: &[u64; 4]) -> SipHash24 {
SipHash24(v[0], v[1], v[2], v[3])
}
pub fn hash(&mut self, nonce: u64, rot_e: u8) {
self.3 ^= nonce;
self.round(rot_e);
self.round(rot_e);
self.0 ^= nonce;
self.2 ^= 0xff;
for _ in 0..4 {
self.round(rot_e);
}
}
pub fn digest(&self) -> u64 {
(self.0 ^ self.1) ^ (self.2 ^ self.3)
}
fn round(&mut self, rot_e: u8) {
self.0 = self.0.wrapping_add(self.1);
self.2 = self.2.wrapping_add(self.3);
rotl!(self.1, 13);
rotl!(self.3, 16);
self.1 ^= self.0;
self.3 ^= self.2;
rotl!(self.0, 32);
self.2 = self.2.wrapping_add(self.1);
self.0 = self.0.wrapping_add(self.3);
rotl!(self.1, 17);
rotl!(self.3, rot_e);
self.1 ^= self.2;
self.3 ^= self.0;
rotl!(self.2, 32);
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn hash_some() {
assert_eq!(siphash24(&[1, 2, 3, 4], 10), 928382149599306901);
assert_eq!(siphash24(&[1, 2, 3, 4], 111), 10524991083049122233);
assert_eq!(siphash24(&[9, 7, 6, 7], 12), 1305683875471634734);
assert_eq!(siphash24(&[9, 7, 6, 7], 10), 11589833042187638814);
}
#[test]
fn hash_block() {
assert_eq!(
siphash_block(&[1, 2, 3, 4], 10, 21, false),
1182162244994096396
);
assert_eq!(
siphash_block(&[1, 2, 3, 4], 123, 21, false),
11303676240481718781
);
assert_eq!(
siphash_block(&[9, 7, 6, 7], 12, 21, false),
4886136884237259030
);
}
}