]> git.lizzy.rs Git - rust.git/blob - src/libcore/hash/sip.rs
Auto merge of #22517 - brson:relnotes, r=Gankro
[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 // ignore-lexer-test FIXME #15883
12
13 //! An implementation of SipHash 2-4.
14
15 use prelude::*;
16 use default::Default;
17
18 use super::{Hasher, Writer};
19
20 /// An implementation of SipHash 2-4.
21 ///
22 /// See: http://131002.net/siphash/
23 ///
24 /// Consider this as a main "general-purpose" hash for all hashtables: it
25 /// runs at good speed (competitive with spooky and city) and permits
26 /// strong _keyed_ hashing. Key your hashtables from a strong RNG,
27 /// such as `rand::Rng`.
28 ///
29 /// Although the SipHash algorithm is considered to be cryptographically
30 /// strong, this implementation has not been reviewed for such purposes.
31 /// As such, all cryptographic uses of this implementation are strongly
32 /// discouraged.
33 pub struct SipHasher {
34     k0: u64,
35     k1: u64,
36     length: uint, // how many bytes we've processed
37     v0: u64,      // hash state
38     v1: u64,
39     v2: u64,
40     v3: u64,
41     tail: u64, // unprocessed bytes le
42     ntail: uint,  // how many bytes in tail are valid
43 }
44
45 // sadly, these macro definitions can't appear later,
46 // because they're needed in the following defs;
47 // this design could be improved.
48
49 macro_rules! u8to64_le {
50     ($buf:expr, $i:expr) =>
51     ($buf[0+$i] as u64 |
52      ($buf[1+$i] as u64) << 8 |
53      ($buf[2+$i] as u64) << 16 |
54      ($buf[3+$i] as u64) << 24 |
55      ($buf[4+$i] as u64) << 32 |
56      ($buf[5+$i] as u64) << 40 |
57      ($buf[6+$i] as u64) << 48 |
58      ($buf[7+$i] as u64) << 56);
59     ($buf:expr, $i:expr, $len:expr) =>
60     ({
61         let mut t = 0;
62         let mut out = 0u64;
63         while t < $len {
64             out |= ($buf[t+$i] as u64) << t*8;
65             t += 1;
66         }
67         out
68     });
69 }
70
71 macro_rules! rotl {
72     ($x:expr, $b:expr) =>
73     (($x << $b) | ($x >> (64 - $b)))
74 }
75
76 macro_rules! compress {
77     ($v0:expr, $v1:expr, $v2:expr, $v3:expr) =>
78     ({
79         $v0 += $v1; $v1 = rotl!($v1, 13); $v1 ^= $v0;
80         $v0 = rotl!($v0, 32);
81         $v2 += $v3; $v3 = rotl!($v3, 16); $v3 ^= $v2;
82         $v0 += $v3; $v3 = rotl!($v3, 21); $v3 ^= $v0;
83         $v2 += $v1; $v1 = rotl!($v1, 17); $v1 ^= $v2;
84         $v2 = rotl!($v2, 32);
85     })
86 }
87
88 impl SipHasher {
89     /// Creates a new `SipHasher` with the two initial keys set to 0.
90     #[inline]
91     pub fn new() -> SipHasher {
92         SipHasher::new_with_keys(0, 0)
93     }
94
95     /// Creates a `SipHasher` that is keyed off the provided keys.
96     #[inline]
97     pub fn new_with_keys(key0: u64, key1: u64) -> SipHasher {
98         let mut state = SipHasher {
99             k0: key0,
100             k1: key1,
101             length: 0,
102             v0: 0,
103             v1: 0,
104             v2: 0,
105             v3: 0,
106             tail: 0,
107             ntail: 0,
108         };
109         state.reset();
110         state
111     }
112
113     /// Returns the computed hash.
114     #[unstable(feature = "hash")]
115     #[deprecated(since = "1.0.0", reason = "renamed to finish")]
116     pub fn result(&self) -> u64 { self.finish() }
117 }
118
119 impl Writer for SipHasher {
120     #[inline]
121     fn write(&mut self, msg: &[u8]) {
122         let length = msg.len();
123         self.length += length;
124
125         let mut needed = 0;
126
127         if self.ntail != 0 {
128             needed = 8 - self.ntail;
129             if length < needed {
130                 self.tail |= u8to64_le!(msg, 0, length) << 8*self.ntail;
131                 self.ntail += length;
132                 return
133             }
134
135             let m = self.tail | u8to64_le!(msg, 0, needed) << 8*self.ntail;
136
137             self.v3 ^= m;
138             compress!(self.v0, self.v1, self.v2, self.v3);
139             compress!(self.v0, self.v1, self.v2, self.v3);
140             self.v0 ^= m;
141
142             self.ntail = 0;
143         }
144
145         // Buffered tail is now flushed, process new input.
146         let len = length - needed;
147         let end = len & (!0x7);
148         let left = len & 0x7;
149
150         let mut i = needed;
151         while i < end {
152             let mi = u8to64_le!(msg, i);
153
154             self.v3 ^= mi;
155             compress!(self.v0, self.v1, self.v2, self.v3);
156             compress!(self.v0, self.v1, self.v2, self.v3);
157             self.v0 ^= mi;
158
159             i += 8;
160         }
161
162         self.tail = u8to64_le!(msg, i, left);
163         self.ntail = left;
164     }
165 }
166
167 impl Hasher for SipHasher {
168     type Output = u64;
169
170     fn reset(&mut self) {
171         self.length = 0;
172         self.v0 = self.k0 ^ 0x736f6d6570736575;
173         self.v1 = self.k1 ^ 0x646f72616e646f6d;
174         self.v2 = self.k0 ^ 0x6c7967656e657261;
175         self.v3 = self.k1 ^ 0x7465646279746573;
176         self.ntail = 0;
177     }
178
179     fn finish(&self) -> u64 {
180         let mut v0 = self.v0;
181         let mut v1 = self.v1;
182         let mut v2 = self.v2;
183         let mut v3 = self.v3;
184
185         let b: u64 = ((self.length as u64 & 0xff) << 56) | self.tail;
186
187         v3 ^= b;
188         compress!(v0, v1, v2, v3);
189         compress!(v0, v1, v2, v3);
190         v0 ^= b;
191
192         v2 ^= 0xff;
193         compress!(v0, v1, v2, v3);
194         compress!(v0, v1, v2, v3);
195         compress!(v0, v1, v2, v3);
196         compress!(v0, v1, v2, v3);
197
198         v0 ^ v1 ^ v2 ^ v3
199     }
200 }
201
202 impl Clone for SipHasher {
203     #[inline]
204     fn clone(&self) -> SipHasher {
205         SipHasher {
206             k0: self.k0,
207             k1: self.k1,
208             length: self.length,
209             v0: self.v0,
210             v1: self.v1,
211             v2: self.v2,
212             v3: self.v3,
213             tail: self.tail,
214             ntail: self.ntail,
215         }
216     }
217 }
218
219 impl Default for SipHasher {
220     fn default() -> SipHasher {
221         SipHasher::new()
222     }
223 }