SecT239Field.cs 9.6 KB

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