SecT163Field.cs 11 KB

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