Colibri Core
SpookyV2.h
Go to the documentation of this file.
1 
38 #include <stddef.h>
39 #include <stdint.h>
40 #define INLINE inline
41 typedef uint64_t uint64;
42 typedef uint32_t uint32;
43 typedef uint16_t uint16;
44 typedef uint8_t uint8;
45 
46 
47 class SpookyHash {
48  public:
49  //
50  // SpookyHash: hash a single message in one call, produce 128-bit output
51  //
52  static void Hash128(
53  const void *message, // message to hash
54  size_t length, // length of message in bytes
55  uint64 *hash1, // in/out: in seed 1, out hash value 1
56  uint64 *hash2); // in/out: in seed 2, out hash value 2
57 
58  //
59  // Hash64: hash a single message in one call, return 64-bit output
60  //
61  static uint64 Hash64(
62  const void *message, // message to hash
63  size_t length, // length of message in bytes
64  uint64 seed=0) // seed
65  {
66  uint64 hash1 = seed;
67  Hash128(message, length, &hash1, &seed);
68  return hash1;
69  }
70 
71  //
72  // Hash32: hash a single message in one call, produce 32-bit output
73  //
74  static uint32 Hash32(
75  const void *message, // message to hash
76  size_t length, // length of message in bytes
77  uint32 seed=0) // seed
78  {
79  uint64 hash1 = seed, hash2 = seed;
80  Hash128(message, length, &hash1, &hash2);
81  return (uint32)hash1;
82  }
83 
84  //
85  // Init: initialize the context of a SpookyHash
86  //
87  void Init(
88  uint64 seed1, // any 64-bit value will do, including 0
89  uint64 seed2); // different seeds produce independent hashes
90 
91  //
92  // Update: add a piece of a message to a SpookyHash state
93  //
94  void Update(
95  const void *message, // message fragment
96  size_t length); // length of message fragment in bytes
97 
98 
99  //
100  // Final: compute the hash for the current SpookyHash state
101  //
102  // This does not modify the state; you can keep updating it afterward
103  //
104  // The result is the same as if SpookyHash() had been called with
105  // all the pieces concatenated into one message.
106  //
107  void Final(
108  uint64 *hash1, // out only: first 64 bits of hash value.
109  uint64 *hash2); // out only: second 64 bits of hash value.
110 
111  //
112  // left rotate a 64-bit value by k bytes
113  //
114  static INLINE uint64 Rot64(uint64 x, int k)
115  {
116  return (x << k) | (x >> (64 - k));
117  }
118 
119  //
120  // This is used if the input is 96 bytes long or longer.
121  //
122  // The internal state is fully overwritten every 96 bytes.
123  // Every input bit appears to cause at least 128 bits of entropy
124  // before 96 other bytes are combined, when run forward or backward
125  // For every input bit,
126  // Two inputs differing in just that input bit
127  // Where "differ" means xor or subtraction
128  // And the base value is random
129  // When run forward or backwards one Mix
130  // I tried 3 pairs of each; they all differed by at least 212 bits.
131  //
132  static INLINE void Mix(
133  const uint64 *data,
134  uint64 &s0, uint64 &s1, uint64 &s2, uint64 &s3,
135  uint64 &s4, uint64 &s5, uint64 &s6, uint64 &s7,
136  uint64 &s8, uint64 &s9, uint64 &s10,uint64 &s11)
137  {
138  s0 += data[0]; s2 ^= s10; s11 ^= s0; s0 = Rot64(s0,11); s11 += s1;
139  s1 += data[1]; s3 ^= s11; s0 ^= s1; s1 = Rot64(s1,32); s0 += s2;
140  s2 += data[2]; s4 ^= s0; s1 ^= s2; s2 = Rot64(s2,43); s1 += s3;
141  s3 += data[3]; s5 ^= s1; s2 ^= s3; s3 = Rot64(s3,31); s2 += s4;
142  s4 += data[4]; s6 ^= s2; s3 ^= s4; s4 = Rot64(s4,17); s3 += s5;
143  s5 += data[5]; s7 ^= s3; s4 ^= s5; s5 = Rot64(s5,28); s4 += s6;
144  s6 += data[6]; s8 ^= s4; s5 ^= s6; s6 = Rot64(s6,39); s5 += s7;
145  s7 += data[7]; s9 ^= s5; s6 ^= s7; s7 = Rot64(s7,57); s6 += s8;
146  s8 += data[8]; s10 ^= s6; s7 ^= s8; s8 = Rot64(s8,55); s7 += s9;
147  s9 += data[9]; s11 ^= s7; s8 ^= s9; s9 = Rot64(s9,54); s8 += s10;
148  s10 += data[10]; s0 ^= s8; s9 ^= s10; s10 = Rot64(s10,22); s9 += s11;
149  s11 += data[11]; s1 ^= s9; s10 ^= s11; s11 = Rot64(s11,46); s10 += s0;
150  }
151 
152  //
153  // Mix all 12 inputs together so that h0, h1 are a hash of them all.
154  //
155  // For two inputs differing in just the input bits
156  // Where "differ" means xor or subtraction
157  // And the base value is random, or a counting value starting at that bit
158  // The final result will have each bit of h0, h1 flip
159  // For every input bit,
160  // with probability 50 +- .3%
161  // For every pair of input bits,
162  // with probability 50 +- 3%
163  //
164  // This does not rely on the last Mix() call having already mixed some.
165  // Two iterations was almost good enough for a 64-bit result, but a
166  // 128-bit result is reported, so End() does three iterations.
167  //
168  static INLINE void EndPartial(
169  uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3,
170  uint64 &h4, uint64 &h5, uint64 &h6, uint64 &h7,
171  uint64 &h8, uint64 &h9, uint64 &h10,uint64 &h11)
172  {
173  h11+= h1; h2 ^= h11; h1 = Rot64(h1,44);
174  h0 += h2; h3 ^= h0; h2 = Rot64(h2,15);
175  h1 += h3; h4 ^= h1; h3 = Rot64(h3,34);
176  h2 += h4; h5 ^= h2; h4 = Rot64(h4,21);
177  h3 += h5; h6 ^= h3; h5 = Rot64(h5,38);
178  h4 += h6; h7 ^= h4; h6 = Rot64(h6,33);
179  h5 += h7; h8 ^= h5; h7 = Rot64(h7,10);
180  h6 += h8; h9 ^= h6; h8 = Rot64(h8,13);
181  h7 += h9; h10^= h7; h9 = Rot64(h9,38);
182  h8 += h10; h11^= h8; h10= Rot64(h10,53);
183  h9 += h11; h0 ^= h9; h11= Rot64(h11,42);
184  h10+= h0; h1 ^= h10; h0 = Rot64(h0,54);
185  }
186 
187  static INLINE void End(
188  const uint64 *data,
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  h0 += data[0]; h1 += data[1]; h2 += data[2]; h3 += data[3];
194  h4 += data[4]; h5 += data[5]; h6 += data[6]; h7 += data[7];
195  h8 += data[8]; h9 += data[9]; h10 += data[10]; h11 += data[11];
196  EndPartial(h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
197  EndPartial(h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
198  EndPartial(h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
199  }
200 
201  //
202  // The goal is for each bit of the input to expand into 128 bits of
203  // apparent entropy before it is fully overwritten.
204  // n trials both set and cleared at least m bits of h0 h1 h2 h3
205  // n: 2 m: 29
206  // n: 3 m: 46
207  // n: 4 m: 57
208  // n: 5 m: 107
209  // n: 6 m: 146
210  // n: 7 m: 152
211  // when run forwards or backwards
212  // for all 1-bit and 2-bit diffs
213  // with diffs defined by either xor or subtraction
214  // with a base of all zeros plus a counter, or plus another bit, or random
215  //
216  static INLINE void ShortMix(uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3)
217  {
218  h2 = Rot64(h2,50); h2 += h3; h0 ^= h2;
219  h3 = Rot64(h3,52); h3 += h0; h1 ^= h3;
220  h0 = Rot64(h0,30); h0 += h1; h2 ^= h0;
221  h1 = Rot64(h1,41); h1 += h2; h3 ^= h1;
222  h2 = Rot64(h2,54); h2 += h3; h0 ^= h2;
223  h3 = Rot64(h3,48); h3 += h0; h1 ^= h3;
224  h0 = Rot64(h0,38); h0 += h1; h2 ^= h0;
225  h1 = Rot64(h1,37); h1 += h2; h3 ^= h1;
226  h2 = Rot64(h2,62); h2 += h3; h0 ^= h2;
227  h3 = Rot64(h3,34); h3 += h0; h1 ^= h3;
228  h0 = Rot64(h0,5); h0 += h1; h2 ^= h0;
229  h1 = Rot64(h1,36); h1 += h2; h3 ^= h1;
230  }
231 
232  //
233  // Mix all 4 inputs together so that h0, h1 are a hash of them all.
234  //
235  // For two inputs differing in just the input bits
236  // Where "differ" means xor or subtraction
237  // And the base value is random, or a counting value starting at that bit
238  // The final result will have each bit of h0, h1 flip
239  // For every input bit,
240  // with probability 50 +- .3% (it is probably better than that)
241  // For every pair of input bits,
242  // with probability 50 +- .75% (the worst case is approximately that)
243  //
244  static INLINE void ShortEnd(uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3)
245  {
246  h3 ^= h2; h2 = Rot64(h2,15); h3 += h2;
247  h0 ^= h3; h3 = Rot64(h3,52); h0 += h3;
248  h1 ^= h0; h0 = Rot64(h0,26); h1 += h0;
249  h2 ^= h1; h1 = Rot64(h1,51); h2 += h1;
250  h3 ^= h2; h2 = Rot64(h2,28); h3 += h2;
251  h0 ^= h3; h3 = Rot64(h3,9); h0 += h3;
252  h1 ^= h0; h0 = Rot64(h0,47); h1 += h0;
253  h2 ^= h1; h1 = Rot64(h1,54); h2 += h1;
254  h3 ^= h2; h2 = Rot64(h2,32); h3 += h2;
255  h0 ^= h3; h3 = Rot64(h3,25); h0 += h3;
256  h1 ^= h0; h0 = Rot64(h0,63); h1 += h0;
257  }
258 
259 private:
260 
261  //
262  // Short is used for messages under 192 bytes in length
263  // Short has a low startup cost, the normal mode is good for long
264  // keys, the cost crossover is at about 192 bytes. The two modes were
265  // held to the same quality bar.
266  //
267  static void Short(
268  const void *message, // message (array of bytes, not necessarily aligned)
269  size_t length, // length of message (in bytes)
270  uint64 *hash1, // in/out: in the seed, out the hash value
271  uint64 *hash2); // in/out: in the seed, out the hash value
272 
273  // number of uint64's in internal state
274  static const size_t sc_numVars = 12;
275 
276  // size of the internal state
277  static const size_t sc_blockSize = sc_numVars*8;
278 
279  // size of buffer of unhashed data, in bytes
280  static const size_t sc_bufSize = 2*sc_blockSize;
281 
282  //
283  // sc_const: a constant which:
284  // * is not zero
285  // * is odd
286  // * is a not-very-regular mix of 1's and 0's
287  // * does not need any other special mathematical properties
288  //
289  static const uint64 sc_const = 0xdeadbeefdeadbeefLL;
290 
291  uint64 m_data[2*sc_numVars]; // unhashed data, for partial messages
292  uint64 m_state[sc_numVars]; // internal state of the hash
293  size_t m_length; // total length of the input so far
294  uint8 m_remainder; // length of unhashed data stashed in m_data
295 };
296 
297 
298 
void Update(const void *message, size_t length)
Definition: SpookyV2.cpp:201
static uint64 Hash64(const void *message, size_t length, uint64 seed=0)
Definition: SpookyV2.h:61
Definition: SpookyV2.h:47
static INLINE void ShortMix(uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3)
Definition: SpookyV2.h:216
uint8_t uint8
Definition: SpookyV2.h:44
static void Hash128(const void *message, size_t length, uint64 *hash1, uint64 *hash2)
Definition: SpookyV2.cpp:127
void Init(uint64 seed1, uint64 seed2)
Definition: SpookyV2.cpp:191
void Final(uint64 *hash1, uint64 *hash2)
Definition: SpookyV2.cpp:305
static INLINE uint64 Rot64(uint64 x, int k)
Definition: SpookyV2.h:114
static INLINE void Mix(const uint64 *data, uint64 &s0, uint64 &s1, uint64 &s2, uint64 &s3, uint64 &s4, uint64 &s5, uint64 &s6, uint64 &s7, uint64 &s8, uint64 &s9, uint64 &s10, uint64 &s11)
Definition: SpookyV2.h:132
uint32_t uint32
Definition: SpookyV2.h:42
static INLINE void ShortEnd(uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3)
Definition: SpookyV2.h:244
uint64_t uint64
Definition: SpookyV2.h:41
static uint32 Hash32(const void *message, size_t length, uint32 seed=0)
Definition: SpookyV2.h:74
static INLINE void EndPartial(uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3, uint64 &h4, uint64 &h5, uint64 &h6, uint64 &h7, uint64 &h8, uint64 &h9, uint64 &h10, uint64 &h11)
Definition: SpookyV2.h:168
uint16_t uint16
Definition: SpookyV2.h:43
#define INLINE
Definition: SpookyV2.h:40
static INLINE void End(const uint64 *data, uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3, uint64 &h4, uint64 &h5, uint64 &h6, uint64 &h7, uint64 &h8, uint64 &h9, uint64 &h10, uint64 &h11)
Definition: SpookyV2.h:187