SecT193Field.cs 9.3 KB

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