Skip to content

Commit

Permalink
Prevent right padded zeros from triggering collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
ogxd committed Oct 13, 2023
1 parent e1c9484 commit f1451b0
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/gxhash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ mod platform_defs {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ];

let mask = vld1q_s8((MASK.as_ptr() as *const i8).offset(size_of::<state>() as isize - len));
vandq_s8(load_unaligned(p), mask)
let mut vec = vandq_s8(load_unaligned(p), mask);

// To avoid collisions for zero right padded inputs, we mutate this vector using its used length
vec = vaddq_s8(vec, vdupq_n_s8(len as i8));

return vec;
}

#[inline]
Expand Down Expand Up @@ -256,6 +261,7 @@ pub fn gxhash(input: &[u8]) -> u32 {
mod tests {

use super::*;
use rand::Rng;

#[test]
fn all_blocks_are_consumed() {
Expand All @@ -273,6 +279,22 @@ mod tests {
}
}

#[test]
fn add_zeroes_mutates_hash() {
let mut bytes = [0u8; 1200];

let mut rng = rand::thread_rng();
rng.fill(&mut bytes[..32]);

let mut ref_hash = 0;

for i in 32..100 {
let new_hash = gxhash(&mut bytes[..i]);
assert_ne!(ref_hash, new_hash, "Same hash at size {i} ({new_hash})");
ref_hash = new_hash;
}
}

#[test]
fn hash_of_zero_is_not_zero() {
assert_ne!(0, gxhash(&[0u8; 0]));
Expand Down

0 comments on commit f1451b0

Please sign in to comment.