SecT193Field.cs 11 KB

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