Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strengthen fastpath #169

Merged
merged 6 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve quality, and add additional quality test.
Signed-off-by: Tom Kaitchuck <[email protected]>
  • Loading branch information
tkaitchuck committed Oct 23, 2023
commit 5d20612ba0797d92a0911ccaff5d4742310d6ba6
15 changes: 6 additions & 9 deletions src/aes_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ impl Hasher for AHasher {
current[1] = aesdec(current[1], tail[1]);
current[2] = aesenc(current[2], tail[2]);
current[3] = aesdec(current[3], tail[3]);
let mut sum: [u128; 2] = [self.key, self.key];
let mut sum: [u128; 2] = [self.key, !self.key];
sum[0] = add_by_64s(sum[0].convert(), tail[0].convert()).convert();
sum[1] = shuffle_and_add(sum[1], tail[1]);
sum[1] = add_by_64s(sum[1].convert(), tail[1].convert()).convert();
sum[0] = shuffle_and_add(sum[0], tail[2]);
sum[1] = shuffle_and_add(sum[1], tail[3]);
while data.len() > 64 {
Expand All @@ -184,12 +184,9 @@ impl Hasher for AHasher {
sum[1] = shuffle_and_add(sum[1], blocks[3]);
data = rest;
}
self.enc = aesenc(self.enc, current[0]);
self.enc = aesenc(self.enc, current[1]);
self.enc = aesenc(self.enc, current[2]);
self.enc = aesenc(self.enc, current[3]);
self.sum = shuffle_and_add(self.sum, sum[0]);
self.sum = shuffle_and_add(self.sum, sum[1]);
self.hash_in_2(current[0], current[1]);
self.hash_in_2(current[2], current[3]);
self.hash_in_2(sum[0], sum[1]);
} else {
//len 33-64
let (head, _) = data.read_u128x2();
Expand All @@ -213,7 +210,7 @@ impl Hasher for AHasher {
fn finish(&self) -> u64 {
let combined = aesdec(self.sum, self.enc);
let result: [u64; 2] = aesenc(aesenc(combined, self.key), combined).convert();
result[0]
result[1]
}
}

Expand Down
36 changes: 35 additions & 1 deletion src/hash_quality_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::hash::{Hash, Hasher};
use std::collections::HashMap;
use std::collections::{HashMap};

fn assert_sufficiently_different(a: u64, b: u64, tolerance: i32) {
let (same_byte_count, same_nibble_count) = count_same_bytes_and_nibbles(a, b);
Expand Down Expand Up @@ -338,6 +338,28 @@ fn test_length_extension<T: Hasher>(hasher: impl Fn(u128, u128) -> T) {
}
}

fn test_sparse<T: Hasher>(hasher: impl Fn() -> T) {
let mut buf = [0u8; 256];
let mut hashes = HashMap::new();
for idx_1 in 0..256 {
for idx_2 in idx_1+1..256 {
for value_1 in [1, 2, 4, 8, 16, 32, 64, 128] {
for value_2 in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 16, 17, 18, 20, 24, 31, 32, 33, 48, 64, 96, 127, 128, 129, 192, 254, 255] {
buf[idx_1] = value_1;
buf[idx_2] = value_2;
let hash_value = hash_with(&buf, &mut hasher());
let keys = hashes.entry(hash_value).or_insert(Vec::new());
keys.push((idx_1, value_1, idx_2, value_2));
buf[idx_1] = 0;
buf[idx_2] = 0;
}
}
}
}
hashes.retain(|_key, value| value.len() != 1);
assert_eq!(0, hashes.len(), "Collision with: {:?}", hashes);
}

#[cfg(test)]
mod fallback_tests {
use crate::fallback_hash::*;
Expand Down Expand Up @@ -404,6 +426,12 @@ mod fallback_tests {
fn fallback_length_extension() {
test_length_extension(|a, b| AHasher::new_with_keys(a, b));
}

#[test]
fn test_no_sparse_collisions() {
test_sparse(|| AHasher::new_with_keys(0, 0));
test_sparse(|| AHasher::new_with_keys(1, 2));
}
}

///Basic sanity tests of the cypto properties of aHash.
Expand Down Expand Up @@ -497,4 +525,10 @@ mod aes_tests {
fn aes_length_extension() {
test_length_extension(|a, b| AHasher::test_with_keys(a, b));
}

#[test]
fn aes_no_sparse_collisions() {
test_sparse(|| AHasher::test_with_keys(0, 0));
test_sparse(|| AHasher::test_with_keys(1, 2));
}
}