1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* Utilities for hashing. */
10 * This file exports functions for hashing data down to a uint32_t (a.k.a.
11 * mozilla::HashNumber), including:
13 * - HashString Hash a char* or char16_t/wchar_t* of known or unknown
16 * - HashBytes Hash a byte array of known length.
18 * - HashGeneric Hash one or more values. Currently, we support uint32_t,
19 * types which can be implicitly cast to uint32_t, data
20 * pointers, and function pointers.
22 * - AddToHash Add one or more values to the given hash. This supports the
23 * same list of types as HashGeneric.
26 * You can chain these functions together to hash complex objects. For example:
31 * uint32_t mUint1, mUint2;
32 * void (*mCallbackFn)();
37 * HashNumber hash = HashString(mStr);
38 * hash = AddToHash(hash, mUint1, mUint2);
39 * return AddToHash(hash, mCallbackFn);
43 * If you want to hash an nsAString or nsACString, use the HashString functions
47 #ifndef mozilla_HashFunctions_h
48 #define mozilla_HashFunctions_h
50 #include "mozilla/Assertions.h"
51 #include "mozilla/Attributes.h"
52 #include "mozilla/Char16.h"
53 #include "mozilla/MathAlgorithms.h"
54 #include "mozilla/Types.h"
55 #include "mozilla/WrappingOperations.h"
61 using HashNumber
= uint32_t;
62 static const uint32_t kHashNumberBits
= 32;
65 * The golden ratio as a 32-bit fixed-point value.
67 static const HashNumber kGoldenRatioU32
= 0x9E3779B9U
;
70 * Given a raw hash code, h, return a number that can be used to select a hash
73 * This function aims to produce as uniform an output distribution as possible,
74 * especially in the most significant (leftmost) bits, even though the input
75 * distribution may be highly nonrandom, given the constraints that this must
76 * be deterministic and quick to compute.
78 * Since the leftmost bits of the result are best, the hash bucket index is
79 * computed by doing ScrambleHashCode(h) / (2^32/N) or the equivalent
80 * right-shift, not ScrambleHashCode(h) % N or the equivalent bit-mask.
82 * FIXME: OrderedHashTable uses a bit-mask; see bug 775896.
85 ScrambleHashCode(HashNumber h
)
88 * Simply returning h would not cause any hash tables to produce wrong
89 * answers. But it can produce pathologically bad performance: The caller
90 * right-shifts the result, keeping only the highest bits. The high bits of
91 * hash codes are very often completely entropy-free. (So are the lowest
94 * So we use Fibonacci hashing, as described in Knuth, The Art of Computer
95 * Programming, 6.4. This mixes all the bits of the input hash code h.
97 * The value of goldenRatio is taken from the hex expansion of the golden
98 * ratio, which starts 1.9E3779B9.... This value is especially good if
99 * values with consecutive hash codes are stored in a hash table; see Knuth
102 return mozilla::WrappingMultiply(h
, kGoldenRatioU32
);
107 MOZ_NO_SANITIZE_UNSIGNED_OVERFLOW
109 RotateLeft5(HashNumber aValue
)
111 return (aValue
<< 5) | (aValue
>> 27);
115 AddU32ToHash(HashNumber aHash
, uint32_t aValue
)
118 * This is the meat of all our hash routines. This hash function is not
119 * particularly sophisticated, but it seems to work well for our mostly
120 * plain-text inputs. Implementation notes follow.
122 * Our use of the golden ratio here is arbitrary; we could pick almost any
125 * * is odd (because otherwise, all our hash values will be even)
127 * * has a reasonably-even mix of 1's and 0's (consider the extreme case
128 * where we multiply by 0x3 or 0xeffffff -- this will not produce good
129 * mixing across all bits of the hash).
131 * The rotation length of 5 is also arbitrary, although an odd number is again
132 * preferable so our hash explores the whole universe of possible rotations.
134 * Finally, we multiply by the golden ratio *after* xor'ing, not before.
135 * Otherwise, if |aHash| is 0 (as it often is for the beginning of a
136 * message), the expression
138 * mozilla::WrappingMultiply(kGoldenRatioU32, RotateLeft5(aHash))
142 * evaluates to |aValue|.
144 * (Number-theoretic aside: Because any odd number |m| is relatively prime to
145 * our modulus (2**32), the list
147 * [x * m (mod 2**32) for 0 <= x < 2**32]
149 * has no duplicate elements. This means that multiplying by |m| does not
150 * cause us to skip any possible hash values.
152 * It's also nice if |m| has large-ish order mod 2**32 -- that is, if the
153 * smallest k such that m**k == 1 (mod 2**32) is large -- so we can safely
154 * multiply our hash value by |m| a few times without negating the
155 * multiplicative effect. Our golden ratio constant has order 2**29, which is
156 * more than enough for our purposes.)
158 return mozilla::WrappingMultiply(kGoldenRatioU32
,
159 RotateLeft5(aHash
) ^ aValue
);
163 * AddUintptrToHash takes sizeof(uintptr_t) as a template parameter.
165 template<size_t PtrSize
>
167 AddUintptrToHash(HashNumber aHash
, uintptr_t aValue
)
169 return AddU32ToHash(aHash
, static_cast<uint32_t>(aValue
));
174 AddUintptrToHash
<8>(HashNumber aHash
, uintptr_t aValue
)
176 uint32_t v1
= static_cast<uint32_t>(aValue
);
177 uint32_t v2
= static_cast<uint32_t>(static_cast<uint64_t>(aValue
) >> 32);
178 return AddU32ToHash(AddU32ToHash(aHash
, v1
), v2
);
181 } /* namespace detail */
184 * AddToHash takes a hash and some values and returns a new hash based on the
187 * Currently, we support hashing uint32_t's, values which we can implicitly
188 * convert to uint32_t, data pointers, and function pointers.
191 bool TypeIsNotIntegral
= !mozilla::IsIntegral
<T
>::value
,
192 typename U
= typename
mozilla::EnableIf
<TypeIsNotIntegral
>::Type
>
193 MOZ_MUST_USE
inline HashNumber
194 AddToHash(HashNumber aHash
, T aA
)
197 * Try to convert |A| to uint32_t implicitly. If this works, great. If not,
200 return detail::AddU32ToHash(aHash
, aA
);
204 MOZ_MUST_USE
inline HashNumber
205 AddToHash(HashNumber aHash
, A
* aA
)
208 * You might think this function should just take a void*. But then we'd only
209 * catch data pointers and couldn't handle function pointers.
212 static_assert(sizeof(aA
) == sizeof(uintptr_t), "Strange pointer!");
214 return detail::AddUintptrToHash
<sizeof(uintptr_t)>(aHash
, uintptr_t(aA
));
217 // We use AddUintptrToHash() for hashing all integral types. 8-byte integral types
218 // are treated the same as 64-bit pointers, and smaller integral types are first
219 // implicitly converted to 32 bits and then passed to AddUintptrToHash() to be hashed.
221 typename U
= typename
mozilla::EnableIf
<mozilla::IsIntegral
<T
>::value
>::Type
>
222 MOZ_MUST_USE
constexpr HashNumber
223 AddToHash(HashNumber aHash
, T aA
)
225 return detail::AddUintptrToHash
<sizeof(T
)>(aHash
, aA
);
228 template<typename A
, typename
... Args
>
229 MOZ_MUST_USE HashNumber
230 AddToHash(HashNumber aHash
, A aArg
, Args
... aArgs
)
232 return AddToHash(AddToHash(aHash
, aArg
), aArgs
...);
236 * The HashGeneric class of functions let you hash one or more values.
238 * If you want to hash together two values x and y, calling HashGeneric(x, y) is
239 * much better than calling AddToHash(x, y), because AddToHash(x, y) assumes
240 * that x has already been hashed.
242 template<typename
... Args
>
243 MOZ_MUST_USE
inline HashNumber
244 HashGeneric(Args
... aArgs
)
246 return AddToHash(0, aArgs
...);
253 HashUntilZero(const T
* aStr
)
256 for (; T c
= *aStr
; aStr
++) {
257 hash
= AddToHash(hash
, c
);
264 HashKnownLength(const T
* aStr
, size_t aLength
)
267 for (size_t i
= 0; i
< aLength
; i
++) {
268 hash
= AddToHash(hash
, aStr
[i
]);
273 } /* namespace detail */
276 * The HashString overloads below do just what you'd expect.
278 * If you have the string's length, you might as well call the overload which
279 * includes the length. It may be marginally faster.
281 MOZ_MUST_USE
inline HashNumber
282 HashString(const char* aStr
)
284 return detail::HashUntilZero(reinterpret_cast<const unsigned char*>(aStr
));
287 MOZ_MUST_USE
inline HashNumber
288 HashString(const char* aStr
, size_t aLength
)
290 return detail::HashKnownLength(reinterpret_cast<const unsigned char*>(aStr
), aLength
);
295 HashString(const unsigned char* aStr
, size_t aLength
)
297 return detail::HashKnownLength(aStr
, aLength
);
300 // You may need to use the
301 // MOZ_{PUSH,POP}_DISABLE_INTEGRAL_CONSTANT_OVERFLOW_WARNING macros if you use
302 // this function. See the comment on those macros' definitions for more detail.
303 MOZ_MUST_USE
constexpr HashNumber
304 HashString(const char16_t
* aStr
)
306 return detail::HashUntilZero(aStr
);
309 MOZ_MUST_USE
inline HashNumber
310 HashString(const char16_t
* aStr
, size_t aLength
)
312 return detail::HashKnownLength(aStr
, aLength
);
316 * On Windows, wchar_t is not the same as char16_t, even though it's
320 MOZ_MUST_USE
inline HashNumber
321 HashString(const wchar_t* aStr
)
323 return detail::HashUntilZero(aStr
);
326 MOZ_MUST_USE
inline HashNumber
327 HashString(const wchar_t* aStr
, size_t aLength
)
329 return detail::HashKnownLength(aStr
, aLength
);
334 * Hash some number of bytes.
336 * This hash walks word-by-word, rather than byte-by-byte, so you won't get the
337 * same result out of HashBytes as you would out of HashString.
339 MOZ_MUST_USE
extern MFBT_API HashNumber
340 HashBytes(const void* bytes
, size_t aLength
);
343 * A pseudorandom function mapping 32-bit integers to 32-bit integers.
345 * This is for when you're feeding private data (like pointer values or credit
346 * card numbers) to a non-crypto hash function (like HashBytes) and then using
347 * the hash code for something that untrusted parties could observe (like a JS
348 * Map). Plug in a HashCodeScrambler before that last step to avoid leaking the
351 * By itself, this does not prevent hash-flooding DoS attacks, because an
352 * attacker can still generate many values with exactly equal hash codes by
353 * attacking the non-crypto hash function alone. Equal hash codes will, of
354 * course, still be equal however much you scramble them.
356 * The algorithm is SipHash-1-3. See <https://131002.net/siphash/>.
358 class HashCodeScrambler
365 /** Creates a new scrambler with the given 128-bit key. */
366 constexpr HashCodeScrambler(uint64_t aK0
, uint64_t aK1
) : mK0(aK0
), mK1(aK1
) {}
369 * Scramble a hash code. Always produces the same result for the same
370 * combination of key and hash code.
372 HashNumber
scramble(HashNumber aHashCode
) const
374 SipHasher
hasher(mK0
, mK1
);
375 return HashNumber(hasher
.sipHash(aHashCode
));
381 SipHasher(uint64_t aK0
, uint64_t aK1
)
383 // 1. Initialization.
384 mV0
= aK0
^ UINT64_C(0x736f6d6570736575);
385 mV1
= aK1
^ UINT64_C(0x646f72616e646f6d);
386 mV2
= aK0
^ UINT64_C(0x6c7967656e657261);
387 mV3
= aK1
^ UINT64_C(0x7465646279746573);
390 uint64_t sipHash(uint64_t aM
)
399 for (int i
= 0; i
< 3; i
++)
401 return mV0
^ mV1
^ mV2
^ mV3
;
406 mV0
= WrappingAdd(mV0
, mV1
);
407 mV1
= RotateLeft(mV1
, 13);
409 mV0
= RotateLeft(mV0
, 32);
410 mV2
= WrappingAdd(mV2
, mV3
);
411 mV3
= RotateLeft(mV3
, 16);
413 mV0
= WrappingAdd(mV0
, mV3
);
414 mV3
= RotateLeft(mV3
, 21);
416 mV2
= WrappingAdd(mV2
, mV1
);
417 mV1
= RotateLeft(mV1
, 17);
419 mV2
= RotateLeft(mV2
, 32);
422 uint64_t mV0
, mV1
, mV2
, mV3
;
426 } /* namespace mozilla */
428 #endif /* mozilla_HashFunctions_h */