SecT193Field.cs 8.9 KB

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