SecT239Field.cs 10 KB

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