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