meow_hash_x64_aesni.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /* ========================================================================
  2. Meow - A Fast Non-cryptographic Hash
  3. (C) Copyright 2018-2019 by Molly Rocket, Inc. (https://mollyrocket.com)
  4. See https://mollyrocket.com/meowhash for details.
  5. ========================================================================
  6. zlib License
  7. (C) Copyright 2018-2019 Molly Rocket, Inc.
  8. This software is provided 'as-is', without any express or implied
  9. warranty. In no event will the authors be held liable for any damages
  10. arising from the use of this software.
  11. Permission is granted to anyone to use this software for any purpose,
  12. including commercial applications, and to alter it and redistribute it
  13. freely, subject to the following restrictions:
  14. 1. The origin of this software must not be misrepresented; you must not
  15. claim that you wrote the original software. If you use this software
  16. in a product, an acknowledgment in the product documentation would be
  17. appreciated but is not required.
  18. 2. Altered source versions must be plainly marked as such, and must not be
  19. misrepresented as being the original software.
  20. 3. This notice may not be removed or altered from any source distribution.
  21. ========================================================================
  22. FAQ
  23. Q: What is it?
  24. A: Meow is a 128-bit Level 3 hash taking 128 bytes of seed. It operates
  25. at very high speeds on x64 processors, and potentially other processors
  26. that provide accelerated AES instructions.
  27. Q: What is it GOOD for?
  28. A: Quickly hashing any amount of data for comparison purposes such as
  29. block deduplication or change detection. It is fast on all buffer
  30. sizes, and can generally be used anywhere you need fast Level 3
  31. hashing without worrying about how big or small the inputs tend to be.
  32. However, substantial speed improvements could be made over Meow
  33. if you either a) know you are always hashing an exact, small number of bytes,
  34. or b) can always supply a small number of bytes in a buffer padded to some
  35. fixed multiple of 16.
  36. Q: What is it BAD for?
  37. A: Anything requiring Level 4 or Level 5 security guarantees (see
  38. http://nohatcoder.dk/2019-05-19-1.html#level3). Also, note that
  39. Meow is a new hash and has not had the extensive community
  40. cryptanalysis necessary to ensure that it is not breakable down to
  41. a lower level of hash, so you must do your due diligence in
  42. deciding when and where to use Meow instead of a slower but
  43. more extensively studied existing hash. We have tried to design
  44. it to provide Level 3 security, but the possibility of the hash
  45. being broken in the future always exists.
  46. Q: Why is it called the "Meow hash"?
  47. A: It is named after a character in Meow the Infinite
  48. (https://meowtheinfinite.com)
  49. Q: Who wrote it?
  50. A: The final Meow Hash was created as a collaboration between
  51. JACOB CHRISTIAN MUNCH-ANDERSEN (https://twitter.com/nohatcoder) and
  52. CASEY MURATORI (https://caseymuratori.com). Casey wrote the original
  53. implementation for use in processing large-footprint assets for the
  54. game 1935 (https://molly1935.com). Jacob was the first to analyze
  55. that implementation and determine the adversarial bit strength, which
  56. was weaker than they would have liked.
  57. Following that, the two collaborated to figure out how the hash
  58. could be strengthened without reducing Meow's 16 bytes/cycle
  59. maximum theoretical throughput. Jacob created the hash candidates
  60. and Casey did the performance validation. After a long and
  61. exhaustive effort, Jacob found the unaligned aes/add/xor formulation
  62. that forms the current Meow hash core.
  63. A number of valuable additions to Meow Hash were also contributed
  64. by other great folks along the way:
  65. JEFF ROBERTS (https://radgametools.com) provided a super slick
  66. way to handle the residual end-of-buffer bytes that dramatically
  67. improved Meow's small hash performance.
  68. MARTINS MOZEIKO (https://matrins.ninja) ported Meow to ARM and
  69. ANSI-C, and added the proper preprocessor dressing for clean
  70. compilation on a variety of compiler configurations.
  71. FABIAN GIESEN (https://fgiesen.wordpress.com) analyzed many
  72. performance oddities that came up during development, and
  73. helped get the benchmarking working properly across a number
  74. of platforms.
  75. ARAS PRANCKEVICIUS (https://aras-p.info) provided the allocation
  76. shim for compilation on Mac OS X.
  77. ======================================================================== */
  78. //
  79. // IMPORTANT(casey): We are currently evaluating this hash construction as
  80. // the final one for Meow Hash. If you find a way to produce collisions
  81. // that should not be possible with a Level 3 hash, find significant performance
  82. // problems, or see any bugs in this version, please be sure to report them
  83. // to the Meow Hash GitHub as soon as possible. We would like to know as
  84. // much as we can about the robustness and performance before committing to
  85. // it as the final construction.
  86. //
  87. #if !defined(MEOW_HASH_X64_AESNI_H)
  88. #define MEOW_HASH_VERSION 5
  89. #define MEOW_HASH_VERSION_NAME "0.5/calico"
  90. #if !defined(meow_u8)
  91. #if _MSC_VER
  92. #if !defined(__clang__)
  93. #define INSTRUCTION_REORDER_BARRIER _ReadWriteBarrier()
  94. #else
  95. #endif
  96. #include <intrin.h>
  97. #else
  98. #include <x86intrin.h>
  99. #endif
  100. #define meow_u8 char unsigned
  101. #define meow_u64 long long unsigned
  102. #define meow_u128 __m128i
  103. #if __x86_64__ || _M_AMD64
  104. #define meow_umm long long unsigned
  105. #define MeowU64From(A, I) (_mm_extract_epi64((A), (I)))
  106. #elif __i386__ || _M_IX86
  107. #define meow_umm int unsigned
  108. #define MeowU64From(A, I) (*(meow_u64 *)&(A))
  109. #else
  110. #error Cannot determine architecture to use!
  111. #endif
  112. #define MeowU32From(A, I) (_mm_extract_epi32((A), (I)))
  113. #define MeowHashesAreEqual(A, B) (_mm_movemask_epi8(_mm_cmpeq_epi8((A), (B))) == 0xFFFF)
  114. #if !defined INSTRUCTION_REORDER_BARRIER
  115. #define INSTRUCTION_REORDER_BARRIER
  116. #endif
  117. #if !defined MEOW_PAGESIZE
  118. #define MEOW_PAGESIZE 4096
  119. #endif
  120. #if !defined MEOW_PREFETCH
  121. #define MEOW_PREFETCH 4096
  122. #endif
  123. #if !defined MEOW_PREFETCH_LIMIT
  124. #define MEOW_PREFETCH_LIMIT 0x3ff
  125. #endif
  126. #endif
  127. #define prefetcht0(A) _mm_prefetch((char *)(A), _MM_HINT_T0)
  128. #define movdqu(A, B) A = _mm_loadu_si128((__m128i *)(B))
  129. #define movdqu_mem(A, B) _mm_storeu_si128((__m128i *)(A), B)
  130. #define movq(A, B) A = _mm_set_epi64x(0, B);
  131. #define aesdec(A, B) A = _mm_aesdec_si128(A, B)
  132. #define pshufb(A, B) A = _mm_shuffle_epi8(A, B)
  133. #define pxor(A, B) A = _mm_xor_si128(A, B)
  134. #define paddq(A, B) A = _mm_add_epi64(A, B)
  135. #define pand(A, B) A = _mm_and_si128(A, B)
  136. #define palignr(A, B, i) A = _mm_alignr_epi8(A, B, i)
  137. #define pxor_clear(A, B) A = _mm_setzero_si128(); // NOTE(casey): pxor_clear is a nonsense thing that is only here because compilers don't detect xor(a, a) is clearing a :(
  138. #define MEOW_MIX_REG(r1, r2, r3, r4, r5, i1, i2, i3, i4) \
  139. aesdec(r1, r2); \
  140. INSTRUCTION_REORDER_BARRIER; \
  141. paddq(r3, i1); \
  142. pxor(r2, i2); \
  143. aesdec(r2, r4); \
  144. INSTRUCTION_REORDER_BARRIER; \
  145. paddq(r5, i3); \
  146. pxor(r4, i4);
  147. #define MEOW_MIX(r1, r2, r3, r4, r5, ptr) \
  148. MEOW_MIX_REG(r1, r2, r3, r4, r5, _mm_loadu_si128( (__m128i *) ((ptr) + 15) ), _mm_loadu_si128( (__m128i *) ((ptr) + 0) ), _mm_loadu_si128( (__m128i *) ((ptr) + 1) ), _mm_loadu_si128( (__m128i *) ((ptr) + 16) ))
  149. #define MEOW_SHUFFLE(r1, r2, r3, r4, r5, r6) \
  150. aesdec(r1, r4); \
  151. paddq(r2, r5); \
  152. pxor(r4, r6); \
  153. aesdec(r4, r2); \
  154. paddq(r5, r6); \
  155. pxor(r2, r3)
  156. #if MEOW_DUMP
  157. struct meow_dump
  158. {
  159. meow_u128 xmm[8];
  160. void *Ptr;
  161. char const *Title;
  162. };
  163. extern "C" meow_dump *MeowDumpTo;
  164. meow_dump *MeowDumpTo;
  165. #define MEOW_DUMP_STATE(T, xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, ptr) \
  166. if(MeowDumpTo) \
  167. { \
  168. MeowDumpTo->xmm[0] = xmm0; \
  169. MeowDumpTo->xmm[1] = xmm1; \
  170. MeowDumpTo->xmm[2] = xmm2; \
  171. MeowDumpTo->xmm[3] = xmm3; \
  172. MeowDumpTo->xmm[4] = xmm4; \
  173. MeowDumpTo->xmm[5] = xmm5; \
  174. MeowDumpTo->xmm[6] = xmm6; \
  175. MeowDumpTo->xmm[7] = xmm7; \
  176. MeowDumpTo->Ptr = ptr; \
  177. MeowDumpTo->Title = T; \
  178. ++MeowDumpTo; \
  179. }
  180. #else
  181. #define MEOW_DUMP_STATE(...)
  182. #endif
  183. static meow_u8 MeowShiftAdjust[32] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  184. static meow_u8 MeowMaskLen[32] = {255,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0};
  185. // NOTE(casey): The default seed is now a "nothing-up-our-sleeves" number for good measure. You may verify that it is just an encoding of Pi.
  186. static meow_u8 MeowDefaultSeed[128] =
  187. {
  188. 0x32, 0x43, 0xF6, 0xA8, 0x88, 0x5A, 0x30, 0x8D,
  189. 0x31, 0x31, 0x98, 0xA2, 0xE0, 0x37, 0x07, 0x34,
  190. 0x4A, 0x40, 0x93, 0x82, 0x22, 0x99, 0xF3, 0x1D,
  191. 0x00, 0x82, 0xEF, 0xA9, 0x8E, 0xC4, 0xE6, 0xC8,
  192. 0x94, 0x52, 0x82, 0x1E, 0x63, 0x8D, 0x01, 0x37,
  193. 0x7B, 0xE5, 0x46, 0x6C, 0xF3, 0x4E, 0x90, 0xC6,
  194. 0xCC, 0x0A, 0xC2, 0x9B, 0x7C, 0x97, 0xC5, 0x0D,
  195. 0xD3, 0xF8, 0x4D, 0x5B, 0x5B, 0x54, 0x70, 0x91,
  196. 0x79, 0x21, 0x6D, 0x5D, 0x98, 0x97, 0x9F, 0xB1,
  197. 0xBD, 0x13, 0x10, 0xBA, 0x69, 0x8D, 0xFB, 0x5A,
  198. 0xC2, 0xFF, 0xD7, 0x2D, 0xBD, 0x01, 0xAD, 0xFB,
  199. 0x7B, 0x8E, 0x1A, 0xFE, 0xD6, 0xA2, 0x67, 0xE9,
  200. 0x6B, 0xA7, 0xC9, 0x04, 0x5F, 0x12, 0xC7, 0xF9,
  201. 0x92, 0x4A, 0x19, 0x94, 0x7B, 0x39, 0x16, 0xCF,
  202. 0x70, 0x80, 0x1F, 0x2E, 0x28, 0x58, 0xEF, 0xC1,
  203. 0x66, 0x36, 0x92, 0x0D, 0x87, 0x15, 0x74, 0xE6
  204. };
  205. //
  206. // NOTE(casey): Single block version
  207. //
  208. static meow_u128
  209. MeowHash(void *Seed128Init, meow_umm Len, void *SourceInit)
  210. {
  211. meow_u128 xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7; // NOTE(casey): xmm0-xmm7 are the hash accumulation lanes
  212. meow_u128 xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15; // NOTE(casey): xmm8-xmm15 hold values to be appended (residual, length)
  213. meow_u8 *rax = (meow_u8 *)SourceInit;
  214. meow_u8 *rcx = (meow_u8 *)Seed128Init;
  215. //
  216. // NOTE(casey): Seed the eight hash registers
  217. //
  218. movdqu(xmm0, rcx + 0x00);
  219. movdqu(xmm1, rcx + 0x10);
  220. movdqu(xmm2, rcx + 0x20);
  221. movdqu(xmm3, rcx + 0x30);
  222. movdqu(xmm4, rcx + 0x40);
  223. movdqu(xmm5, rcx + 0x50);
  224. movdqu(xmm6, rcx + 0x60);
  225. movdqu(xmm7, rcx + 0x70);
  226. MEOW_DUMP_STATE("Seed", xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, 0);
  227. //
  228. // NOTE(casey): Hash all full 256-byte blocks
  229. //
  230. meow_umm BlockCount = (Len >> 8);
  231. if(BlockCount > MEOW_PREFETCH_LIMIT)
  232. {
  233. // NOTE(casey): For large input, modern Intel x64's can't hit full speed without prefetching, so we use this loop
  234. while(BlockCount--)
  235. {
  236. prefetcht0(rax + MEOW_PREFETCH + 0x00);
  237. prefetcht0(rax + MEOW_PREFETCH + 0x40);
  238. prefetcht0(rax + MEOW_PREFETCH + 0x80);
  239. prefetcht0(rax + MEOW_PREFETCH + 0xc0);
  240. MEOW_MIX(xmm0,xmm4,xmm6,xmm1,xmm2, rax + 0x00);
  241. MEOW_MIX(xmm1,xmm5,xmm7,xmm2,xmm3, rax + 0x20);
  242. MEOW_MIX(xmm2,xmm6,xmm0,xmm3,xmm4, rax + 0x40);
  243. MEOW_MIX(xmm3,xmm7,xmm1,xmm4,xmm5, rax + 0x60);
  244. MEOW_MIX(xmm4,xmm0,xmm2,xmm5,xmm6, rax + 0x80);
  245. MEOW_MIX(xmm5,xmm1,xmm3,xmm6,xmm7, rax + 0xa0);
  246. MEOW_MIX(xmm6,xmm2,xmm4,xmm7,xmm0, rax + 0xc0);
  247. MEOW_MIX(xmm7,xmm3,xmm5,xmm0,xmm1, rax + 0xe0);
  248. rax += 0x100;
  249. }
  250. }
  251. else
  252. {
  253. // NOTE(casey): For small input, modern Intel x64's can't hit full speed _with_ prefetching (because of port pressure), so we use this loop.
  254. while(BlockCount--)
  255. {
  256. MEOW_MIX(xmm0,xmm4,xmm6,xmm1,xmm2, rax + 0x00);
  257. MEOW_MIX(xmm1,xmm5,xmm7,xmm2,xmm3, rax + 0x20);
  258. MEOW_MIX(xmm2,xmm6,xmm0,xmm3,xmm4, rax + 0x40);
  259. MEOW_MIX(xmm3,xmm7,xmm1,xmm4,xmm5, rax + 0x60);
  260. MEOW_MIX(xmm4,xmm0,xmm2,xmm5,xmm6, rax + 0x80);
  261. MEOW_MIX(xmm5,xmm1,xmm3,xmm6,xmm7, rax + 0xa0);
  262. MEOW_MIX(xmm6,xmm2,xmm4,xmm7,xmm0, rax + 0xc0);
  263. MEOW_MIX(xmm7,xmm3,xmm5,xmm0,xmm1, rax + 0xe0);
  264. rax += 0x100;
  265. }
  266. }
  267. MEOW_DUMP_STATE("PostBlocks", xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, 0);
  268. //
  269. // NOTE(casey): Load any less-than-32-byte residual
  270. //
  271. pxor_clear(xmm9, xmm9);
  272. pxor_clear(xmm11, xmm11);
  273. //
  274. // TODO(casey): I need to put more thought into how the end-of-buffer stuff is actually working out here,
  275. // because I _think_ it may be possible to remove the first branch (on Len8) and let the mask zero out the
  276. // result, but it would take a little thought to make sure it couldn't read off the end of the buffer due
  277. // to the & 0xf on the align computation.
  278. //
  279. // NOTE(casey): First, we have to load the part that is _not_ 16-byte aligned
  280. meow_u8 *Last = (meow_u8 *)SourceInit + (Len & ~0xf);
  281. int unsigned Len8 = (Len & 0xf);
  282. if(Len8)
  283. {
  284. // NOTE(casey): Load the mask early
  285. movdqu(xmm8, &MeowMaskLen[0x10 - Len8]);
  286. meow_u8 *LastOk = (meow_u8*)((((meow_umm)(((meow_u8 *)SourceInit)+Len - 1)) | (MEOW_PAGESIZE - 1)) - 16);
  287. int Align = (Last > LastOk) ? ((int)(meow_umm)Last) & 0xf : 0;
  288. movdqu(xmm10, &MeowShiftAdjust[Align]);
  289. movdqu(xmm9, Last - Align);
  290. pshufb(xmm9, xmm10);
  291. // NOTE(jeffr): and off the extra bytes
  292. pand(xmm9, xmm8);
  293. }
  294. // NOTE(casey): Next, we have to load the part that _is_ 16-byte aligned
  295. if(Len & 0x10)
  296. {
  297. xmm11 = xmm9;
  298. movdqu(xmm9, Last - 0x10);
  299. }
  300. //
  301. // NOTE(casey): Construct the residual and length injests
  302. //
  303. xmm8 = xmm9;
  304. xmm10 = xmm9;
  305. palignr(xmm8, xmm11, 15);
  306. palignr(xmm10, xmm11, 1);
  307. // NOTE(casey): We have room for a 128-bit nonce and a 64-bit none here, but
  308. // the decision was made to leave them zero'd so as not to confuse people
  309. // about hwo to use them or what security implications they had.
  310. pxor_clear(xmm12, xmm12);
  311. pxor_clear(xmm13, xmm13);
  312. pxor_clear(xmm14, xmm14);
  313. movq(xmm15, Len);
  314. palignr(xmm12, xmm15, 15);
  315. palignr(xmm14, xmm15, 1);
  316. MEOW_DUMP_STATE("Residuals", xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 0);
  317. // NOTE(casey): To maintain the mix-down pattern, we always Meow Mix the less-than-32-byte residual, even if it was empty
  318. MEOW_MIX_REG(xmm0, xmm4, xmm6, xmm1, xmm2, xmm8, xmm9, xmm10, xmm11);
  319. // NOTE(casey): Append the length, to avoid problems with our 32-byte padding
  320. MEOW_MIX_REG(xmm1, xmm5, xmm7, xmm2, xmm3, xmm12, xmm13, xmm14, xmm15);
  321. MEOW_DUMP_STATE("PostAppend", xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, 0);
  322. //
  323. // NOTE(casey): Hash all full 32-byte blocks
  324. //
  325. int unsigned LaneCount = (Len >> 5) & 0x7;
  326. if(LaneCount == 0) goto MixDown; MEOW_MIX(xmm2,xmm6,xmm0,xmm3,xmm4, rax + 0x00); --LaneCount;
  327. if(LaneCount == 0) goto MixDown; MEOW_MIX(xmm3,xmm7,xmm1,xmm4,xmm5, rax + 0x20); --LaneCount;
  328. if(LaneCount == 0) goto MixDown; MEOW_MIX(xmm4,xmm0,xmm2,xmm5,xmm6, rax + 0x40); --LaneCount;
  329. if(LaneCount == 0) goto MixDown; MEOW_MIX(xmm5,xmm1,xmm3,xmm6,xmm7, rax + 0x60); --LaneCount;
  330. if(LaneCount == 0) goto MixDown; MEOW_MIX(xmm6,xmm2,xmm4,xmm7,xmm0, rax + 0x80); --LaneCount;
  331. if(LaneCount == 0) goto MixDown; MEOW_MIX(xmm7,xmm3,xmm5,xmm0,xmm1, rax + 0xa0); --LaneCount;
  332. if(LaneCount == 0) goto MixDown; MEOW_MIX(xmm0,xmm4,xmm6,xmm1,xmm2, rax + 0xc0); --LaneCount;
  333. //
  334. // NOTE(casey): Mix the eight lanes down to one 128-bit hash
  335. //
  336. MixDown:
  337. MEOW_DUMP_STATE("PostLanes", xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, 0);
  338. MEOW_SHUFFLE(xmm0, xmm1, xmm2, xmm4, xmm5, xmm6);
  339. MEOW_SHUFFLE(xmm1, xmm2, xmm3, xmm5, xmm6, xmm7);
  340. MEOW_SHUFFLE(xmm2, xmm3, xmm4, xmm6, xmm7, xmm0);
  341. MEOW_SHUFFLE(xmm3, xmm4, xmm5, xmm7, xmm0, xmm1);
  342. MEOW_SHUFFLE(xmm4, xmm5, xmm6, xmm0, xmm1, xmm2);
  343. MEOW_SHUFFLE(xmm5, xmm6, xmm7, xmm1, xmm2, xmm3);
  344. MEOW_SHUFFLE(xmm6, xmm7, xmm0, xmm2, xmm3, xmm4);
  345. MEOW_SHUFFLE(xmm7, xmm0, xmm1, xmm3, xmm4, xmm5);
  346. MEOW_SHUFFLE(xmm0, xmm1, xmm2, xmm4, xmm5, xmm6);
  347. MEOW_SHUFFLE(xmm1, xmm2, xmm3, xmm5, xmm6, xmm7);
  348. MEOW_SHUFFLE(xmm2, xmm3, xmm4, xmm6, xmm7, xmm0);
  349. MEOW_SHUFFLE(xmm3, xmm4, xmm5, xmm7, xmm0, xmm1);
  350. MEOW_DUMP_STATE("PostMix", xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, 0);
  351. paddq(xmm0, xmm2);
  352. paddq(xmm1, xmm3);
  353. paddq(xmm4, xmm6);
  354. paddq(xmm5, xmm7);
  355. pxor(xmm0, xmm1);
  356. pxor(xmm4, xmm5);
  357. paddq(xmm0, xmm4);
  358. MEOW_DUMP_STATE("PostFold", xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, 0);
  359. return(xmm0);
  360. }
  361. //
  362. // NOTE(casey): Streaming construction
  363. //
  364. typedef struct meow_state
  365. {
  366. meow_u128 xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7;
  367. meow_u64 TotalLengthInBytes;
  368. int unsigned BufferLen;
  369. meow_u8 Buffer[256];
  370. meow_u128 Pad[2]; // NOTE(casey): So we know we can over-read Buffer as necessary
  371. } meow_state;
  372. static void
  373. MeowBegin(meow_state *State, void *Seed128)
  374. {
  375. meow_u8 *rcx = (meow_u8 *)Seed128;
  376. movdqu(State->xmm0, rcx + 0x00);
  377. movdqu(State->xmm1, rcx + 0x10);
  378. movdqu(State->xmm2, rcx + 0x20);
  379. movdqu(State->xmm3, rcx + 0x30);
  380. movdqu(State->xmm4, rcx + 0x40);
  381. movdqu(State->xmm5, rcx + 0x50);
  382. movdqu(State->xmm6, rcx + 0x60);
  383. movdqu(State->xmm7, rcx + 0x70);
  384. MEOW_DUMP_STATE("Seed", State->xmm0, State->xmm1, State->xmm2, State->xmm3, State->xmm4, State->xmm5, State->xmm6, State->xmm7, 0);
  385. State->BufferLen = 0;
  386. State->TotalLengthInBytes = 0;
  387. }
  388. static void
  389. MeowAbsorbBlocks(meow_state *State, meow_umm BlockCount, meow_u8 *rax)
  390. {
  391. meow_u128 xmm0 = State->xmm0;
  392. meow_u128 xmm1 = State->xmm1;
  393. meow_u128 xmm2 = State->xmm2;
  394. meow_u128 xmm3 = State->xmm3;
  395. meow_u128 xmm4 = State->xmm4;
  396. meow_u128 xmm5 = State->xmm5;
  397. meow_u128 xmm6 = State->xmm6;
  398. meow_u128 xmm7 = State->xmm7;
  399. if(BlockCount > MEOW_PREFETCH_LIMIT)
  400. {
  401. while(BlockCount--)
  402. {
  403. prefetcht0(rax + MEOW_PREFETCH + 0x00);
  404. prefetcht0(rax + MEOW_PREFETCH + 0x40);
  405. prefetcht0(rax + MEOW_PREFETCH + 0x80);
  406. prefetcht0(rax + MEOW_PREFETCH + 0xc0);
  407. MEOW_MIX(xmm0,xmm4,xmm6,xmm1,xmm2, rax + 0x00);
  408. MEOW_MIX(xmm1,xmm5,xmm7,xmm2,xmm3, rax + 0x20);
  409. MEOW_MIX(xmm2,xmm6,xmm0,xmm3,xmm4, rax + 0x40);
  410. MEOW_MIX(xmm3,xmm7,xmm1,xmm4,xmm5, rax + 0x60);
  411. MEOW_MIX(xmm4,xmm0,xmm2,xmm5,xmm6, rax + 0x80);
  412. MEOW_MIX(xmm5,xmm1,xmm3,xmm6,xmm7, rax + 0xa0);
  413. MEOW_MIX(xmm6,xmm2,xmm4,xmm7,xmm0, rax + 0xc0);
  414. MEOW_MIX(xmm7,xmm3,xmm5,xmm0,xmm1, rax + 0xe0);
  415. rax += 0x100;
  416. }
  417. }
  418. else
  419. {
  420. while(BlockCount--)
  421. {
  422. MEOW_MIX(xmm0,xmm4,xmm6,xmm1,xmm2, rax + 0x00);
  423. MEOW_MIX(xmm1,xmm5,xmm7,xmm2,xmm3, rax + 0x20);
  424. MEOW_MIX(xmm2,xmm6,xmm0,xmm3,xmm4, rax + 0x40);
  425. MEOW_MIX(xmm3,xmm7,xmm1,xmm4,xmm5, rax + 0x60);
  426. MEOW_MIX(xmm4,xmm0,xmm2,xmm5,xmm6, rax + 0x80);
  427. MEOW_MIX(xmm5,xmm1,xmm3,xmm6,xmm7, rax + 0xa0);
  428. MEOW_MIX(xmm6,xmm2,xmm4,xmm7,xmm0, rax + 0xc0);
  429. MEOW_MIX(xmm7,xmm3,xmm5,xmm0,xmm1, rax + 0xe0);
  430. rax += 0x100;
  431. }
  432. }
  433. State->xmm0 = xmm0;
  434. State->xmm1 = xmm1;
  435. State->xmm2 = xmm2;
  436. State->xmm3 = xmm3;
  437. State->xmm4 = xmm4;
  438. State->xmm5 = xmm5;
  439. State->xmm6 = xmm6;
  440. State->xmm7 = xmm7;
  441. }
  442. static void
  443. MeowAbsorb(meow_state *State, meow_umm Len, void *SourceInit)
  444. {
  445. State->TotalLengthInBytes += Len;
  446. meow_u8 *Source = (meow_u8 *)SourceInit;
  447. // NOTE(casey): Handle any buffered residual
  448. if(State->BufferLen)
  449. {
  450. int unsigned Fill = (sizeof(State->Buffer) - State->BufferLen);
  451. if(Fill > Len)
  452. {
  453. Fill = (int unsigned)Len;
  454. }
  455. Len -= Fill;
  456. while(Fill--)
  457. {
  458. State->Buffer[State->BufferLen++] = *Source++;
  459. }
  460. if(State->BufferLen == sizeof(State->Buffer))
  461. {
  462. MeowAbsorbBlocks(State, 1, State->Buffer);
  463. State->BufferLen = 0;
  464. }
  465. }
  466. // NOTE(casey): Handle any full blocks
  467. meow_u64 BlockCount = (Len >> 8);
  468. meow_u64 Advance = (BlockCount << 8);
  469. MeowAbsorbBlocks(State, BlockCount, Source);
  470. Len -= Advance;
  471. Source += Advance;
  472. // NOTE(casey): Store residual
  473. while(Len--)
  474. {
  475. State->Buffer[State->BufferLen++] = *Source++;
  476. }
  477. }
  478. static meow_u128
  479. MeowEnd(meow_state *State, meow_u8 *Store128)
  480. {
  481. meow_umm Len = State->TotalLengthInBytes;
  482. meow_u128 xmm0 = State->xmm0;
  483. meow_u128 xmm1 = State->xmm1;
  484. meow_u128 xmm2 = State->xmm2;
  485. meow_u128 xmm3 = State->xmm3;
  486. meow_u128 xmm4 = State->xmm4;
  487. meow_u128 xmm5 = State->xmm5;
  488. meow_u128 xmm6 = State->xmm6;
  489. meow_u128 xmm7 = State->xmm7;
  490. meow_u128 xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15;
  491. meow_u8 *rax = State->Buffer;
  492. pxor_clear(xmm9, xmm9);
  493. pxor_clear(xmm11, xmm11);
  494. meow_u8 *Last = (meow_u8 *)rax + (Len & 0xf0);
  495. int unsigned Len8 = (Len & 0xf);
  496. if(Len8)
  497. {
  498. movdqu(xmm8, &MeowMaskLen[0x10 - Len8]);
  499. movdqu(xmm9, Last);
  500. pand(xmm9, xmm8);
  501. }
  502. if(Len & 0x10)
  503. {
  504. xmm11 = xmm9;
  505. movdqu(xmm9, Last - 0x10);
  506. }
  507. xmm8 = xmm9;
  508. xmm10 = xmm9;
  509. palignr(xmm8, xmm11, 15);
  510. palignr(xmm10, xmm11, 1);
  511. pxor_clear(xmm12, xmm12);
  512. pxor_clear(xmm13, xmm13);
  513. pxor_clear(xmm14, xmm14);
  514. movq(xmm15, Len);
  515. palignr(xmm12, xmm15, 15);
  516. palignr(xmm14, xmm15, 1);
  517. MEOW_DUMP_STATE("PostBlocks", xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, 0);
  518. MEOW_DUMP_STATE("Residuals", xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15, 0);
  519. // NOTE(casey): To maintain the mix-down pattern, we always Meow Mix the less-than-32-byte residual, even if it was empty
  520. MEOW_MIX_REG(xmm0, xmm4, xmm6, xmm1, xmm2, xmm8, xmm9, xmm10, xmm11);
  521. // NOTE(casey): Append the length, to avoid problems with our 32-byte padding
  522. MEOW_MIX_REG(xmm1, xmm5, xmm7, xmm2, xmm3, xmm12, xmm13, xmm14, xmm15);
  523. MEOW_DUMP_STATE("PostAppend", xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, 0);
  524. //
  525. // NOTE(casey): Hash all full 32-byte blocks
  526. //
  527. int unsigned LaneCount = (Len >> 5) & 0x7;
  528. if(LaneCount == 0) goto MixDown; MEOW_MIX(xmm2,xmm6,xmm0,xmm3,xmm4, rax + 0x00); --LaneCount;
  529. if(LaneCount == 0) goto MixDown; MEOW_MIX(xmm3,xmm7,xmm1,xmm4,xmm5, rax + 0x20); --LaneCount;
  530. if(LaneCount == 0) goto MixDown; MEOW_MIX(xmm4,xmm0,xmm2,xmm5,xmm6, rax + 0x40); --LaneCount;
  531. if(LaneCount == 0) goto MixDown; MEOW_MIX(xmm5,xmm1,xmm3,xmm6,xmm7, rax + 0x60); --LaneCount;
  532. if(LaneCount == 0) goto MixDown; MEOW_MIX(xmm6,xmm2,xmm4,xmm7,xmm0, rax + 0x80); --LaneCount;
  533. if(LaneCount == 0) goto MixDown; MEOW_MIX(xmm7,xmm3,xmm5,xmm0,xmm1, rax + 0xa0); --LaneCount;
  534. if(LaneCount == 0) goto MixDown; MEOW_MIX(xmm0,xmm4,xmm6,xmm1,xmm2, rax + 0xc0); --LaneCount;
  535. //
  536. // NOTE(casey): Mix the eight lanes down to one 128-bit hash
  537. //
  538. MixDown:
  539. MEOW_DUMP_STATE("PostLanes", xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, 0);
  540. MEOW_SHUFFLE(xmm0, xmm1, xmm2, xmm4, xmm5, xmm6);
  541. MEOW_SHUFFLE(xmm1, xmm2, xmm3, xmm5, xmm6, xmm7);
  542. MEOW_SHUFFLE(xmm2, xmm3, xmm4, xmm6, xmm7, xmm0);
  543. MEOW_SHUFFLE(xmm3, xmm4, xmm5, xmm7, xmm0, xmm1);
  544. MEOW_SHUFFLE(xmm4, xmm5, xmm6, xmm0, xmm1, xmm2);
  545. MEOW_SHUFFLE(xmm5, xmm6, xmm7, xmm1, xmm2, xmm3);
  546. MEOW_SHUFFLE(xmm6, xmm7, xmm0, xmm2, xmm3, xmm4);
  547. MEOW_SHUFFLE(xmm7, xmm0, xmm1, xmm3, xmm4, xmm5);
  548. MEOW_SHUFFLE(xmm0, xmm1, xmm2, xmm4, xmm5, xmm6);
  549. MEOW_SHUFFLE(xmm1, xmm2, xmm3, xmm5, xmm6, xmm7);
  550. MEOW_SHUFFLE(xmm2, xmm3, xmm4, xmm6, xmm7, xmm0);
  551. MEOW_SHUFFLE(xmm3, xmm4, xmm5, xmm7, xmm0, xmm1);
  552. MEOW_DUMP_STATE("PostMix", xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, 0);
  553. if(Store128)
  554. {
  555. movdqu_mem(Store128 + 0x00, xmm0);
  556. movdqu_mem(Store128 + 0x10, xmm1);
  557. movdqu_mem(Store128 + 0x20, xmm2);
  558. movdqu_mem(Store128 + 0x30, xmm3);
  559. movdqu_mem(Store128 + 0x40, xmm4);
  560. movdqu_mem(Store128 + 0x50, xmm5);
  561. movdqu_mem(Store128 + 0x60, xmm6);
  562. movdqu_mem(Store128 + 0x70, xmm7);
  563. }
  564. paddq(xmm0, xmm2);
  565. paddq(xmm1, xmm3);
  566. paddq(xmm4, xmm6);
  567. paddq(xmm5, xmm7);
  568. pxor(xmm0, xmm1);
  569. pxor(xmm4, xmm5);
  570. paddq(xmm0, xmm4);
  571. MEOW_DUMP_STATE("PostFold", xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, 0);
  572. return(xmm0);
  573. }
  574. #undef INSTRUCTION_REORDER_BARRIER
  575. #undef prefetcht0
  576. #undef movdqu
  577. #undef movdqu_mem
  578. #undef movq
  579. #undef aesdec
  580. #undef pshufb
  581. #undef pxor
  582. #undef paddq
  583. #undef pand
  584. #undef palignr
  585. #undef pxor_clear
  586. #undef MEOW_MIX
  587. #undef MEOW_MIX_REG
  588. #undef MEOW_SHUFFLE
  589. #undef MEOW_DUMP_STATE
  590. //
  591. // NOTE(casey): If you need to create your own seed from non-random data, you can use MeowExpandSeed
  592. // to create a seed which you then store for repeated use. It is _expensive_ to generate the seed,
  593. // so you do not want to do this every time you hash. You _only_ want to do it when you actually
  594. // need to create a new seed.
  595. //
  596. static void
  597. MeowExpandSeed(meow_umm InputLen, void *Input, meow_u8 *SeedResult)
  598. {
  599. meow_state State;
  600. meow_u64 LengthTab = (meow_u64)InputLen; // NOTE(casey): We need to always injest 8-byte lengths exactly, even on 32-bit builds, to ensure identical results
  601. meow_umm InjestCount = (256 / InputLen) + 2;
  602. MeowBegin(&State, MeowDefaultSeed);
  603. MeowAbsorb(&State, sizeof(LengthTab), &LengthTab);
  604. while(InjestCount--)
  605. {
  606. MeowAbsorb(&State, InputLen, Input);
  607. }
  608. MeowEnd(&State, SeedResult);
  609. }
  610. #define MEOW_HASH_X64_AESNI_H
  611. #endif