lookup3.c revision 1.1.1.4 1 1.1 christos /*
2 1.1.1.3 christos May 2019(Wouter) patch to enable the valgrind clean implementation all the
3 1.1.1.3 christos time. This enables better security audit and checks, which is better
4 1.1.1.3 christos than the speedup. Git issue #30. Renamed the define ARRAY_CLEAN_ACCESS.
5 1.1 christos February 2013(Wouter) patch defines for BSD endianness, from Brad Smith.
6 1.1 christos January 2012(Wouter) added randomised initial value, fallout from 28c3.
7 1.1 christos March 2007(Wouter) adapted from lookup3.c original, add config.h include.
8 1.1 christos added #ifdef VALGRIND to remove 298,384,660 'unused variable k8' warnings.
9 1.1 christos added include of lookup3.h to check definitions match declarations.
10 1.1 christos removed include of stdint - config.h takes care of platform independence.
11 1.1.1.2 christos added fallthrough comments for new gcc warning suppression.
12 1.1 christos url http://burtleburtle.net/bob/hash/index.html.
13 1.1 christos */
14 1.1 christos /*
15 1.1 christos -------------------------------------------------------------------------------
16 1.1 christos lookup3.c, by Bob Jenkins, May 2006, Public Domain.
17 1.1 christos
18 1.1 christos These are functions for producing 32-bit hashes for hash table lookup.
19 1.1 christos hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
20 1.1 christos are externally useful functions. Routines to test the hash are included
21 1.1 christos if SELF_TEST is defined. You can use this free for any purpose. It's in
22 1.1 christos the public domain. It has no warranty.
23 1.1 christos
24 1.1 christos You probably want to use hashlittle(). hashlittle() and hashbig()
25 1.1 christos hash byte arrays. hashlittle() is is faster than hashbig() on
26 1.1 christos little-endian machines. Intel and AMD are little-endian machines.
27 1.1 christos On second thought, you probably want hashlittle2(), which is identical to
28 1.1 christos hashlittle() except it returns two 32-bit hashes for the price of one.
29 1.1 christos You could implement hashbig2() if you wanted but I haven't bothered here.
30 1.1 christos
31 1.1 christos If you want to find a hash of, say, exactly 7 integers, do
32 1.1 christos a = i1; b = i2; c = i3;
33 1.1 christos mix(a,b,c);
34 1.1 christos a += i4; b += i5; c += i6;
35 1.1 christos mix(a,b,c);
36 1.1 christos a += i7;
37 1.1 christos final(a,b,c);
38 1.1 christos then use c as the hash value. If you have a variable length array of
39 1.1 christos 4-byte integers to hash, use hashword(). If you have a byte array (like
40 1.1 christos a character string), use hashlittle(). If you have several byte arrays, or
41 1.1 christos a mix of things, see the comments above hashlittle().
42 1.1 christos
43 1.1 christos Why is this so big? I read 12 bytes at a time into 3 4-byte integers,
44 1.1 christos then mix those integers. This is fast (you can do a lot more thorough
45 1.1 christos mixing with 12*3 instructions on 3 integers than you can with 3 instructions
46 1.1 christos on 1 byte), but shoehorning those bytes into integers efficiently is messy.
47 1.1 christos -------------------------------------------------------------------------------
48 1.1 christos */
49 1.1 christos /*#define SELF_TEST 1*/
50 1.1.1.3 christos #define ARRAY_CLEAN_ACCESS 1
51 1.1 christos
52 1.1 christos #include "config.h"
53 1.1 christos #include "util/storage/lookup3.h"
54 1.1 christos #include <stdio.h> /* defines printf for tests */
55 1.1 christos #include <time.h> /* defines time_t for timings in the test */
56 1.1.1.4 christos
57 1.1.1.4 christos /*
58 1.1.1.4 christos * If our build system provides endianness info, signalled by
59 1.1.1.4 christos * HAVE_TARGET_ENDIANNESS and the presence or absence of TARGET_IS_BIG_ENDIAN,
60 1.1.1.4 christos * use that. Otherwise try to work out the endianness.
61 1.1.1.4 christos */
62 1.1.1.4 christos #if defined(HAVE_TARGET_ENDIANNESS)
63 1.1.1.4 christos # if defined(TARGET_IS_BIG_ENDIAN)
64 1.1.1.4 christos # define HASH_LITTLE_ENDIAN 0
65 1.1.1.4 christos # define HASH_BIG_ENDIAN 1
66 1.1.1.4 christos # else
67 1.1.1.4 christos # define HASH_LITTLE_ENDIAN 1
68 1.1.1.4 christos # define HASH_BIG_ENDIAN 0
69 1.1.1.4 christos # endif
70 1.1.1.4 christos #else
71 1.1.1.4 christos # include <sys/param.h> /* attempt to define endianness */
72 1.1.1.4 christos # ifdef HAVE_SYS_TYPES_H
73 1.1.1.4 christos # include <sys/types.h> /* attempt to define endianness (solaris) */
74 1.1.1.4 christos # endif
75 1.1.1.4 christos # if defined(linux) || defined(__OpenBSD__)
76 1.1 christos # ifdef HAVE_ENDIAN_H
77 1.1 christos # include <endian.h> /* attempt to define endianness */
78 1.1 christos # else
79 1.1 christos # include <machine/endian.h> /* on older OpenBSD */
80 1.1 christos # endif
81 1.1.1.4 christos # endif
82 1.1.1.4 christos # if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
83 1.1.1.4 christos # include <sys/endian.h> /* attempt to define endianness */
84 1.1.1.4 christos # endif
85 1.1.1.4 christos /*
86 1.1.1.4 christos * My best guess at if you are big-endian or little-endian. This may
87 1.1.1.4 christos * need adjustment.
88 1.1.1.4 christos */
89 1.1.1.4 christos # if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
90 1.1.1.4 christos __BYTE_ORDER == __LITTLE_ENDIAN) || \
91 1.1.1.4 christos (defined(i386) || defined(__i386__) || defined(__i486__) || \
92 1.1.1.4 christos defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL) || defined(__x86))
93 1.1.1.4 christos # define HASH_LITTLE_ENDIAN 1
94 1.1.1.4 christos # define HASH_BIG_ENDIAN 0
95 1.1.1.4 christos # elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
96 1.1.1.4 christos __BYTE_ORDER == __BIG_ENDIAN) || \
97 1.1.1.4 christos (defined(sparc) || defined(__sparc) || defined(__sparc__) || defined(POWERPC) || defined(mc68000) || defined(sel))
98 1.1.1.4 christos # define HASH_LITTLE_ENDIAN 0
99 1.1.1.4 christos # define HASH_BIG_ENDIAN 1
100 1.1.1.4 christos # elif defined(_MACHINE_ENDIAN_H_)
101 1.1.1.4 christos /* test for machine_endian_h protects failure if some are empty strings */
102 1.1.1.4 christos # if defined(_BYTE_ORDER) && defined(_BIG_ENDIAN) && _BYTE_ORDER == _BIG_ENDIAN
103 1.1.1.4 christos # define HASH_LITTLE_ENDIAN 0
104 1.1.1.4 christos # define HASH_BIG_ENDIAN 1
105 1.1.1.4 christos # endif
106 1.1.1.4 christos # if defined(_BYTE_ORDER) && defined(_LITTLE_ENDIAN) && _BYTE_ORDER == _LITTLE_ENDIAN
107 1.1.1.4 christos # define HASH_LITTLE_ENDIAN 1
108 1.1.1.4 christos # define HASH_BIG_ENDIAN 0
109 1.1.1.4 christos # endif /* _MACHINE_ENDIAN_H_ */
110 1.1.1.4 christos # else
111 1.1.1.4 christos # define HASH_LITTLE_ENDIAN 0
112 1.1.1.4 christos # define HASH_BIG_ENDIAN 0
113 1.1.1.4 christos # endif
114 1.1.1.4 christos #endif /* defined(HAVE_TARGET_ENDIANNESS) */
115 1.1.1.4 christos
116 1.1.1.4 christos #define hashsize(n) ((uint32_t)1<<(n))
117 1.1.1.4 christos #define hashmask(n) (hashsize(n)-1)
118 1.1.1.4 christos #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
119 1.1 christos
120 1.1 christos /* random initial value */
121 1.1 christos static uint32_t raninit = (uint32_t)0xdeadbeef;
122 1.1 christos
123 1.1 christos void
124 1.1 christos hash_set_raninit(uint32_t v)
125 1.1 christos {
126 1.1 christos raninit = v;
127 1.1 christos }
128 1.1 christos
129 1.1 christos /*
130 1.1 christos -------------------------------------------------------------------------------
131 1.1 christos mix -- mix 3 32-bit values reversibly.
132 1.1 christos
133 1.1 christos This is reversible, so any information in (a,b,c) before mix() is
134 1.1 christos still in (a,b,c) after mix().
135 1.1 christos
136 1.1 christos If four pairs of (a,b,c) inputs are run through mix(), or through
137 1.1 christos mix() in reverse, there are at least 32 bits of the output that
138 1.1 christos are sometimes the same for one pair and different for another pair.
139 1.1 christos This was tested for:
140 1.1 christos * pairs that differed by one bit, by two bits, in any combination
141 1.1 christos of top bits of (a,b,c), or in any combination of bottom bits of
142 1.1 christos (a,b,c).
143 1.1 christos * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
144 1.1 christos the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
145 1.1 christos is commonly produced by subtraction) look like a single 1-bit
146 1.1 christos difference.
147 1.1 christos * the base values were pseudorandom, all zero but one bit set, or
148 1.1 christos all zero plus a counter that starts at zero.
149 1.1 christos
150 1.1 christos Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
151 1.1 christos satisfy this are
152 1.1 christos 4 6 8 16 19 4
153 1.1 christos 9 15 3 18 27 15
154 1.1 christos 14 9 3 7 17 3
155 1.1 christos Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
156 1.1 christos for "differ" defined as + with a one-bit base and a two-bit delta. I
157 1.1 christos used http://burtleburtle.net/bob/hash/avalanche.html to choose
158 1.1 christos the operations, constants, and arrangements of the variables.
159 1.1 christos
160 1.1 christos This does not achieve avalanche. There are input bits of (a,b,c)
161 1.1 christos that fail to affect some output bits of (a,b,c), especially of a. The
162 1.1 christos most thoroughly mixed value is c, but it doesn't really even achieve
163 1.1 christos avalanche in c.
164 1.1 christos
165 1.1 christos This allows some parallelism. Read-after-writes are good at doubling
166 1.1 christos the number of bits affected, so the goal of mixing pulls in the opposite
167 1.1 christos direction as the goal of parallelism. I did what I could. Rotates
168 1.1 christos seem to cost as much as shifts on every machine I could lay my hands
169 1.1 christos on, and rotates are much kinder to the top and bottom bits, so I used
170 1.1 christos rotates.
171 1.1 christos -------------------------------------------------------------------------------
172 1.1 christos */
173 1.1 christos #define mix(a,b,c) \
174 1.1 christos { \
175 1.1 christos a -= c; a ^= rot(c, 4); c += b; \
176 1.1 christos b -= a; b ^= rot(a, 6); a += c; \
177 1.1 christos c -= b; c ^= rot(b, 8); b += a; \
178 1.1 christos a -= c; a ^= rot(c,16); c += b; \
179 1.1 christos b -= a; b ^= rot(a,19); a += c; \
180 1.1 christos c -= b; c ^= rot(b, 4); b += a; \
181 1.1 christos }
182 1.1 christos
183 1.1 christos /*
184 1.1 christos -------------------------------------------------------------------------------
185 1.1 christos final -- final mixing of 3 32-bit values (a,b,c) into c
186 1.1 christos
187 1.1 christos Pairs of (a,b,c) values differing in only a few bits will usually
188 1.1 christos produce values of c that look totally different. This was tested for
189 1.1 christos * pairs that differed by one bit, by two bits, in any combination
190 1.1 christos of top bits of (a,b,c), or in any combination of bottom bits of
191 1.1 christos (a,b,c).
192 1.1 christos * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
193 1.1 christos the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
194 1.1 christos is commonly produced by subtraction) look like a single 1-bit
195 1.1 christos difference.
196 1.1 christos * the base values were pseudorandom, all zero but one bit set, or
197 1.1 christos all zero plus a counter that starts at zero.
198 1.1 christos
199 1.1 christos These constants passed:
200 1.1 christos 14 11 25 16 4 14 24
201 1.1 christos 12 14 25 16 4 14 24
202 1.1 christos and these came close:
203 1.1 christos 4 8 15 26 3 22 24
204 1.1 christos 10 8 15 26 3 22 24
205 1.1 christos 11 8 15 26 3 22 24
206 1.1 christos -------------------------------------------------------------------------------
207 1.1 christos */
208 1.1 christos #define final(a,b,c) \
209 1.1 christos { \
210 1.1 christos c ^= b; c -= rot(b,14); \
211 1.1 christos a ^= c; a -= rot(c,11); \
212 1.1 christos b ^= a; b -= rot(a,25); \
213 1.1 christos c ^= b; c -= rot(b,16); \
214 1.1 christos a ^= c; a -= rot(c,4); \
215 1.1 christos b ^= a; b -= rot(a,14); \
216 1.1 christos c ^= b; c -= rot(b,24); \
217 1.1 christos }
218 1.1 christos
219 1.1 christos /*
220 1.1 christos --------------------------------------------------------------------
221 1.1 christos This works on all machines. To be useful, it requires
222 1.1 christos -- that the key be an array of uint32_t's, and
223 1.1 christos -- that the length be the number of uint32_t's in the key
224 1.1 christos
225 1.1 christos The function hashword() is identical to hashlittle() on little-endian
226 1.1 christos machines, and identical to hashbig() on big-endian machines,
227 1.1 christos except that the length has to be measured in uint32_ts rather than in
228 1.1 christos bytes. hashlittle() is more complicated than hashword() only because
229 1.1 christos hashlittle() has to dance around fitting the key bytes into registers.
230 1.1 christos --------------------------------------------------------------------
231 1.1 christos */
232 1.1 christos uint32_t hashword(
233 1.1 christos const uint32_t *k, /* the key, an array of uint32_t values */
234 1.1 christos size_t length, /* the length of the key, in uint32_ts */
235 1.1 christos uint32_t initval) /* the previous hash, or an arbitrary value */
236 1.1 christos {
237 1.1 christos uint32_t a,b,c;
238 1.1 christos
239 1.1 christos /* Set up the internal state */
240 1.1 christos a = b = c = raninit + (((uint32_t)length)<<2) + initval;
241 1.1 christos
242 1.1 christos /*------------------------------------------------- handle most of the key */
243 1.1 christos while (length > 3)
244 1.1 christos {
245 1.1 christos a += k[0];
246 1.1 christos b += k[1];
247 1.1 christos c += k[2];
248 1.1 christos mix(a,b,c);
249 1.1 christos length -= 3;
250 1.1 christos k += 3;
251 1.1 christos }
252 1.1 christos
253 1.1 christos /*------------------------------------------- handle the last 3 uint32_t's */
254 1.1 christos switch(length) /* all the case statements fall through */
255 1.1 christos {
256 1.1 christos case 3 : c+=k[2];
257 1.1.1.2 christos /* fallthrough */
258 1.1 christos case 2 : b+=k[1];
259 1.1.1.2 christos /* fallthrough */
260 1.1 christos case 1 : a+=k[0];
261 1.1 christos final(a,b,c);
262 1.1 christos case 0: /* case 0: nothing left to add */
263 1.1 christos break;
264 1.1 christos }
265 1.1 christos /*------------------------------------------------------ report the result */
266 1.1 christos return c;
267 1.1 christos }
268 1.1 christos
269 1.1 christos
270 1.1 christos #ifdef SELF_TEST
271 1.1 christos
272 1.1 christos /*
273 1.1 christos --------------------------------------------------------------------
274 1.1 christos hashword2() -- same as hashword(), but take two seeds and return two
275 1.1 christos 32-bit values. pc and pb must both be nonnull, and *pc and *pb must
276 1.1 christos both be initialized with seeds. If you pass in (*pb)==0, the output
277 1.1 christos (*pc) will be the same as the return value from hashword().
278 1.1 christos --------------------------------------------------------------------
279 1.1 christos */
280 1.1 christos void hashword2 (
281 1.1 christos const uint32_t *k, /* the key, an array of uint32_t values */
282 1.1 christos size_t length, /* the length of the key, in uint32_ts */
283 1.1 christos uint32_t *pc, /* IN: seed OUT: primary hash value */
284 1.1 christos uint32_t *pb) /* IN: more seed OUT: secondary hash value */
285 1.1 christos {
286 1.1 christos uint32_t a,b,c;
287 1.1 christos
288 1.1 christos /* Set up the internal state */
289 1.1 christos a = b = c = raninit + ((uint32_t)(length<<2)) + *pc;
290 1.1 christos c += *pb;
291 1.1 christos
292 1.1 christos /*------------------------------------------------- handle most of the key */
293 1.1 christos while (length > 3)
294 1.1 christos {
295 1.1 christos a += k[0];
296 1.1 christos b += k[1];
297 1.1 christos c += k[2];
298 1.1 christos mix(a,b,c);
299 1.1 christos length -= 3;
300 1.1 christos k += 3;
301 1.1 christos }
302 1.1 christos
303 1.1 christos /*------------------------------------------- handle the last 3 uint32_t's */
304 1.1 christos switch(length) /* all the case statements fall through */
305 1.1 christos {
306 1.1 christos case 3 : c+=k[2];
307 1.1 christos case 2 : b+=k[1];
308 1.1 christos case 1 : a+=k[0];
309 1.1 christos final(a,b,c);
310 1.1 christos case 0: /* case 0: nothing left to add */
311 1.1 christos break;
312 1.1 christos }
313 1.1 christos /*------------------------------------------------------ report the result */
314 1.1 christos *pc=c; *pb=b;
315 1.1 christos }
316 1.1 christos
317 1.1 christos #endif /* SELF_TEST */
318 1.1 christos
319 1.1 christos /*
320 1.1 christos -------------------------------------------------------------------------------
321 1.1 christos hashlittle() -- hash a variable-length key into a 32-bit value
322 1.1 christos k : the key (the unaligned variable-length array of bytes)
323 1.1 christos length : the length of the key, counting by bytes
324 1.1 christos initval : can be any 4-byte value
325 1.1 christos Returns a 32-bit value. Every bit of the key affects every bit of
326 1.1 christos the return value. Two keys differing by one or two bits will have
327 1.1 christos totally different hash values.
328 1.1 christos
329 1.1 christos The best hash table sizes are powers of 2. There is no need to do
330 1.1 christos mod a prime (mod is sooo slow!). If you need less than 32 bits,
331 1.1 christos use a bitmask. For example, if you need only 10 bits, do
332 1.1 christos h = (h & hashmask(10));
333 1.1 christos In which case, the hash table should have hashsize(10) elements.
334 1.1 christos
335 1.1 christos If you are hashing n strings (uint8_t **)k, do it like this:
336 1.1 christos for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
337 1.1 christos
338 1.1 christos By Bob Jenkins, 2006. bob_jenkins (at) burtleburtle.net. You may use this
339 1.1 christos code any way you wish, private, educational, or commercial. It's free.
340 1.1 christos
341 1.1 christos Use for hash table lookup, or anything where one collision in 2^^32 is
342 1.1 christos acceptable. Do NOT use for cryptographic purposes.
343 1.1 christos -------------------------------------------------------------------------------
344 1.1 christos */
345 1.1 christos
346 1.1 christos uint32_t hashlittle( const void *key, size_t length, uint32_t initval)
347 1.1 christos {
348 1.1 christos uint32_t a,b,c; /* internal state */
349 1.1 christos union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
350 1.1 christos
351 1.1 christos /* Set up the internal state */
352 1.1 christos a = b = c = raninit + ((uint32_t)length) + initval;
353 1.1 christos
354 1.1 christos u.ptr = key;
355 1.1 christos if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
356 1.1 christos const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
357 1.1.1.3 christos #ifdef ARRAY_CLEAN_ACCESS
358 1.1 christos const uint8_t *k8;
359 1.1 christos #endif
360 1.1 christos
361 1.1 christos /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
362 1.1 christos while (length > 12)
363 1.1 christos {
364 1.1 christos a += k[0];
365 1.1 christos b += k[1];
366 1.1 christos c += k[2];
367 1.1 christos mix(a,b,c);
368 1.1 christos length -= 12;
369 1.1 christos k += 3;
370 1.1 christos }
371 1.1 christos
372 1.1 christos /*----------------------------- handle the last (probably partial) block */
373 1.1 christos /*
374 1.1 christos * "k[2]&0xffffff" actually reads beyond the end of the string, but
375 1.1 christos * then masks off the part it's not allowed to read. Because the
376 1.1 christos * string is aligned, the masked-off tail is in the same word as the
377 1.1 christos * rest of the string. Every machine with memory protection I've seen
378 1.1 christos * does it on word boundaries, so is OK with this. But VALGRIND will
379 1.1 christos * still catch it and complain. The masking trick does make the hash
380 1.1 christos * noticeably faster for short strings (like English words).
381 1.1 christos */
382 1.1.1.3 christos #ifndef ARRAY_CLEAN_ACCESS
383 1.1 christos
384 1.1 christos switch(length)
385 1.1 christos {
386 1.1 christos case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
387 1.1 christos case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
388 1.1 christos case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
389 1.1 christos case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
390 1.1 christos case 8 : b+=k[1]; a+=k[0]; break;
391 1.1 christos case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
392 1.1 christos case 6 : b+=k[1]&0xffff; a+=k[0]; break;
393 1.1 christos case 5 : b+=k[1]&0xff; a+=k[0]; break;
394 1.1 christos case 4 : a+=k[0]; break;
395 1.1 christos case 3 : a+=k[0]&0xffffff; break;
396 1.1 christos case 2 : a+=k[0]&0xffff; break;
397 1.1 christos case 1 : a+=k[0]&0xff; break;
398 1.1 christos case 0 : return c; /* zero length strings require no mixing */
399 1.1 christos }
400 1.1 christos
401 1.1 christos #else /* make valgrind happy */
402 1.1 christos
403 1.1 christos k8 = (const uint8_t *)k;
404 1.1 christos switch(length)
405 1.1 christos {
406 1.1 christos case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
407 1.1 christos case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
408 1.1 christos case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
409 1.1 christos case 9 : c+=k8[8]; /* fall through */
410 1.1 christos case 8 : b+=k[1]; a+=k[0]; break;
411 1.1 christos case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
412 1.1 christos case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
413 1.1 christos case 5 : b+=k8[4]; /* fall through */
414 1.1 christos case 4 : a+=k[0]; break;
415 1.1 christos case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
416 1.1 christos case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
417 1.1 christos case 1 : a+=k8[0]; break;
418 1.1 christos case 0 : return c;
419 1.1 christos }
420 1.1 christos
421 1.1 christos #endif /* !valgrind */
422 1.1 christos
423 1.1 christos } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
424 1.1 christos const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
425 1.1 christos const uint8_t *k8;
426 1.1 christos
427 1.1 christos /*--------------- all but last block: aligned reads and different mixing */
428 1.1 christos while (length > 12)
429 1.1 christos {
430 1.1 christos a += k[0] + (((uint32_t)k[1])<<16);
431 1.1 christos b += k[2] + (((uint32_t)k[3])<<16);
432 1.1 christos c += k[4] + (((uint32_t)k[5])<<16);
433 1.1 christos mix(a,b,c);
434 1.1 christos length -= 12;
435 1.1 christos k += 6;
436 1.1 christos }
437 1.1 christos
438 1.1 christos /*----------------------------- handle the last (probably partial) block */
439 1.1 christos k8 = (const uint8_t *)k;
440 1.1 christos switch(length)
441 1.1 christos {
442 1.1 christos case 12: c+=k[4]+(((uint32_t)k[5])<<16);
443 1.1 christos b+=k[2]+(((uint32_t)k[3])<<16);
444 1.1 christos a+=k[0]+(((uint32_t)k[1])<<16);
445 1.1 christos break;
446 1.1 christos case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
447 1.1 christos case 10: c+=k[4];
448 1.1 christos b+=k[2]+(((uint32_t)k[3])<<16);
449 1.1 christos a+=k[0]+(((uint32_t)k[1])<<16);
450 1.1 christos break;
451 1.1 christos case 9 : c+=k8[8]; /* fall through */
452 1.1 christos case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
453 1.1 christos a+=k[0]+(((uint32_t)k[1])<<16);
454 1.1 christos break;
455 1.1 christos case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
456 1.1 christos case 6 : b+=k[2];
457 1.1 christos a+=k[0]+(((uint32_t)k[1])<<16);
458 1.1 christos break;
459 1.1 christos case 5 : b+=k8[4]; /* fall through */
460 1.1 christos case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
461 1.1 christos break;
462 1.1 christos case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
463 1.1 christos case 2 : a+=k[0];
464 1.1 christos break;
465 1.1 christos case 1 : a+=k8[0];
466 1.1 christos break;
467 1.1 christos case 0 : return c; /* zero length requires no mixing */
468 1.1 christos }
469 1.1 christos
470 1.1 christos } else { /* need to read the key one byte at a time */
471 1.1 christos const uint8_t *k = (const uint8_t *)key;
472 1.1 christos
473 1.1 christos /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
474 1.1 christos while (length > 12)
475 1.1 christos {
476 1.1 christos a += k[0];
477 1.1 christos a += ((uint32_t)k[1])<<8;
478 1.1 christos a += ((uint32_t)k[2])<<16;
479 1.1 christos a += ((uint32_t)k[3])<<24;
480 1.1 christos b += k[4];
481 1.1 christos b += ((uint32_t)k[5])<<8;
482 1.1 christos b += ((uint32_t)k[6])<<16;
483 1.1 christos b += ((uint32_t)k[7])<<24;
484 1.1 christos c += k[8];
485 1.1 christos c += ((uint32_t)k[9])<<8;
486 1.1 christos c += ((uint32_t)k[10])<<16;
487 1.1 christos c += ((uint32_t)k[11])<<24;
488 1.1 christos mix(a,b,c);
489 1.1 christos length -= 12;
490 1.1 christos k += 12;
491 1.1 christos }
492 1.1 christos
493 1.1 christos /*-------------------------------- last block: affect all 32 bits of (c) */
494 1.1 christos switch(length) /* all the case statements fall through */
495 1.1 christos {
496 1.1 christos case 12: c+=((uint32_t)k[11])<<24;
497 1.1.1.2 christos /* fallthrough */
498 1.1 christos case 11: c+=((uint32_t)k[10])<<16;
499 1.1.1.2 christos /* fallthrough */
500 1.1 christos case 10: c+=((uint32_t)k[9])<<8;
501 1.1.1.2 christos /* fallthrough */
502 1.1 christos case 9 : c+=k[8];
503 1.1.1.2 christos /* fallthrough */
504 1.1 christos case 8 : b+=((uint32_t)k[7])<<24;
505 1.1.1.2 christos /* fallthrough */
506 1.1 christos case 7 : b+=((uint32_t)k[6])<<16;
507 1.1.1.2 christos /* fallthrough */
508 1.1 christos case 6 : b+=((uint32_t)k[5])<<8;
509 1.1.1.2 christos /* fallthrough */
510 1.1 christos case 5 : b+=k[4];
511 1.1.1.2 christos /* fallthrough */
512 1.1 christos case 4 : a+=((uint32_t)k[3])<<24;
513 1.1.1.2 christos /* fallthrough */
514 1.1 christos case 3 : a+=((uint32_t)k[2])<<16;
515 1.1.1.2 christos /* fallthrough */
516 1.1 christos case 2 : a+=((uint32_t)k[1])<<8;
517 1.1.1.2 christos /* fallthrough */
518 1.1 christos case 1 : a+=k[0];
519 1.1 christos break;
520 1.1 christos case 0 : return c;
521 1.1 christos }
522 1.1 christos }
523 1.1 christos
524 1.1 christos final(a,b,c);
525 1.1 christos return c;
526 1.1 christos }
527 1.1 christos
528 1.1 christos #ifdef SELF_TEST
529 1.1 christos
530 1.1 christos /*
531 1.1 christos * hashlittle2: return 2 32-bit hash values
532 1.1 christos *
533 1.1 christos * This is identical to hashlittle(), except it returns two 32-bit hash
534 1.1 christos * values instead of just one. This is good enough for hash table
535 1.1 christos * lookup with 2^^64 buckets, or if you want a second hash if you're not
536 1.1 christos * happy with the first, or if you want a probably-unique 64-bit ID for
537 1.1 christos * the key. *pc is better mixed than *pb, so use *pc first. If you want
538 1.1 christos * a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)".
539 1.1 christos */
540 1.1 christos void hashlittle2(
541 1.1 christos const void *key, /* the key to hash */
542 1.1 christos size_t length, /* length of the key */
543 1.1 christos uint32_t *pc, /* IN: primary initval, OUT: primary hash */
544 1.1 christos uint32_t *pb) /* IN: secondary initval, OUT: secondary hash */
545 1.1 christos {
546 1.1 christos uint32_t a,b,c; /* internal state */
547 1.1 christos union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
548 1.1 christos
549 1.1 christos /* Set up the internal state */
550 1.1 christos a = b = c = raninit + ((uint32_t)length) + *pc;
551 1.1 christos c += *pb;
552 1.1 christos
553 1.1 christos u.ptr = key;
554 1.1 christos if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
555 1.1 christos const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
556 1.1 christos #ifdef VALGRIND
557 1.1 christos const uint8_t *k8;
558 1.1 christos #endif
559 1.1 christos
560 1.1 christos /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
561 1.1 christos while (length > 12)
562 1.1 christos {
563 1.1 christos a += k[0];
564 1.1 christos b += k[1];
565 1.1 christos c += k[2];
566 1.1 christos mix(a,b,c);
567 1.1 christos length -= 12;
568 1.1 christos k += 3;
569 1.1 christos }
570 1.1 christos
571 1.1 christos /*----------------------------- handle the last (probably partial) block */
572 1.1 christos /*
573 1.1 christos * "k[2]&0xffffff" actually reads beyond the end of the string, but
574 1.1 christos * then masks off the part it's not allowed to read. Because the
575 1.1 christos * string is aligned, the masked-off tail is in the same word as the
576 1.1 christos * rest of the string. Every machine with memory protection I've seen
577 1.1 christos * does it on word boundaries, so is OK with this. But VALGRIND will
578 1.1 christos * still catch it and complain. The masking trick does make the hash
579 1.1 christos * noticeably faster for short strings (like English words).
580 1.1 christos */
581 1.1 christos #ifndef VALGRIND
582 1.1 christos
583 1.1 christos switch(length)
584 1.1 christos {
585 1.1 christos case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
586 1.1 christos case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
587 1.1 christos case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
588 1.1 christos case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
589 1.1 christos case 8 : b+=k[1]; a+=k[0]; break;
590 1.1 christos case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
591 1.1 christos case 6 : b+=k[1]&0xffff; a+=k[0]; break;
592 1.1 christos case 5 : b+=k[1]&0xff; a+=k[0]; break;
593 1.1 christos case 4 : a+=k[0]; break;
594 1.1 christos case 3 : a+=k[0]&0xffffff; break;
595 1.1 christos case 2 : a+=k[0]&0xffff; break;
596 1.1 christos case 1 : a+=k[0]&0xff; break;
597 1.1 christos case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
598 1.1 christos }
599 1.1 christos
600 1.1 christos #else /* make valgrind happy */
601 1.1 christos
602 1.1 christos k8 = (const uint8_t *)k;
603 1.1 christos switch(length)
604 1.1 christos {
605 1.1 christos case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
606 1.1 christos case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
607 1.1 christos case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
608 1.1 christos case 9 : c+=k8[8]; /* fall through */
609 1.1 christos case 8 : b+=k[1]; a+=k[0]; break;
610 1.1 christos case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
611 1.1 christos case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
612 1.1 christos case 5 : b+=k8[4]; /* fall through */
613 1.1 christos case 4 : a+=k[0]; break;
614 1.1 christos case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
615 1.1 christos case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
616 1.1 christos case 1 : a+=k8[0]; break;
617 1.1 christos case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
618 1.1 christos }
619 1.1 christos
620 1.1 christos #endif /* !valgrind */
621 1.1 christos
622 1.1 christos } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
623 1.1 christos const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
624 1.1 christos const uint8_t *k8;
625 1.1 christos
626 1.1 christos /*--------------- all but last block: aligned reads and different mixing */
627 1.1 christos while (length > 12)
628 1.1 christos {
629 1.1 christos a += k[0] + (((uint32_t)k[1])<<16);
630 1.1 christos b += k[2] + (((uint32_t)k[3])<<16);
631 1.1 christos c += k[4] + (((uint32_t)k[5])<<16);
632 1.1 christos mix(a,b,c);
633 1.1 christos length -= 12;
634 1.1 christos k += 6;
635 1.1 christos }
636 1.1 christos
637 1.1 christos /*----------------------------- handle the last (probably partial) block */
638 1.1 christos k8 = (const uint8_t *)k;
639 1.1 christos switch(length)
640 1.1 christos {
641 1.1 christos case 12: c+=k[4]+(((uint32_t)k[5])<<16);
642 1.1 christos b+=k[2]+(((uint32_t)k[3])<<16);
643 1.1 christos a+=k[0]+(((uint32_t)k[1])<<16);
644 1.1 christos break;
645 1.1 christos case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
646 1.1 christos case 10: c+=k[4];
647 1.1 christos b+=k[2]+(((uint32_t)k[3])<<16);
648 1.1 christos a+=k[0]+(((uint32_t)k[1])<<16);
649 1.1 christos break;
650 1.1 christos case 9 : c+=k8[8]; /* fall through */
651 1.1 christos case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
652 1.1 christos a+=k[0]+(((uint32_t)k[1])<<16);
653 1.1 christos break;
654 1.1 christos case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
655 1.1 christos case 6 : b+=k[2];
656 1.1 christos a+=k[0]+(((uint32_t)k[1])<<16);
657 1.1 christos break;
658 1.1 christos case 5 : b+=k8[4]; /* fall through */
659 1.1 christos case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
660 1.1 christos break;
661 1.1 christos case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
662 1.1 christos case 2 : a+=k[0];
663 1.1 christos break;
664 1.1 christos case 1 : a+=k8[0];
665 1.1 christos break;
666 1.1 christos case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
667 1.1 christos }
668 1.1 christos
669 1.1 christos } else { /* need to read the key one byte at a time */
670 1.1 christos const uint8_t *k = (const uint8_t *)key;
671 1.1 christos
672 1.1 christos /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
673 1.1 christos while (length > 12)
674 1.1 christos {
675 1.1 christos a += k[0];
676 1.1 christos a += ((uint32_t)k[1])<<8;
677 1.1 christos a += ((uint32_t)k[2])<<16;
678 1.1 christos a += ((uint32_t)k[3])<<24;
679 1.1 christos b += k[4];
680 1.1 christos b += ((uint32_t)k[5])<<8;
681 1.1 christos b += ((uint32_t)k[6])<<16;
682 1.1 christos b += ((uint32_t)k[7])<<24;
683 1.1 christos c += k[8];
684 1.1 christos c += ((uint32_t)k[9])<<8;
685 1.1 christos c += ((uint32_t)k[10])<<16;
686 1.1 christos c += ((uint32_t)k[11])<<24;
687 1.1 christos mix(a,b,c);
688 1.1 christos length -= 12;
689 1.1 christos k += 12;
690 1.1 christos }
691 1.1 christos
692 1.1 christos /*-------------------------------- last block: affect all 32 bits of (c) */
693 1.1 christos switch(length) /* all the case statements fall through */
694 1.1 christos {
695 1.1 christos case 12: c+=((uint32_t)k[11])<<24;
696 1.1 christos case 11: c+=((uint32_t)k[10])<<16;
697 1.1 christos case 10: c+=((uint32_t)k[9])<<8;
698 1.1 christos case 9 : c+=k[8];
699 1.1 christos case 8 : b+=((uint32_t)k[7])<<24;
700 1.1 christos case 7 : b+=((uint32_t)k[6])<<16;
701 1.1 christos case 6 : b+=((uint32_t)k[5])<<8;
702 1.1 christos case 5 : b+=k[4];
703 1.1 christos case 4 : a+=((uint32_t)k[3])<<24;
704 1.1 christos case 3 : a+=((uint32_t)k[2])<<16;
705 1.1 christos case 2 : a+=((uint32_t)k[1])<<8;
706 1.1 christos case 1 : a+=k[0];
707 1.1 christos break;
708 1.1 christos case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
709 1.1 christos }
710 1.1 christos }
711 1.1 christos
712 1.1 christos final(a,b,c);
713 1.1 christos *pc=c; *pb=b;
714 1.1 christos }
715 1.1 christos
716 1.1 christos #endif /* SELF_TEST */
717 1.1 christos
718 1.1 christos #if 0 /* currently not used */
719 1.1 christos
720 1.1 christos /*
721 1.1 christos * hashbig():
722 1.1 christos * This is the same as hashword() on big-endian machines. It is different
723 1.1 christos * from hashlittle() on all machines. hashbig() takes advantage of
724 1.1 christos * big-endian byte ordering.
725 1.1 christos */
726 1.1 christos uint32_t hashbig( const void *key, size_t length, uint32_t initval)
727 1.1 christos {
728 1.1 christos uint32_t a,b,c;
729 1.1 christos union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */
730 1.1 christos
731 1.1 christos /* Set up the internal state */
732 1.1 christos a = b = c = raninit + ((uint32_t)length) + initval;
733 1.1 christos
734 1.1 christos u.ptr = key;
735 1.1 christos if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
736 1.1 christos const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
737 1.1 christos #ifdef VALGRIND
738 1.1 christos const uint8_t *k8;
739 1.1 christos #endif
740 1.1 christos
741 1.1 christos /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
742 1.1 christos while (length > 12)
743 1.1 christos {
744 1.1 christos a += k[0];
745 1.1 christos b += k[1];
746 1.1 christos c += k[2];
747 1.1 christos mix(a,b,c);
748 1.1 christos length -= 12;
749 1.1 christos k += 3;
750 1.1 christos }
751 1.1 christos
752 1.1 christos /*----------------------------- handle the last (probably partial) block */
753 1.1 christos /*
754 1.1 christos * "k[2]<<8" actually reads beyond the end of the string, but
755 1.1 christos * then shifts out the part it's not allowed to read. Because the
756 1.1 christos * string is aligned, the illegal read is in the same word as the
757 1.1 christos * rest of the string. Every machine with memory protection I've seen
758 1.1 christos * does it on word boundaries, so is OK with this. But VALGRIND will
759 1.1 christos * still catch it and complain. The masking trick does make the hash
760 1.1 christos * noticeably faster for short strings (like English words).
761 1.1 christos */
762 1.1 christos #ifndef VALGRIND
763 1.1 christos
764 1.1 christos switch(length)
765 1.1 christos {
766 1.1 christos case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
767 1.1 christos case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;
768 1.1 christos case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;
769 1.1 christos case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break;
770 1.1 christos case 8 : b+=k[1]; a+=k[0]; break;
771 1.1 christos case 7 : b+=k[1]&0xffffff00; a+=k[0]; break;
772 1.1 christos case 6 : b+=k[1]&0xffff0000; a+=k[0]; break;
773 1.1 christos case 5 : b+=k[1]&0xff000000; a+=k[0]; break;
774 1.1 christos case 4 : a+=k[0]; break;
775 1.1 christos case 3 : a+=k[0]&0xffffff00; break;
776 1.1 christos case 2 : a+=k[0]&0xffff0000; break;
777 1.1 christos case 1 : a+=k[0]&0xff000000; break;
778 1.1 christos case 0 : return c; /* zero length strings require no mixing */
779 1.1 christos }
780 1.1 christos
781 1.1 christos #else /* make valgrind happy */
782 1.1 christos
783 1.1 christos k8 = (const uint8_t *)k;
784 1.1 christos switch(length) /* all the case statements fall through */
785 1.1 christos {
786 1.1 christos case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
787 1.1 christos case 11: c+=((uint32_t)k8[10])<<8; /* fall through */
788 1.1 christos case 10: c+=((uint32_t)k8[9])<<16; /* fall through */
789 1.1 christos case 9 : c+=((uint32_t)k8[8])<<24; /* fall through */
790 1.1 christos case 8 : b+=k[1]; a+=k[0]; break;
791 1.1 christos case 7 : b+=((uint32_t)k8[6])<<8; /* fall through */
792 1.1 christos case 6 : b+=((uint32_t)k8[5])<<16; /* fall through */
793 1.1 christos case 5 : b+=((uint32_t)k8[4])<<24; /* fall through */
794 1.1 christos case 4 : a+=k[0]; break;
795 1.1 christos case 3 : a+=((uint32_t)k8[2])<<8; /* fall through */
796 1.1 christos case 2 : a+=((uint32_t)k8[1])<<16; /* fall through */
797 1.1 christos case 1 : a+=((uint32_t)k8[0])<<24; break;
798 1.1 christos case 0 : return c;
799 1.1 christos }
800 1.1 christos
801 1.1 christos #endif /* !VALGRIND */
802 1.1 christos
803 1.1 christos } else { /* need to read the key one byte at a time */
804 1.1 christos const uint8_t *k = (const uint8_t *)key;
805 1.1 christos
806 1.1 christos /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
807 1.1 christos while (length > 12)
808 1.1 christos {
809 1.1 christos a += ((uint32_t)k[0])<<24;
810 1.1 christos a += ((uint32_t)k[1])<<16;
811 1.1 christos a += ((uint32_t)k[2])<<8;
812 1.1 christos a += ((uint32_t)k[3]);
813 1.1 christos b += ((uint32_t)k[4])<<24;
814 1.1 christos b += ((uint32_t)k[5])<<16;
815 1.1 christos b += ((uint32_t)k[6])<<8;
816 1.1 christos b += ((uint32_t)k[7]);
817 1.1 christos c += ((uint32_t)k[8])<<24;
818 1.1 christos c += ((uint32_t)k[9])<<16;
819 1.1 christos c += ((uint32_t)k[10])<<8;
820 1.1 christos c += ((uint32_t)k[11]);
821 1.1 christos mix(a,b,c);
822 1.1 christos length -= 12;
823 1.1 christos k += 12;
824 1.1 christos }
825 1.1 christos
826 1.1 christos /*-------------------------------- last block: affect all 32 bits of (c) */
827 1.1 christos switch(length) /* all the case statements fall through */
828 1.1 christos {
829 1.1 christos case 12: c+=k[11];
830 1.1 christos case 11: c+=((uint32_t)k[10])<<8;
831 1.1 christos case 10: c+=((uint32_t)k[9])<<16;
832 1.1 christos case 9 : c+=((uint32_t)k[8])<<24;
833 1.1 christos case 8 : b+=k[7];
834 1.1 christos case 7 : b+=((uint32_t)k[6])<<8;
835 1.1 christos case 6 : b+=((uint32_t)k[5])<<16;
836 1.1 christos case 5 : b+=((uint32_t)k[4])<<24;
837 1.1 christos case 4 : a+=k[3];
838 1.1 christos case 3 : a+=((uint32_t)k[2])<<8;
839 1.1 christos case 2 : a+=((uint32_t)k[1])<<16;
840 1.1 christos case 1 : a+=((uint32_t)k[0])<<24;
841 1.1 christos break;
842 1.1 christos case 0 : return c;
843 1.1 christos }
844 1.1 christos }
845 1.1 christos
846 1.1 christos final(a,b,c);
847 1.1 christos return c;
848 1.1 christos }
849 1.1 christos
850 1.1 christos #endif /* 0 == currently not used */
851 1.1 christos
852 1.1 christos #ifdef SELF_TEST
853 1.1 christos
854 1.1 christos /* used for timings */
855 1.1.1.2 christos void driver1(void)
856 1.1 christos {
857 1.1 christos uint8_t buf[256];
858 1.1 christos uint32_t i;
859 1.1 christos uint32_t h=0;
860 1.1 christos time_t a,z;
861 1.1 christos
862 1.1 christos time(&a);
863 1.1 christos for (i=0; i<256; ++i) buf[i] = 'x';
864 1.1 christos for (i=0; i<1; ++i)
865 1.1 christos {
866 1.1 christos h = hashlittle(&buf[0],1,h);
867 1.1 christos }
868 1.1 christos time(&z);
869 1.1 christos if (z-a > 0) printf("time %d %.8x\n", z-a, h);
870 1.1 christos }
871 1.1 christos
872 1.1 christos /* check that every input bit changes every output bit half the time */
873 1.1 christos #define HASHSTATE 1
874 1.1 christos #define HASHLEN 1
875 1.1 christos #define MAXPAIR 60
876 1.1 christos #define MAXLEN 70
877 1.1.1.2 christos void driver2(void)
878 1.1 christos {
879 1.1 christos uint8_t qa[MAXLEN+1], qb[MAXLEN+2], *a = &qa[0], *b = &qb[1];
880 1.1 christos uint32_t c[HASHSTATE], d[HASHSTATE], i=0, j=0, k, l, m=0, z;
881 1.1 christos uint32_t e[HASHSTATE],f[HASHSTATE],g[HASHSTATE],h[HASHSTATE];
882 1.1 christos uint32_t x[HASHSTATE],y[HASHSTATE];
883 1.1 christos uint32_t hlen;
884 1.1 christos
885 1.1 christos printf("No more than %d trials should ever be needed \n",MAXPAIR/2);
886 1.1 christos for (hlen=0; hlen < MAXLEN; ++hlen)
887 1.1 christos {
888 1.1 christos z=0;
889 1.1 christos for (i=0; i<hlen; ++i) /*----------------------- for each input byte, */
890 1.1 christos {
891 1.1 christos for (j=0; j<8; ++j) /*------------------------ for each input bit, */
892 1.1 christos {
893 1.1 christos for (m=1; m<8; ++m) /*------------ for several possible initvals, */
894 1.1 christos {
895 1.1 christos for (l=0; l<HASHSTATE; ++l)
896 1.1 christos e[l]=f[l]=g[l]=h[l]=x[l]=y[l]=~((uint32_t)0);
897 1.1 christos
898 1.1 christos /*---- check that every output bit is affected by that input bit */
899 1.1 christos for (k=0; k<MAXPAIR; k+=2)
900 1.1 christos {
901 1.1 christos uint32_t finished=1;
902 1.1 christos /* keys have one bit different */
903 1.1 christos for (l=0; l<hlen+1; ++l) {a[l] = b[l] = (uint8_t)0;}
904 1.1 christos /* have a and b be two keys differing in only one bit */
905 1.1 christos a[i] ^= (k<<j);
906 1.1 christos a[i] ^= (k>>(8-j));
907 1.1 christos c[0] = hashlittle(a, hlen, m);
908 1.1 christos b[i] ^= ((k+1)<<j);
909 1.1 christos b[i] ^= ((k+1)>>(8-j));
910 1.1 christos d[0] = hashlittle(b, hlen, m);
911 1.1 christos /* check every bit is 1, 0, set, and not set at least once */
912 1.1 christos for (l=0; l<HASHSTATE; ++l)
913 1.1 christos {
914 1.1 christos e[l] &= (c[l]^d[l]);
915 1.1 christos f[l] &= ~(c[l]^d[l]);
916 1.1 christos g[l] &= c[l];
917 1.1 christos h[l] &= ~c[l];
918 1.1 christos x[l] &= d[l];
919 1.1 christos y[l] &= ~d[l];
920 1.1 christos if (e[l]|f[l]|g[l]|h[l]|x[l]|y[l]) finished=0;
921 1.1 christos }
922 1.1 christos if (finished) break;
923 1.1 christos }
924 1.1 christos if (k>z) z=k;
925 1.1 christos if (k==MAXPAIR)
926 1.1 christos {
927 1.1 christos printf("Some bit didn't change: ");
928 1.1 christos printf("%.8x %.8x %.8x %.8x %.8x %.8x ",
929 1.1 christos e[0],f[0],g[0],h[0],x[0],y[0]);
930 1.1 christos printf("i %d j %d m %d len %d\n", i, j, m, hlen);
931 1.1 christos }
932 1.1 christos if (z==MAXPAIR) goto done;
933 1.1 christos }
934 1.1 christos }
935 1.1 christos }
936 1.1 christos done:
937 1.1 christos if (z < MAXPAIR)
938 1.1 christos {
939 1.1 christos printf("Mix success %2d bytes %2d initvals ",i,m);
940 1.1 christos printf("required %d trials\n", z/2);
941 1.1 christos }
942 1.1 christos }
943 1.1 christos printf("\n");
944 1.1 christos }
945 1.1 christos
946 1.1 christos /* Check for reading beyond the end of the buffer and alignment problems */
947 1.1.1.2 christos void driver3(void)
948 1.1 christos {
949 1.1 christos uint8_t buf[MAXLEN+20], *b;
950 1.1 christos uint32_t len;
951 1.1 christos uint8_t q[] = "This is the time for all good men to come to the aid of their country...";
952 1.1 christos uint32_t h;
953 1.1 christos uint8_t qq[] = "xThis is the time for all good men to come to the aid of their country...";
954 1.1 christos uint32_t i;
955 1.1 christos uint8_t qqq[] = "xxThis is the time for all good men to come to the aid of their country...";
956 1.1 christos uint32_t j;
957 1.1 christos uint8_t qqqq[] = "xxxThis is the time for all good men to come to the aid of their country...";
958 1.1 christos uint32_t ref,x,y;
959 1.1 christos uint8_t *p;
960 1.1 christos
961 1.1 christos printf("Endianness. These lines should all be the same (for values filled in):\n");
962 1.1 christos printf("%.8x %.8x %.8x\n",
963 1.1 christos hashword((const uint32_t *)q, (sizeof(q)-1)/4, 13),
964 1.1 christos hashword((const uint32_t *)q, (sizeof(q)-5)/4, 13),
965 1.1 christos hashword((const uint32_t *)q, (sizeof(q)-9)/4, 13));
966 1.1 christos p = q;
967 1.1 christos printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
968 1.1 christos hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
969 1.1 christos hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
970 1.1 christos hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
971 1.1 christos hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
972 1.1 christos hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
973 1.1 christos hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
974 1.1 christos p = &qq[1];
975 1.1 christos printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
976 1.1 christos hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
977 1.1 christos hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
978 1.1 christos hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
979 1.1 christos hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
980 1.1 christos hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
981 1.1 christos hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
982 1.1 christos p = &qqq[2];
983 1.1 christos printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
984 1.1 christos hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
985 1.1 christos hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
986 1.1 christos hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
987 1.1 christos hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
988 1.1 christos hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
989 1.1 christos hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
990 1.1 christos p = &qqqq[3];
991 1.1 christos printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n",
992 1.1 christos hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13),
993 1.1 christos hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13),
994 1.1 christos hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13),
995 1.1 christos hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13),
996 1.1 christos hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13),
997 1.1 christos hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13));
998 1.1 christos printf("\n");
999 1.1 christos
1000 1.1 christos /* check that hashlittle2 and hashlittle produce the same results */
1001 1.1 christos i=47; j=0;
1002 1.1 christos hashlittle2(q, sizeof(q), &i, &j);
1003 1.1 christos if (hashlittle(q, sizeof(q), 47) != i)
1004 1.1 christos printf("hashlittle2 and hashlittle mismatch\n");
1005 1.1 christos
1006 1.1 christos /* check that hashword2 and hashword produce the same results */
1007 1.1 christos len = raninit;
1008 1.1 christos i=47, j=0;
1009 1.1 christos hashword2(&len, 1, &i, &j);
1010 1.1 christos if (hashword(&len, 1, 47) != i)
1011 1.1 christos printf("hashword2 and hashword mismatch %x %x\n",
1012 1.1 christos i, hashword(&len, 1, 47));
1013 1.1 christos
1014 1.1 christos /* check hashlittle doesn't read before or after the ends of the string */
1015 1.1 christos for (h=0, b=buf+1; h<8; ++h, ++b)
1016 1.1 christos {
1017 1.1 christos for (i=0; i<MAXLEN; ++i)
1018 1.1 christos {
1019 1.1 christos len = i;
1020 1.1 christos for (j=0; j<i; ++j) *(b+j)=0;
1021 1.1 christos
1022 1.1 christos /* these should all be equal */
1023 1.1 christos ref = hashlittle(b, len, (uint32_t)1);
1024 1.1 christos *(b+i)=(uint8_t)~0;
1025 1.1 christos *(b-1)=(uint8_t)~0;
1026 1.1 christos x = hashlittle(b, len, (uint32_t)1);
1027 1.1 christos y = hashlittle(b, len, (uint32_t)1);
1028 1.1 christos if ((ref != x) || (ref != y))
1029 1.1 christos {
1030 1.1 christos printf("alignment error: %.8x %.8x %.8x %d %d\n",ref,x,y,
1031 1.1 christos h, i);
1032 1.1 christos }
1033 1.1 christos }
1034 1.1 christos }
1035 1.1 christos }
1036 1.1 christos
1037 1.1 christos /* check for problems with nulls */
1038 1.1.1.2 christos void driver4(void)
1039 1.1 christos {
1040 1.1 christos uint8_t buf[1];
1041 1.1 christos uint32_t h,i,state[HASHSTATE];
1042 1.1 christos
1043 1.1 christos
1044 1.1 christos buf[0] = ~0;
1045 1.1 christos for (i=0; i<HASHSTATE; ++i) state[i] = 1;
1046 1.1 christos printf("These should all be different\n");
1047 1.1 christos for (i=0, h=0; i<8; ++i)
1048 1.1 christos {
1049 1.1 christos h = hashlittle(buf, 0, h);
1050 1.1 christos printf("%2ld 0-byte strings, hash is %.8x\n", i, h);
1051 1.1 christos }
1052 1.1 christos }
1053 1.1 christos
1054 1.1 christos
1055 1.1.1.2 christos int main(void)
1056 1.1 christos {
1057 1.1 christos driver1(); /* test that the key is hashed: used for timings */
1058 1.1 christos driver2(); /* test that whole key is hashed thoroughly */
1059 1.1 christos driver3(); /* test that nothing but the key is hashed */
1060 1.1 christos driver4(); /* test hashing multiple buffers (all buffers are null) */
1061 1.1 christos return 1;
1062 1.1 christos }
1063 1.1 christos
1064 1.1 christos #endif /* SELF_TEST */
1065