SecT131Field.cs 12 KB

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