]> git.lizzy.rs Git - rust.git/blob - src/libcore/hash/sip.rs
Auto merge of #45359 - arielb1:escaping-borrow, r=eddyb
[rust.git] / src / libcore / hash / sip.rs
1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! An implementation of SipHash.
12
13 #![allow(deprecated)]
14
15 use marker::PhantomData;
16 use ptr;
17 use cmp;
18 use mem;
19
20 /// An implementation of SipHash 1-3.
21 ///
22 /// This is currently the default hashing function used by standard library
23 /// (eg. `collections::HashMap` uses it by default).
24 ///
25 /// See: https://131002.net/siphash/
26 #[unstable(feature = "sip_hash_13", issue = "34767")]
27 #[rustc_deprecated(since = "1.13.0",
28                    reason = "use `std::collections::hash_map::DefaultHasher` instead")]
29 #[derive(Debug, Clone, Default)]
30 pub struct SipHasher13 {
31     hasher: Hasher<Sip13Rounds>,
32 }
33
34 /// An implementation of SipHash 2-4.
35 ///
36 /// See: https://131002.net/siphash/
37 #[unstable(feature = "sip_hash_13", issue = "34767")]
38 #[rustc_deprecated(since = "1.13.0",
39                    reason = "use `std::collections::hash_map::DefaultHasher` instead")]
40 #[derive(Debug, Clone, Default)]
41 pub struct SipHasher24 {
42     hasher: Hasher<Sip24Rounds>,
43 }
44
45 /// An implementation of SipHash 2-4.
46 ///
47 /// See: https://131002.net/siphash/
48 ///
49 /// SipHash is a general-purpose hashing function: it runs at a good
50 /// speed (competitive with Spooky and City) and permits strong _keyed_
51 /// hashing. This lets you key your hashtables from a strong RNG, such as
52 /// [`rand::os::OsRng`](https://doc.rust-lang.org/rand/rand/os/struct.OsRng.html).
53 ///
54 /// Although the SipHash algorithm is considered to be generally strong,
55 /// it is not intended for cryptographic purposes. As such, all
56 /// cryptographic uses of this implementation are _strongly discouraged_.
57 #[stable(feature = "rust1", since = "1.0.0")]
58 #[rustc_deprecated(since = "1.13.0",
59                    reason = "use `std::collections::hash_map::DefaultHasher` instead")]
60 #[derive(Debug, Clone, Default)]
61 pub struct SipHasher(SipHasher24);
62
63 #[derive(Debug)]
64 struct Hasher<S: Sip> {
65     k0: u64,
66     k1: u64,
67     length: usize, // how many bytes we've processed
68     state: State, // hash State
69     tail: u64, // unprocessed bytes le
70     ntail: usize, // how many bytes in tail are valid
71     _marker: PhantomData<S>,
72 }
73
74 #[derive(Debug, Clone, Copy)]
75 #[repr(C)]
76 struct State {
77     // v0, v2 and v1, v3 show up in pairs in the algorithm,
78     // and simd implementations of SipHash will use vectors
79     // of v02 and v13. By placing them in this order in the struct,
80     // the compiler can pick up on just a few simd optimizations by itself.
81     v0: u64,
82     v2: u64,
83     v1: u64,
84     v3: u64,
85 }
86
87 macro_rules! compress {
88     ($state:expr) => ({
89         compress!($state.v0, $state.v1, $state.v2, $state.v3)
90     });
91     ($v0:expr, $v1:expr, $v2:expr, $v3:expr) =>
92     ({
93         $v0 = $v0.wrapping_add($v1); $v1 = $v1.rotate_left(13); $v1 ^= $v0;
94         $v0 = $v0.rotate_left(32);
95         $v2 = $v2.wrapping_add($v3); $v3 = $v3.rotate_left(16); $v3 ^= $v2;
96         $v0 = $v0.wrapping_add($v3); $v3 = $v3.rotate_left(21); $v3 ^= $v0;
97         $v2 = $v2.wrapping_add($v1); $v1 = $v1.rotate_left(17); $v1 ^= $v2;
98         $v2 = $v2.rotate_left(32);
99     });
100 }
101
102 /// Load an integer of the desired type from a byte stream, in LE order. Uses
103 /// `copy_nonoverlapping` to let the compiler generate the most efficient way
104 /// to load it from a possibly unaligned address.
105 ///
106 /// Unsafe because: unchecked indexing at i..i+size_of(int_ty)
107 macro_rules! load_int_le {
108     ($buf:expr, $i:expr, $int_ty:ident) =>
109     ({
110        debug_assert!($i + mem::size_of::<$int_ty>() <= $buf.len());
111        let mut data = 0 as $int_ty;
112        ptr::copy_nonoverlapping($buf.get_unchecked($i),
113                                 &mut data as *mut _ as *mut u8,
114                                 mem::size_of::<$int_ty>());
115        data.to_le()
116     });
117 }
118
119 /// Load an u64 using up to 7 bytes of a byte slice.
120 ///
121 /// Unsafe because: unchecked indexing at start..start+len
122 #[inline]
123 unsafe fn u8to64_le(buf: &[u8], start: usize, len: usize) -> u64 {
124     debug_assert!(len < 8);
125     let mut i = 0; // current byte index (from LSB) in the output u64
126     let mut out = 0;
127     if i + 3 < len {
128         out = load_int_le!(buf, start + i, u32) as u64;
129         i += 4;
130     }
131     if i + 1 < len {
132         out |= (load_int_le!(buf, start + i, u16) as u64) << (i * 8);
133         i += 2
134     }
135     if i < len {
136         out |= (*buf.get_unchecked(start + i) as u64) << (i * 8);
137         i += 1;
138     }
139     debug_assert_eq!(i, len);
140     out
141 }
142
143 impl SipHasher {
144     /// Creates a new `SipHasher` with the two initial keys set to 0.
145     #[inline]
146     #[stable(feature = "rust1", since = "1.0.0")]
147     #[rustc_deprecated(since = "1.13.0",
148                        reason = "use `std::collections::hash_map::DefaultHasher` instead")]
149     pub fn new() -> SipHasher {
150         SipHasher::new_with_keys(0, 0)
151     }
152
153     /// Creates a `SipHasher` that is keyed off the provided keys.
154     #[inline]
155     #[stable(feature = "rust1", since = "1.0.0")]
156     #[rustc_deprecated(since = "1.13.0",
157                        reason = "use `std::collections::hash_map::DefaultHasher` instead")]
158     pub fn new_with_keys(key0: u64, key1: u64) -> SipHasher {
159         SipHasher(SipHasher24::new_with_keys(key0, key1))
160     }
161 }
162
163 impl SipHasher13 {
164     /// Creates a new `SipHasher13` with the two initial keys set to 0.
165     #[inline]
166     #[unstable(feature = "sip_hash_13", issue = "34767")]
167     #[rustc_deprecated(since = "1.13.0",
168                        reason = "use `std::collections::hash_map::DefaultHasher` instead")]
169     pub fn new() -> SipHasher13 {
170         SipHasher13::new_with_keys(0, 0)
171     }
172
173     /// Creates a `SipHasher13` that is keyed off the provided keys.
174     #[inline]
175     #[unstable(feature = "sip_hash_13", issue = "34767")]
176     #[rustc_deprecated(since = "1.13.0",
177                        reason = "use `std::collections::hash_map::DefaultHasher` instead")]
178     pub fn new_with_keys(key0: u64, key1: u64) -> SipHasher13 {
179         SipHasher13 {
180             hasher: Hasher::new_with_keys(key0, key1)
181         }
182     }
183 }
184
185 impl SipHasher24 {
186     /// Creates a new `SipHasher24` with the two initial keys set to 0.
187     #[inline]
188     #[unstable(feature = "sip_hash_13", issue = "34767")]
189     #[rustc_deprecated(since = "1.13.0",
190                        reason = "use `std::collections::hash_map::DefaultHasher` instead")]
191     pub fn new() -> SipHasher24 {
192         SipHasher24::new_with_keys(0, 0)
193     }
194
195     /// Creates a `SipHasher24` that is keyed off the provided keys.
196     #[inline]
197     #[unstable(feature = "sip_hash_13", issue = "34767")]
198     #[rustc_deprecated(since = "1.13.0",
199                        reason = "use `std::collections::hash_map::DefaultHasher` instead")]
200     pub fn new_with_keys(key0: u64, key1: u64) -> SipHasher24 {
201         SipHasher24 {
202             hasher: Hasher::new_with_keys(key0, key1)
203         }
204     }
205 }
206
207 impl<S: Sip> Hasher<S> {
208     #[inline]
209     fn new_with_keys(key0: u64, key1: u64) -> Hasher<S> {
210         let mut state = Hasher {
211             k0: key0,
212             k1: key1,
213             length: 0,
214             state: State {
215                 v0: 0,
216                 v1: 0,
217                 v2: 0,
218                 v3: 0,
219             },
220             tail: 0,
221             ntail: 0,
222             _marker: PhantomData,
223         };
224         state.reset();
225         state
226     }
227
228     #[inline]
229     fn reset(&mut self) {
230         self.length = 0;
231         self.state.v0 = self.k0 ^ 0x736f6d6570736575;
232         self.state.v1 = self.k1 ^ 0x646f72616e646f6d;
233         self.state.v2 = self.k0 ^ 0x6c7967656e657261;
234         self.state.v3 = self.k1 ^ 0x7465646279746573;
235         self.ntail = 0;
236     }
237
238     // Specialized write function that is only valid for buffers with len <= 8.
239     // It's used to force inlining of write_u8 and write_usize, those would normally be inlined
240     // except for composite types (that includes slices and str hashing because of delimiter).
241     // Without this extra push the compiler is very reluctant to inline delimiter writes,
242     // degrading performance substantially for the most common use cases.
243     #[inline]
244     fn short_write(&mut self, msg: &[u8]) {
245         debug_assert!(msg.len() <= 8);
246         let length = msg.len();
247         self.length += length;
248
249         let needed = 8 - self.ntail;
250         let fill = cmp::min(length, needed);
251         if fill == 8 {
252             self.tail = unsafe { load_int_le!(msg, 0, u64) };
253         } else {
254             self.tail |= unsafe { u8to64_le(msg, 0, fill) } << (8 * self.ntail);
255             if length < needed {
256                 self.ntail += length;
257                 return;
258             }
259         }
260         self.state.v3 ^= self.tail;
261         S::c_rounds(&mut self.state);
262         self.state.v0 ^= self.tail;
263
264         // Buffered tail is now flushed, process new input.
265         self.ntail = length - needed;
266         self.tail = unsafe { u8to64_le(msg, needed, self.ntail) };
267     }
268 }
269
270 #[stable(feature = "rust1", since = "1.0.0")]
271 impl super::Hasher for SipHasher {
272     #[inline]
273     fn write(&mut self, msg: &[u8]) {
274         self.0.write(msg)
275     }
276
277     #[inline]
278     fn finish(&self) -> u64 {
279         self.0.finish()
280     }
281 }
282
283 #[unstable(feature = "sip_hash_13", issue = "34767")]
284 impl super::Hasher for SipHasher13 {
285     #[inline]
286     fn write(&mut self, msg: &[u8]) {
287         self.hasher.write(msg)
288     }
289
290     #[inline]
291     fn finish(&self) -> u64 {
292         self.hasher.finish()
293     }
294 }
295
296 #[unstable(feature = "sip_hash_13", issue = "34767")]
297 impl super::Hasher for SipHasher24 {
298     #[inline]
299     fn write(&mut self, msg: &[u8]) {
300         self.hasher.write(msg)
301     }
302
303     #[inline]
304     fn finish(&self) -> u64 {
305         self.hasher.finish()
306     }
307 }
308
309 impl<S: Sip> super::Hasher for Hasher<S> {
310     // see short_write comment for explanation
311     #[inline]
312     fn write_usize(&mut self, i: usize) {
313         let bytes = unsafe {
314             ::slice::from_raw_parts(&i as *const usize as *const u8, mem::size_of::<usize>())
315         };
316         self.short_write(bytes);
317     }
318
319     // see short_write comment for explanation
320     #[inline]
321     fn write_u8(&mut self, i: u8) {
322         self.short_write(&[i]);
323     }
324
325     #[inline]
326     fn write(&mut self, msg: &[u8]) {
327         let length = msg.len();
328         self.length += length;
329
330         let mut needed = 0;
331
332         if self.ntail != 0 {
333             needed = 8 - self.ntail;
334             self.tail |= unsafe { u8to64_le(msg, 0, cmp::min(length, needed)) } << 8 * self.ntail;
335             if length < needed {
336                 self.ntail += length;
337                 return
338             } else {
339                 self.state.v3 ^= self.tail;
340                 S::c_rounds(&mut self.state);
341                 self.state.v0 ^= self.tail;
342                 self.ntail = 0;
343             }
344         }
345
346         // Buffered tail is now flushed, process new input.
347         let len = length - needed;
348         let left = len & 0x7;
349
350         let mut i = needed;
351         while i < len - left {
352             let mi = unsafe { load_int_le!(msg, i, u64) };
353
354             self.state.v3 ^= mi;
355             S::c_rounds(&mut self.state);
356             self.state.v0 ^= mi;
357
358             i += 8;
359         }
360
361         self.tail = unsafe { u8to64_le(msg, i, left) };
362         self.ntail = left;
363     }
364
365     #[inline]
366     fn finish(&self) -> u64 {
367         let mut state = self.state;
368
369         let b: u64 = ((self.length as u64 & 0xff) << 56) | self.tail;
370
371         state.v3 ^= b;
372         S::c_rounds(&mut state);
373         state.v0 ^= b;
374
375         state.v2 ^= 0xff;
376         S::d_rounds(&mut state);
377
378         state.v0 ^ state.v1 ^ state.v2 ^ state.v3
379     }
380 }
381
382 impl<S: Sip> Clone for Hasher<S> {
383     #[inline]
384     fn clone(&self) -> Hasher<S> {
385         Hasher {
386             k0: self.k0,
387             k1: self.k1,
388             length: self.length,
389             state: self.state,
390             tail: self.tail,
391             ntail: self.ntail,
392             _marker: self._marker,
393         }
394     }
395 }
396
397 impl<S: Sip> Default for Hasher<S> {
398     /// Creates a `Hasher<S>` with the two initial keys set to 0.
399     #[inline]
400     fn default() -> Hasher<S> {
401         Hasher::new_with_keys(0, 0)
402     }
403 }
404
405 #[doc(hidden)]
406 trait Sip {
407     fn c_rounds(_: &mut State);
408     fn d_rounds(_: &mut State);
409 }
410
411 #[derive(Debug, Clone, Default)]
412 struct Sip13Rounds;
413
414 impl Sip for Sip13Rounds {
415     #[inline]
416     fn c_rounds(state: &mut State) {
417         compress!(state);
418     }
419
420     #[inline]
421     fn d_rounds(state: &mut State) {
422         compress!(state);
423         compress!(state);
424         compress!(state);
425     }
426 }
427
428 #[derive(Debug, Clone, Default)]
429 struct Sip24Rounds;
430
431 impl Sip for Sip24Rounds {
432     #[inline]
433     fn c_rounds(state: &mut State) {
434         compress!(state);
435         compress!(state);
436     }
437
438     #[inline]
439     fn d_rounds(state: &mut State) {
440         compress!(state);
441         compress!(state);
442         compress!(state);
443         compress!(state);
444     }
445 }