ChaCha7539Engine.cs 17 KB

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