The Pedigree Project  0.1
SpookyV2.h
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 //
21 // SpookyHash: a 128-bit noncryptographic hash function
22 // By Bob Jenkins, public domain
23 // Oct 31 2010: alpha, framework + SpookyHash::Mix appears right
24 // Oct 31 2011: alpha again, Mix only good to 2^^69 but rest appears right
25 // Dec 31 2011: beta, improved Mix, tested it for 2-bit deltas
26 // Feb 2 2012: production, same bits as beta
27 // Feb 5 2012: adjusted definitions of uint* to be more portable
28 // Mar 30 2012: 3 bytes/cycle, not 4. Alpha was 4 but wasn't thorough enough.
29 // August 5 2012: SpookyV2 (different results)
30 //
31 // Up to 3 bytes/cycle for long messages. Reasonably fast for short messages.
32 // All 1 or 2 bit deltas achieve avalanche within 1% bias per output bit.
33 //
34 // This was developed for and tested on 64-bit x86-compatible processors.
35 // It assumes the processor is little-endian. There is a macro
36 // controlling whether unaligned reads are allowed (by default they are).
37 // This should be an equally good hash on big-endian machines, but it will
38 // compute different results on them than on little-endian machines.
39 //
40 // Google's CityHash has similar specs to SpookyHash, and CityHash is faster
41 // on new Intel boxes. MD4 and MD5 also have similar specs, but they are orders
42 // of magnitude slower. CRCs are two or more times slower, but unlike
43 // SpookyHash, they have nice math for combining the CRCs of pieces to form
44 // the CRCs of wholes. There are also cryptographic hashes, but those are even
45 // slower than MD5.
46 //
47 
48 #include <stddef.h>
49 
50 #ifdef _MSC_VER
51 # define INLINE __forceinline
52  typedef unsigned __int64 uint64;
53  typedef unsigned __int32 uint32;
54  typedef unsigned __int16 uint16;
55  typedef unsigned __int8 uint8;
56 #else
57 # include <stdint.h>
58 # define INLINE inline
59  typedef uint64_t uint64;
60  typedef uint32_t uint32;
61  typedef uint16_t uint16;
62  typedef uint8_t uint8;
63 #endif
64 
65 
67 {
68 public:
69  //
70  // SpookyHash: hash a single message in one call, produce 128-bit output
71  //
72  static void Hash128(
73  const void *message, // message to hash
74  size_t length, // length of message in bytes
75  uint64 *hash1, // in/out: in seed 1, out hash value 1
76  uint64 *hash2); // in/out: in seed 2, out hash value 2
77 
78  //
79  // Hash64: hash a single message in one call, return 64-bit output
80  //
81  static uint64 Hash64(
82  const void *message, // message to hash
83  size_t length, // length of message in bytes
84  uint64 seed) // seed
85  {
86  uint64 hash1 = seed;
87  Hash128(message, length, &hash1, &seed);
88  return hash1;
89  }
90 
91  //
92  // Hash32: hash a single message in one call, produce 32-bit output
93  //
94  static uint32 Hash32(
95  const void *message, // message to hash
96  size_t length, // length of message in bytes
97  uint32 seed) // seed
98  {
99  uint64 hash1 = seed, hash2 = seed;
100  Hash128(message, length, &hash1, &hash2);
101  return (uint32)hash1;
102  }
103 
104  //
105  // Init: initialize the context of a SpookyHash
106  //
107  void Init(
108  uint64 seed1, // any 64-bit value will do, including 0
109  uint64 seed2); // different seeds produce independent hashes
110 
111  //
112  // Update: add a piece of a message to a SpookyHash state
113  //
114  void Update(
115  const void *message, // message fragment
116  size_t length); // length of message fragment in bytes
117 
118 
119  //
120  // Final: compute the hash for the current SpookyHash state
121  //
122  // This does not modify the state; you can keep updating it afterward
123  //
124  // The result is the same as if SpookyHash() had been called with
125  // all the pieces concatenated into one message.
126  //
127  void Final(
128  uint64 *hash1, // out only: first 64 bits of hash value.
129  uint64 *hash2); // out only: second 64 bits of hash value.
130 
131  //
132  // left rotate a 64-bit value by k bytes
133  //
134  static INLINE uint64 Rot64(uint64 x, int k)
135  {
136  return (x << k) | (x >> (64 - k));
137  }
138 
139  //
140  // This is used if the input is 96 bytes long or longer.
141  //
142  // The internal state is fully overwritten every 96 bytes.
143  // Every input bit appears to cause at least 128 bits of entropy
144  // before 96 other bytes are combined, when run forward or backward
145  // For every input bit,
146  // Two inputs differing in just that input bit
147  // Where "differ" means xor or subtraction
148  // And the base value is random
149  // When run forward or backwards one Mix
150  // I tried 3 pairs of each; they all differed by at least 212 bits.
151  //
152  static INLINE void Mix(
153  const uint64 *data,
154  uint64 &s0, uint64 &s1, uint64 &s2, uint64 &s3,
155  uint64 &s4, uint64 &s5, uint64 &s6, uint64 &s7,
156  uint64 &s8, uint64 &s9, uint64 &s10,uint64 &s11)
157  {
158  s0 += data[0]; s2 ^= s10; s11 ^= s0; s0 = Rot64(s0,11); s11 += s1;
159  s1 += data[1]; s3 ^= s11; s0 ^= s1; s1 = Rot64(s1,32); s0 += s2;
160  s2 += data[2]; s4 ^= s0; s1 ^= s2; s2 = Rot64(s2,43); s1 += s3;
161  s3 += data[3]; s5 ^= s1; s2 ^= s3; s3 = Rot64(s3,31); s2 += s4;
162  s4 += data[4]; s6 ^= s2; s3 ^= s4; s4 = Rot64(s4,17); s3 += s5;
163  s5 += data[5]; s7 ^= s3; s4 ^= s5; s5 = Rot64(s5,28); s4 += s6;
164  s6 += data[6]; s8 ^= s4; s5 ^= s6; s6 = Rot64(s6,39); s5 += s7;
165  s7 += data[7]; s9 ^= s5; s6 ^= s7; s7 = Rot64(s7,57); s6 += s8;
166  s8 += data[8]; s10 ^= s6; s7 ^= s8; s8 = Rot64(s8,55); s7 += s9;
167  s9 += data[9]; s11 ^= s7; s8 ^= s9; s9 = Rot64(s9,54); s8 += s10;
168  s10 += data[10]; s0 ^= s8; s9 ^= s10; s10 = Rot64(s10,22); s9 += s11;
169  s11 += data[11]; s1 ^= s9; s10 ^= s11; s11 = Rot64(s11,46); s10 += s0;
170  }
171 
172  //
173  // Mix all 12 inputs together so that h0, h1 are a hash of them all.
174  //
175  // For two inputs differing in just the input bits
176  // Where "differ" means xor or subtraction
177  // And the base value is random, or a counting value starting at that bit
178  // The final result will have each bit of h0, h1 flip
179  // For every input bit,
180  // with probability 50 +- .3%
181  // For every pair of input bits,
182  // with probability 50 +- 3%
183  //
184  // This does not rely on the last Mix() call having already mixed some.
185  // Two iterations was almost good enough for a 64-bit result, but a
186  // 128-bit result is reported, so End() does three iterations.
187  //
188  static INLINE void EndPartial(
189  uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3,
190  uint64 &h4, uint64 &h5, uint64 &h6, uint64 &h7,
191  uint64 &h8, uint64 &h9, uint64 &h10,uint64 &h11)
192  {
193  h11+= h1; h2 ^= h11; h1 = Rot64(h1,44);
194  h0 += h2; h3 ^= h0; h2 = Rot64(h2,15);
195  h1 += h3; h4 ^= h1; h3 = Rot64(h3,34);
196  h2 += h4; h5 ^= h2; h4 = Rot64(h4,21);
197  h3 += h5; h6 ^= h3; h5 = Rot64(h5,38);
198  h4 += h6; h7 ^= h4; h6 = Rot64(h6,33);
199  h5 += h7; h8 ^= h5; h7 = Rot64(h7,10);
200  h6 += h8; h9 ^= h6; h8 = Rot64(h8,13);
201  h7 += h9; h10^= h7; h9 = Rot64(h9,38);
202  h8 += h10; h11^= h8; h10= Rot64(h10,53);
203  h9 += h11; h0 ^= h9; h11= Rot64(h11,42);
204  h10+= h0; h1 ^= h10; h0 = Rot64(h0,54);
205  }
206 
207  static INLINE void End(
208  const uint64 *data,
209  uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3,
210  uint64 &h4, uint64 &h5, uint64 &h6, uint64 &h7,
211  uint64 &h8, uint64 &h9, uint64 &h10,uint64 &h11)
212  {
213  h0 += data[0]; h1 += data[1]; h2 += data[2]; h3 += data[3];
214  h4 += data[4]; h5 += data[5]; h6 += data[6]; h7 += data[7];
215  h8 += data[8]; h9 += data[9]; h10 += data[10]; h11 += data[11];
216  EndPartial(h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
217  EndPartial(h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
218  EndPartial(h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
219  }
220 
221  //
222  // The goal is for each bit of the input to expand into 128 bits of
223  // apparent entropy before it is fully overwritten.
224  // n trials both set and cleared at least m bits of h0 h1 h2 h3
225  // n: 2 m: 29
226  // n: 3 m: 46
227  // n: 4 m: 57
228  // n: 5 m: 107
229  // n: 6 m: 146
230  // n: 7 m: 152
231  // when run forwards or backwards
232  // for all 1-bit and 2-bit diffs
233  // with diffs defined by either xor or subtraction
234  // with a base of all zeros plus a counter, or plus another bit, or random
235  //
236  static INLINE void ShortMix(uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3)
237  {
238  h2 = Rot64(h2,50); h2 += h3; h0 ^= h2;
239  h3 = Rot64(h3,52); h3 += h0; h1 ^= h3;
240  h0 = Rot64(h0,30); h0 += h1; h2 ^= h0;
241  h1 = Rot64(h1,41); h1 += h2; h3 ^= h1;
242  h2 = Rot64(h2,54); h2 += h3; h0 ^= h2;
243  h3 = Rot64(h3,48); h3 += h0; h1 ^= h3;
244  h0 = Rot64(h0,38); h0 += h1; h2 ^= h0;
245  h1 = Rot64(h1,37); h1 += h2; h3 ^= h1;
246  h2 = Rot64(h2,62); h2 += h3; h0 ^= h2;
247  h3 = Rot64(h3,34); h3 += h0; h1 ^= h3;
248  h0 = Rot64(h0,5); h0 += h1; h2 ^= h0;
249  h1 = Rot64(h1,36); h1 += h2; h3 ^= h1;
250  }
251 
252  //
253  // Mix all 4 inputs together so that h0, h1 are a hash of them all.
254  //
255  // For two inputs differing in just the input bits
256  // Where "differ" means xor or subtraction
257  // And the base value is random, or a counting value starting at that bit
258  // The final result will have each bit of h0, h1 flip
259  // For every input bit,
260  // with probability 50 +- .3% (it is probably better than that)
261  // For every pair of input bits,
262  // with probability 50 +- .75% (the worst case is approximately that)
263  //
264  static INLINE void ShortEnd(uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3)
265  {
266  h3 ^= h2; h2 = Rot64(h2,15); h3 += h2;
267  h0 ^= h3; h3 = Rot64(h3,52); h0 += h3;
268  h1 ^= h0; h0 = Rot64(h0,26); h1 += h0;
269  h2 ^= h1; h1 = Rot64(h1,51); h2 += h1;
270  h3 ^= h2; h2 = Rot64(h2,28); h3 += h2;
271  h0 ^= h3; h3 = Rot64(h3,9); h0 += h3;
272  h1 ^= h0; h0 = Rot64(h0,47); h1 += h0;
273  h2 ^= h1; h1 = Rot64(h1,54); h2 += h1;
274  h3 ^= h2; h2 = Rot64(h2,32); h3 += h2;
275  h0 ^= h3; h3 = Rot64(h3,25); h0 += h3;
276  h1 ^= h0; h0 = Rot64(h0,63); h1 += h0;
277  }
278 
279 private:
280 
281  //
282  // Short is used for messages under 192 bytes in length
283  // Short has a low startup cost, the normal mode is good for long
284  // keys, the cost crossover is at about 192 bytes. The two modes were
285  // held to the same quality bar.
286  //
287  static void Short(
288  const void *message, // message (array of bytes, not necessarily aligned)
289  size_t length, // length of message (in bytes)
290  uint64 *hash1, // in/out: in the seed, out the hash value
291  uint64 *hash2); // in/out: in the seed, out the hash value
292 
293  // number of uint64's in internal state
294  static const size_t sc_numVars = 12;
295 
296  // size of the internal state
297  static const size_t sc_blockSize = sc_numVars*8;
298 
299  // size of buffer of unhashed data, in bytes
300  static const size_t sc_bufSize = 2*sc_blockSize;
301 
302  //
303  // sc_const: a constant which:
304  // * is not zero
305  // * is odd
306  // * is a not-very-regular mix of 1's and 0's
307  // * does not need any other special mathematical properties
308  //
309  static const uint64 sc_const = 0xdeadbeefdeadbeefLL;
310 
311  uint64 m_data[2*sc_numVars]; // unhashed data, for partial messages
312  uint64 m_state[sc_numVars]; // internal state of the hash
313  size_t m_length; // total length of the input so far
314  uint8 m_remainder; // length of unhashed data stashed in m_data
315 };