SecT239Field.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Diagnostics;
  5. #if NETCOREAPP3_0_OR_GREATER
  6. using System.Runtime.Intrinsics;
  7. using System.Runtime.Intrinsics.X86;
  8. #endif
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
  10. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
  11. {
  12. internal class SecT239Field
  13. {
  14. private const ulong M47 = ulong.MaxValue >> 17;
  15. private const ulong M60 = ulong.MaxValue >> 4;
  16. public static void Add(ulong[] x, ulong[] y, ulong[] z)
  17. {
  18. z[0] = x[0] ^ y[0];
  19. z[1] = x[1] ^ y[1];
  20. z[2] = x[2] ^ y[2];
  21. z[3] = x[3] ^ y[3];
  22. }
  23. public static void AddExt(ulong[] xx, ulong[] yy, ulong[] zz)
  24. {
  25. zz[0] = xx[0] ^ yy[0];
  26. zz[1] = xx[1] ^ yy[1];
  27. zz[2] = xx[2] ^ yy[2];
  28. zz[3] = xx[3] ^ yy[3];
  29. zz[4] = xx[4] ^ yy[4];
  30. zz[5] = xx[5] ^ yy[5];
  31. zz[6] = xx[6] ^ yy[6];
  32. zz[7] = xx[7] ^ yy[7];
  33. }
  34. public static void AddOne(ulong[] x, ulong[] z)
  35. {
  36. z[0] = x[0] ^ 1UL;
  37. z[1] = x[1];
  38. z[2] = x[2];
  39. z[3] = x[3];
  40. }
  41. private static void AddTo(ulong[] x, ulong[] z)
  42. {
  43. z[0] ^= x[0];
  44. z[1] ^= x[1];
  45. z[2] ^= x[2];
  46. z[3] ^= x[3];
  47. }
  48. public static ulong[] FromBigInteger(BigInteger x)
  49. {
  50. return Nat.FromBigInteger64(239, x);
  51. }
  52. public static void HalfTrace(ulong[] x, ulong[] z)
  53. {
  54. ulong[] tt = Nat256.CreateExt64();
  55. Nat256.Copy64(x, z);
  56. for (int i = 1; i < 239; i += 2)
  57. {
  58. ImplSquare(z, tt);
  59. Reduce(tt, z);
  60. ImplSquare(z, tt);
  61. Reduce(tt, z);
  62. AddTo(x, z);
  63. }
  64. }
  65. public static void Invert(ulong[] x, ulong[] z)
  66. {
  67. if (Nat256.IsZero64(x))
  68. throw new InvalidOperationException();
  69. // Itoh-Tsujii inversion
  70. ulong[] t0 = Nat256.Create64();
  71. ulong[] t1 = Nat256.Create64();
  72. Square(x, t0);
  73. Multiply(t0, x, t0);
  74. Square(t0, t0);
  75. Multiply(t0, x, t0);
  76. SquareN(t0, 3, t1);
  77. Multiply(t1, t0, t1);
  78. Square(t1, t1);
  79. Multiply(t1, x, t1);
  80. SquareN(t1, 7, t0);
  81. Multiply(t0, t1, t0);
  82. SquareN(t0, 14, t1);
  83. Multiply(t1, t0, t1);
  84. Square(t1, t1);
  85. Multiply(t1, x, t1);
  86. SquareN(t1, 29, t0);
  87. Multiply(t0, t1, t0);
  88. Square(t0, t0);
  89. Multiply(t0, x, t0);
  90. SquareN(t0, 59, t1);
  91. Multiply(t1, t0, t1);
  92. Square(t1, t1);
  93. Multiply(t1, x, t1);
  94. SquareN(t1, 119, t0);
  95. Multiply(t0, t1, t0);
  96. Square(t0, z);
  97. }
  98. public static void Multiply(ulong[] x, ulong[] y, ulong[] z)
  99. {
  100. ulong[] tt = Nat256.CreateExt64();
  101. ImplMultiply(x, y, tt);
  102. Reduce(tt, z);
  103. }
  104. public static void MultiplyAddToExt(ulong[] x, ulong[] y, ulong[] zz)
  105. {
  106. ulong[] tt = Nat256.CreateExt64();
  107. ImplMultiply(x, y, tt);
  108. AddExt(zz, tt, zz);
  109. }
  110. public static void Reduce(ulong[] xx, ulong[] z)
  111. {
  112. ulong x0 = xx[0], x1 = xx[1], x2 = xx[2], x3 = xx[3];
  113. ulong x4 = xx[4], x5 = xx[5], x6 = xx[6], x7 = xx[7];
  114. x3 ^= (x7 << 17);
  115. x4 ^= (x7 >> 47);
  116. x5 ^= (x7 << 47);
  117. x6 ^= (x7 >> 17);
  118. x2 ^= (x6 << 17);
  119. x3 ^= (x6 >> 47);
  120. x4 ^= (x6 << 47);
  121. x5 ^= (x6 >> 17);
  122. x1 ^= (x5 << 17);
  123. x2 ^= (x5 >> 47);
  124. x3 ^= (x5 << 47);
  125. x4 ^= (x5 >> 17);
  126. x0 ^= (x4 << 17);
  127. x1 ^= (x4 >> 47);
  128. x2 ^= (x4 << 47);
  129. x3 ^= (x4 >> 17);
  130. ulong t = x3 >> 47;
  131. z[0] = x0 ^ t;
  132. z[1] = x1;
  133. z[2] = x2 ^ (t << 30);
  134. z[3] = x3 & M47;
  135. }
  136. public static void Reduce17(ulong[] z, int zOff)
  137. {
  138. ulong z3 = z[zOff + 3], t = z3 >> 47;
  139. z[zOff ] ^= t;
  140. z[zOff + 2] ^= (t << 30);
  141. z[zOff + 3] = z3 & M47;
  142. }
  143. public static void Sqrt(ulong[] x, ulong[] z)
  144. {
  145. ulong c0 = Interleave.Unshuffle(x[0], x[1], out ulong e0);
  146. ulong c1 = Interleave.Unshuffle(x[2], x[3], out ulong e1);
  147. ulong c2, c3;
  148. c3 = (c1 >> 49);
  149. c2 = (c0 >> 49) | (c1 << 15);
  150. c1 ^= (c0 << 15);
  151. ulong[] tt = Nat256.CreateExt64();
  152. int[] shifts = { 39, 120 };
  153. for (int i = 0; i < shifts.Length; ++i)
  154. {
  155. int w = shifts[i] >> 6, s = shifts[i] & 63;
  156. Debug.Assert(s != 0);
  157. tt[w ] ^= (c0 << s);
  158. tt[w + 1] ^= (c1 << s) | (c0 >> -s);
  159. tt[w + 2] ^= (c2 << s) | (c1 >> -s);
  160. tt[w + 3] ^= (c3 << s) | (c2 >> -s);
  161. tt[w + 4] ^= (c3 >> -s);
  162. }
  163. Reduce(tt, z);
  164. z[0] ^= e0;
  165. z[1] ^= e1;
  166. }
  167. public static void Square(ulong[] x, ulong[] z)
  168. {
  169. ulong[] tt = Nat256.CreateExt64();
  170. ImplSquare(x, tt);
  171. Reduce(tt, z);
  172. }
  173. public static void SquareAddToExt(ulong[] x, ulong[] zz)
  174. {
  175. ulong[] tt = Nat256.CreateExt64();
  176. ImplSquare(x, tt);
  177. AddExt(zz, tt, zz);
  178. }
  179. public static void SquareN(ulong[] x, int n, ulong[] z)
  180. {
  181. Debug.Assert(n > 0);
  182. ulong[] tt = Nat256.CreateExt64();
  183. ImplSquare(x, tt);
  184. Reduce(tt, z);
  185. while (--n > 0)
  186. {
  187. ImplSquare(z, tt);
  188. Reduce(tt, z);
  189. }
  190. }
  191. public static uint Trace(ulong[] x)
  192. {
  193. // Non-zero-trace bits: 0, 81, 162
  194. return (uint)(x[0] ^ (x[1] >> 17) ^ (x[2] >> 34)) & 1U;
  195. }
  196. protected static void ImplCompactExt(ulong[] zz)
  197. {
  198. ulong z0 = zz[0], z1 = zz[1], z2 = zz[2], z3 = zz[3], z4 = zz[4], z5 = zz[5], z6 = zz[6], z7 = zz[7];
  199. zz[0] = z0 ^ (z1 << 60);
  200. zz[1] = (z1 >> 4) ^ (z2 << 56);
  201. zz[2] = (z2 >> 8) ^ (z3 << 52);
  202. zz[3] = (z3 >> 12) ^ (z4 << 48);
  203. zz[4] = (z4 >> 16) ^ (z5 << 44);
  204. zz[5] = (z5 >> 20) ^ (z6 << 40);
  205. zz[6] = (z6 >> 24) ^ (z7 << 36);
  206. zz[7] = (z7 >> 28);
  207. }
  208. protected static void ImplExpand(ulong[] x, ulong[] z)
  209. {
  210. ulong x0 = x[0], x1 = x[1], x2 = x[2], x3 = x[3];
  211. z[0] = x0 & M60;
  212. z[1] = ((x0 >> 60) ^ (x1 << 4)) & M60;
  213. z[2] = ((x1 >> 56) ^ (x2 << 8)) & M60;
  214. z[3] = ((x2 >> 52) ^ (x3 << 12));
  215. }
  216. protected static void ImplMultiply(ulong[] x, ulong[] y, ulong[] zz)
  217. {
  218. #if NETCOREAPP3_0_OR_GREATER
  219. if (Pclmulqdq.IsSupported)
  220. {
  221. var X01 = Vector128.Create(x[0], x[1]);
  222. var X23 = Vector128.Create(x[2], x[3]);
  223. var Y01 = Vector128.Create(y[0], y[1]);
  224. var Y23 = Vector128.Create(y[2], y[3]);
  225. var X03 = Sse2.Xor(X01, X23);
  226. var Y03 = Sse2.Xor(Y01, Y23);
  227. var Z01 = Pclmulqdq.CarrylessMultiply(X01, Y01, 0x00);
  228. var Z12 = Sse2.Xor(Pclmulqdq.CarrylessMultiply(X01, Y01, 0x01),
  229. Pclmulqdq.CarrylessMultiply(X01, Y01, 0x10));
  230. var Z23 = Pclmulqdq.CarrylessMultiply(X01, Y01, 0x11);
  231. var Z45 = Pclmulqdq.CarrylessMultiply(X23, Y23, 0x00);
  232. var Z56 = Sse2.Xor(Pclmulqdq.CarrylessMultiply(X23, Y23, 0x01),
  233. Pclmulqdq.CarrylessMultiply(X23, Y23, 0x10));
  234. var Z67 = Pclmulqdq.CarrylessMultiply(X23, Y23, 0x11);
  235. var K01 = Pclmulqdq.CarrylessMultiply(X03, Y03, 0x00);
  236. var K12 = Sse2.Xor(Pclmulqdq.CarrylessMultiply(X03, Y03, 0x01),
  237. Pclmulqdq.CarrylessMultiply(X03, Y03, 0x10));
  238. var K23 = Pclmulqdq.CarrylessMultiply(X03, Y03, 0x11);
  239. K01 = Sse2.Xor(K01, Z01);
  240. K12 = Sse2.Xor(K12, Z12);
  241. K23 = Sse2.Xor(K23, Z23);
  242. K01 = Sse2.Xor(K01, Z45);
  243. K12 = Sse2.Xor(K12, Z56);
  244. K23 = Sse2.Xor(K23, Z67);
  245. Z23 = Sse2.Xor(Z23, K01);
  246. Z45 = Sse2.Xor(Z45, K23);
  247. zz[0] = Z01.GetElement(0);
  248. zz[1] = Z01.GetElement(1) ^ Z12.GetElement(0);
  249. zz[2] = Z23.GetElement(0) ^ Z12.GetElement(1);
  250. zz[3] = Z23.GetElement(1) ^ K12.GetElement(0);
  251. zz[4] = Z45.GetElement(0) ^ K12.GetElement(1);
  252. zz[5] = Z45.GetElement(1) ^ Z56.GetElement(0);
  253. zz[6] = Z67.GetElement(0) ^ Z56.GetElement(1);
  254. zz[7] = Z67.GetElement(1);
  255. return;
  256. }
  257. #endif
  258. /*
  259. * "Two-level seven-way recursion" as described in "Batch binary Edwards", Daniel J. Bernstein.
  260. */
  261. ulong[] f = new ulong[4], g = new ulong[4];
  262. ImplExpand(x, f);
  263. ImplExpand(y, g);
  264. ulong[] u = new ulong[8];
  265. ImplMulwAcc(u, f[0], g[0], zz, 0);
  266. ImplMulwAcc(u, f[1], g[1], zz, 1);
  267. ImplMulwAcc(u, f[2], g[2], zz, 2);
  268. ImplMulwAcc(u, f[3], g[3], zz, 3);
  269. // U *= (1 - t^n)
  270. for (int i = 5; i > 0; --i)
  271. {
  272. zz[i] ^= zz[i - 1];
  273. }
  274. ImplMulwAcc(u, f[0] ^ f[1], g[0] ^ g[1], zz, 1);
  275. ImplMulwAcc(u, f[2] ^ f[3], g[2] ^ g[3], zz, 3);
  276. // V *= (1 - t^2n)
  277. for (int i = 7; i > 1; --i)
  278. {
  279. zz[i] ^= zz[i - 2];
  280. }
  281. // Double-length recursion
  282. {
  283. ulong c0 = f[0] ^ f[2], c1 = f[1] ^ f[3];
  284. ulong d0 = g[0] ^ g[2], d1 = g[1] ^ g[3];
  285. ImplMulwAcc(u, c0 ^ c1, d0 ^ d1, zz, 3);
  286. ulong[] t = new ulong[3];
  287. ImplMulwAcc(u, c0, d0, t, 0);
  288. ImplMulwAcc(u, c1, d1, t, 1);
  289. ulong t0 = t[0], t1 = t[1], t2 = t[2];
  290. zz[2] ^= t0;
  291. zz[3] ^= t0 ^ t1;
  292. zz[4] ^= t2 ^ t1;
  293. zz[5] ^= t2;
  294. }
  295. ImplCompactExt(zz);
  296. }
  297. protected static void ImplMulwAcc(ulong[] u, ulong x, ulong y, ulong[] z, int zOff)
  298. {
  299. Debug.Assert(x >> 60 == 0);
  300. Debug.Assert(y >> 60 == 0);
  301. //u[0] = 0;
  302. u[1] = y;
  303. u[2] = u[1] << 1;
  304. u[3] = u[2] ^ y;
  305. u[4] = u[2] << 1;
  306. u[5] = u[4] ^ y;
  307. u[6] = u[3] << 1;
  308. u[7] = u[6] ^ y;
  309. uint j = (uint)x;
  310. ulong g, h = 0, l = u[j & 7]
  311. ^ (u[(j >> 3) & 7] << 3);
  312. int k = 54;
  313. do
  314. {
  315. j = (uint)(x >> k);
  316. g = u[j & 7]
  317. ^ u[(j >> 3) & 7] << 3;
  318. l ^= (g << k);
  319. h ^= (g >> -k);
  320. }
  321. while ((k -= 6) > 0);
  322. h ^= ((x & 0x0820820820820820L) & (ulong)(((long)y << 4) >> 63)) >> 5;
  323. Debug.Assert(h >> 55 == 0);
  324. z[zOff ] ^= l & M60;
  325. z[zOff + 1] ^= (l >> 60) ^ (h << 4);
  326. }
  327. protected static void ImplSquare(ulong[] x, ulong[] zz)
  328. {
  329. Interleave.Expand64To128(x, 0, 4, zz, 0);
  330. }
  331. }
  332. }
  333. #pragma warning restore
  334. #endif