SpookyV2.h 12 KB

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