1 //! An implementation of SipHash.
3 // ignore-tidy-undocumented-unsafe
5 #![allow(deprecated)] // the types in this module are deprecated
8 use crate::marker::PhantomData;
12 /// An implementation of SipHash 1-3.
14 /// This is currently the default hashing function used by standard library
15 /// (e.g., `collections::HashMap` uses it by default).
17 /// See: <https://131002.net/siphash>
18 #[unstable(feature = "hashmap_internals", issue = "none")]
21 reason = "use `std::collections::hash_map::DefaultHasher` instead"
23 #[derive(Debug, Clone, Default)]
25 pub struct SipHasher13 {
26 hasher: Hasher<Sip13Rounds>,
29 /// An implementation of SipHash 2-4.
31 /// See: <https://131002.net/siphash/>
32 #[unstable(feature = "hashmap_internals", issue = "none")]
35 reason = "use `std::collections::hash_map::DefaultHasher` instead"
37 #[derive(Debug, Clone, Default)]
39 hasher: Hasher<Sip24Rounds>,
42 /// An implementation of SipHash 2-4.
44 /// See: <https://131002.net/siphash/>
46 /// SipHash is a general-purpose hashing function: it runs at a good
47 /// speed (competitive with Spooky and City) and permits strong _keyed_
48 /// hashing. This lets you key your hashtables from a strong RNG, such as
49 /// [`rand::os::OsRng`](https://doc.rust-lang.org/rand/rand/os/struct.OsRng.html).
51 /// Although the SipHash algorithm is considered to be generally strong,
52 /// it is not intended for cryptographic purposes. As such, all
53 /// cryptographic uses of this implementation are _strongly discouraged_.
54 #[stable(feature = "rust1", since = "1.0.0")]
57 reason = "use `std::collections::hash_map::DefaultHasher` instead"
59 #[derive(Debug, Clone, Default)]
60 pub struct SipHasher(SipHasher24);
63 struct Hasher<S: Sip> {
66 length: usize, // how many bytes we've processed
67 state: State, // hash State
68 tail: u64, // unprocessed bytes le
69 ntail: usize, // how many bytes in tail are valid
70 _marker: PhantomData<S>,
73 #[derive(Debug, Clone, Copy)]
76 // v0, v2 and v1, v3 show up in pairs in the algorithm,
77 // and simd implementations of SipHash will use vectors
78 // of v02 and v13. By placing them in this order in the struct,
79 // the compiler can pick up on just a few simd optimizations by itself.
86 macro_rules! compress {
87 ($state:expr) => {{ compress!($state.v0, $state.v1, $state.v2, $state.v3) }};
88 ($v0:expr, $v1:expr, $v2:expr, $v3:expr) => {{
89 $v0 = $v0.wrapping_add($v1);
90 $v1 = $v1.rotate_left(13);
92 $v0 = $v0.rotate_left(32);
93 $v2 = $v2.wrapping_add($v3);
94 $v3 = $v3.rotate_left(16);
96 $v0 = $v0.wrapping_add($v3);
97 $v3 = $v3.rotate_left(21);
99 $v2 = $v2.wrapping_add($v1);
100 $v1 = $v1.rotate_left(17);
102 $v2 = $v2.rotate_left(32);
106 /// Loads an integer of the desired type from a byte stream, in LE order. Uses
107 /// `copy_nonoverlapping` to let the compiler generate the most efficient way
108 /// to load it from a possibly unaligned address.
110 /// Unsafe because: unchecked indexing at i..i+size_of(int_ty)
111 macro_rules! load_int_le {
112 ($buf:expr, $i:expr, $int_ty:ident) => {{
113 debug_assert!($i + mem::size_of::<$int_ty>() <= $buf.len());
114 let mut data = 0 as $int_ty;
115 ptr::copy_nonoverlapping(
116 $buf.get_unchecked($i),
117 &mut data as *mut _ as *mut u8,
118 mem::size_of::<$int_ty>(),
124 /// Loads an u64 using up to 7 bytes of a byte slice.
126 /// Unsafe because: unchecked indexing at start..start+len
128 unsafe fn u8to64_le(buf: &[u8], start: usize, len: usize) -> u64 {
129 debug_assert!(len < 8);
130 let mut i = 0; // current byte index (from LSB) in the output u64
133 out = load_int_le!(buf, start + i, u32) as u64;
137 out |= (load_int_le!(buf, start + i, u16) as u64) << (i * 8);
141 out |= (*buf.get_unchecked(start + i) as u64) << (i * 8);
144 debug_assert_eq!(i, len);
149 /// Creates a new `SipHasher` with the two initial keys set to 0.
151 #[stable(feature = "rust1", since = "1.0.0")]
154 reason = "use `std::collections::hash_map::DefaultHasher` instead"
156 pub fn new() -> SipHasher {
157 SipHasher::new_with_keys(0, 0)
160 /// Creates a `SipHasher` that is keyed off the provided keys.
162 #[stable(feature = "rust1", since = "1.0.0")]
165 reason = "use `std::collections::hash_map::DefaultHasher` instead"
167 pub fn new_with_keys(key0: u64, key1: u64) -> SipHasher {
168 SipHasher(SipHasher24 { hasher: Hasher::new_with_keys(key0, key1) })
173 /// Creates a new `SipHasher13` with the two initial keys set to 0.
175 #[unstable(feature = "hashmap_internals", issue = "none")]
178 reason = "use `std::collections::hash_map::DefaultHasher` instead"
180 pub fn new() -> SipHasher13 {
181 SipHasher13::new_with_keys(0, 0)
184 /// Creates a `SipHasher13` that is keyed off the provided keys.
186 #[unstable(feature = "hashmap_internals", issue = "none")]
189 reason = "use `std::collections::hash_map::DefaultHasher` instead"
191 pub fn new_with_keys(key0: u64, key1: u64) -> SipHasher13 {
192 SipHasher13 { hasher: Hasher::new_with_keys(key0, key1) }
196 impl<S: Sip> Hasher<S> {
198 fn new_with_keys(key0: u64, key1: u64) -> Hasher<S> {
199 let mut state = Hasher {
203 state: State { v0: 0, v1: 0, v2: 0, v3: 0 },
206 _marker: PhantomData,
213 fn reset(&mut self) {
215 self.state.v0 = self.k0 ^ 0x736f6d6570736575;
216 self.state.v1 = self.k1 ^ 0x646f72616e646f6d;
217 self.state.v2 = self.k0 ^ 0x6c7967656e657261;
218 self.state.v3 = self.k1 ^ 0x7465646279746573;
222 // Specialized write function that is only valid for buffers with len <= 8.
223 // It's used to force inlining of write_u8 and write_usize, those would normally be inlined
224 // except for composite types (that includes slices and str hashing because of delimiter).
225 // Without this extra push the compiler is very reluctant to inline delimiter writes,
226 // degrading performance substantially for the most common use cases.
228 fn short_write(&mut self, msg: &[u8]) {
229 debug_assert!(msg.len() <= 8);
230 let length = msg.len();
231 self.length += length;
233 let needed = 8 - self.ntail;
234 let fill = cmp::min(length, needed);
236 self.tail = unsafe { load_int_le!(msg, 0, u64) };
238 self.tail |= unsafe { u8to64_le(msg, 0, fill) } << (8 * self.ntail);
240 self.ntail += length;
244 self.state.v3 ^= self.tail;
245 S::c_rounds(&mut self.state);
246 self.state.v0 ^= self.tail;
248 // Buffered tail is now flushed, process new input.
249 self.ntail = length - needed;
250 self.tail = unsafe { u8to64_le(msg, needed, self.ntail) };
254 #[stable(feature = "rust1", since = "1.0.0")]
255 impl super::Hasher for SipHasher {
257 fn write(&mut self, msg: &[u8]) {
258 self.0.hasher.write(msg)
262 fn finish(&self) -> u64 {
263 self.0.hasher.finish()
267 #[unstable(feature = "hashmap_internals", issue = "none")]
268 impl super::Hasher for SipHasher13 {
270 fn write(&mut self, msg: &[u8]) {
271 self.hasher.write(msg)
275 fn finish(&self) -> u64 {
280 impl<S: Sip> super::Hasher for Hasher<S> {
281 // see short_write comment for explanation
283 fn write_usize(&mut self, i: usize) {
285 crate::slice::from_raw_parts(&i as *const usize as *const u8, mem::size_of::<usize>())
287 self.short_write(bytes);
290 // see short_write comment for explanation
292 fn write_u8(&mut self, i: u8) {
293 self.short_write(&[i]);
297 fn write(&mut self, msg: &[u8]) {
298 let length = msg.len();
299 self.length += length;
304 needed = 8 - self.ntail;
305 self.tail |= unsafe { u8to64_le(msg, 0, cmp::min(length, needed)) } << 8 * self.ntail;
307 self.ntail += length;
310 self.state.v3 ^= self.tail;
311 S::c_rounds(&mut self.state);
312 self.state.v0 ^= self.tail;
317 // Buffered tail is now flushed, process new input.
318 let len = length - needed;
319 let left = len & 0x7;
322 while i < len - left {
323 let mi = unsafe { load_int_le!(msg, i, u64) };
326 S::c_rounds(&mut self.state);
332 self.tail = unsafe { u8to64_le(msg, i, left) };
337 fn finish(&self) -> u64 {
338 let mut state = self.state;
340 let b: u64 = ((self.length as u64 & 0xff) << 56) | self.tail;
343 S::c_rounds(&mut state);
347 S::d_rounds(&mut state);
349 state.v0 ^ state.v1 ^ state.v2 ^ state.v3
353 impl<S: Sip> Clone for Hasher<S> {
355 fn clone(&self) -> Hasher<S> {
363 _marker: self._marker,
368 impl<S: Sip> Default for Hasher<S> {
369 /// Creates a `Hasher<S>` with the two initial keys set to 0.
371 fn default() -> Hasher<S> {
372 Hasher::new_with_keys(0, 0)
378 fn c_rounds(_: &mut State);
379 fn d_rounds(_: &mut State);
382 #[derive(Debug, Clone, Default)]
385 impl Sip for Sip13Rounds {
387 fn c_rounds(state: &mut State) {
392 fn d_rounds(state: &mut State) {
399 #[derive(Debug, Clone, Default)]
402 impl Sip for Sip24Rounds {
404 fn c_rounds(state: &mut State) {
410 fn d_rounds(state: &mut State) {