Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

protect unsafety in plainhasher; get more unique hashes #1841

Merged
merged 1 commit into from
Aug 5, 2016
Merged
Changes from all commits
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
28 changes: 22 additions & 6 deletions util/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,27 +543,37 @@ impl_hash!(H2048, 256);
// Specialized HashMap and HashSet

/// Hasher that just takes 8 bytes of the provided value.
pub struct PlainHasher(u64);
/// May only be used for keys which are 32 bytes.
pub struct PlainHasher {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe should be #[repr(C)] but probably doesn't have to be.

prefix: [u8; 8],
_marker: [u64; 0], // for alignment
}

impl Default for PlainHasher {
#[inline]
fn default() -> PlainHasher {
PlainHasher(0)
PlainHasher {
prefix: [0; 8],
_marker: [0; 0],
}
}
}

impl Hasher for PlainHasher {
#[inline]
fn finish(&self) -> u64 {
self.0
unsafe { ::std::mem::transmute(self.prefix) }
}

#[inline]
fn write(&mut self, bytes: &[u8]) {
debug_assert!(bytes.len() == 32);
let mut prefix = [0u8; 8];
prefix.clone_from_slice(&bytes[0..8]);
self.0 = unsafe { ::std::mem::transmute(prefix) };

for quarter in bytes.chunks(8) {
for (x, y) in self.prefix.iter_mut().zip(quarter) {
*x ^= *y
}
}
}
}

Expand All @@ -578,6 +588,12 @@ mod tests {
use bigint::uint::*;
use std::str::FromStr;

#[test]
fn hasher_alignment() {
use std::mem::align_of;
assert_eq!(align_of::<u64>(), align_of::<PlainHasher>());
}

#[test]
#[cfg_attr(feature="dev", allow(eq_op))]
fn hash() {
Expand Down