SecT131Field.cs 10 KB

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