SecT283Field.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 SecT283Field
  13. {
  14. private const ulong M27 = ulong.MaxValue >> 37;
  15. private const ulong M57 = ulong.MaxValue >> 7;
  16. private static readonly ulong[] ROOT_Z = new ulong[]{ 0x0C30C30C30C30808UL, 0x30C30C30C30C30C3UL,
  17. 0x820820820820830CUL, 0x0820820820820820UL, 0x2082082UL };
  18. public static void Add(ulong[] x, ulong[] y, ulong[] z)
  19. {
  20. z[0] = x[0] ^ y[0];
  21. z[1] = x[1] ^ y[1];
  22. z[2] = x[2] ^ y[2];
  23. z[3] = x[3] ^ y[3];
  24. z[4] = x[4] ^ y[4];
  25. }
  26. public static void AddExt(ulong[] xx, ulong[] yy, ulong[] zz)
  27. {
  28. zz[0] = xx[0] ^ yy[0];
  29. zz[1] = xx[1] ^ yy[1];
  30. zz[2] = xx[2] ^ yy[2];
  31. zz[3] = xx[3] ^ yy[3];
  32. zz[4] = xx[4] ^ yy[4];
  33. zz[5] = xx[5] ^ yy[5];
  34. zz[6] = xx[6] ^ yy[6];
  35. zz[7] = xx[7] ^ yy[7];
  36. zz[8] = xx[8] ^ yy[8];
  37. }
  38. public static void AddOne(ulong[] x, ulong[] z)
  39. {
  40. z[0] = x[0] ^ 1UL;
  41. z[1] = x[1];
  42. z[2] = x[2];
  43. z[3] = x[3];
  44. z[4] = x[4];
  45. }
  46. private static void AddTo(ulong[] x, ulong[] z)
  47. {
  48. z[0] ^= x[0];
  49. z[1] ^= x[1];
  50. z[2] ^= x[2];
  51. z[3] ^= x[3];
  52. z[4] ^= x[4];
  53. }
  54. public static ulong[] FromBigInteger(BigInteger x)
  55. {
  56. return Nat.FromBigInteger64(283, x);
  57. }
  58. public static void HalfTrace(ulong[] x, ulong[] z)
  59. {
  60. ulong[] tt = Nat.Create64(9);
  61. Nat320.Copy64(x, z);
  62. for (int i = 1; i < 283; i += 2)
  63. {
  64. ImplSquare(z, tt);
  65. Reduce(tt, z);
  66. ImplSquare(z, tt);
  67. Reduce(tt, z);
  68. AddTo(x, z);
  69. }
  70. }
  71. public static void Invert(ulong[] x, ulong[] z)
  72. {
  73. if (Nat320.IsZero64(x))
  74. throw new InvalidOperationException();
  75. // Itoh-Tsujii inversion
  76. ulong[] t0 = Nat320.Create64();
  77. ulong[] t1 = Nat320.Create64();
  78. Square(x, t0);
  79. Multiply(t0, x, t0);
  80. SquareN(t0, 2, t1);
  81. Multiply(t1, t0, t1);
  82. SquareN(t1, 4, t0);
  83. Multiply(t0, t1, t0);
  84. SquareN(t0, 8, t1);
  85. Multiply(t1, t0, t1);
  86. Square(t1, t1);
  87. Multiply(t1, x, t1);
  88. SquareN(t1, 17, t0);
  89. Multiply(t0, t1, t0);
  90. Square(t0, t0);
  91. Multiply(t0, x, t0);
  92. SquareN(t0, 35, t1);
  93. Multiply(t1, t0, t1);
  94. SquareN(t1, 70, t0);
  95. Multiply(t0, t1, t0);
  96. Square(t0, t0);
  97. Multiply(t0, x, t0);
  98. SquareN(t0, 141, t1);
  99. Multiply(t1, t0, t1);
  100. Square(t1, z);
  101. }
  102. public static void Multiply(ulong[] x, ulong[] y, ulong[] z)
  103. {
  104. ulong[] tt = Nat320.CreateExt64();
  105. ImplMultiply(x, y, tt);
  106. Reduce(tt, z);
  107. }
  108. public static void MultiplyAddToExt(ulong[] x, ulong[] y, ulong[] zz)
  109. {
  110. ulong[] tt = Nat320.CreateExt64();
  111. ImplMultiply(x, y, tt);
  112. AddExt(zz, tt, zz);
  113. }
  114. public static void Reduce(ulong[] xx, ulong[] z)
  115. {
  116. ulong x0 = xx[0], x1 = xx[1], x2 = xx[2], x3 = xx[3], x4 = xx[4];
  117. ulong x5 = xx[5], x6 = xx[6], x7 = xx[7], x8 = xx[8];
  118. x3 ^= (x8 << 37) ^ (x8 << 42) ^ (x8 << 44) ^ (x8 << 49);
  119. x4 ^= (x8 >> 27) ^ (x8 >> 22) ^ (x8 >> 20) ^ (x8 >> 15);
  120. x2 ^= (x7 << 37) ^ (x7 << 42) ^ (x7 << 44) ^ (x7 << 49);
  121. x3 ^= (x7 >> 27) ^ (x7 >> 22) ^ (x7 >> 20) ^ (x7 >> 15);
  122. x1 ^= (x6 << 37) ^ (x6 << 42) ^ (x6 << 44) ^ (x6 << 49);
  123. x2 ^= (x6 >> 27) ^ (x6 >> 22) ^ (x6 >> 20) ^ (x6 >> 15);
  124. x0 ^= (x5 << 37) ^ (x5 << 42) ^ (x5 << 44) ^ (x5 << 49);
  125. x1 ^= (x5 >> 27) ^ (x5 >> 22) ^ (x5 >> 20) ^ (x5 >> 15);
  126. ulong t = x4 >> 27;
  127. z[0] = x0 ^ t ^ (t << 5) ^ (t << 7) ^ (t << 12);
  128. z[1] = x1;
  129. z[2] = x2;
  130. z[3] = x3;
  131. z[4] = x4 & M27;
  132. }
  133. public static void Reduce37(ulong[] z, int zOff)
  134. {
  135. ulong z4 = z[zOff + 4], t = z4 >> 27;
  136. z[zOff ] ^= t ^ (t << 5) ^ (t << 7) ^ (t << 12);
  137. z[zOff + 4] = z4 & M27;
  138. }
  139. public static void Sqrt(ulong[] x, ulong[] z)
  140. {
  141. ulong[] odd = Nat320.Create64();
  142. odd[0] = Interleave.Unshuffle(x[0], x[1], out ulong e0);
  143. odd[1] = Interleave.Unshuffle(x[2], x[3], out ulong e1);
  144. odd[2] = Interleave.Unshuffle(x[4] , out ulong e2);
  145. Multiply(odd, ROOT_Z, z);
  146. z[0] ^= e0;
  147. z[1] ^= e1;
  148. z[2] ^= e2;
  149. }
  150. public static void Square(ulong[] x, ulong[] z)
  151. {
  152. ulong[] tt = Nat.Create64(9);
  153. ImplSquare(x, tt);
  154. Reduce(tt, z);
  155. }
  156. public static void SquareAddToExt(ulong[] x, ulong[] zz)
  157. {
  158. ulong[] tt = Nat.Create64(9);
  159. ImplSquare(x, tt);
  160. AddExt(zz, tt, zz);
  161. }
  162. public static void SquareN(ulong[] x, int n, ulong[] z)
  163. {
  164. Debug.Assert(n > 0);
  165. ulong[] tt = Nat.Create64(9);
  166. ImplSquare(x, tt);
  167. Reduce(tt, z);
  168. while (--n > 0)
  169. {
  170. ImplSquare(z, tt);
  171. Reduce(tt, z);
  172. }
  173. }
  174. public static uint Trace(ulong[] x)
  175. {
  176. // Non-zero-trace bits: 0, 271
  177. return (uint)(x[0] ^ (x[4] >> 15)) & 1U;
  178. }
  179. protected static void ImplCompactExt(ulong[] zz)
  180. {
  181. ulong z0 = zz[0], z1 = zz[1], z2 = zz[2], z3 = zz[3], z4 = zz[4];
  182. ulong z5 = zz[5], z6 = zz[6], z7 = zz[7], z8 = zz[8], z9 = zz[9];
  183. zz[0] = z0 ^ (z1 << 57);
  184. zz[1] = (z1 >> 7) ^ (z2 << 50);
  185. zz[2] = (z2 >> 14) ^ (z3 << 43);
  186. zz[3] = (z3 >> 21) ^ (z4 << 36);
  187. zz[4] = (z4 >> 28) ^ (z5 << 29);
  188. zz[5] = (z5 >> 35) ^ (z6 << 22);
  189. zz[6] = (z6 >> 42) ^ (z7 << 15);
  190. zz[7] = (z7 >> 49) ^ (z8 << 8);
  191. zz[8] = (z8 >> 56) ^ (z9 << 1);
  192. zz[9] = (z9 >> 63); // Zero!
  193. }
  194. protected static void ImplExpand(ulong[] x, ulong[] z)
  195. {
  196. ulong x0 = x[0], x1 = x[1], x2 = x[2], x3 = x[3], x4 = x[4];
  197. z[0] = x0 & M57;
  198. z[1] = ((x0 >> 57) ^ (x1 << 7)) & M57;
  199. z[2] = ((x1 >> 50) ^ (x2 << 14)) & M57;
  200. z[3] = ((x2 >> 43) ^ (x3 << 21)) & M57;
  201. z[4] = ((x3 >> 36) ^ (x4 << 28));
  202. }
  203. //protected static void AddMs(ulong[] zz, int zOff, ulong[] p, params int[] ms)
  204. //{
  205. // ulong t0 = 0, t1 = 0;
  206. // foreach (int m in ms)
  207. // {
  208. // int i = (m - 1) << 1;
  209. // t0 ^= p[i ];
  210. // t1 ^= p[i + 1];
  211. // }
  212. // zz[zOff ] ^= t0;
  213. // zz[zOff + 1] ^= t1;
  214. //}
  215. protected static void ImplMultiply(ulong[] x, ulong[] y, ulong[] zz)
  216. {
  217. #if NETCOREAPP3_0_OR_GREATER
  218. if (Pclmulqdq.IsSupported)
  219. {
  220. var X01 = Vector128.Create(x[0], x[1]);
  221. var X23 = Vector128.Create(x[2], x[3]);
  222. var X4_ = Vector128.CreateScalar(x[4]);
  223. var Y01 = Vector128.Create(y[0], y[1]);
  224. var Y23 = Vector128.Create(y[2], y[3]);
  225. var Y4_ = Vector128.CreateScalar(y[4]);
  226. var Z01 = Pclmulqdq.CarrylessMultiply(X01, Y01, 0x00);
  227. var Z12 = Sse2.Xor(Pclmulqdq.CarrylessMultiply(X01, Y01, 0x01),
  228. Pclmulqdq.CarrylessMultiply(X01, Y01, 0x10));
  229. var Z23 = Sse2.Xor(Pclmulqdq.CarrylessMultiply(X01, Y23, 0x00),
  230. Sse2.Xor(Pclmulqdq.CarrylessMultiply(X01, Y01, 0x11),
  231. Pclmulqdq.CarrylessMultiply(X23, Y01, 0x00)));
  232. var Z34 = Sse2.Xor(Pclmulqdq.CarrylessMultiply(X01, Y23, 0x01),
  233. Sse2.Xor(Pclmulqdq.CarrylessMultiply(X01, Y23, 0x10),
  234. Sse2.Xor(Pclmulqdq.CarrylessMultiply(X23, Y01, 0x01),
  235. Pclmulqdq.CarrylessMultiply(X23, Y01, 0x10))));
  236. var Z45 = Sse2.Xor(Pclmulqdq.CarrylessMultiply(X01, Y4_, 0x00),
  237. Sse2.Xor(Pclmulqdq.CarrylessMultiply(X01, Y23, 0x11),
  238. Sse2.Xor(Pclmulqdq.CarrylessMultiply(X23, Y23, 0x00),
  239. Sse2.Xor(Pclmulqdq.CarrylessMultiply(X23, Y01, 0x11),
  240. Pclmulqdq.CarrylessMultiply(X4_, Y01, 0x00)))));
  241. var Z56 = Sse2.Xor(Pclmulqdq.CarrylessMultiply(X01, Y4_, 0x01),
  242. Sse2.Xor(Pclmulqdq.CarrylessMultiply(X23, Y23, 0x01),
  243. Sse2.Xor(Pclmulqdq.CarrylessMultiply(X23, Y23, 0x10),
  244. Pclmulqdq.CarrylessMultiply(X4_, Y01, 0x10))));
  245. var Z67 = Sse2.Xor(Pclmulqdq.CarrylessMultiply(X23, Y4_, 0x00),
  246. Sse2.Xor(Pclmulqdq.CarrylessMultiply(X23, Y23, 0x11),
  247. Pclmulqdq.CarrylessMultiply(X4_, Y23, 0x00)));
  248. var Z78 = Sse2.Xor(Pclmulqdq.CarrylessMultiply(X23, Y4_, 0x01),
  249. Pclmulqdq.CarrylessMultiply(X4_, Y23, 0x10));
  250. var Z89 = Pclmulqdq.CarrylessMultiply(X4_, Y4_, 0x00);
  251. zz[0] = Z01.GetElement(0);
  252. zz[1] = Z01.GetElement(1) ^ Z12.GetElement(0);
  253. zz[2] = Z23.GetElement(0) ^ Z12.GetElement(1);
  254. zz[3] = Z23.GetElement(1) ^ Z34.GetElement(0);
  255. zz[4] = Z45.GetElement(0) ^ Z34.GetElement(1);
  256. zz[5] = Z45.GetElement(1) ^ Z56.GetElement(0);
  257. zz[6] = Z67.GetElement(0) ^ Z56.GetElement(1);
  258. zz[7] = Z67.GetElement(1) ^ Z78.GetElement(0);
  259. zz[8] = Z89.GetElement(0) ^ Z78.GetElement(1);
  260. zz[9] = Z89.GetElement(1);
  261. return;
  262. }
  263. #endif
  264. /*
  265. * Formula (17) from "Some New Results on Binary Polynomial Multiplication",
  266. * Murat Cenk and M. Anwar Hasan.
  267. *
  268. * The formula as given contained an error in the term t25, as noted below
  269. */
  270. ulong[] a = new ulong[5], b = new ulong[5];
  271. ImplExpand(x, a);
  272. ImplExpand(y, b);
  273. ulong[] u = zz;
  274. ulong[] p = new ulong[26];
  275. ImplMulw(u, a[0], b[0], p, 0); // m1
  276. ImplMulw(u, a[1], b[1], p, 2); // m2
  277. ImplMulw(u, a[2], b[2], p, 4); // m3
  278. ImplMulw(u, a[3], b[3], p, 6); // m4
  279. ImplMulw(u, a[4], b[4], p, 8); // m5
  280. ulong u0 = a[0] ^ a[1], v0 = b[0] ^ b[1];
  281. ulong u1 = a[0] ^ a[2], v1 = b[0] ^ b[2];
  282. ulong u2 = a[2] ^ a[4], v2 = b[2] ^ b[4];
  283. ulong u3 = a[3] ^ a[4], v3 = b[3] ^ b[4];
  284. ImplMulw(u, u1 ^ a[3], v1 ^ b[3], p, 18); // m10
  285. ImplMulw(u, u2 ^ a[1], v2 ^ b[1], p, 20); // m11
  286. ulong A4 = u0 ^ u3 , B4 = v0 ^ v3;
  287. ulong A5 = A4 ^ a[2], B5 = B4 ^ b[2];
  288. ImplMulw(u, A4, B4, p, 22); // m12
  289. ImplMulw(u, A5, B5, p, 24); // m13
  290. ImplMulw(u, u0, v0, p, 10); // m6
  291. ImplMulw(u, u1, v1, p, 12); // m7
  292. ImplMulw(u, u2, v2, p, 14); // m8
  293. ImplMulw(u, u3, v3, p, 16); // m9
  294. // Original method, corresponding to formula (16)
  295. //AddMs(zz, 0, p, 1);
  296. //AddMs(zz, 1, p, 1, 2, 6);
  297. //AddMs(zz, 2, p, 1, 2, 3, 7);
  298. //AddMs(zz, 3, p, 1, 3, 4, 5, 8, 10, 12, 13);
  299. //AddMs(zz, 4, p, 1, 2, 4, 5, 6, 9, 10, 11, 13);
  300. //AddMs(zz, 5, p, 1, 2, 3, 5, 7, 11, 12, 13);
  301. //AddMs(zz, 6, p, 3, 4, 5, 8);
  302. //AddMs(zz, 7, p, 4, 5, 9);
  303. //AddMs(zz, 8, p, 5);
  304. // Improved method factors out common single-word terms
  305. // NOTE: p1,...,p26 in the paper maps to p[0],...,p[25] here
  306. zz[0] = p[ 0];
  307. zz[9] = p[ 9];
  308. ulong t1 = p[ 0] ^ p[ 1];
  309. ulong t2 = t1 ^ p[ 2];
  310. ulong t3 = t2 ^ p[10];
  311. zz[1] = t3;
  312. ulong t4 = p[ 3] ^ p[ 4];
  313. ulong t5 = p[11] ^ p[12];
  314. ulong t6 = t4 ^ t5;
  315. ulong t7 = t2 ^ t6;
  316. zz[2] = t7;
  317. ulong t8 = t1 ^ t4;
  318. ulong t9 = p[ 5] ^ p[ 6];
  319. ulong t10 = t8 ^ t9;
  320. ulong t11 = t10 ^ p[ 8];
  321. ulong t12 = p[13] ^ p[14];
  322. ulong t13 = t11 ^ t12;
  323. ulong t14 = p[18] ^ p[22];
  324. ulong t15 = t14 ^ p[24];
  325. ulong t16 = t13 ^ t15;
  326. zz[3] = t16;
  327. ulong t17 = p[ 7] ^ p[ 8];
  328. ulong t18 = t17 ^ p[ 9];
  329. ulong t19 = t18 ^ p[17];
  330. zz[8] = t19;
  331. ulong t20 = t18 ^ t9;
  332. ulong t21 = p[15] ^ p[16];
  333. ulong t22 = t20 ^ t21;
  334. zz[7] = t22;
  335. ulong t23 = t22 ^ t3;
  336. ulong t24 = p[19] ^ p[20];
  337. // ulong t25 = p[23] ^ p[24];
  338. ulong t25 = p[25] ^ p[24]; // Fixes an error in the paper: p[23] -> p{25]
  339. ulong t26 = p[18] ^ p[23];
  340. ulong t27 = t24 ^ t25;
  341. ulong t28 = t27 ^ t26;
  342. ulong t29 = t28 ^ t23;
  343. zz[4] = t29;
  344. ulong t30 = t7 ^ t19;
  345. ulong t31 = t27 ^ t30;
  346. ulong t32 = p[21] ^ p[22];
  347. ulong t33 = t31 ^ t32;
  348. zz[5] = t33;
  349. ulong t34 = t11 ^ p[0];
  350. ulong t35 = t34 ^ p[9];
  351. ulong t36 = t35 ^ t12;
  352. ulong t37 = t36 ^ p[21];
  353. ulong t38 = t37 ^ p[23];
  354. ulong t39 = t38 ^ p[25];
  355. zz[6] = t39;
  356. ImplCompactExt(zz);
  357. }
  358. protected static void ImplMulw(ulong[] u, ulong x, ulong y, ulong[] z, int zOff)
  359. {
  360. Debug.Assert(x >> 57 == 0);
  361. Debug.Assert(y >> 57 == 0);
  362. //u[0] = 0;
  363. u[1] = y;
  364. u[2] = u[1] << 1;
  365. u[3] = u[2] ^ y;
  366. u[4] = u[2] << 1;
  367. u[5] = u[4] ^ y;
  368. u[6] = u[3] << 1;
  369. u[7] = u[6] ^ y;
  370. uint j = (uint)x;
  371. ulong g, h = 0, l = u[j & 7];
  372. int k = 48;
  373. do
  374. {
  375. j = (uint)(x >> k);
  376. g = u[j & 7]
  377. ^ u[(j >> 3) & 7] << 3
  378. ^ u[(j >> 6) & 7] << 6;
  379. l ^= (g << k);
  380. h ^= (g >> -k);
  381. }
  382. while ((k -= 9) > 0);
  383. h ^= ((x & 0x0100804020100800L) & (ulong)(((long)y << 7) >> 63)) >> 8;
  384. Debug.Assert(h >> 49 == 0);
  385. z[zOff ] = l & M57;
  386. z[zOff + 1] = (l >> 57) ^ (h << 7);
  387. }
  388. protected static void ImplSquare(ulong[] x, ulong[] zz)
  389. {
  390. Interleave.Expand64To128(x, 0, 4, zz, 0);
  391. zz[8] = Interleave.Expand32to64((uint)x[4]);
  392. }
  393. }
  394. }
  395. #pragma warning restore
  396. #endif