Skip to content

Commit

Permalink
Introduce new-type for the hash of SipHasher128(Hash)
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau authored and michaelwoerister committed Jul 9, 2024
1 parent f30cf8e commit 5185cef
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased

- Use new-type for returned-hash of `SipHasher128`(`Hash`) (#8)
- Introduce multi hasher support (#8)
- `StableHasher::finish` now returns a small hash instead of being fatal (#6)
- Remove `StableHasher::finalize` (#4)
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod stable_hasher;
/// Hashers collection
pub mod hashers {
#[doc(inline)]
pub use super::sip128::SipHasher128;
pub use super::sip128::{SipHasher128, SipHasher128Hash};

/// Stable 128-bits Sip Hasher
///
Expand All @@ -29,4 +29,4 @@ pub use stable_hasher::StableHasherResult;
pub use stable_hasher::ExtendedHasher;

#[doc(inline)]
pub use hashers::StableSipHasher128;
pub use hashers::{SipHasher128Hash, StableSipHasher128};
12 changes: 8 additions & 4 deletions src/sip128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ const BUFFER_WITH_SPILL_SIZE: usize = BUFFER_WITH_SPILL_CAPACITY * ELEM_SIZE;
// Index of the spill element in the buffer.
const BUFFER_SPILL_INDEX: usize = BUFFER_WITH_SPILL_CAPACITY - 1;

/// Hashing result of [`SipHasher128`]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SipHasher128Hash(pub [u64; 2]);

#[derive(Debug, Clone)]
#[repr(C)]
pub struct SipHasher128 {
Expand Down Expand Up @@ -421,7 +425,7 @@ impl Default for SipHasher128 {
}

impl ExtendedHasher for SipHasher128 {
type Hash = [u64; 2];
type Hash = SipHasher128Hash;

#[inline]
fn short_write<const LEN: usize>(&mut self, bytes: [u8; LEN]) {
Expand All @@ -446,10 +450,10 @@ impl ExtendedHasher for SipHasher128 {
}

#[inline(always)]
fn finish(mut self) -> [u64; 2] {
unsafe {
fn finish(mut self) -> SipHasher128Hash {
SipHasher128Hash(unsafe {
SipHasher128::finish128_inner(self.nbuf, &mut self.buf, self.state, self.processed)
}
})
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/sip128/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ impl<'a> Hash for Bytes<'a> {
}
}

fn hash_with<T: Hash>(mut st: SipHasher128, x: &T) -> [u64; 2] {
fn hash_with<T: Hash>(mut st: SipHasher128, x: &T) -> SipHasher128Hash {
x.hash(&mut st);
st.finish()
}

fn hash<T: Hash>(x: &T) -> [u64; 2] {
fn hash<T: Hash>(x: &T) -> SipHasher128Hash {
hash_with(SipHasher128::new_with_keys(0, 0), x)
}

Expand Down Expand Up @@ -119,7 +119,7 @@ fn test_siphash_1_3_test_vector() {
| ((TEST_VECTOR[i][15] as u64) << 56),
];

assert_eq!(out, expected);
assert_eq!(out.0, expected);
input.push(i as u8);
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/stable_hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,15 @@ pub trait ExtendedHasher: Hasher {
/// # Example
///
/// ```
/// use rustc_stable_hash::{StableHasher, StableHasherResult, StableSipHasher128};
/// use rustc_stable_hash::hashers::{StableSipHasher128, SipHasher128Hash};
/// use rustc_stable_hash::{StableHasher, StableHasherResult};
/// use std::hash::Hasher;
///
/// struct Hash128([u64; 2]);
/// impl StableHasherResult for Hash128 {
/// type Hash = [u64; 2];
/// type Hash = SipHasher128Hash;
///
/// fn finish(hash: [u64; 2]) -> Hash128 {
/// fn finish(SipHasher128Hash(hash): SipHasher128Hash) -> Hash128 {
/// Hash128(hash)
/// }
/// }
Expand Down Expand Up @@ -106,7 +107,7 @@ pub struct StableHasher<H: ExtendedHasher> {
/// fn finish(hash: [u64; 2]) -> Hash128 {
/// let upper: u128 = hash[0] as u128;
/// let lower: u128 = hash[1] as u128;
///
///
/// Hash128((upper << 64) | lower)
/// }
/// }
Expand Down
6 changes: 3 additions & 3 deletions src/stable_hasher/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::hash::Hash;

use super::*;
use crate::StableSipHasher128;
use crate::{SipHasher128Hash, StableSipHasher128};

// The tests below compare the computed hashes to particular expected values
// in order to test that we produce the same results on different platforms,
Expand All @@ -14,9 +14,9 @@ use crate::StableSipHasher128;
struct TestHash([u64; 2]);

impl StableHasherResult for TestHash {
type Hash = [u64; 2];
type Hash = SipHasher128Hash;

fn finish(hash: Self::Hash) -> TestHash {
fn finish(SipHasher128Hash(hash): Self::Hash) -> TestHash {
TestHash(hash)
}
}
Expand Down

0 comments on commit 5185cef

Please sign in to comment.