SecT163Field.cs 9.7 KB

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