Skip to content

Commit

Permalink
Specialize randomstate (Requires version bump) (#218)
Browse files Browse the repository at this point in the history
* Make RandomState typed to the type to be hashed
* Add test from #213
* All smhasher tests pass

Signed-off-by: Tom Kaitchuck <[email protected]>
  • Loading branch information
tkaitchuck authored Mar 3, 2024
1 parent 7778357 commit 8c3f257
Show file tree
Hide file tree
Showing 13 changed files with 260 additions and 283 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ahash"
version = "0.8.10"
version = "0.9.0"
authors = ["Tom Kaitchuck <[email protected]>"]
license = "MIT OR Apache-2.0"
description = "A non-cryptographic hash function using AES-NI for high performance"
Expand All @@ -12,7 +12,7 @@ edition = "2018"
readme = "README.md"
build = "./build.rs"
exclude = ["/smhasher", "/benchmark_tools"]
rust-version = "1.60.0"
rust-version = "1.72.0"

[lib]
name = "ahash"
Expand Down
2 changes: 1 addition & 1 deletion smhasher/ahash-cbindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ use std::hash::{BuildHasher};
#[no_mangle]
pub extern "C" fn ahash64(buf: *const (), len: usize, seed: u64) -> u64 {
let buf: &[u8] = unsafe { slice::from_raw_parts(buf as *const u8, len) };
let build_hasher = RandomState::with_seeds(seed, seed, seed, seed);
let build_hasher = RandomState::<&[u8]>::with_seeds(seed, seed, seed, seed);
build_hasher.hash_one(&buf)
}
5 changes: 3 additions & 2 deletions src/aes_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl AHasher {
/// println!("Hash is {:x}!", hasher.finish());
/// ```
#[inline]
#[cfg(test)]
pub(crate) fn new_with_keys(key1: u128, key2: u128) -> Self {
let pi: [u128; 2] = PI.convert();
let key1 = key1 ^ pi[0];
Expand All @@ -69,7 +70,7 @@ impl AHasher {
}

#[inline]
pub(crate) fn from_random_state(rand_state: &RandomState) -> Self {
pub(crate) fn from_random_state<T>(rand_state: &RandomState<T>) -> Self {
let key1 = [rand_state.k0, rand_state.k1].convert();
let key2 = [rand_state.k2, rand_state.k3].convert();
Self {
Expand Down Expand Up @@ -368,7 +369,7 @@ mod tests {
use std::hash::{BuildHasher, Hasher};
#[test]
fn test_sanity() {
let mut hasher = RandomState::with_seeds(1, 2, 3, 4).build_hasher();
let mut hasher = RandomState::<()>::with_seeds(1, 2, 3, 4).build_hasher();
hasher.write_u64(0);
let h1 = hasher.finish();
hasher.write(&[1, 0, 0, 0, 0, 0, 0, 0]);
Expand Down
1 change: 1 addition & 0 deletions src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ macro_rules! as_array {
}};
}

#[allow(dead_code)]
pub(crate) trait ReadFromSlice {
fn read_u16(&self) -> (u16, &[u8]);
fn read_u32(&self) -> (u32, &[u8]);
Expand Down
18 changes: 9 additions & 9 deletions src/fallback_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct AHasher {
impl AHasher {
/// Creates a new hasher keyed to the provided key.
#[inline]
#[allow(dead_code)] // Is not called if non-fallback hash is used.
#[cfg(test)]
pub(crate) fn new_with_keys(key1: u128, key2: u128) -> AHasher {
let pi: [u128; 2] = PI.convert();
let key1: [u64; 2] = (key1 ^ pi[0]).convert();
Expand All @@ -54,7 +54,7 @@ impl AHasher {

#[inline]
#[allow(dead_code)] // Is not called if non-fallback hash is used.
pub(crate) fn from_random_state(rand_state: &RandomState) -> AHasher {
pub(crate) fn from_random_state<T>(rand_state: &RandomState<T>) -> AHasher {
AHasher {
buffer: rand_state.k1,
pad: rand_state.k0,
Expand All @@ -72,7 +72,7 @@ impl AHasher {
///
/// This version avoids this vulnerability while still only using a single multiply. It takes advantage
/// of the fact that when a 64 bit multiply is performed the upper 64 bits are usually computed and thrown
/// away. Instead it creates two 128 bit values where the upper 64 bits are zeros and multiplies them.
/// away. Instead, it creates two 128 bit values where the upper 64 bits are zeros and multiplies them.
/// (The compiler is smart enough to turn this into a 64 bit multiplication in the assembly)
/// Then the upper bits are xored with the lower bits to produce a single 64 bit result.
///
Expand All @@ -85,24 +85,24 @@ impl AHasher {
/// This is impervious to attack because every bit buffer at the end is dependent on every bit in
/// `new_data ^ buffer`. For example suppose two inputs differed in only the 5th bit. Then when the
/// multiplication is performed the `result` will differ in bits 5-69. More specifically it will differ by
/// 2^5 * MULTIPLE. However in the next step bits 65-128 are turned into a separate 64 bit value. So the
/// 2^5 * MULTIPLE. However, in the next step bits 65-128 are turned into a separate 64 bit value. So the
/// differing bits will be in the lower 6 bits of this value. The two intermediate values that differ in
/// bits 5-63 and in bits 0-5 respectively get added together. Producing an output that differs in every
/// bit. The addition carries in the multiplication and at the end additionally mean that the even if an
/// attacker somehow knew part of (but not all) the contents of the buffer before hand,
/// bit. The addition carries in the multiplication and at the end additionally mean that even if an
/// attacker somehow knew part of (but not all) the contents of the buffer beforehand,
/// they would not be able to predict any of the bits in the buffer at the end.
#[inline(always)]
fn update(&mut self, new_data: u64) {
self.buffer = folded_multiply(new_data ^ self.buffer, MULTIPLE);
}

/// Similar to the above this function performs an update using a "folded multiply".
/// However it takes in 128 bits of data instead of 64. Both halves must be masked.
/// However, it takes in 128 bits of data instead of 64. Both halves must be masked.
///
/// This makes it impossible for an attacker to place a single bit difference between
/// two blocks so as to cancel each other.
///
/// However this is not sufficient. to prevent (a,b) from hashing the same as (b,a) the buffer itself must
/// However, this is not sufficient. to prevent (a,b) from hashing the same as (b,a) the buffer itself must
/// be updated between calls in a way that does not commute. To achieve this XOR and Rotate are used.
/// Add followed by xor is not the same as xor followed by add, and rotate ensures that the same out bits
/// can't be changed by the same set of input bits. To cancel this sequence with subsequent input would require
Expand Down Expand Up @@ -142,7 +142,7 @@ impl Hasher for AHasher {

#[inline]
fn write_u64(&mut self, i: u64) {
self.update(i as u64);
self.update(i);
}

#[inline]
Expand Down
24 changes: 15 additions & 9 deletions src/hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ use crate::RandomState;
/// A [`HashMap`](std::collections::HashMap) using [`RandomState`](crate::RandomState) to hash the items.
/// (Requires the `std` feature to be enabled.)
#[derive(Clone)]
pub struct AHashMap<K, V, S = crate::RandomState>(HashMap<K, V, S>);
pub struct AHashMap<K, V, S = crate::RandomState<K>>(HashMap<K, V, S>);

impl<K, V> From<HashMap<K, V, crate::RandomState>> for AHashMap<K, V> {
fn from(item: HashMap<K, V, crate::RandomState>) -> Self {
impl<K, V> From<HashMap<K, V, crate::RandomState<K>>> for AHashMap<K, V> {
fn from(item: HashMap<K, V, crate::RandomState<K>>) -> Self {
AHashMap(item)
}
}
Expand All @@ -44,21 +44,25 @@ where
}
}

impl<K, V> Into<HashMap<K, V, crate::RandomState>> for AHashMap<K, V> {
fn into(self) -> HashMap<K, V, crate::RandomState> {
impl<K, V> Into<HashMap<K, V, crate::RandomState<K>>> for AHashMap<K, V> {
fn into(self) -> HashMap<K, V, crate::RandomState<K>> {
self.0
}
}

impl<K, V> AHashMap<K, V, RandomState> {
impl<K, V> AHashMap<K, V, RandomState<K>> {
/// This crates a hashmap using [RandomState::new] which obtains its keys from [RandomSource].
/// See the documentation in [RandomSource] for notes about key strength.
///
/// [RandomSource]: crate::random_state::RandomSource
pub fn new() -> Self {
AHashMap(HashMap::with_hasher(RandomState::new()))
}

/// This crates a hashmap with the specified capacity using [RandomState::new].
/// See the documentation in [RandomSource] for notes about key strength.
///
/// [RandomSource]: crate::random_state::RandomSource
pub fn with_capacity(capacity: usize) -> Self {
AHashMap(HashMap::with_capacity_and_hasher(capacity, RandomState::new()))
}
Expand Down Expand Up @@ -344,12 +348,14 @@ where
}
}

impl<K, V> FromIterator<(K, V)> for AHashMap<K, V, RandomState>
impl<K, V> FromIterator<(K, V)> for AHashMap<K, V, RandomState<K>>
where
K: Eq + Hash,
{
/// This crates a hashmap from the provided iterator using [RandomState::new].
/// See the documentation in [RandomSource] for notes about key strength.
///
/// [RandomSource]: crate::random_state::RandomSource
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
let mut inner = HashMap::with_hasher(RandomState::new());
inner.extend(iter);
Expand Down Expand Up @@ -408,9 +414,9 @@ where
/// `compile-time-rng` are enabled. This is to prevent weakly keyed maps from being accidentally created. Instead one of
/// constructors for [RandomState] must be used.
#[cfg(any(feature = "compile-time-rng", feature = "runtime-rng", feature = "no-rng"))]
impl<K, V> Default for AHashMap<K, V, RandomState> {
impl<K, V> Default for AHashMap<K, V, RandomState<K>> {
#[inline]
fn default() -> AHashMap<K, V, RandomState> {
fn default() -> AHashMap<K, V, RandomState<K>> {
AHashMap(HashMap::default())
}
}
Expand Down
24 changes: 15 additions & 9 deletions src/hash_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ use serde::{
/// A [`HashSet`](std::collections::HashSet) using [`RandomState`](crate::RandomState) to hash the items.
/// (Requires the `std` feature to be enabled.)
#[derive(Clone)]
pub struct AHashSet<T, S = RandomState>(HashSet<T, S>);
pub struct AHashSet<T, S = RandomState<T>>(HashSet<T, S>);

impl<T> From<HashSet<T, RandomState>> for AHashSet<T> {
fn from(item: HashSet<T, RandomState>) -> Self {
impl<T> From<HashSet<T, RandomState<T>>> for AHashSet<T> {
fn from(item: HashSet<T, RandomState<T>>) -> Self {
AHashSet(item)
}
}
Expand All @@ -40,21 +40,25 @@ where
}
}

impl<T> Into<HashSet<T, RandomState>> for AHashSet<T> {
fn into(self) -> HashSet<T, RandomState> {
impl<T> Into<HashSet<T, RandomState<T>>> for AHashSet<T> {
fn into(self) -> HashSet<T, RandomState<T>> {
self.0
}
}

impl<T> AHashSet<T, RandomState> {
impl<T> AHashSet<T, RandomState<T>> {
/// This crates a hashset using [RandomState::new].
/// See the documentation in [RandomSource] for notes about key strength.
///
/// [RandomSource]: crate::random_state::RandomSource
pub fn new() -> Self {
AHashSet(HashSet::with_hasher(RandomState::new()))
}

/// This crates a hashset with the specified capacity using [RandomState::new].
/// See the documentation in [RandomSource] for notes about key strength.
///
/// [RandomSource]: crate::random_state::RandomSource
pub fn with_capacity(capacity: usize) -> Self {
AHashSet(HashSet::with_capacity_and_hasher(capacity, RandomState::new()))
}
Expand Down Expand Up @@ -241,12 +245,14 @@ where
}
}

impl<T> FromIterator<T> for AHashSet<T, RandomState>
impl<T> FromIterator<T> for AHashSet<T, RandomState<T>>
where
T: Eq + Hash,
{
/// This crates a hashset from the provided iterator using [RandomState::new].
/// See the documentation in [RandomSource] for notes about key strength.
///
/// [RandomSource]: crate::random_state::RandomSource
#[inline]
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> AHashSet<T> {
let mut inner = HashSet::with_hasher(RandomState::new());
Expand Down Expand Up @@ -297,10 +303,10 @@ where
/// `compile-time-rng` are enabled. This is to prevent weakly keyed maps from being accidentally created. Instead one of
/// constructors for [RandomState] must be used.
#[cfg(any(feature = "compile-time-rng", feature = "runtime-rng", feature = "no-rng"))]
impl<T> Default for AHashSet<T, RandomState> {
impl<T> Default for AHashSet<T, RandomState<T>> {
/// Creates an empty `AHashSet<T, S>` with the `Default` value for the hasher.
#[inline]
fn default() -> AHashSet<T, RandomState> {
fn default() -> AHashSet<T, RandomState<T>> {
AHashSet(HashSet::default())
}
}
Expand Down
Loading

0 comments on commit 8c3f257

Please sign in to comment.