SecT193Field.cs 8.8 KB

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