FastChaCha7539Engine.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  8. namespace Best.HTTP.Shared.TLS.Crypto.Impl
  9. {
  10. /// <summary>
  11. /// Implementation of Daniel J. Bernstein's ChaCha stream cipher.
  12. /// </summary>
  13. [Best.HTTP.Shared.PlatformSupport.IL2CPP.Il2CppEagerStaticClassConstructionAttribute]
  14. public sealed class FastChaCha7539Engine
  15. : FastSalsa20Engine
  16. {
  17. /// <summary>
  18. /// Creates a 20 rounds ChaCha engine.
  19. /// </summary>
  20. public FastChaCha7539Engine()
  21. : base()
  22. {
  23. }
  24. public override string AlgorithmName
  25. {
  26. get { return "ChaCha7539"; }
  27. }
  28. protected override int NonceSize
  29. {
  30. get { return 12; }
  31. }
  32. protected override void AdvanceCounter()
  33. {
  34. if (++engineState[12] == 0)
  35. throw new InvalidOperationException("attempt to increase counter past 2^32.");
  36. }
  37. protected override void ResetCounter()
  38. {
  39. engineState[12] = 0;
  40. }
  41. protected override void SetKey(byte[] keyBytes, byte[] ivBytes)
  42. {
  43. if (keyBytes != null)
  44. {
  45. if (keyBytes.Length != 32)
  46. throw new ArgumentException(AlgorithmName + " requires 256 bit key");
  47. PackTauOrSigma(keyBytes.Length, engineState, 0);
  48. // Key
  49. Pack.LE_To_UInt32(keyBytes, 0, engineState, 4, 8);
  50. }
  51. // IV
  52. Pack.LE_To_UInt32(ivBytes, 0, engineState, 13, 3);
  53. }
  54. protected override void GenerateKeyStream(byte[] output)
  55. {
  56. FastChaChaEngineHelper.ChachaCore(rounds, engineState, output);
  57. }
  58. internal void DoFinal(byte[] inBuf, int inOff, int inLen, byte[] outBuf, int outOff)
  59. {
  60. if (!initialised)
  61. throw new InvalidOperationException(AlgorithmName + " not initialised");
  62. if (index != 0)
  63. throw new InvalidOperationException(AlgorithmName + " not in block-aligned state");
  64. Check.DataLength(inBuf, inOff, inLen, "input buffer too short");
  65. Check.OutputLength(outBuf, outOff, inLen, "output buffer too short");
  66. while (inLen >= 128)
  67. {
  68. #if BESTHTTP_WITH_BURST && (NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER)
  69. FastChaCha7539EngineHelper.ProcessBlocks2(inBuf.AsSpan(inOff), outBuf.AsSpan(outOff), engineState, rounds, keyStream);
  70. #elif NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  71. //ProcessBlocks2(inBuf.AsSpan(inOff), outBuf.AsSpan(outOff));
  72. var input = inBuf.AsSpan(inOff);
  73. var output = outBuf.AsSpan(outOff);
  74. ImplProcessBlock(inBuf.AsSpan(inOff), outBuf.AsSpan(outOff));
  75. ImplProcessBlock(input[64..], output[64..]);
  76. #else
  77. ProcessBlocks2(inBuf, inOff, outBuf, outOff);
  78. #endif
  79. inOff += 128;
  80. inLen -= 128;
  81. outOff += 128;
  82. }
  83. if (inLen >= 64)
  84. {
  85. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  86. ImplProcessBlock(inBuf.AsSpan(inOff), outBuf.AsSpan(outOff));
  87. #else
  88. ImplProcessBlock(inBuf, inOff, outBuf, outOff);
  89. #endif
  90. inOff += 64;
  91. inLen -= 64;
  92. outOff += 64;
  93. }
  94. if (inLen > 0)
  95. {
  96. GenerateKeyStream(keyStream);
  97. //AdvanceCounter();
  98. if (++engineState[12] == 0)
  99. throw new InvalidOperationException("attempt to increase counter past 2^32.");
  100. for (int i = 0; i < inLen; ++i)
  101. {
  102. outBuf[outOff + i] = (byte)(inBuf[i + inOff] ^ keyStream[i]);
  103. }
  104. }
  105. engineState[12] = 0;
  106. // TODO Prevent re-use if encrypting
  107. }
  108. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  109. internal void ProcessBlock(ReadOnlySpan<byte> input, Span<byte> output)
  110. {
  111. if (!initialised)
  112. throw new InvalidOperationException(AlgorithmName + " not initialised");
  113. if (LimitExceeded(64U))
  114. throw new MaxBytesExceededException("2^38 byte limit per IV would be exceeded; Change IV");
  115. UnityEngine.Debug.Assert(index == 0);
  116. ImplProcessBlock(input, output);
  117. }
  118. internal void ProcessBlocks2(ReadOnlySpan<byte> input, Span<byte> output)
  119. {
  120. if (!initialised)
  121. throw new InvalidOperationException(AlgorithmName + " not initialised");
  122. if (LimitExceeded(128U))
  123. throw new MaxBytesExceededException("2^38 byte limit per IV would be exceeded; Change IV");
  124. UnityEngine.Debug.Assert(index == 0);
  125. #if NETCOREAPP3_0_OR_GREATER
  126. if (Avx2.IsSupported)
  127. {
  128. ImplProcessBlocks2_X86_Avx2(rounds, engineState, input, output);
  129. return;
  130. }
  131. if (Sse2.IsSupported)
  132. {
  133. ImplProcessBlocks2_X86_Sse2(rounds, engineState, input, output);
  134. return;
  135. }
  136. #endif
  137. {
  138. ImplProcessBlock(input, output);
  139. ImplProcessBlock(input[64..], output[64..]);
  140. }
  141. }
  142. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  143. internal void ImplProcessBlock(ReadOnlySpan<byte> input, Span<byte> output)
  144. {
  145. FastChaChaEngineHelper.ChachaCore(rounds, engineState, keyStream);
  146. //AdvanceCounter();
  147. if (++engineState[12] == 0)
  148. throw new InvalidOperationException("attempt to increase counter past 2^32.");
  149. FastChaChaEngineHelper.ImplProcessBlock(input, output, keyStream);
  150. }
  151. #else
  152. internal void ProcessBlock(byte[] inBytes, int inOff, byte[] outBytes, int outOff)
  153. {
  154. if (!initialised)
  155. throw new InvalidOperationException(AlgorithmName + " not initialised");
  156. if (LimitExceeded(64U))
  157. throw new MaxBytesExceededException("2^38 byte limit per IV would be exceeded; Change IV");
  158. UnityEngine.Debug.Assert(index == 0);
  159. ImplProcessBlock(inBytes, inOff, outBytes, outOff);
  160. }
  161. internal void ProcessBlocks2(byte[] inBytes, int inOff, byte[] outBytes, int outOff)
  162. {
  163. if (!initialised)
  164. throw new InvalidOperationException(AlgorithmName + " not initialised");
  165. if (LimitExceeded(128U))
  166. throw new MaxBytesExceededException("2^38 byte limit per IV would be exceeded; Change IV");
  167. UnityEngine.Debug.Assert(index == 0);
  168. {
  169. ImplProcessBlock(inBytes, inOff, outBytes, outOff);
  170. ImplProcessBlock(inBytes, inOff + 64, outBytes, outOff + 64);
  171. }
  172. }
  173. #if NETSTANDARD1_0_OR_GREATER || NETCOREAPP1_0_OR_GREATER || UNITY_2021_2_OR_NEWER
  174. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  175. #endif
  176. internal void ImplProcessBlock(byte[] inBuf, int inOff, byte[] outBuf, int outOff)
  177. {
  178. ChaChaEngine.ChachaCore(rounds, engineState, keyStream);
  179. AdvanceCounter();
  180. for (int i = 0; i < 64; ++i)
  181. {
  182. outBuf[outOff + i] = (byte)(keyStream[i] ^ inBuf[inOff + i]);
  183. }
  184. }
  185. #endif
  186. #if NETCOREAPP3_0_OR_GREATER
  187. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  188. internal static void ImplProcessBlocks2_X86_Avx2(int rounds, uint[] state, ReadOnlySpan<byte> input,
  189. Span<byte> output)
  190. {
  191. if (!Avx2.IsSupported)
  192. throw new PlatformNotSupportedException();
  193. Debug.Assert(rounds % 2 == 0);
  194. Debug.Assert(state.Length >= 16);
  195. Debug.Assert(input.Length >= 128);
  196. Debug.Assert(output.Length >= 128);
  197. var t0 = Load128_UInt32(state.AsSpan());
  198. var t1 = Load128_UInt32(state.AsSpan(4));
  199. var t2 = Load128_UInt32(state.AsSpan(8));
  200. var t3 = Load128_UInt32(state.AsSpan(12));
  201. ++state[12];
  202. var t4 = Load128_UInt32(state.AsSpan(12));
  203. ++state[12];
  204. var x0 = Vector256.Create(t0, t0);
  205. var x1 = Vector256.Create(t1, t1);
  206. var x2 = Vector256.Create(t2, t2);
  207. var x3 = Vector256.Create(t3, t4);
  208. var v0 = x0;
  209. var v1 = x1;
  210. var v2 = x2;
  211. var v3 = x3;
  212. for (int i = rounds; i > 0; i -= 2)
  213. {
  214. v0 = Avx2.Add(v0, v1);
  215. v3 = Avx2.Xor(v3, v0);
  216. v3 = Avx2.Xor(Avx2.ShiftLeftLogical(v3, 16), Avx2.ShiftRightLogical(v3, 16));
  217. v2 = Avx2.Add(v2, v3);
  218. v1 = Avx2.Xor(v1, v2);
  219. v1 = Avx2.Xor(Avx2.ShiftLeftLogical(v1, 12), Avx2.ShiftRightLogical(v1, 20));
  220. v0 = Avx2.Add(v0, v1);
  221. v3 = Avx2.Xor(v3, v0);
  222. v3 = Avx2.Xor(Avx2.ShiftLeftLogical(v3, 8), Avx2.ShiftRightLogical(v3, 24));
  223. v2 = Avx2.Add(v2, v3);
  224. v1 = Avx2.Xor(v1, v2);
  225. v1 = Avx2.Xor(Avx2.ShiftLeftLogical(v1, 7), Avx2.ShiftRightLogical(v1, 25));
  226. v1 = Avx2.Shuffle(v1, 0x39);
  227. v2 = Avx2.Shuffle(v2, 0x4E);
  228. v3 = Avx2.Shuffle(v3, 0x93);
  229. v0 = Avx2.Add(v0, v1);
  230. v3 = Avx2.Xor(v3, v0);
  231. v3 = Avx2.Xor(Avx2.ShiftLeftLogical(v3, 16), Avx2.ShiftRightLogical(v3, 16));
  232. v2 = Avx2.Add(v2, v3);
  233. v1 = Avx2.Xor(v1, v2);
  234. v1 = Avx2.Xor(Avx2.ShiftLeftLogical(v1, 12), Avx2.ShiftRightLogical(v1, 20));
  235. v0 = Avx2.Add(v0, v1);
  236. v3 = Avx2.Xor(v3, v0);
  237. v3 = Avx2.Xor(Avx2.ShiftLeftLogical(v3, 8), Avx2.ShiftRightLogical(v3, 24));
  238. v2 = Avx2.Add(v2, v3);
  239. v1 = Avx2.Xor(v1, v2);
  240. v1 = Avx2.Xor(Avx2.ShiftLeftLogical(v1, 7), Avx2.ShiftRightLogical(v1, 25));
  241. v1 = Avx2.Shuffle(v1, 0x93);
  242. v2 = Avx2.Shuffle(v2, 0x4E);
  243. v3 = Avx2.Shuffle(v3, 0x39);
  244. }
  245. v0 = Avx2.Add(v0, x0);
  246. v1 = Avx2.Add(v1, x1);
  247. v2 = Avx2.Add(v2, x2);
  248. v3 = Avx2.Add(v3, x3);
  249. var n0 = Avx2.Permute2x128(v0, v1, 0x20).AsByte();
  250. var n1 = Avx2.Permute2x128(v2, v3, 0x20).AsByte();
  251. var n2 = Avx2.Permute2x128(v0, v1, 0x31).AsByte();
  252. var n3 = Avx2.Permute2x128(v2, v3, 0x31).AsByte();
  253. n0 = Avx2.Xor(n0, Load256_Byte(input));
  254. n1 = Avx2.Xor(n1, Load256_Byte(input[0x20..]));
  255. n2 = Avx2.Xor(n2, Load256_Byte(input[0x40..]));
  256. n3 = Avx2.Xor(n3, Load256_Byte(input[0x60..]));
  257. Store256_Byte(n0, output);
  258. Store256_Byte(n1, output[0x20..]);
  259. Store256_Byte(n2, output[0x40..]);
  260. Store256_Byte(n3, output[0x60..]);
  261. }
  262. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  263. internal static void ImplProcessBlocks2_X86_Sse2(int rounds, uint[] state, ReadOnlySpan<byte> input,
  264. Span<byte> output)
  265. {
  266. if (!Sse2.IsSupported)
  267. throw new PlatformNotSupportedException();
  268. Debug.Assert(rounds % 2 == 0);
  269. Debug.Assert(state.Length >= 16);
  270. Debug.Assert(input.Length >= 128);
  271. Debug.Assert(output.Length >= 128);
  272. var x0 = Load128_UInt32(state.AsSpan());
  273. var x1 = Load128_UInt32(state.AsSpan(4));
  274. var x2 = Load128_UInt32(state.AsSpan(8));
  275. var x3 = Load128_UInt32(state.AsSpan(12));
  276. ++state[12];
  277. var v0 = x0;
  278. var v1 = x1;
  279. var v2 = x2;
  280. var v3 = x3;
  281. for (int i = rounds; i > 0; i -= 2)
  282. {
  283. v0 = Sse2.Add(v0, v1);
  284. v3 = Sse2.Xor(v3, v0);
  285. v3 = Sse2.Xor(Sse2.ShiftLeftLogical(v3, 16), Sse2.ShiftRightLogical(v3, 16));
  286. v2 = Sse2.Add(v2, v3);
  287. v1 = Sse2.Xor(v1, v2);
  288. v1 = Sse2.Xor(Sse2.ShiftLeftLogical(v1, 12), Sse2.ShiftRightLogical(v1, 20));
  289. v0 = Sse2.Add(v0, v1);
  290. v3 = Sse2.Xor(v3, v0);
  291. v3 = Sse2.Xor(Sse2.ShiftLeftLogical(v3, 8), Sse2.ShiftRightLogical(v3, 24));
  292. v2 = Sse2.Add(v2, v3);
  293. v1 = Sse2.Xor(v1, v2);
  294. v1 = Sse2.Xor(Sse2.ShiftLeftLogical(v1, 7), Sse2.ShiftRightLogical(v1, 25));
  295. v1 = Sse2.Shuffle(v1, 0x39);
  296. v2 = Sse2.Shuffle(v2, 0x4E);
  297. v3 = Sse2.Shuffle(v3, 0x93);
  298. v0 = Sse2.Add(v0, v1);
  299. v3 = Sse2.Xor(v3, v0);
  300. v3 = Sse2.Xor(Sse2.ShiftLeftLogical(v3, 16), Sse2.ShiftRightLogical(v3, 16));
  301. v2 = Sse2.Add(v2, v3);
  302. v1 = Sse2.Xor(v1, v2);
  303. v1 = Sse2.Xor(Sse2.ShiftLeftLogical(v1, 12), Sse2.ShiftRightLogical(v1, 20));
  304. v0 = Sse2.Add(v0, v1);
  305. v3 = Sse2.Xor(v3, v0);
  306. v3 = Sse2.Xor(Sse2.ShiftLeftLogical(v3, 8), Sse2.ShiftRightLogical(v3, 24));
  307. v2 = Sse2.Add(v2, v3);
  308. v1 = Sse2.Xor(v1, v2);
  309. v1 = Sse2.Xor(Sse2.ShiftLeftLogical(v1, 7), Sse2.ShiftRightLogical(v1, 25));
  310. v1 = Sse2.Shuffle(v1, 0x93);
  311. v2 = Sse2.Shuffle(v2, 0x4E);
  312. v3 = Sse2.Shuffle(v3, 0x39);
  313. }
  314. v0 = Sse2.Add(v0, x0);
  315. v1 = Sse2.Add(v1, x1);
  316. v2 = Sse2.Add(v2, x2);
  317. v3 = Sse2.Add(v3, x3);
  318. var n0 = Load128_Byte(input);
  319. var n1 = Load128_Byte(input[0x10..]);
  320. var n2 = Load128_Byte(input[0x20..]);
  321. var n3 = Load128_Byte(input[0x30..]);
  322. n0 = Sse2.Xor(n0, v0.AsByte());
  323. n1 = Sse2.Xor(n1, v1.AsByte());
  324. n2 = Sse2.Xor(n2, v2.AsByte());
  325. n3 = Sse2.Xor(n3, v3.AsByte());
  326. Store128_Byte(n0, output);
  327. Store128_Byte(n1, output[0x10..]);
  328. Store128_Byte(n2, output[0x20..]);
  329. Store128_Byte(n3, output[0x30..]);
  330. x3 = Load128_UInt32(state.AsSpan(12));
  331. ++state[12];
  332. v0 = x0;
  333. v1 = x1;
  334. v2 = x2;
  335. v3 = x3;
  336. for (int i = rounds; i > 0; i -= 2)
  337. {
  338. v0 = Sse2.Add(v0, v1);
  339. v3 = Sse2.Xor(v3, v0);
  340. v3 = Sse2.Xor(Sse2.ShiftLeftLogical(v3, 16), Sse2.ShiftRightLogical(v3, 16));
  341. v2 = Sse2.Add(v2, v3);
  342. v1 = Sse2.Xor(v1, v2);
  343. v1 = Sse2.Xor(Sse2.ShiftLeftLogical(v1, 12), Sse2.ShiftRightLogical(v1, 20));
  344. v0 = Sse2.Add(v0, v1);
  345. v3 = Sse2.Xor(v3, v0);
  346. v3 = Sse2.Xor(Sse2.ShiftLeftLogical(v3, 8), Sse2.ShiftRightLogical(v3, 24));
  347. v2 = Sse2.Add(v2, v3);
  348. v1 = Sse2.Xor(v1, v2);
  349. v1 = Sse2.Xor(Sse2.ShiftLeftLogical(v1, 7), Sse2.ShiftRightLogical(v1, 25));
  350. v1 = Sse2.Shuffle(v1, 0x39);
  351. v2 = Sse2.Shuffle(v2, 0x4E);
  352. v3 = Sse2.Shuffle(v3, 0x93);
  353. v0 = Sse2.Add(v0, v1);
  354. v3 = Sse2.Xor(v3, v0);
  355. v3 = Sse2.Xor(Sse2.ShiftLeftLogical(v3, 16), Sse2.ShiftRightLogical(v3, 16));
  356. v2 = Sse2.Add(v2, v3);
  357. v1 = Sse2.Xor(v1, v2);
  358. v1 = Sse2.Xor(Sse2.ShiftLeftLogical(v1, 12), Sse2.ShiftRightLogical(v1, 20));
  359. v0 = Sse2.Add(v0, v1);
  360. v3 = Sse2.Xor(v3, v0);
  361. v3 = Sse2.Xor(Sse2.ShiftLeftLogical(v3, 8), Sse2.ShiftRightLogical(v3, 24));
  362. v2 = Sse2.Add(v2, v3);
  363. v1 = Sse2.Xor(v1, v2);
  364. v1 = Sse2.Xor(Sse2.ShiftLeftLogical(v1, 7), Sse2.ShiftRightLogical(v1, 25));
  365. v1 = Sse2.Shuffle(v1, 0x93);
  366. v2 = Sse2.Shuffle(v2, 0x4E);
  367. v3 = Sse2.Shuffle(v3, 0x39);
  368. }
  369. v0 = Sse2.Add(v0, x0);
  370. v1 = Sse2.Add(v1, x1);
  371. v2 = Sse2.Add(v2, x2);
  372. v3 = Sse2.Add(v3, x3);
  373. n0 = Load128_Byte(input[0x40..]);
  374. n1 = Load128_Byte(input[0x50..]);
  375. n2 = Load128_Byte(input[0x60..]);
  376. n3 = Load128_Byte(input[0x70..]);
  377. n0 = Sse2.Xor(n0, v0.AsByte());
  378. n1 = Sse2.Xor(n1, v1.AsByte());
  379. n2 = Sse2.Xor(n2, v2.AsByte());
  380. n3 = Sse2.Xor(n3, v3.AsByte());
  381. Store128_Byte(n0, output[0x40..]);
  382. Store128_Byte(n1, output[0x50..]);
  383. Store128_Byte(n2, output[0x60..]);
  384. Store128_Byte(n3, output[0x70..]);
  385. }
  386. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  387. private static Vector128<byte> Load128_Byte(ReadOnlySpan<byte> t)
  388. {
  389. if (BitConverter.IsLittleEndian && Unsafe.SizeOf<Vector128<byte>>() == 16)
  390. return MemoryMarshal.Read<Vector128<byte>>(t);
  391. return Vector128.Create(
  392. BinaryPrimitives.ReadUInt64LittleEndian(t[..8]),
  393. BinaryPrimitives.ReadUInt64LittleEndian(t[8..])
  394. ).AsByte();
  395. }
  396. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  397. private static Vector128<uint> Load128_UInt32(ReadOnlySpan<uint> t)
  398. {
  399. if (BitConverter.IsLittleEndian && Unsafe.SizeOf<Vector128<uint>>() == 16)
  400. return MemoryMarshal.Read<Vector128<uint>>(MemoryMarshal.AsBytes(t));
  401. return Vector128.Create(t[0], t[1], t[2], t[3]);
  402. }
  403. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  404. private static Vector256<byte> Load256_Byte(ReadOnlySpan<byte> t)
  405. {
  406. if (BitConverter.IsLittleEndian && Unsafe.SizeOf<Vector256<byte>>() == 32)
  407. return MemoryMarshal.Read<Vector256<byte>>(t);
  408. return Vector256.Create(
  409. BinaryPrimitives.ReadUInt64LittleEndian(t[ 0.. 8]),
  410. BinaryPrimitives.ReadUInt64LittleEndian(t[ 8..16]),
  411. BinaryPrimitives.ReadUInt64LittleEndian(t[16..24]),
  412. BinaryPrimitives.ReadUInt64LittleEndian(t[24..32])
  413. ).AsByte();
  414. }
  415. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  416. private static void Store128_Byte(Vector128<byte> s, Span<byte> t)
  417. {
  418. if (BitConverter.IsLittleEndian && Unsafe.SizeOf<Vector128<byte>>() == 16)
  419. {
  420. MemoryMarshal.Write(t, ref s);
  421. return;
  422. }
  423. var u = s.AsUInt64();
  424. BinaryPrimitives.WriteUInt64LittleEndian(t[..8], u.GetElement(0));
  425. BinaryPrimitives.WriteUInt64LittleEndian(t[8..], u.GetElement(1));
  426. }
  427. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  428. private static void Store256_Byte(Vector256<byte> s, Span<byte> t)
  429. {
  430. if (BitConverter.IsLittleEndian && Unsafe.SizeOf<Vector256<byte>>() == 32)
  431. {
  432. MemoryMarshal.Write(t, ref s);
  433. return;
  434. }
  435. var u = s.AsUInt64();
  436. BinaryPrimitives.WriteUInt64LittleEndian(t[ 0.. 8], u.GetElement(0));
  437. BinaryPrimitives.WriteUInt64LittleEndian(t[ 8..16], u.GetElement(1));
  438. BinaryPrimitives.WriteUInt64LittleEndian(t[16..24], u.GetElement(2));
  439. BinaryPrimitives.WriteUInt64LittleEndian(t[24..32], u.GetElement(3));
  440. }
  441. #endif
  442. }
  443. }
  444. #pragma warning restore
  445. #endif