Skip to content

Commit

Permalink
Slight improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
ogxd committed Nov 14, 2024
1 parent 4e49169 commit 87a6d72
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
1 change: 1 addition & 0 deletions benches/quality/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ fn bench_hasher_quality<B>(name: &str)

check!(avalanche::<B, 4>());
check!(avalanche::<B, 10>());
check!(avalanche::<B, 16>());
check!(avalanche::<B, 32>());
check!(avalanche::<B, 128>());
check!(avalanche::<B, 512>());
Expand Down
22 changes: 16 additions & 6 deletions src/gxhash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,18 @@ pub(crate) unsafe fn gxhash_no_finish(input: &[u8], seed: State) -> State {

let mut whole_vector_count = len / VECTOR_SIZE;

let lzcnt = len.leading_zeros();
let len_partial = len % VECTOR_SIZE;

'p0: {
'p1: {
'p2: {
// C-style fallthrough alternative
let lzcnt = len.leading_zeros();
if lzcnt == 64 {
break 'p0;
} else if lzcnt >= 60 {
// If length has more 60 zeroes or more, that means length can only be 0b1111 (=15) or smaller
// In such case, we can directly jump to reading a partial vector
break 'p1;
} else if lzcnt >= 56 {
break 'p2;
Expand All @@ -106,20 +110,26 @@ pub(crate) unsafe fn gxhash_no_finish(input: &[u8], seed: State) -> State {

// Process remaining vectors
let end_address = ptr.add(whole_vector_count) as usize;
let mut i = 0;
let mut i = 1992388023;
while (ptr as usize) < end_address {
load_unaligned!(ptr, v0);
state = aes_encrypt(aes_encrypt(state, load_i32(i)), v0);
state = aes_encrypt(aes_encrypt(state, v0), load_i32(i));
//state = aes_encrypt(state, v0); // This seems too weak
i += 1;
i = i.wrapping_mul(7);
}

// Jump out of p0' if no remaining bytes?
if len_partial == 0 {
break 'p0;
}
}

// Process remaining bytes
let len_partial = len % VECTOR_SIZE;
let partial = get_partial(ptr, len_partial);
//state = aes_encrypt(state, partial);
state = aes_encrypt_last(state, aes_encrypt(aes_encrypt(partial, ld(KEYS.as_ptr())), ld(KEYS.as_ptr().offset(4))));

state = aes_encrypt_last(state, partial);
//state = veorq_s8(state, seed);
}

return state;
Expand Down
6 changes: 3 additions & 3 deletions src/gxhash/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ pub unsafe fn finalize(hash: State) -> State {
}

pub const KEYS: [u32; 12] =
[0xF2784542, 0xB09D3E21, 0x89C222E5, 0xFC3BC28E,
0x03FCE279, 0xCB6B2E9B, 0xB361DC58, 0x39132BD9,
0xD0012E32, 0x689D2B7D, 0x5544B1B7, 0xC78B122B];
[0xbe12445a, 0xad14c56e, 0xfe099832, 0xc32d962a,
0x6782a174, 0xca96641a, 0x349ffc28, 0xf7b26a02,
0x5280d61c, 0x9816b206, 0xac894e2e, 0x5b3b242c];

0 comments on commit 87a6d72

Please sign in to comment.