Skip to content

Commit

Permalink
Fix permutation quality issue (#48)
Browse files Browse the repository at this point in the history
* Fix permutation quality issue

* Add more quality tests

---------

Co-authored-by: Olivier Giniaux <[email protected]>
  • Loading branch information
ogxd and ogxd committed Dec 23, 2023
1 parent 1e21d4b commit 1da7ad6
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "gxhash"
authors = ["Olivier Giniaux"]
version = "2.3.0"
version = "2.3.1"
edition = "2021"
description = "GxHash non-cryptographic algorithm"
license = "MIT"
Expand Down
5 changes: 4 additions & 1 deletion benches/quality/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{hash::{Hash, Hasher, BuildHasher}, iter};
use std::hash::{Hash, Hasher, BuildHasher};
use rand::Rng;
use criterion::black_box;

Expand Down Expand Up @@ -60,11 +60,14 @@ fn bench_hasher_quality<B>(name: &str)
check!(collisions_flipped_n_bits::<B, 256>(2));

check!(collisions_powerset_bytes::<B>(&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]));
check!(collisions_powerset_bytes::<B>(&[0, 1, 2, 4, 8, 16, 32, 64, 128]));

check!(collisions_permuted_hasher_values::<B, u8>(&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]));
check!(collisions_permuted_hasher_values::<B, u32>(&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]));
check!(collisions_permuted_hasher_values::<B, u32>(&[0, 1, 2, 4, 8, 16, 32, 64, 128, 256]));

check!(collisions_powerset_hasher_values::<B, u32>(&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]));
check!(collisions_powerset_hasher_values::<B, u32>(&[0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384]));
}

fn collisions_permuted_hasher_values<B, D>(data: &[impl Hash]) -> f64
Expand Down
10 changes: 10 additions & 0 deletions src/gxhash/platform/arm_128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ pub unsafe fn compress_fast(a: int8x16_t, b: int8x16_t) -> int8x16_t {
vreinterpretq_s8_u8(aes_encrypt(vreinterpretq_u8_s8(a), vreinterpretq_u8_s8(b)))
}

#[inline(always)]
pub unsafe fn compress_1(a: int8x16_t, b: int8x16_t) -> int8x16_t {
let keys_1 = vld1q_u32([0xFC3BC28E, 0x89C222E5, 0xB09D3E21, 0xF2784542].as_ptr());

let mut bs = vreinterpretq_u8_s8(b);
bs = aes_encrypt(bs, vreinterpretq_u8_u32(keys_1));

vreinterpretq_s8_u8(aes_encrypt_last(vreinterpretq_u8_s8(a), bs))
}

#[inline(always)]
// See https://blog.michaelbrase.com/2018/05/08/emulating-x86-aes-intrinsics-on-armv8-a
unsafe fn aes_encrypt(data: uint8x16_t, keys: uint8x16_t) -> uint8x16_t {
Expand Down
9 changes: 9 additions & 0 deletions src/gxhash/platform/x86_128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ pub unsafe fn compress_fast(a: State, b: State) -> State {
_mm_aesenc_si128(a, b)
}

#[inline(always)]
#[allow(overflowing_literals)]
pub unsafe fn compress_1(a: State, b: State) -> State {
let keys_1 = _mm_set_epi32(0xF2784542, 0xB09D3E21, 0x89C222E5, 0xFC3BC28E);

let mut b = _mm_aesenc_si128(b, keys_1);
_mm_aesenclast_si128(a, b)
}

#[inline(always)]
#[allow(overflowing_literals)]
pub unsafe fn finalize(hash: State) -> State {
Expand Down
9 changes: 9 additions & 0 deletions src/gxhash/platform/x86_256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ pub unsafe fn compress_fast(a: State, b: State) -> State {
return _mm256_aesenc_epi128(a, b);
}

#[inline(always)]
#[allow(overflowing_literals)]
pub unsafe fn compress_1(a: State, b: State) -> State {
let keys_1 = _mm256_set_epi32(0xFC3BC28E, 0x89C222E5, 0xB09D3E21, 0xF2784542, 0x4155EE07, 0xC897CCE2, 0x780AF2C3, 0x8A72B781);

let mut b = _mm256_aesenc_epi128(b, keys_1);
return _mm256_aesenclast_epi128(a, b);
}

#[inline(always)]
#[allow(overflowing_literals)]
pub unsafe fn finalize(hash: State) -> State {
Expand Down
4 changes: 2 additions & 2 deletions src/hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ macro_rules! write {
#[inline]
fn $name(&mut self, value: $type) {
self.state = unsafe {
compress_fast($load(value), self.state)
compress_1($load(value), self.state)
};
}
}
Expand All @@ -115,7 +115,7 @@ impl Hasher for GxHasher {

#[inline]
fn write(&mut self, bytes: &[u8]) {
self.state = unsafe { compress_fast(compress_all(bytes), self.state) };
self.state = unsafe { compress_1(compress_all(bytes), self.state) };
}

write!(write_u8, u8, load_u8);
Expand Down

0 comments on commit 1da7ad6

Please sign in to comment.