Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. using System.Runtime.CompilerServices;
  2. using Unity.Burst;
  3. using Unity.Burst.Intrinsics;
  4. using Unity.Collections.LowLevel.Unsafe;
  5. using Unity.Mathematics;
  6. using UnityEngine;
  7. #if UNITY_EDITOR
  8. using UnityEditor;
  9. #endif
  10. namespace Unity.Collections
  11. {
  12. /// <summary>
  13. /// A feature complete hashing API based on xxHash3 (https://github.com/Cyan4973/xxHash)
  14. /// </summary>
  15. /// <remarks>
  16. /// Features:
  17. /// - Compute 64bits or 128bits hash keys, based on a private key, with an optional given seed value.
  18. /// - Hash on buffer (with or without a ulong based seed value)
  19. /// - Hash on buffer while copying the data to a destination
  20. /// - Use instances of <see cref="xxHash3.StreamingState"/> to accumulate data to hash in multiple calls, suited for small data, then retrieve the hash key at the end.
  21. /// - xxHash3 has several implementation based on the size to hash to ensure best performances
  22. /// - We currently have two implementations:
  23. /// - A generic one based on Unity.Mathematics, that should always be executed compiled with Burst.
  24. /// - An AVX2 based implementation for platforms supporting it, using Burst intrinsics.
  25. /// - Whether or not the call site is compiled with burst, the hashing function will be executed by Burst(*) to ensure optimal performance.
  26. /// (*) Only when the hashing size justifies such transition.
  27. /// </remarks>
  28. [BurstCompile]
  29. [GenerateTestsForBurstCompatibility]
  30. public static partial class xxHash3
  31. {
  32. #region Public API
  33. /// <summary>
  34. /// Compute a 64bits hash of a memory region
  35. /// </summary>
  36. /// <param name="input">The memory buffer, can't be null</param>
  37. /// <param name="length">The length of the memory buffer, can be zero</param>
  38. /// <returns>The hash result</returns>
  39. public static unsafe uint2 Hash64(void* input, long length)
  40. {
  41. fixed (void* secret = xxHashDefaultKey.kSecret)
  42. {
  43. return ToUint2(Hash64Internal((byte*) input, null, length, (byte*) secret, 0));
  44. }
  45. }
  46. /// <summary>
  47. /// Compute a 64bits hash from the contents of the input struct
  48. /// </summary>
  49. /// <typeparam name="T">The input type.</typeparam>
  50. /// <param name="input">The input struct that will be hashed</param>
  51. /// <returns>The hash result</returns>
  52. [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int) })]
  53. public static unsafe uint2 Hash64<T>(in T input) where T : unmanaged
  54. {
  55. return Hash64(UnsafeUtilityExtensions.AddressOf(input), UnsafeUtility.SizeOf<T>());
  56. }
  57. /// <summary>
  58. /// Compute a 64bits hash of a memory region using a given seed value
  59. /// </summary>
  60. /// <param name="input">The memory buffer, can't be null</param>
  61. /// <param name="length">The length of the memory buffer, can be zero</param>
  62. /// <param name="seed">The seed value to alter the hash computation from</param>
  63. /// <returns>The hash result</returns>
  64. public static unsafe uint2 Hash64(void* input, long length, ulong seed)
  65. {
  66. fixed (byte* secret = xxHashDefaultKey.kSecret)
  67. {
  68. return ToUint2(Hash64Internal((byte*) input, null, length, secret, seed));
  69. }
  70. }
  71. /// <summary>
  72. /// Compute a 128bits hash of a memory region
  73. /// </summary>
  74. /// <param name="input">The memory buffer, can't be null</param>
  75. /// <param name="length">The length of the memory buffer, can be zero</param>
  76. /// <returns>The hash result</returns>
  77. public static unsafe uint4 Hash128(void* input, long length)
  78. {
  79. fixed (void* secret = xxHashDefaultKey.kSecret)
  80. {
  81. Hash128Internal((byte*) input, null, length, (byte*) secret, 0, out var result);
  82. return result;
  83. }
  84. }
  85. /// <summary>
  86. /// Compute a 128bits hash from the contents of the input struct
  87. /// </summary>
  88. /// <typeparam name="T">The input type.</typeparam>
  89. /// <param name="input">The input struct that will be hashed</param>
  90. /// <returns>The hash result</returns>
  91. [GenerateTestsForBurstCompatibility(GenericTypeArguments = new [] { typeof(int) })]
  92. public static unsafe uint4 Hash128<T>(in T input) where T : unmanaged
  93. {
  94. return Hash128(UnsafeUtilityExtensions.AddressOf(input), UnsafeUtility.SizeOf<T>());
  95. }
  96. /// <summary>
  97. /// Compute a 128bits hash while copying the data to a destination buffer
  98. /// </summary>
  99. /// <param name="input">The memory buffer to compute the hash and copy from, can't be null</param>
  100. /// <param name="destination">The destination buffer, can't be null and must be at least big enough to match the input's length</param>
  101. /// <param name="length">The length of the memory buffer, can be zero</param>
  102. /// <returns>The hash result</returns>
  103. /// <remarks>Use this API to avoid a double memory scan in situations where the hash as to be compute and the data copied at the same time. Performances improvements vary between 30-50% on
  104. /// big data.</remarks>
  105. public static unsafe uint4 Hash128(void* input, void* destination, long length)
  106. {
  107. fixed (byte* secret = xxHashDefaultKey.kSecret)
  108. {
  109. Hash128Internal((byte*) input, (byte*) destination, length, secret, 0, out var result);
  110. return result;
  111. }
  112. }
  113. /// <summary>
  114. /// Compute a 128bits hash of a memory region using a given seed value
  115. /// </summary>
  116. /// <param name="input">The memory buffer, can't be null</param>
  117. /// <param name="length">The length of the memory buffer, can be zero</param>
  118. /// <param name="seed">The seed value to alter the hash computation from</param>
  119. /// <returns>The hash result</returns>
  120. public static unsafe uint4 Hash128(void* input, long length, ulong seed)
  121. {
  122. fixed (byte* secret = xxHashDefaultKey.kSecret)
  123. {
  124. Hash128Internal((byte*) input, null, length, secret, seed, out var result);
  125. return result;
  126. }
  127. }
  128. /// <summary>
  129. /// Compute a 128bits hash while copying the data to a destination buffer using a given seed value
  130. /// </summary>
  131. /// <param name="input">The memory buffer to compute the hash and copy from, can't be null</param>
  132. /// <param name="destination">The destination buffer, can't be null and must be at least big enough to match the input's length</param>
  133. /// <param name="length">The length of the memory buffer, can be zero</param>
  134. /// <param name="seed">The seed value to alter the hash computation from</param>
  135. /// <returns>The hash result</returns>
  136. public static unsafe uint4 Hash128(void* input, void* destination, long length, ulong seed)
  137. {
  138. fixed (byte* secret = xxHashDefaultKey.kSecret)
  139. {
  140. Hash128Internal((byte*) input, (byte*) destination, length, secret, seed, out var result);
  141. return result;
  142. }
  143. }
  144. #endregion
  145. #region Constants
  146. private const int STRIPE_LEN = 64;
  147. private const int ACC_NB = STRIPE_LEN / 8; // Accumulators are ulong sized
  148. private const int SECRET_CONSUME_RATE = 8;
  149. private const int SECRET_KEY_SIZE = 192;
  150. private const int SECRET_KEY_MIN_SIZE = 136;
  151. private const int SECRET_LASTACC_START = 7;
  152. private const int NB_ROUNDS = (SECRET_KEY_SIZE - STRIPE_LEN) / SECRET_CONSUME_RATE;
  153. private const int BLOCK_LEN = STRIPE_LEN * NB_ROUNDS;
  154. private const uint PRIME32_1 = 0x9E3779B1U;
  155. private const uint PRIME32_2 = 0x85EBCA77U;
  156. private const uint PRIME32_3 = 0xC2B2AE3DU;
  157. // static readonly uint PRIME32_4 = 0x27D4EB2FU;
  158. private const uint PRIME32_5 = 0x165667B1U;
  159. private const ulong PRIME64_1 = 0x9E3779B185EBCA87UL;
  160. private const ulong PRIME64_2 = 0xC2B2AE3D27D4EB4FUL;
  161. private const ulong PRIME64_3 = 0x165667B19E3779F9UL;
  162. private const ulong PRIME64_4 = 0x85EBCA77C2B2AE63UL;
  163. private const ulong PRIME64_5 = 0x27D4EB2F165667C5UL;
  164. private const int MIDSIZE_MAX = 240;
  165. private const int MIDSIZE_STARTOFFSET = 3;
  166. private const int MIDSIZE_LASTOFFSET = 17;
  167. private const int SECRET_MERGEACCS_START = 11;
  168. #endregion
  169. private struct ulong2
  170. {
  171. public ulong x;
  172. public ulong y;
  173. public ulong2(ulong x, ulong y)
  174. {
  175. this.x = x;
  176. this.y = y;
  177. }
  178. }
  179. internal static unsafe ulong Hash64Internal(byte* input, byte* dest, long length, byte* secret, ulong seed)
  180. {
  181. if (length < 16)
  182. {
  183. return Hash64Len0To16(input, length, secret, seed);
  184. }
  185. if (length < 128)
  186. {
  187. return Hash64Len17To128(input, length, secret, seed);
  188. }
  189. if (length < MIDSIZE_MAX)
  190. {
  191. return Hash64Len129To240(input, length, secret, seed);
  192. }
  193. if (seed != 0)
  194. {
  195. var newSecret = (byte*) Memory.Unmanaged.Allocate(SECRET_KEY_SIZE, 64, Allocator.Temp);
  196. EncodeSecretKey(newSecret, secret, seed);
  197. var result = Hash64Long(input, dest, length, newSecret);
  198. Memory.Unmanaged.Free(newSecret, Allocator.Temp);
  199. return result;
  200. }
  201. else
  202. {
  203. return Hash64Long(input, dest, length, secret);
  204. }
  205. }
  206. internal static unsafe void Hash128Internal(byte* input, byte* dest, long length, byte* secret, ulong seed,
  207. out uint4 result)
  208. {
  209. if (dest != null && length < MIDSIZE_MAX)
  210. {
  211. UnsafeUtility.MemCpy(dest, input, length);
  212. }
  213. if (length < 16)
  214. {
  215. Hash128Len0To16(input, length, secret, seed, out result);
  216. return;
  217. }
  218. if (length < 128)
  219. {
  220. Hash128Len17To128(input, length, secret, seed, out result);
  221. return;
  222. }
  223. if (length < MIDSIZE_MAX)
  224. {
  225. Hash128Len129To240(input, length, secret, seed, out result);
  226. return;
  227. }
  228. if (seed != 0)
  229. {
  230. var addr = stackalloc byte[SECRET_KEY_SIZE + 31];
  231. // Aligned the allocated address on 32 bytes
  232. var newSecret = (byte*) ((ulong) addr + 31 & 0xFFFFFFFFFFFFFFE0);
  233. EncodeSecretKey(newSecret, secret, seed);
  234. Hash128Long(input, dest, length, newSecret, out result);
  235. }
  236. else
  237. {
  238. Hash128Long(input, dest, length, secret, out result);
  239. }
  240. }
  241. #region 64-bits hash, size dependent implementations
  242. private static unsafe ulong Hash64Len1To3(byte* input, long len, byte* secret, ulong seed)
  243. {
  244. unchecked
  245. {
  246. var c1 = input[0];
  247. var c2 = input[len >> 1];
  248. var c3 = input[len - 1];
  249. var combined = ((uint)c1 << 16) | ((uint)c2 << 24) | ((uint)c3 << 0) | ((uint)len << 8);
  250. ulong bitflip = (Read32LE(secret) ^ Read32LE(secret+4)) + seed;
  251. ulong keyed = (ulong)combined ^ bitflip;
  252. return AvalancheH64(keyed);
  253. }
  254. }
  255. private static unsafe ulong Hash64Len4To8(byte* input, long length, byte* secret, ulong seed)
  256. {
  257. unchecked
  258. {
  259. seed ^= (ulong)Swap32((uint)seed) << 32;
  260. var input1 = Read32LE(input);
  261. var input2 = Read32LE(input + length - 4);
  262. var bitflip = (Read64LE(secret+8) ^ Read64LE(secret+16)) - seed;
  263. var input64 = input2 + (((ulong)input1) << 32);
  264. var keyed = input64 ^ bitflip;
  265. return rrmxmx(keyed, (ulong)length);
  266. }
  267. }
  268. private static unsafe ulong Hash64Len9To16(byte* input, long length, byte* secret, ulong seed)
  269. {
  270. unchecked
  271. {
  272. var bitflip1 = (Read64LE(secret+24) ^ Read64LE(secret+32)) + seed;
  273. var bitflip2 = (Read64LE(secret+40) ^ Read64LE(secret+48)) - seed;
  274. var input_lo = Read64LE(input) ^ bitflip1;
  275. var input_hi = Read64LE(input + length - 8) ^ bitflip2;
  276. var acc = (ulong)length + Swap64(input_lo) + input_hi + Mul128Fold64(input_lo, input_hi);
  277. return Avalanche(acc);
  278. }
  279. }
  280. private static unsafe ulong Hash64Len0To16(byte* input, long length, byte* secret, ulong seed)
  281. {
  282. if (length > 8)
  283. {
  284. return Hash64Len9To16(input, length, secret, seed);
  285. }
  286. if (length >= 4)
  287. {
  288. return Hash64Len4To8(input, length, secret, seed);
  289. }
  290. if (length > 0)
  291. {
  292. return Hash64Len1To3(input, length, secret, seed);
  293. }
  294. return AvalancheH64(seed ^ (Read64LE(secret+56) ^ Read64LE(secret+64)));
  295. }
  296. private static unsafe ulong Hash64Len17To128(byte* input, long length, byte* secret, ulong seed)
  297. {
  298. unchecked
  299. {
  300. var acc = (ulong) length * PRIME64_1;
  301. if (length > 32)
  302. {
  303. if (length > 64)
  304. {
  305. if (length > 96)
  306. {
  307. acc += Mix16(input + 48, secret + 96, seed);
  308. acc += Mix16(input + length - 64, secret + 112, seed);
  309. }
  310. acc += Mix16(input + 32, secret + 64, seed);
  311. acc += Mix16(input + length - 48, secret + 80, seed);
  312. }
  313. acc += Mix16(input + 16, secret + 32, seed);
  314. acc += Mix16(input + length - 32, secret + 48, seed);
  315. }
  316. acc += Mix16(input + 0, secret + 0, seed);
  317. acc += Mix16(input + length - 16, secret + 16, seed);
  318. return Avalanche(acc);
  319. }
  320. }
  321. private static unsafe ulong Hash64Len129To240(byte* input, long length, byte* secret, ulong seed)
  322. {
  323. unchecked
  324. {
  325. var acc = (ulong) length * PRIME64_1;
  326. var nbRounds = (int) length / 16;
  327. for (var i = 0; i < 8; i++)
  328. {
  329. acc += Mix16(input + (16 * i), secret + (16 * i), seed);
  330. }
  331. acc = Avalanche(acc);
  332. for (var i = 8; i < nbRounds; i++)
  333. {
  334. acc += Mix16(input + (16 * i), secret + (16 * (i - 8)) + MIDSIZE_STARTOFFSET, seed);
  335. }
  336. acc += Mix16(input + length - 16, secret + SECRET_KEY_MIN_SIZE - MIDSIZE_LASTOFFSET, seed);
  337. return Avalanche(acc);
  338. }
  339. }
  340. [BurstCompile]
  341. private static unsafe ulong Hash64Long(byte* input, byte* dest, long length, byte* secret)
  342. {
  343. var addr = stackalloc byte[STRIPE_LEN + 31];
  344. var acc = (ulong*) ((ulong) addr + 31 & 0xFFFFFFFFFFFFFFE0); // Aligned the allocated address on 32 bytes
  345. acc[0] = PRIME32_3;
  346. acc[1] = PRIME64_1;
  347. acc[2] = PRIME64_2;
  348. acc[3] = PRIME64_3;
  349. acc[4] = PRIME64_4;
  350. acc[5] = PRIME32_2;
  351. acc[6] = PRIME64_5;
  352. acc[7] = PRIME32_1;
  353. unchecked
  354. {
  355. if (X86.Avx2.IsAvx2Supported)
  356. {
  357. Avx2HashLongInternalLoop(acc, input, dest, length, secret, 1);
  358. }
  359. else
  360. {
  361. DefaultHashLongInternalLoop(acc, input, dest, length, secret, 1);
  362. }
  363. return MergeAcc(acc, secret + SECRET_MERGEACCS_START, (ulong) length * PRIME64_1);
  364. }
  365. }
  366. #endregion
  367. #region 128-bits hash, size dependent implementations
  368. private static unsafe void Hash128Len1To3(byte* input, long length, byte* secret, ulong seed,
  369. out uint4 result)
  370. {
  371. unchecked
  372. {
  373. var c1 = input[0];
  374. var c2 = input[length >> 1];
  375. var c3 = input[length - 1];
  376. var combinedl = ((uint) c1 << 16) + (((uint) c2) << 24) + (((uint) c3) << 0) + (((uint) length) << 8);
  377. var combinedh = RotL32(Swap32(combinedl), 13);
  378. var bitflipl = (Read32LE(secret) ^ Read32LE(secret+4)) + seed;
  379. var bitfliph = (Read32LE(secret+8) ^ Read32LE(secret+12)) - seed;
  380. var keyed_lo = combinedl ^ bitflipl;
  381. var keyed_hi = combinedh ^ bitfliph;
  382. result = ToUint4(AvalancheH64(keyed_lo), AvalancheH64(keyed_hi));
  383. }
  384. }
  385. private static unsafe void Hash128Len4To8(byte* input, long len, byte* secret, ulong seed,
  386. out uint4 result)
  387. {
  388. unchecked
  389. {
  390. seed ^= (ulong)Swap32((uint)seed) << 32;
  391. var input_lo = Read32LE(input);
  392. var input_hi = Read32LE(input + len - 4);
  393. var input_64 = input_lo + ((ulong)input_hi << 32);
  394. var bitflip = (Read64LE(secret+16) ^ Read64LE(secret+24)) + seed;
  395. var keyed = input_64 ^ bitflip;
  396. var low = Common.umul128(keyed, PRIME64_1 + (ulong)(len << 2), out var high);
  397. high += (low << 1);
  398. low ^= (high >> 3);
  399. low = XorShift64(low, 35);
  400. low*= 0x9FB21C651E98DF25UL;
  401. low = XorShift64(low, 28);
  402. high = Avalanche(high);
  403. result = ToUint4(low, high);
  404. }
  405. }
  406. private static unsafe void Hash128Len9To16(byte* input, long len, byte* secret, ulong seed,
  407. out uint4 result)
  408. {
  409. unchecked
  410. {
  411. var bitflipl = (Read64LE(secret+32) ^ Read64LE(secret+40)) - seed;
  412. var bitfliph = (Read64LE(secret+48) ^ Read64LE(secret+56)) + seed;
  413. var input_lo = Read64LE(input);
  414. var input_hi = Read64LE(input + len - 8);
  415. var low = Common.umul128(input_lo ^ input_hi ^ bitflipl, PRIME64_1, out var high);
  416. low += (ulong)(len - 1) << 54;
  417. input_hi ^= bitfliph;
  418. high += input_hi + Mul32To64((uint)input_hi, PRIME32_2 - 1);
  419. low ^= Swap64(high);
  420. var hlow = Common.umul128(low, PRIME64_2, out var hhigh);
  421. hhigh += high * PRIME64_2;
  422. result = ToUint4(Avalanche(hlow), Avalanche(hhigh));
  423. }
  424. }
  425. private static unsafe void Hash128Len0To16(byte* input, long length, byte* secret, ulong seed,
  426. out uint4 result)
  427. {
  428. if (length > 8)
  429. {
  430. Hash128Len9To16(input, length, secret, seed, out result);
  431. return;
  432. }
  433. if (length >= 4)
  434. {
  435. Hash128Len4To8(input, length, secret, seed, out result);
  436. return;
  437. }
  438. if (length > 0)
  439. {
  440. Hash128Len1To3(input, length, secret, seed, out result);
  441. return;
  442. }
  443. var bitflipl = Read64LE(secret+64) ^ Read64LE(secret+72);
  444. var bitfliph = Read64LE(secret+80) ^ Read64LE(secret+88);
  445. var low = AvalancheH64(seed ^ bitflipl);
  446. var hi = AvalancheH64( seed ^ bitfliph);
  447. result = ToUint4(low, hi);
  448. }
  449. private static unsafe void Hash128Len17To128(byte* input, long length, byte* secret, ulong seed,
  450. out uint4 result)
  451. {
  452. unchecked
  453. {
  454. var acc = new ulong2((ulong) length * PRIME64_1, 0);
  455. if (length > 32)
  456. {
  457. if (length > 64)
  458. {
  459. if (length > 96)
  460. {
  461. acc = Mix32(acc, input + 48, input + length - 64, secret + 96, seed);
  462. }
  463. acc = Mix32(acc, input + 32, input + length - 48, secret + 64, seed);
  464. }
  465. acc = Mix32(acc, input + 16, input + length - 32, secret + 32, seed);
  466. }
  467. acc = Mix32(acc, input, input + length - 16, secret, seed);
  468. var low64 = acc.x + acc.y;
  469. var high64 = acc.x * PRIME64_1 + acc.y * PRIME64_4 + ((ulong) length - seed) * PRIME64_2;
  470. result = ToUint4(Avalanche(low64), 0ul - Avalanche(high64));
  471. }
  472. }
  473. private static unsafe void Hash128Len129To240(byte* input, long length, byte* secret, ulong seed,
  474. out uint4 result)
  475. {
  476. unchecked
  477. {
  478. var acc = new ulong2((ulong) length * PRIME64_1, 0);
  479. var nbRounds = length / 32;
  480. int i;
  481. for (i = 0; i < 4; i++)
  482. {
  483. acc = Mix32(acc, input + 32 * i, input + 32 * i + 16, secret + 32 * i, seed);
  484. }
  485. acc.x = Avalanche(acc.x);
  486. acc.y = Avalanche(acc.y);
  487. for (i = 4; i < nbRounds; i++)
  488. {
  489. acc = Mix32(acc, input + 32 * i, input + 32 * i + 16, secret + MIDSIZE_STARTOFFSET + 32 * (i - 4),
  490. seed);
  491. }
  492. acc = Mix32(acc, input + length - 16, input + length - 32,
  493. secret + SECRET_KEY_MIN_SIZE - MIDSIZE_LASTOFFSET - 16, 0UL - seed);
  494. var low64 = acc.x + acc.y;
  495. var high64 = acc.x * PRIME64_1 + acc.y * PRIME64_4 + ((ulong) length - seed) * PRIME64_2;
  496. result = ToUint4(Avalanche(low64), 0ul - Avalanche(high64));
  497. }
  498. }
  499. [BurstCompile]
  500. private static unsafe void Hash128Long(byte* input, byte* dest, long length, byte* secret, out uint4 result)
  501. {
  502. // var acc = stackalloc ulong[ACC_NB];
  503. var addr = stackalloc byte[STRIPE_LEN + 31];
  504. var acc = (ulong*) ((ulong) addr + 31 & 0xFFFFFFFFFFFFFFE0); // Aligned the allocated address on 32 bytes
  505. acc[0] = PRIME32_3;
  506. acc[1] = PRIME64_1;
  507. acc[2] = PRIME64_2;
  508. acc[3] = PRIME64_3;
  509. acc[4] = PRIME64_4;
  510. acc[5] = PRIME32_2;
  511. acc[6] = PRIME64_5;
  512. acc[7] = PRIME32_1;
  513. unchecked
  514. {
  515. if (X86.Avx2.IsAvx2Supported)
  516. {
  517. Avx2HashLongInternalLoop(acc, input, dest, length, secret, 0);
  518. }
  519. else
  520. {
  521. DefaultHashLongInternalLoop(acc, input, dest, length, secret, 0);
  522. }
  523. var low64 = MergeAcc(acc, secret + SECRET_MERGEACCS_START, (ulong) length * PRIME64_1);
  524. var high64 = MergeAcc(acc, secret + SECRET_KEY_SIZE - 64 - SECRET_MERGEACCS_START,
  525. ~((ulong) length * PRIME64_2));
  526. result = ToUint4(low64, high64);
  527. }
  528. }
  529. #endregion
  530. #region Internal helpers
  531. internal static uint2 ToUint2(ulong u)
  532. {
  533. return new uint2((uint)(u & 0xFFFFFFFF), (uint)(u >> 32));
  534. }
  535. internal static uint4 ToUint4(ulong ul0, ulong ul1)
  536. {
  537. return new uint4((uint)(ul0 & 0xFFFFFFFF), (uint)(ul0 >> 32), (uint)(ul1 & 0xFFFFFFFF), (uint)(ul1 >> 32));
  538. }
  539. internal static unsafe void EncodeSecretKey(byte* dst, byte* secret, ulong seed)
  540. {
  541. unchecked
  542. {
  543. var seedInitCount = SECRET_KEY_SIZE / (8 * 2);
  544. for (var i = 0; i < seedInitCount; i++)
  545. {
  546. Write64LE(dst + 16 * i + 0, Read64LE(secret + 16 * i + 0) + seed);
  547. Write64LE(dst + 16 * i + 8, Read64LE(secret + 16 * i + 8) - seed);
  548. }
  549. }
  550. }
  551. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  552. private static unsafe ulong Read64LE(void* addr) => *(ulong*) addr;
  553. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  554. private static unsafe uint Read32LE(void* addr) => *(uint*) addr;
  555. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  556. private static unsafe void Write64LE(void* addr, ulong value) => *(ulong*) addr = value;
  557. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  558. private static unsafe void Read32LE(void* addr, uint value) => *(uint*) addr = value;
  559. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  560. private static ulong Mul32To64(uint x, uint y) => (ulong) x * y;
  561. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  562. private static ulong Swap64(ulong x)
  563. {
  564. return ((x << 56) & 0xff00000000000000UL) |
  565. ((x << 40) & 0x00ff000000000000UL) |
  566. ((x << 24) & 0x0000ff0000000000UL) |
  567. ((x << 8) & 0x000000ff00000000UL) |
  568. ((x >> 8) & 0x00000000ff000000UL) |
  569. ((x >> 24) & 0x0000000000ff0000UL) |
  570. ((x >> 40) & 0x000000000000ff00UL) |
  571. ((x >> 56) & 0x00000000000000ffUL);
  572. }
  573. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  574. private static uint Swap32(uint x)
  575. {
  576. return ((x << 24) & 0xff000000) |
  577. ((x << 8) & 0x00ff0000) |
  578. ((x >> 8) & 0x0000ff00) |
  579. ((x >> 24) & 0x000000ff);
  580. }
  581. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  582. private static uint RotL32(uint x, int r) => (((x) << (r)) | ((x) >> (32 - (r))));
  583. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  584. private static ulong RotL64(ulong x, int r) => (((x) << (r)) | ((x) >> (64 - (r))));
  585. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  586. private static ulong XorShift64(ulong v64, int shift)
  587. {
  588. return v64 ^ (v64 >> shift);
  589. }
  590. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  591. private static ulong Mul128Fold64(ulong lhs, ulong rhs)
  592. {
  593. var lo = Common.umul128(lhs, rhs, out var hi);
  594. return lo ^ hi;
  595. }
  596. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  597. private static unsafe ulong Mix16(byte* input, byte* secret, ulong seed)
  598. {
  599. var input_lo = Read64LE(input);
  600. var input_hi = Read64LE(input + 8);
  601. return Mul128Fold64(
  602. input_lo ^ (Read64LE(secret + 0) + seed),
  603. input_hi ^ (Read64LE(secret + 8) - seed));
  604. }
  605. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  606. private static unsafe ulong2 Mix32(ulong2 acc, byte* input_1, byte* input_2, byte* secret, ulong seed)
  607. {
  608. unchecked
  609. {
  610. var l0 = acc.x + Mix16(input_1, secret + 0, seed);
  611. l0 ^= Read64LE(input_2) + Read64LE(input_2 + 8);
  612. var l1 = acc.y + Mix16(input_2, secret + 16, seed);
  613. l1 ^= Read64LE(input_1) + Read64LE(input_1 + 8);
  614. return new ulong2(l0, l1);
  615. }
  616. }
  617. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  618. private static ulong Avalanche(ulong h64)
  619. {
  620. unchecked
  621. {
  622. h64 = XorShift64(h64, 37);
  623. h64 *= 0x165667919E3779F9UL;
  624. h64 = XorShift64(h64, 32);
  625. return h64;
  626. }
  627. }
  628. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  629. private static ulong AvalancheH64(ulong h64)
  630. {
  631. unchecked
  632. {
  633. h64 ^= h64 >> 33;
  634. h64 *= PRIME64_2;
  635. h64 ^= h64 >> 29;
  636. h64 *= PRIME64_3;
  637. h64 ^= h64 >> 32;
  638. return h64;
  639. }
  640. }
  641. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  642. private static ulong rrmxmx(ulong h64, ulong length)
  643. {
  644. h64 ^= RotL64(h64, 49) ^ RotL64(h64, 24);
  645. h64 *= 0x9FB21C651E98DF25UL;
  646. h64 ^= (h64 >> 35) + length ;
  647. h64 *= 0x9FB21C651E98DF25UL;
  648. return XorShift64(h64, 28);
  649. }
  650. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  651. private static unsafe ulong Mix2Acc(ulong acc0, ulong acc1, byte* secret)
  652. {
  653. return Mul128Fold64(acc0 ^ Read64LE(secret), acc1 ^ Read64LE(secret+8));
  654. }
  655. internal static unsafe ulong MergeAcc(ulong* acc, byte* secret, ulong start)
  656. {
  657. unchecked
  658. {
  659. var result64 = start;
  660. result64 += Mix2Acc(acc[0], acc[1], secret + 0);
  661. result64 += Mix2Acc(acc[2], acc[3], secret + 16);
  662. result64 += Mix2Acc(acc[4], acc[5], secret + 32);
  663. result64 += Mix2Acc(acc[6], acc[7], secret + 48);
  664. return Avalanche(result64);
  665. }
  666. }
  667. #endregion
  668. #region Default Implementation
  669. private static unsafe void DefaultHashLongInternalLoop(ulong* acc, byte* input, byte* dest, long length, byte* secret, int isHash64)
  670. {
  671. // Process packets of 512 bits
  672. var nb_blocks = (length-1) / BLOCK_LEN;
  673. for (int n = 0; n < nb_blocks; n++)
  674. {
  675. DefaultAccumulate(acc, input + n * BLOCK_LEN, dest == null ? null : dest + n * BLOCK_LEN, secret,
  676. NB_ROUNDS, isHash64);
  677. DefaultScrambleAcc(acc, secret + SECRET_KEY_SIZE - STRIPE_LEN);
  678. }
  679. var nbStripes = ((length-1) - (BLOCK_LEN * nb_blocks)) / STRIPE_LEN;
  680. DefaultAccumulate(acc, input + nb_blocks * BLOCK_LEN, dest == null ? null : dest + nb_blocks * BLOCK_LEN,
  681. secret, nbStripes, isHash64);
  682. var p = input + length - STRIPE_LEN;
  683. DefaultAccumulate512(acc, p, null, secret + SECRET_KEY_SIZE - STRIPE_LEN - SECRET_LASTACC_START,
  684. isHash64);
  685. if (dest != null)
  686. {
  687. var remaining = length % STRIPE_LEN;
  688. if (remaining != 0)
  689. {
  690. UnsafeUtility.MemCpy(dest + length - remaining, input + length - remaining, remaining);
  691. }
  692. }
  693. }
  694. internal static unsafe void DefaultAccumulate(ulong* acc, byte* input, byte* dest, byte* secret, long nbStripes, int isHash64)
  695. {
  696. for (int n = 0; n < nbStripes; n++)
  697. {
  698. DefaultAccumulate512(acc, input + n * STRIPE_LEN, dest == null ? null : dest + n * STRIPE_LEN,
  699. secret + n * SECRET_CONSUME_RATE, isHash64);
  700. }
  701. }
  702. internal static unsafe void DefaultAccumulate512(ulong* acc, byte* input, byte* dest, byte* secret, int isHash64)
  703. {
  704. var count = ACC_NB;
  705. for (var i = 0; i < count; i++)
  706. {
  707. var data_val = Read64LE(input + 8 * i);
  708. var data_key = data_val ^ Read64LE(secret + i * 8);
  709. if (dest != null)
  710. {
  711. Write64LE(dest + 8 * i, data_val);
  712. }
  713. acc[i ^ 1] += data_val;
  714. acc[i] += Mul32To64((uint) (data_key & 0xFFFFFFFF), (uint) (data_key >> 32));
  715. }
  716. }
  717. internal static unsafe void DefaultScrambleAcc(ulong* acc, byte* secret)
  718. {
  719. for (var i = 0; i < ACC_NB; i++)
  720. {
  721. var key64 = Read64LE(secret + 8 * i);
  722. var acc64 = acc[i];
  723. acc64 = XorShift64(acc64, 47);
  724. acc64 ^= key64;
  725. acc64 *= PRIME32_1;
  726. acc[i] = acc64;
  727. }
  728. }
  729. #endregion
  730. }
  731. static class xxHashDefaultKey
  732. {
  733. // The default xxHash3 encoding key, other implementations of this algorithm should use the same key to produce identical hashes
  734. public static readonly byte[] kSecret =
  735. {
  736. 0xb8, 0xfe, 0x6c, 0x39, 0x23, 0xa4, 0x4b, 0xbe, 0x7c, 0x01, 0x81, 0x2c, 0xf7, 0x21, 0xad, 0x1c,
  737. 0xde, 0xd4, 0x6d, 0xe9, 0x83, 0x90, 0x97, 0xdb, 0x72, 0x40, 0xa4, 0xa4, 0xb7, 0xb3, 0x67, 0x1f,
  738. 0xcb, 0x79, 0xe6, 0x4e, 0xcc, 0xc0, 0xe5, 0x78, 0x82, 0x5a, 0xd0, 0x7d, 0xcc, 0xff, 0x72, 0x21,
  739. 0xb8, 0x08, 0x46, 0x74, 0xf7, 0x43, 0x24, 0x8e, 0xe0, 0x35, 0x90, 0xe6, 0x81, 0x3a, 0x26, 0x4c,
  740. 0x3c, 0x28, 0x52, 0xbb, 0x91, 0xc3, 0x00, 0xcb, 0x88, 0xd0, 0x65, 0x8b, 0x1b, 0x53, 0x2e, 0xa3,
  741. 0x71, 0x64, 0x48, 0x97, 0xa2, 0x0d, 0xf9, 0x4e, 0x38, 0x19, 0xef, 0x46, 0xa9, 0xde, 0xac, 0xd8,
  742. 0xa8, 0xfa, 0x76, 0x3f, 0xe3, 0x9c, 0x34, 0x3f, 0xf9, 0xdc, 0xbb, 0xc7, 0xc7, 0x0b, 0x4f, 0x1d,
  743. 0x8a, 0x51, 0xe0, 0x4b, 0xcd, 0xb4, 0x59, 0x31, 0xc8, 0x9f, 0x7e, 0xc9, 0xd9, 0x78, 0x73, 0x64,
  744. 0xea, 0xc5, 0xac, 0x83, 0x34, 0xd3, 0xeb, 0xc3, 0xc5, 0x81, 0xa0, 0xff, 0xfa, 0x13, 0x63, 0xeb,
  745. 0x17, 0x0d, 0xdd, 0x51, 0xb7, 0xf0, 0xda, 0x49, 0xd3, 0x16, 0x55, 0x26, 0x29, 0xd4, 0x68, 0x9e,
  746. 0x2b, 0x16, 0xbe, 0x58, 0x7d, 0x47, 0xa1, 0xfc, 0x8f, 0xf8, 0xb8, 0xd1, 0x7a, 0xd0, 0x31, 0xce,
  747. 0x45, 0xcb, 0x3a, 0x8f, 0x95, 0x16, 0x04, 0x28, 0xaf, 0xd7, 0xfb, 0xca, 0xbb, 0x4b, 0x40, 0x7e,
  748. };
  749. }
  750. }