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 32-bit value,
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 * uint32_t 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"
62 * The golden ratio as a 32-bit fixed-point value.
64 static const uint32_t kGoldenRatioU32
= 0x9E3779B9U
;
67 RotateBitsLeft32(uint32_t aValue
, uint8_t aBits
)
69 MOZ_ASSERT(aBits
< 32);
70 return (aValue
<< aBits
) | (aValue
>> (32 - aBits
));
76 AddU32ToHash(uint32_t aHash
, uint32_t aValue
)
79 * This is the meat of all our hash routines. This hash function is not
80 * particularly sophisticated, but it seems to work well for our mostly
81 * plain-text inputs. Implementation notes follow.
83 * Our use of the golden ratio here is arbitrary; we could pick almost any
86 * * is odd (because otherwise, all our hash values will be even)
88 * * has a reasonably-even mix of 1's and 0's (consider the extreme case
89 * where we multiply by 0x3 or 0xeffffff -- this will not produce good
90 * mixing across all bits of the hash).
92 * The rotation length of 5 is also arbitrary, although an odd number is again
93 * preferable so our hash explores the whole universe of possible rotations.
95 * Finally, we multiply by the golden ratio *after* xor'ing, not before.
96 * Otherwise, if |aHash| is 0 (as it often is for the beginning of a
97 * message), the expression
99 * (kGoldenRatioU32 * RotateBitsLeft(aHash, 5)) |xor| aValue
101 * evaluates to |aValue|.
103 * (Number-theoretic aside: Because any odd number |m| is relatively prime to
104 * our modulus (2^32), the list
106 * [x * m (mod 2^32) for 0 <= x < 2^32]
108 * has no duplicate elements. This means that multiplying by |m| does not
109 * cause us to skip any possible hash values.
111 * It's also nice if |m| has large-ish order mod 2^32 -- that is, if the
112 * smallest k such that m^k == 1 (mod 2^32) is large -- so we can safely
113 * multiply our hash value by |m| a few times without negating the
114 * multiplicative effect. Our golden ratio constant has order 2^29, which is
115 * more than enough for our purposes.)
117 return kGoldenRatioU32
* (RotateBitsLeft32(aHash
, 5) ^ aValue
);
121 * AddUintptrToHash takes sizeof(uintptr_t) as a template parameter.
123 template<size_t PtrSize
>
125 AddUintptrToHash(uint32_t aHash
, uintptr_t aValue
)
127 return AddU32ToHash(aHash
, static_cast<uint32_t>(aValue
));
132 AddUintptrToHash
<8>(uint32_t aHash
, uintptr_t aValue
)
134 uint32_t v1
= static_cast<uint32_t>(aValue
);
135 uint32_t v2
= static_cast<uint32_t>(static_cast<uint64_t>(aValue
) >> 32);
136 return AddU32ToHash(AddU32ToHash(aHash
, v1
), v2
);
139 } /* namespace detail */
142 * AddToHash takes a hash and some values and returns a new hash based on the
145 * Currently, we support hashing uint32_t's, values which we can implicitly
146 * convert to uint32_t, data pointers, and function pointers.
149 bool TypeIsNotIntegral
= !mozilla::IsIntegral
<T
>::value
,
150 typename U
= typename
mozilla::EnableIf
<TypeIsNotIntegral
>::Type
>
151 MOZ_MUST_USE
inline uint32_t
152 AddToHash(uint32_t aHash
, T aA
)
155 * Try to convert |A| to uint32_t implicitly. If this works, great. If not,
158 return detail::AddU32ToHash(aHash
, aA
);
162 MOZ_MUST_USE
inline uint32_t
163 AddToHash(uint32_t aHash
, A
* aA
)
166 * You might think this function should just take a void*. But then we'd only
167 * catch data pointers and couldn't handle function pointers.
170 static_assert(sizeof(aA
) == sizeof(uintptr_t), "Strange pointer!");
172 return detail::AddUintptrToHash
<sizeof(uintptr_t)>(aHash
, uintptr_t(aA
));
175 // We use AddUintptrToHash() for hashing all integral types. 8-byte integral types
176 // are treated the same as 64-bit pointers, and smaller integral types are first
177 // implicitly converted to 32 bits and then passed to AddUintptrToHash() to be hashed.
179 typename U
= typename
mozilla::EnableIf
<mozilla::IsIntegral
<T
>::value
>::Type
>
180 MOZ_MUST_USE
inline uint32_t
181 AddToHash(uint32_t aHash
, T aA
)
183 return detail::AddUintptrToHash
<sizeof(T
)>(aHash
, aA
);
186 template<typename A
, typename
... Args
>
187 MOZ_MUST_USE
uint32_t
188 AddToHash(uint32_t aHash
, A aArg
, Args
... aArgs
)
190 return AddToHash(AddToHash(aHash
, aArg
), aArgs
...);
194 * The HashGeneric class of functions let you hash one or more values.
196 * If you want to hash together two values x and y, calling HashGeneric(x, y) is
197 * much better than calling AddToHash(x, y), because AddToHash(x, y) assumes
198 * that x has already been hashed.
200 template<typename
... Args
>
201 MOZ_MUST_USE
inline uint32_t
202 HashGeneric(Args
... aArgs
)
204 return AddToHash(0, aArgs
...);
211 HashUntilZero(const T
* aStr
)
214 for (T c
; (c
= *aStr
); aStr
++) {
215 hash
= AddToHash(hash
, c
);
222 HashKnownLength(const T
* aStr
, size_t aLength
)
225 for (size_t i
= 0; i
< aLength
; i
++) {
226 hash
= AddToHash(hash
, aStr
[i
]);
231 } /* namespace detail */
234 * The HashString overloads below do just what you'd expect.
236 * If you have the string's length, you might as well call the overload which
237 * includes the length. It may be marginally faster.
239 MOZ_MUST_USE
inline uint32_t
240 HashString(const char* aStr
)
242 return detail::HashUntilZero(reinterpret_cast<const unsigned char*>(aStr
));
245 MOZ_MUST_USE
inline uint32_t
246 HashString(const char* aStr
, size_t aLength
)
248 return detail::HashKnownLength(reinterpret_cast<const unsigned char*>(aStr
), aLength
);
253 HashString(const unsigned char* aStr
, size_t aLength
)
255 return detail::HashKnownLength(aStr
, aLength
);
258 MOZ_MUST_USE
inline uint32_t
259 HashString(const char16_t
* aStr
)
261 return detail::HashUntilZero(aStr
);
264 MOZ_MUST_USE
inline uint32_t
265 HashString(const char16_t
* aStr
, size_t aLength
)
267 return detail::HashKnownLength(aStr
, aLength
);
271 * On Windows, wchar_t is not the same as char16_t, even though it's
275 MOZ_MUST_USE
inline uint32_t
276 HashString(const wchar_t* aStr
)
278 return detail::HashUntilZero(aStr
);
281 MOZ_MUST_USE
inline uint32_t
282 HashString(const wchar_t* aStr
, size_t aLength
)
284 return detail::HashKnownLength(aStr
, aLength
);
289 * Hash some number of bytes.
291 * This hash walks word-by-word, rather than byte-by-byte, so you won't get the
292 * same result out of HashBytes as you would out of HashString.
294 MOZ_MUST_USE
extern MFBT_API
uint32_t
295 HashBytes(const void* bytes
, size_t aLength
);
298 * A pseudorandom function mapping 32-bit integers to 32-bit integers.
300 * This is for when you're feeding private data (like pointer values or credit
301 * card numbers) to a non-crypto hash function (like HashBytes) and then using
302 * the hash code for something that untrusted parties could observe (like a JS
303 * Map). Plug in a HashCodeScrambler before that last step to avoid leaking the
306 * By itself, this does not prevent hash-flooding DoS attacks, because an
307 * attacker can still generate many values with exactly equal hash codes by
308 * attacking the non-crypto hash function alone. Equal hash codes will, of
309 * course, still be equal however much you scramble them.
311 * The algorithm is SipHash-1-3. See <https://131002.net/siphash/>.
313 class HashCodeScrambler
320 /** Creates a new scrambler with the given 128-bit key. */
321 constexpr HashCodeScrambler(uint64_t aK0
, uint64_t aK1
) : mK0(aK0
), mK1(aK1
) {}
324 * Scramble a hash code. Always produces the same result for the same
325 * combination of key and hash code.
327 uint32_t scramble(uint32_t aHashCode
) const
329 SipHasher
hasher(mK0
, mK1
);
330 return uint32_t(hasher
.sipHash(aHashCode
));
336 SipHasher(uint64_t aK0
, uint64_t aK1
)
338 // 1. Initialization.
339 mV0
= aK0
^ UINT64_C(0x736f6d6570736575);
340 mV1
= aK1
^ UINT64_C(0x646f72616e646f6d);
341 mV2
= aK0
^ UINT64_C(0x6c7967656e657261);
342 mV3
= aK1
^ UINT64_C(0x7465646279746573);
345 uint64_t sipHash(uint64_t aM
)
354 for (int i
= 0; i
< 3; i
++)
356 return mV0
^ mV1
^ mV2
^ mV3
;
362 mV1
= RotateLeft(mV1
, 13);
364 mV0
= RotateLeft(mV0
, 32);
366 mV3
= RotateLeft(mV3
, 16);
369 mV3
= RotateLeft(mV3
, 21);
372 mV1
= RotateLeft(mV1
, 17);
374 mV2
= RotateLeft(mV2
, 32);
377 uint64_t mV0
, mV1
, mV2
, mV3
;
381 } /* namespace mozilla */
382 #endif /* __cplusplus */
384 #endif /* mozilla_HashFunctions_h */