SecT163Field.cs 9.8 KB

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