SecT113Field.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 SecT113Field
  13. {
  14. private const ulong M49 = ulong.MaxValue >> 15;
  15. private const ulong M57 = ulong.MaxValue >> 7;
  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. }
  21. public static void AddExt(ulong[] xx, ulong[] yy, ulong[] zz)
  22. {
  23. zz[0] = xx[0] ^ yy[0];
  24. zz[1] = xx[1] ^ yy[1];
  25. zz[2] = xx[2] ^ yy[2];
  26. zz[3] = xx[3] ^ yy[3];
  27. }
  28. public static void AddOne(ulong[] x, ulong[] z)
  29. {
  30. z[0] = x[0] ^ 1UL;
  31. z[1] = x[1];
  32. }
  33. private static void AddTo(ulong[] x, ulong[] z)
  34. {
  35. z[0] ^= x[0];
  36. z[1] ^= x[1];
  37. }
  38. public static ulong[] FromBigInteger(BigInteger x)
  39. {
  40. return Nat.FromBigInteger64(113, x);
  41. }
  42. public static void HalfTrace(ulong[] x, ulong[] z)
  43. {
  44. ulong[] tt = Nat128.CreateExt64();
  45. Nat128.Copy64(x, z);
  46. for (int i = 1; i < 113; i += 2)
  47. {
  48. ImplSquare(z, tt);
  49. Reduce(tt, z);
  50. ImplSquare(z, tt);
  51. Reduce(tt, z);
  52. AddTo(x, z);
  53. }
  54. }
  55. public static void Invert(ulong[] x, ulong[] z)
  56. {
  57. if (Nat128.IsZero64(x))
  58. throw new InvalidOperationException();
  59. // Itoh-Tsujii inversion
  60. ulong[] t0 = Nat128.Create64();
  61. ulong[] t1 = Nat128.Create64();
  62. Square(x, t0);
  63. Multiply(t0, x, t0);
  64. Square(t0, t0);
  65. Multiply(t0, x, t0);
  66. SquareN(t0, 3, t1);
  67. Multiply(t1, t0, t1);
  68. Square(t1, t1);
  69. Multiply(t1, x, t1);
  70. SquareN(t1, 7, t0);
  71. Multiply(t0, t1, t0);
  72. SquareN(t0, 14, t1);
  73. Multiply(t1, t0, t1);
  74. SquareN(t1, 28, t0);
  75. Multiply(t0, t1, t0);
  76. SquareN(t0, 56, t1);
  77. Multiply(t1, t0, t1);
  78. Square(t1, z);
  79. }
  80. public static void Multiply(ulong[] x, ulong[] y, ulong[] z)
  81. {
  82. ulong[] tt = new ulong[8];
  83. ImplMultiply(x, y, tt);
  84. Reduce(tt, z);
  85. }
  86. public static void MultiplyAddToExt(ulong[] x, ulong[] y, ulong[] zz)
  87. {
  88. ulong[] tt = new ulong[8];
  89. ImplMultiply(x, y, tt);
  90. AddExt(zz, tt, zz);
  91. }
  92. public static void Reduce(ulong[] xx, ulong[] z)
  93. {
  94. ulong x0 = xx[0], x1 = xx[1], x2 = xx[2], x3 = xx[3];
  95. x1 ^= (x3 << 15) ^ (x3 << 24);
  96. x2 ^= (x3 >> 49) ^ (x3 >> 40);
  97. x0 ^= (x2 << 15) ^ (x2 << 24);
  98. x1 ^= (x2 >> 49) ^ (x2 >> 40);
  99. ulong t = x1 >> 49;
  100. z[0] = x0 ^ t ^ (t << 9);
  101. z[1] = x1 & M49;
  102. }
  103. public static void Reduce15(ulong[] z, int zOff)
  104. {
  105. ulong z1 = z[zOff + 1], t = z1 >> 49;
  106. z[zOff ] ^= t ^ (t << 9);
  107. z[zOff + 1] = z1 & M49;
  108. }
  109. public static void Sqrt(ulong[] x, ulong[] z)
  110. {
  111. ulong c0 = Interleave.Unshuffle(x[0], x[1], out ulong e0);
  112. z[0] = e0 ^ (c0 << 57) ^ (c0 << 5);
  113. z[1] = (c0 >> 7) ^ (c0 >> 59);
  114. }
  115. public static void Square(ulong[] x, ulong[] z)
  116. {
  117. ulong[] tt = Nat128.CreateExt64();
  118. ImplSquare(x, tt);
  119. Reduce(tt, z);
  120. }
  121. public static void SquareAddToExt(ulong[] x, ulong[] zz)
  122. {
  123. ulong[] tt = Nat128.CreateExt64();
  124. ImplSquare(x, tt);
  125. AddExt(zz, tt, zz);
  126. }
  127. public static void SquareN(ulong[] x, int n, ulong[] z)
  128. {
  129. Debug.Assert(n > 0);
  130. ulong[] tt = Nat128.CreateExt64();
  131. ImplSquare(x, tt);
  132. Reduce(tt, z);
  133. while (--n > 0)
  134. {
  135. ImplSquare(z, tt);
  136. Reduce(tt, z);
  137. }
  138. }
  139. public static uint Trace(ulong[] x)
  140. {
  141. // Non-zero-trace bits: 0
  142. return (uint)(x[0]) & 1U;
  143. }
  144. protected static void ImplMultiply(ulong[] x, ulong[] y, ulong[] zz)
  145. {
  146. #if NETCOREAPP3_0_OR_GREATER
  147. if (Pclmulqdq.IsSupported)
  148. {
  149. var X01 = Vector128.Create(x[0], x[1]);
  150. var Y01 = Vector128.Create(y[0], y[1]);
  151. var Z01 = Pclmulqdq.CarrylessMultiply(X01, Y01, 0x00);
  152. var Z12 = Sse2.Xor(Pclmulqdq.CarrylessMultiply(X01, Y01, 0x01),
  153. Pclmulqdq.CarrylessMultiply(X01, Y01, 0x10));
  154. var Z23 = Pclmulqdq.CarrylessMultiply(X01, Y01, 0x11);
  155. zz[0] = Z01.GetElement(0);
  156. zz[1] = Z01.GetElement(1) ^ Z12.GetElement(0);
  157. zz[2] = Z23.GetElement(0) ^ Z12.GetElement(1);
  158. zz[3] = Z23.GetElement(1);
  159. return;
  160. }
  161. #endif
  162. /*
  163. * "Three-way recursion" as described in "Batch binary Edwards", Daniel J. Bernstein.
  164. */
  165. ulong f0 = x[0], f1 = x[1];
  166. f1 = ((f0 >> 57) ^ (f1 << 7)) & M57;
  167. f0 &= M57;
  168. ulong g0 = y[0], g1 = y[1];
  169. g1 = ((g0 >> 57) ^ (g1 << 7)) & M57;
  170. g0 &= M57;
  171. ulong[] u = zz;
  172. ulong[] H = new ulong[6];
  173. ImplMulw(u, f0, g0, H, 0); // H(0) 57/56 bits
  174. ImplMulw(u, f1, g1, H, 2); // H(INF) 57/54 bits
  175. ImplMulw(u, f0 ^ f1, g0 ^ g1, H, 4); // H(1) 57/56 bits
  176. ulong r = H[1] ^ H[2];
  177. ulong z0 = H[0],
  178. z3 = H[3],
  179. z1 = H[4] ^ z0 ^ r,
  180. z2 = H[5] ^ z3 ^ r;
  181. zz[0] = z0 ^ (z1 << 57);
  182. zz[1] = (z1 >> 7) ^ (z2 << 50);
  183. zz[2] = (z2 >> 14) ^ (z3 << 43);
  184. zz[3] = (z3 >> 21);
  185. }
  186. protected static void ImplMulw(ulong[] u, ulong x, ulong y, ulong[] z, int zOff)
  187. {
  188. Debug.Assert(x >> 57 == 0);
  189. Debug.Assert(y >> 57 == 0);
  190. //u[0] = 0;
  191. u[1] = y;
  192. u[2] = u[1] << 1;
  193. u[3] = u[2] ^ y;
  194. u[4] = u[2] << 1;
  195. u[5] = u[4] ^ y;
  196. u[6] = u[3] << 1;
  197. u[7] = u[6] ^ y;
  198. uint j = (uint)x;
  199. ulong g, h = 0, l = u[j & 7];
  200. int k = 48;
  201. do
  202. {
  203. j = (uint)(x >> k);
  204. g = u[j & 7]
  205. ^ u[(j >> 3) & 7] << 3
  206. ^ u[(j >> 6) & 7] << 6;
  207. l ^= (g << k);
  208. h ^= (g >> -k);
  209. }
  210. while ((k -= 9) > 0);
  211. h ^= ((x & 0x0100804020100800UL) & (ulong)(((long)y << 7) >> 63)) >> 8;
  212. Debug.Assert(h >> 49 == 0);
  213. z[zOff ] = l & M57;
  214. z[zOff + 1] = (l >> 57) ^ (h << 7);
  215. }
  216. protected static void ImplSquare(ulong[] x, ulong[] zz)
  217. {
  218. Interleave.Expand64To128(x, 0, 2, zz, 0);
  219. }
  220. }
  221. }
  222. #pragma warning restore
  223. #endif