HashFunctions.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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/. */
  6. /* Utilities for hashing. */
  7. /*
  8. * This file exports functions for hashing data down to a 32-bit value,
  9. * including:
  10. *
  11. * - HashString Hash a char* or char16_t/wchar_t* of known or unknown
  12. * length.
  13. *
  14. * - HashBytes Hash a byte array of known length.
  15. *
  16. * - HashGeneric Hash one or more values. Currently, we support uint32_t,
  17. * types which can be implicitly cast to uint32_t, data
  18. * pointers, and function pointers.
  19. *
  20. * - AddToHash Add one or more values to the given hash. This supports the
  21. * same list of types as HashGeneric.
  22. *
  23. *
  24. * You can chain these functions together to hash complex objects. For example:
  25. *
  26. * class ComplexObject
  27. * {
  28. * char* mStr;
  29. * uint32_t mUint1, mUint2;
  30. * void (*mCallbackFn)();
  31. *
  32. * public:
  33. * uint32_t hash()
  34. * {
  35. * uint32_t hash = HashString(mStr);
  36. * hash = AddToHash(hash, mUint1, mUint2);
  37. * return AddToHash(hash, mCallbackFn);
  38. * }
  39. * };
  40. *
  41. * If you want to hash an nsAString or nsACString, use the HashString functions
  42. * in nsHashKeys.h.
  43. */
  44. #ifndef mozilla_HashFunctions_h
  45. #define mozilla_HashFunctions_h
  46. #include "mozilla/Assertions.h"
  47. #include "mozilla/Attributes.h"
  48. #include "mozilla/Char16.h"
  49. #include "mozilla/MathAlgorithms.h"
  50. #include "mozilla/Types.h"
  51. #include <stdint.h>
  52. #ifdef __cplusplus
  53. namespace mozilla {
  54. /**
  55. * The golden ratio as a 32-bit fixed-point value.
  56. */
  57. static const uint32_t kGoldenRatioU32 = 0x9E3779B9U;
  58. inline uint32_t
  59. RotateBitsLeft32(uint32_t aValue, uint8_t aBits)
  60. {
  61. MOZ_ASSERT(aBits < 32);
  62. return (aValue << aBits) | (aValue >> (32 - aBits));
  63. }
  64. namespace detail {
  65. inline uint32_t
  66. AddU32ToHash(uint32_t aHash, uint32_t aValue)
  67. {
  68. /*
  69. * This is the meat of all our hash routines. This hash function is not
  70. * particularly sophisticated, but it seems to work well for our mostly
  71. * plain-text inputs. Implementation notes follow.
  72. *
  73. * Our use of the golden ratio here is arbitrary; we could pick almost any
  74. * number which:
  75. *
  76. * * is odd (because otherwise, all our hash values will be even)
  77. *
  78. * * has a reasonably-even mix of 1's and 0's (consider the extreme case
  79. * where we multiply by 0x3 or 0xeffffff -- this will not produce good
  80. * mixing across all bits of the hash).
  81. *
  82. * The rotation length of 5 is also arbitrary, although an odd number is again
  83. * preferable so our hash explores the whole universe of possible rotations.
  84. *
  85. * Finally, we multiply by the golden ratio *after* xor'ing, not before.
  86. * Otherwise, if |aHash| is 0 (as it often is for the beginning of a
  87. * message), the expression
  88. *
  89. * (kGoldenRatioU32 * RotateBitsLeft(aHash, 5)) |xor| aValue
  90. *
  91. * evaluates to |aValue|.
  92. *
  93. * (Number-theoretic aside: Because any odd number |m| is relatively prime to
  94. * our modulus (2^32), the list
  95. *
  96. * [x * m (mod 2^32) for 0 <= x < 2^32]
  97. *
  98. * has no duplicate elements. This means that multiplying by |m| does not
  99. * cause us to skip any possible hash values.
  100. *
  101. * It's also nice if |m| has large-ish order mod 2^32 -- that is, if the
  102. * smallest k such that m^k == 1 (mod 2^32) is large -- so we can safely
  103. * multiply our hash value by |m| a few times without negating the
  104. * multiplicative effect. Our golden ratio constant has order 2^29, which is
  105. * more than enough for our purposes.)
  106. */
  107. return kGoldenRatioU32 * (RotateBitsLeft32(aHash, 5) ^ aValue);
  108. }
  109. /**
  110. * AddUintptrToHash takes sizeof(uintptr_t) as a template parameter.
  111. */
  112. template<size_t PtrSize>
  113. inline uint32_t
  114. AddUintptrToHash(uint32_t aHash, uintptr_t aValue);
  115. template<>
  116. inline uint32_t
  117. AddUintptrToHash<4>(uint32_t aHash, uintptr_t aValue)
  118. {
  119. return AddU32ToHash(aHash, static_cast<uint32_t>(aValue));
  120. }
  121. template<>
  122. inline uint32_t
  123. AddUintptrToHash<8>(uint32_t aHash, uintptr_t aValue)
  124. {
  125. /*
  126. * The static cast to uint64_t below is necessary because this function
  127. * sometimes gets compiled on 32-bit platforms (yes, even though it's a
  128. * template and we never call this particular override in a 32-bit build). If
  129. * we do aValue >> 32 on a 32-bit machine, we're shifting a 32-bit uintptr_t
  130. * right 32 bits, and the compiler throws an error.
  131. */
  132. uint32_t v1 = static_cast<uint32_t>(aValue);
  133. uint32_t v2 = static_cast<uint32_t>(static_cast<uint64_t>(aValue) >> 32);
  134. return AddU32ToHash(AddU32ToHash(aHash, v1), v2);
  135. }
  136. } /* namespace detail */
  137. /**
  138. * AddToHash takes a hash and some values and returns a new hash based on the
  139. * inputs.
  140. *
  141. * Currently, we support hashing uint32_t's, values which we can implicitly
  142. * convert to uint32_t, data pointers, and function pointers.
  143. */
  144. template<typename A>
  145. MOZ_MUST_USE inline uint32_t
  146. AddToHash(uint32_t aHash, A aA)
  147. {
  148. /*
  149. * Try to convert |A| to uint32_t implicitly. If this works, great. If not,
  150. * we'll error out.
  151. */
  152. return detail::AddU32ToHash(aHash, aA);
  153. }
  154. template<typename A>
  155. MOZ_MUST_USE inline uint32_t
  156. AddToHash(uint32_t aHash, A* aA)
  157. {
  158. /*
  159. * You might think this function should just take a void*. But then we'd only
  160. * catch data pointers and couldn't handle function pointers.
  161. */
  162. static_assert(sizeof(aA) == sizeof(uintptr_t), "Strange pointer!");
  163. return detail::AddUintptrToHash<sizeof(uintptr_t)>(aHash, uintptr_t(aA));
  164. }
  165. template<>
  166. MOZ_MUST_USE inline uint32_t
  167. AddToHash(uint32_t aHash, uintptr_t aA)
  168. {
  169. return detail::AddUintptrToHash<sizeof(uintptr_t)>(aHash, aA);
  170. }
  171. template<typename A, typename... Args>
  172. MOZ_MUST_USE uint32_t
  173. AddToHash(uint32_t aHash, A aArg, Args... aArgs)
  174. {
  175. return AddToHash(AddToHash(aHash, aArg), aArgs...);
  176. }
  177. /**
  178. * The HashGeneric class of functions let you hash one or more values.
  179. *
  180. * If you want to hash together two values x and y, calling HashGeneric(x, y) is
  181. * much better than calling AddToHash(x, y), because AddToHash(x, y) assumes
  182. * that x has already been hashed.
  183. */
  184. template<typename... Args>
  185. MOZ_MUST_USE inline uint32_t
  186. HashGeneric(Args... aArgs)
  187. {
  188. return AddToHash(0, aArgs...);
  189. }
  190. namespace detail {
  191. template<typename T>
  192. uint32_t
  193. HashUntilZero(const T* aStr)
  194. {
  195. uint32_t hash = 0;
  196. for (T c; (c = *aStr); aStr++) {
  197. hash = AddToHash(hash, c);
  198. }
  199. return hash;
  200. }
  201. template<typename T>
  202. uint32_t
  203. HashKnownLength(const T* aStr, size_t aLength)
  204. {
  205. uint32_t hash = 0;
  206. for (size_t i = 0; i < aLength; i++) {
  207. hash = AddToHash(hash, aStr[i]);
  208. }
  209. return hash;
  210. }
  211. } /* namespace detail */
  212. /**
  213. * The HashString overloads below do just what you'd expect.
  214. *
  215. * If you have the string's length, you might as well call the overload which
  216. * includes the length. It may be marginally faster.
  217. */
  218. MOZ_MUST_USE inline uint32_t
  219. HashString(const char* aStr)
  220. {
  221. return detail::HashUntilZero(reinterpret_cast<const unsigned char*>(aStr));
  222. }
  223. MOZ_MUST_USE inline uint32_t
  224. HashString(const char* aStr, size_t aLength)
  225. {
  226. return detail::HashKnownLength(reinterpret_cast<const unsigned char*>(aStr), aLength);
  227. }
  228. MOZ_MUST_USE
  229. inline uint32_t
  230. HashString(const unsigned char* aStr, size_t aLength)
  231. {
  232. return detail::HashKnownLength(aStr, aLength);
  233. }
  234. MOZ_MUST_USE inline uint32_t
  235. HashString(const char16_t* aStr)
  236. {
  237. return detail::HashUntilZero(aStr);
  238. }
  239. MOZ_MUST_USE inline uint32_t
  240. HashString(const char16_t* aStr, size_t aLength)
  241. {
  242. return detail::HashKnownLength(aStr, aLength);
  243. }
  244. /*
  245. * On Windows, wchar_t is not the same as char16_t, even though it's
  246. * the same width!
  247. */
  248. #ifdef WIN32
  249. MOZ_MUST_USE inline uint32_t
  250. HashString(const wchar_t* aStr)
  251. {
  252. return detail::HashUntilZero(aStr);
  253. }
  254. MOZ_MUST_USE inline uint32_t
  255. HashString(const wchar_t* aStr, size_t aLength)
  256. {
  257. return detail::HashKnownLength(aStr, aLength);
  258. }
  259. #endif
  260. /**
  261. * Hash some number of bytes.
  262. *
  263. * This hash walks word-by-word, rather than byte-by-byte, so you won't get the
  264. * same result out of HashBytes as you would out of HashString.
  265. */
  266. MOZ_MUST_USE extern MFBT_API uint32_t
  267. HashBytes(const void* bytes, size_t aLength);
  268. /**
  269. * A pseudorandom function mapping 32-bit integers to 32-bit integers.
  270. *
  271. * This is for when you're feeding private data (like pointer values or credit
  272. * card numbers) to a non-crypto hash function (like HashBytes) and then using
  273. * the hash code for something that untrusted parties could observe (like a JS
  274. * Map). Plug in a HashCodeScrambler before that last step to avoid leaking the
  275. * private data.
  276. *
  277. * By itself, this does not prevent hash-flooding DoS attacks, because an
  278. * attacker can still generate many values with exactly equal hash codes by
  279. * attacking the non-crypto hash function alone. Equal hash codes will, of
  280. * course, still be equal however much you scramble them.
  281. *
  282. * The algorithm is SipHash-1-3. See <https://131002.net/siphash/>.
  283. */
  284. class HashCodeScrambler
  285. {
  286. struct SipHasher;
  287. uint64_t mK0, mK1;
  288. public:
  289. /** Creates a new scrambler with the given 128-bit key. */
  290. constexpr HashCodeScrambler(uint64_t aK0, uint64_t aK1) : mK0(aK0), mK1(aK1) {}
  291. /**
  292. * Scramble a hash code. Always produces the same result for the same
  293. * combination of key and hash code.
  294. */
  295. uint32_t scramble(uint32_t aHashCode) const
  296. {
  297. SipHasher hasher(mK0, mK1);
  298. return uint32_t(hasher.sipHash(aHashCode));
  299. }
  300. private:
  301. struct SipHasher
  302. {
  303. SipHasher(uint64_t aK0, uint64_t aK1)
  304. {
  305. // 1. Initialization.
  306. mV0 = aK0 ^ UINT64_C(0x736f6d6570736575);
  307. mV1 = aK1 ^ UINT64_C(0x646f72616e646f6d);
  308. mV2 = aK0 ^ UINT64_C(0x6c7967656e657261);
  309. mV3 = aK1 ^ UINT64_C(0x7465646279746573);
  310. }
  311. uint64_t sipHash(uint64_t aM)
  312. {
  313. // 2. Compression.
  314. mV3 ^= aM;
  315. sipRound();
  316. mV0 ^= aM;
  317. // 3. Finalization.
  318. mV2 ^= 0xff;
  319. for (int i = 0; i < 3; i++)
  320. sipRound();
  321. return mV0 ^ mV1 ^ mV2 ^ mV3;
  322. }
  323. void sipRound()
  324. {
  325. mV0 += mV1;
  326. mV1 = RotateLeft(mV1, 13);
  327. mV1 ^= mV0;
  328. mV0 = RotateLeft(mV0, 32);
  329. mV2 += mV3;
  330. mV3 = RotateLeft(mV3, 16);
  331. mV3 ^= mV2;
  332. mV0 += mV3;
  333. mV3 = RotateLeft(mV3, 21);
  334. mV3 ^= mV0;
  335. mV2 += mV1;
  336. mV1 = RotateLeft(mV1, 17);
  337. mV1 ^= mV2;
  338. mV2 = RotateLeft(mV2, 32);
  339. }
  340. uint64_t mV0, mV1, mV2, mV3;
  341. };
  342. };
  343. } /* namespace mozilla */
  344. #endif /* __cplusplus */
  345. #endif /* mozilla_HashFunctions_h */