Evo C++ Library v0.5.1
hash.h
Go to the documentation of this file.
1 // Evo C++ Library
2 /* Copyright 2019 Justin Crowell
3 Distributed under the BSD 2-Clause License -- see included file LICENSE.txt for details.
4 */
6 
7 #pragma once
8 #ifndef INCL_evo_impl_hash_h
9 #define INCL_evo_impl_hash_h
10 
11 #include "sys.h"
12 
14 // The following was mostly copied from SpookyHash V2 by Bob Jenkins (public domain) -- Aug 5 2012
15 // http://burtleburtle.net/bob/hash/spooky.html
16 
18 #define SPOOKYHASH_ALLOW_UNALIGNED_READS 0
19 
20 class SpookyHash
21 {
22 public:
23  // SpookyHash: hash a single message in one call, produce 128-bit output
24  static void Hash128(
25  const void *message, // message to hash
26  ulong length, // length of message in bytes
27  uint64 *hash1, // in/out: in seed 1, out hash value 1
28  uint64 *hash2) // in/out: in seed 2, out hash value 2
29  {
30  if (length < sc_bufSize) {
31  Short(message, length, hash1, hash2);
32  return;
33  }
34 
35  uint64 h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11;
36  uint64 buf[sc_numVars];
37  uint64 *end;
38  union {
39  const uint8 *p8;
40  uint64 *p64;
41  size_t i;
42  } u;
43  size_t remainder;
44 
45  h0=h3=h6=h9 = *hash1;
46  h1=h4=h7=h10 = *hash2;
47  h2=h5=h8=h11 = sc_const;
48 
49  u.p8 = (const uint8 *)message;
50  end = u.p64 + (length/sc_blockSize)*sc_numVars;
51 
52  // handle all whole sc_blockSize blocks of bytes
53  if (SPOOKYHASH_ALLOW_UNALIGNED_READS || ((u.i & 0x7) == 0)) {
54  while (u.p64 < end) {
55  Mix(u.p64, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
56  u.p64 += sc_numVars;
57  }
58  } else {
59  while (u.p64 < end) {
60  memcpy(buf, u.p64, sc_blockSize);
61  Mix(buf, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
62  u.p64 += sc_numVars;
63  }
64  }
65 
66  // handle the last partial block of sc_blockSize bytes
67  remainder = (length - ((const uint8 *)end-(const uint8 *)message));
68  memcpy(buf, end, remainder);
69  memset(((uint8 *)buf)+remainder, 0, sc_blockSize-remainder);
70  ((uint8 *)buf)[sc_blockSize-1] = (uint8)remainder;
71 
72  // do some final mixing
73  End(buf, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
74  *hash1 = h0;
75  *hash2 = h1;
76  }
77 
78  // Hash64: hash a single message in one call, return 64-bit output
79  static uint64 Hash64(
80  const void *message, // message to hash
81  ulong length, // length of message in bytes
82  uint64 seed) // seed
83  {
84  uint64 hash1 = seed;
85  Hash128(message, length, &hash1, &seed);
86  return hash1;
87  }
88 
89  // Hash32: hash a single message in one call, produce 32-bit output
90  static uint32 Hash32(
91  const void *message, // message to hash
92  ulong length, // length of message in bytes
93  uint32 seed) // seed
94  {
95  uint64 hash1 = seed, hash2 = seed;
96  Hash128(message, length, &hash1, &hash2);
97  return (uint32)hash1;
98  }
99 
100  // Hash for ulong value
101  static ulong Hash(const void* message, ulong length, ulong seed) {
102  #ifdef EVO_32
103  return (ulong)Hash32(message, length, seed);
104  #elif EVO_64
105  return (ulong)Hash64(message, length, seed);
106  #endif
107  }
108  static ulong hash(const void* message, ulong length, ulong seed) {
109  #ifdef EVO_32
110  return (ulong)Hash32(message, length, seed);
111  #elif EVO_64
112  return (ulong)Hash64(message, length, seed);
113  #endif
114  }
115  template<class T>
116  static ulong hash_pod(T message, ulong seed) {
117  #ifdef EVO_32
118  return (ulong)Hash32(&message, sizeof(T), seed);
119  #elif EVO_64
120  return (ulong)Hash64(&message, sizeof(T), seed);
121  #endif
122  }
123 
124  // Init: initialize the context of a SpookyHash
125  void Init(
126  uint64 seed1, // any 64-bit value will do, including 0
127  uint64 seed2) // different seeds produce independent hashes
128  {
129  m_length = 0;
130  m_remainder = 0;
131  m_state[0] = seed1;
132  m_state[1] = seed2;
133  }
134 
135  // Update: add a piece of a message to a SpookyHash state
136  void Update(
137  const void *message, // message fragment
138  ulong length) // length of message fragment in bytes
139  {
140  uint64 h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11;
141  ulong newLength = length + m_remainder;
142  uint8 remainder;
143  union {
144  const uint8 *p8;
145  uint64 *p64;
146  ulong i;
147  } u;
148  const uint64 *end;
149 
150  // Is this message fragment too short? If it is, stuff it away.
151  if (newLength < sc_bufSize) {
152  memcpy(&((uint8 *)m_data)[m_remainder], message, length);
153  m_length = length + m_length;
154  m_remainder = (uint8)newLength;
155  return;
156  }
157 
158  // init the variables
159  if (m_length < sc_bufSize) {
160  h0=h3=h6=h9 = m_state[0];
161  h1=h4=h7=h10 = m_state[1];
162  h2=h5=h8=h11 = sc_const;
163  } else {
164  h0 = m_state[0];
165  h1 = m_state[1];
166  h2 = m_state[2];
167  h3 = m_state[3];
168  h4 = m_state[4];
169  h5 = m_state[5];
170  h6 = m_state[6];
171  h7 = m_state[7];
172  h8 = m_state[8];
173  h9 = m_state[9];
174  h10 = m_state[10];
175  h11 = m_state[11];
176  }
177  m_length = length + m_length;
178 
179  // if we've got anything stuffed away, use it now
180  if (m_remainder) {
181  uint8 prefix = sc_bufSize-m_remainder;
182  memcpy(&(((uint8 *)m_data)[m_remainder]), message, prefix);
183  u.p64 = m_data;
184  Mix(u.p64, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
185  Mix(&u.p64[sc_numVars], h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
186  u.p8 = ((const uint8 *)message) + prefix;
187  length -= prefix;
188  } else {
189  u.p8 = (const uint8 *)message;
190  }
191 
192  // handle all whole blocks of sc_blockSize bytes
193  end = u.p64 + (length/sc_blockSize)*sc_numVars;
194  remainder = (uint8)(length-((const uint8 *)end-u.p8));
195  if (SPOOKYHASH_ALLOW_UNALIGNED_READS || (u.i & 0x7) == 0) {
196  while (u.p64 < end) {
197  Mix(u.p64, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
198  u.p64 += sc_numVars;
199  }
200  } else {
201  while (u.p64 < end) {
202  memcpy(m_data, u.p8, sc_blockSize);
203  Mix(m_data, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
204  u.p64 += sc_numVars;
205  }
206  }
207 
208  // stuff away the last few bytes
209  m_remainder = remainder;
210  memcpy(m_data, end, remainder);
211 
212  // stuff away the variables
213  m_state[0] = h0;
214  m_state[1] = h1;
215  m_state[2] = h2;
216  m_state[3] = h3;
217  m_state[4] = h4;
218  m_state[5] = h5;
219  m_state[6] = h6;
220  m_state[7] = h7;
221  m_state[8] = h8;
222  m_state[9] = h9;
223  m_state[10] = h10;
224  m_state[11] = h11;
225  }
226 
227  // Final: compute the hash for the current SpookyHash state
228  // This does not modify the state; you can keep updating it afterward
229  // The result is the same as if SpookyHash() had been called with
230  // all the pieces concatenated into one message.
231  void Final(
232  uint64 *hash1, // out only: first 64 bits of hash value.
233  uint64 *hash2) // out only: second 64 bits of hash value.
234  {
235  // init the variables
236  if (m_length < sc_bufSize) {
237  *hash1 = m_state[0];
238  *hash2 = m_state[1];
239  Short( m_data, m_length, hash1, hash2);
240  return;
241  }
242 
243  const uint64 *data = (const uint64 *)m_data;
244  uint8 remainder = m_remainder;
245 
246  uint64 h0 = m_state[0];
247  uint64 h1 = m_state[1];
248  uint64 h2 = m_state[2];
249  uint64 h3 = m_state[3];
250  uint64 h4 = m_state[4];
251  uint64 h5 = m_state[5];
252  uint64 h6 = m_state[6];
253  uint64 h7 = m_state[7];
254  uint64 h8 = m_state[8];
255  uint64 h9 = m_state[9];
256  uint64 h10 = m_state[10];
257  uint64 h11 = m_state[11];
258 
259  if (remainder >= sc_blockSize) {
260  // m_data can contain two blocks; handle any whole first block
261  Mix(data, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
262  data += sc_numVars;
263  remainder -= sc_blockSize;
264  }
265 
266  // mix in the last partial block, and the length mod sc_blockSize
267  memset(&((uint8 *)data)[remainder], 0, (sc_blockSize-remainder));
268 
269  ((uint8 *)data)[sc_blockSize-1] = remainder;
270 
271  // do some final mixing
272  End(data, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
273 
274  *hash1 = h0;
275  *hash2 = h1;
276  }
277 
278  // left rotate a 64-bit value by k bytes
279  static uint64 Rot64(uint64 x, int k)
280  { return (x << k) | (x >> (64 - k)); }
281 
282  // This is used if the input is 96 bytes long or longer.
283  // The internal state is fully overwritten every 96 bytes.
284  // Every input bit appears to cause at least 128 bits of entropy
285  // before 96 other bytes are combined, when run forward or backward
286  // For every input bit,
287  // Two inputs differing in just that input bit
288  // Where "differ" means xor or subtraction
289  // And the base value is random
290  // When run forward or backwards one Mix
291  // I tried 3 pairs of each; they all differed by at least 212 bits.
292  static void Mix(const uint64 *data,
293  uint64 &s0, uint64 &s1, uint64 &s2, uint64 &s3,
294  uint64 &s4, uint64 &s5, uint64 &s6, uint64 &s7,
295  uint64 &s8, uint64 &s9, uint64 &s10,uint64 &s11)
296  {
297  s0 += data[0]; s2 ^= s10; s11 ^= s0; s0 = Rot64(s0,11); s11 += s1;
298  s1 += data[1]; s3 ^= s11; s0 ^= s1; s1 = Rot64(s1,32); s0 += s2;
299  s2 += data[2]; s4 ^= s0; s1 ^= s2; s2 = Rot64(s2,43); s1 += s3;
300  s3 += data[3]; s5 ^= s1; s2 ^= s3; s3 = Rot64(s3,31); s2 += s4;
301  s4 += data[4]; s6 ^= s2; s3 ^= s4; s4 = Rot64(s4,17); s3 += s5;
302  s5 += data[5]; s7 ^= s3; s4 ^= s5; s5 = Rot64(s5,28); s4 += s6;
303  s6 += data[6]; s8 ^= s4; s5 ^= s6; s6 = Rot64(s6,39); s5 += s7;
304  s7 += data[7]; s9 ^= s5; s6 ^= s7; s7 = Rot64(s7,57); s6 += s8;
305  s8 += data[8]; s10 ^= s6; s7 ^= s8; s8 = Rot64(s8,55); s7 += s9;
306  s9 += data[9]; s11 ^= s7; s8 ^= s9; s9 = Rot64(s9,54); s8 += s10;
307  s10 += data[10]; s0 ^= s8; s9 ^= s10; s10 = Rot64(s10,22); s9 += s11;
308  s11 += data[11]; s1 ^= s9; s10 ^= s11; s11 = Rot64(s11,46); s10 += s0;
309  }
310 
311  // Mix all 12 inputs together so that h0, h1 are a hash of them all.
312  //
313  // For two inputs differing in just the input bits
314  // Where "differ" means xor or subtraction
315  // And the base value is random, or a counting value starting at that bit
316  // The final result will have each bit of h0, h1 flip
317  // For every input bit,
318  // with probability 50 +- .3%
319  // For every pair of input bits,
320  // with probability 50 +- 3%
321  //
322  // This does not rely on the last Mix() call having already mixed some.
323  // Two iterations was almost good enough for a 64-bit result, but a
324  // 128-bit result is reported, so End() does three iterations.
325  static void EndPartial(
326  uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3,
327  uint64 &h4, uint64 &h5, uint64 &h6, uint64 &h7,
328  uint64 &h8, uint64 &h9, uint64 &h10,uint64 &h11)
329  {
330  h11+= h1; h2 ^= h11; h1 = Rot64(h1,44);
331  h0 += h2; h3 ^= h0; h2 = Rot64(h2,15);
332  h1 += h3; h4 ^= h1; h3 = Rot64(h3,34);
333  h2 += h4; h5 ^= h2; h4 = Rot64(h4,21);
334  h3 += h5; h6 ^= h3; h5 = Rot64(h5,38);
335  h4 += h6; h7 ^= h4; h6 = Rot64(h6,33);
336  h5 += h7; h8 ^= h5; h7 = Rot64(h7,10);
337  h6 += h8; h9 ^= h6; h8 = Rot64(h8,13);
338  h7 += h9; h10^= h7; h9 = Rot64(h9,38);
339  h8 += h10; h11^= h8; h10= Rot64(h10,53);
340  h9 += h11; h0 ^= h9; h11= Rot64(h11,42);
341  h10+= h0; h1 ^= h10; h0 = Rot64(h0,54);
342  }
343 
344  static void End(const uint64 *data,
345  uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3,
346  uint64 &h4, uint64 &h5, uint64 &h6, uint64 &h7,
347  uint64 &h8, uint64 &h9, uint64 &h10,uint64 &h11)
348  {
349  h0 += data[0]; h1 += data[1]; h2 += data[2]; h3 += data[3];
350  h4 += data[4]; h5 += data[5]; h6 += data[6]; h7 += data[7];
351  h8 += data[8]; h9 += data[9]; h10 += data[10]; h11 += data[11];
352  EndPartial(h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
353  EndPartial(h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
354  EndPartial(h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
355  }
356 
357  // The goal is for each bit of the input to expand into 128 bits of
358  // apparent entropy before it is fully overwritten.
359  // n trials both set and cleared at least m bits of h0 h1 h2 h3
360  // n: 2 m: 29
361  // n: 3 m: 46
362  // n: 4 m: 57
363  // n: 5 m: 107
364  // n: 6 m: 146
365  // n: 7 m: 152
366  // when run forwards or backwards
367  // for all 1-bit and 2-bit diffs
368  // with diffs defined by either xor or subtraction
369  // with a base of all zeros plus a counter, or plus another bit, or random
370  static void ShortMix(uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3) {
371  h2 = Rot64(h2,50); h2 += h3; h0 ^= h2;
372  h3 = Rot64(h3,52); h3 += h0; h1 ^= h3;
373  h0 = Rot64(h0,30); h0 += h1; h2 ^= h0;
374  h1 = Rot64(h1,41); h1 += h2; h3 ^= h1;
375  h2 = Rot64(h2,54); h2 += h3; h0 ^= h2;
376  h3 = Rot64(h3,48); h3 += h0; h1 ^= h3;
377  h0 = Rot64(h0,38); h0 += h1; h2 ^= h0;
378  h1 = Rot64(h1,37); h1 += h2; h3 ^= h1;
379  h2 = Rot64(h2,62); h2 += h3; h0 ^= h2;
380  h3 = Rot64(h3,34); h3 += h0; h1 ^= h3;
381  h0 = Rot64(h0,5); h0 += h1; h2 ^= h0;
382  h1 = Rot64(h1,36); h1 += h2; h3 ^= h1;
383  }
384 
385  // Mix all 4 inputs together so that h0, h1 are a hash of them all.
386  //
387  // For two inputs differing in just the input bits
388  // Where "differ" means xor or subtraction
389  // And the base value is random, or a counting value starting at that bit
390  // The final result will have each bit of h0, h1 flip
391  // For every input bit,
392  // with probability 50 +- .3% (it is probably better than that)
393  // For every pair of input bits,
394  // with probability 50 +- .75% (the worst case is approximately that)
395  static void ShortEnd(uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3) {
396  h3 ^= h2; h2 = Rot64(h2,15); h3 += h2;
397  h0 ^= h3; h3 = Rot64(h3,52); h0 += h3;
398  h1 ^= h0; h0 = Rot64(h0,26); h1 += h0;
399  h2 ^= h1; h1 = Rot64(h1,51); h2 += h1;
400  h3 ^= h2; h2 = Rot64(h2,28); h3 += h2;
401  h0 ^= h3; h3 = Rot64(h3,9); h0 += h3;
402  h1 ^= h0; h0 = Rot64(h0,47); h1 += h0;
403  h2 ^= h1; h1 = Rot64(h1,54); h2 += h1;
404  h3 ^= h2; h2 = Rot64(h2,32); h3 += h2;
405  h0 ^= h3; h3 = Rot64(h3,25); h0 += h3;
406  h1 ^= h0; h0 = Rot64(h0,63); h1 += h0;
407  }
408 
409 private:
410  // Short is used for messages under 192 bytes in length
411  // Short has a low startup cost, the normal mode is good for long
412  // keys, the cost crossover is at about 192 bytes. The two modes were
413  // held to the same quality bar.
414  static void Short(
415  const void *message, // message (array of bytes, not necessarily aligned)
416  ulong length, // length of message (in bytes)
417  uint64 *hash1, // in/out: in the seed, out the hash value
418  uint64 *hash2) // in/out: in the seed, out the hash value
419  {
420  uint64 buf[2*sc_numVars];
421  union {
422  const uint8 *p8;
423  uint32 *p32;
424  uint64 *p64;
425  ulong i;
426  } u;
427 
428  u.p8 = (const uint8 *)message;
429 
430  if (!SPOOKYHASH_ALLOW_UNALIGNED_READS && (u.i & 0x7)) {
431  memcpy(buf, message, length);
432  u.p64 = buf;
433  }
434 
435  ulong remainder = length%32;
436  uint64 a=*hash1;
437  uint64 b=*hash2;
438  uint64 c=sc_const;
439  uint64 d=sc_const;
440 
441  if (length > 15) {
442  const uint64 *end = u.p64 + (length/32)*4;
443 
444  // handle all complete sets of 32 bytes
445  for (; u.p64 < end; u.p64 += 4) {
446  c += u.p64[0];
447  d += u.p64[1];
448  ShortMix(a,b,c,d);
449  a += u.p64[2];
450  b += u.p64[3];
451  }
452 
453  //Handle the case of 16+ remaining bytes.
454  if (remainder >= 16) {
455  c += u.p64[0];
456  d += u.p64[1];
457  ShortMix(a,b,c,d);
458  u.p64 += 2;
459  remainder -= 16;
460  }
461  }
462 
463  // Handle the last 0..15 bytes, and its length
464  d += ((uint64)length) << 56;
465  switch (remainder) {
466  case 15:
467  d += ((uint64)u.p8[14]) << 48;
468  case 14:
469  d += ((uint64)u.p8[13]) << 40;
470  case 13:
471  d += ((uint64)u.p8[12]) << 32;
472  case 12:
473  d += u.p32[2];
474  c += u.p64[0];
475  break;
476  case 11:
477  d += ((uint64)u.p8[10]) << 16;
478  case 10:
479  d += ((uint64)u.p8[9]) << 8;
480  case 9:
481  d += (uint64)u.p8[8];
482  case 8:
483  c += u.p64[0];
484  break;
485  case 7:
486  c += ((uint64)u.p8[6]) << 48;
487  case 6:
488  c += ((uint64)u.p8[5]) << 40;
489  case 5:
490  c += ((uint64)u.p8[4]) << 32;
491  case 4:
492  c += u.p32[0];
493  break;
494  case 3:
495  c += ((uint64)u.p8[2]) << 16;
496  case 2:
497  c += ((uint64)u.p8[1]) << 8;
498  case 1:
499  c += (uint64)u.p8[0];
500  break;
501  case 0:
502  c += sc_const;
503  d += sc_const;
504  }
505  ShortEnd(a,b,c,d);
506  *hash1 = a;
507  *hash2 = b;
508  }
509 
510  // number of uint64's in internal state
511  static const ulong sc_numVars = 12;
512 
513  // size of the internal state
514  static const ulong sc_blockSize = sc_numVars*8;
515 
516  // size of buffer of unhashed data, in bytes
517  static const ulong sc_bufSize = 2*sc_blockSize;
518 
519  // sc_const: a constant which:
520  // * is not zero
521  // * is odd
522  // * is a not-very-regular mix of 1's and 0's
523  // * does not need any other special mathematical properties
524  static const uint64 sc_const = (uint64)0xdeadbeefdeadbeefLL;
525 
526  uint64 m_data[2*sc_numVars]; // unhashed data, for partial messages
527  uint64 m_state[sc_numVars]; // internal state of the hash
528  ulong m_length; // total length of the input so far
529  uint8 m_remainder; // length of unhashed data stashed in m_data
530 };
531 
532 // Cleanup
533 #undef SPOOKYHASH_ALLOW_UNALIGNED_READS
534 
536 #endif
Evo implementation detail for system portability – this is included by most Evo headers, include this via: include <evo/type.h>.
IntegerT< short > Short
Basic integer type (short) – see IntegerT.
Definition: type.h:1102