SecT571Field.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 SecT571Field
  8. {
  9. private const ulong M59 = ulong.MaxValue >> 5;
  10. private const ulong RM = 0xEF7BDEF7BDEF7BDEUL;
  11. private static readonly ulong[] ROOT_Z = new ulong[]{ 0x2BE1195F08CAFB99UL, 0x95F08CAF84657C23UL, 0xCAF84657C232BE11UL, 0x657C232BE1195F08UL,
  12. 0xF84657C2308CAF84UL, 0x7C232BE1195F08CAUL, 0xBE1195F08CAF8465UL, 0x5F08CAF84657C232UL, 0x784657C232BE119UL };
  13. public static void Add(ulong[] x, ulong[] y, ulong[] z)
  14. {
  15. for (int i = 0; i < 9; ++i)
  16. {
  17. z[i] = x[i] ^ y[i];
  18. }
  19. }
  20. private static void Add(ulong[] x, int xOff, ulong[] y, int yOff, ulong[] z, int zOff)
  21. {
  22. for (int i = 0; i < 9; ++i)
  23. {
  24. z[zOff + i] = x[xOff + i] ^ y[yOff + i];
  25. }
  26. }
  27. private static void AddBothTo(ulong[] x, int xOff, ulong[] y, int yOff, ulong[] z, int zOff)
  28. {
  29. for (int i = 0; i < 9; ++i)
  30. {
  31. z[zOff + i] ^= x[xOff + i] ^ y[yOff + i];
  32. }
  33. }
  34. public static void AddExt(ulong[] xx, ulong[] yy, ulong[] zz)
  35. {
  36. for (int i = 0; i < 18; ++i)
  37. {
  38. zz[i] = xx[i] ^ yy[i];
  39. }
  40. }
  41. public static void AddOne(ulong[] x, ulong[] z)
  42. {
  43. z[0] = x[0] ^ 1UL;
  44. for (int i = 1; i < 9; ++i)
  45. {
  46. z[i] = x[i];
  47. }
  48. }
  49. public static ulong[] FromBigInteger(BigInteger x)
  50. {
  51. ulong[] z = Nat576.FromBigInteger64(x);
  52. Reduce5(z, 0);
  53. return z;
  54. }
  55. public static void Invert(ulong[] x, ulong[] z)
  56. {
  57. if (Nat576.IsZero64(x))
  58. throw new InvalidOperationException();
  59. // Itoh-Tsujii inversion with bases { 2, 3, 5 }
  60. ulong[] t0 = Nat576.Create64();
  61. ulong[] t1 = Nat576.Create64();
  62. ulong[] t2 = Nat576.Create64();
  63. Square(x, t2);
  64. // 5 | 570
  65. Square(t2, t0);
  66. Square(t0, t1);
  67. Multiply(t0, t1, t0);
  68. SquareN(t0, 2, t1);
  69. Multiply(t0, t1, t0);
  70. Multiply(t0, t2, t0);
  71. // 3 | 114
  72. SquareN(t0, 5, t1);
  73. Multiply(t0, t1, t0);
  74. SquareN(t1, 5, t1);
  75. Multiply(t0, t1, t0);
  76. // 2 | 38
  77. SquareN(t0, 15, t1);
  78. Multiply(t0, t1, t2);
  79. // ! {2,3,5} | 19
  80. SquareN(t2, 30, t0);
  81. SquareN(t0, 30, t1);
  82. Multiply(t0, t1, t0);
  83. // 3 | 9
  84. SquareN(t0, 60, t1);
  85. Multiply(t0, t1, t0);
  86. SquareN(t1, 60, t1);
  87. Multiply(t0, t1, t0);
  88. // 3 | 3
  89. SquareN(t0, 180, t1);
  90. Multiply(t0, t1, t0);
  91. SquareN(t1, 180, t1);
  92. Multiply(t0, t1, t0);
  93. Multiply(t0, t2, z);
  94. }
  95. public static void Multiply(ulong[] x, ulong[] y, ulong[] z)
  96. {
  97. ulong[] tt = Nat576.CreateExt64();
  98. ImplMultiply(x, y, tt);
  99. Reduce(tt, z);
  100. }
  101. public static void MultiplyAddToExt(ulong[] x, ulong[] y, ulong[] zz)
  102. {
  103. ulong[] tt = Nat576.CreateExt64();
  104. ImplMultiply(x, y, tt);
  105. AddExt(zz, tt, zz);
  106. }
  107. public static void Reduce(ulong[] xx, ulong[] z)
  108. {
  109. ulong xx09 = xx[9];
  110. ulong u = xx[17], v = xx09;
  111. xx09 = v ^ (u >> 59) ^ (u >> 57) ^ (u >> 54) ^ (u >> 49);
  112. v = xx[8] ^ (u << 5) ^ (u << 7) ^ (u << 10) ^ (u << 15);
  113. for (int i = 16; i >= 10; --i)
  114. {
  115. u = xx[i];
  116. z[i - 8] = v ^ (u >> 59) ^ (u >> 57) ^ (u >> 54) ^ (u >> 49);
  117. v = xx[i - 9] ^ (u << 5) ^ (u << 7) ^ (u << 10) ^ (u << 15);
  118. }
  119. u = xx09;
  120. z[1] = v ^ (u >> 59) ^ (u >> 57) ^ (u >> 54) ^ (u >> 49);
  121. v = xx[0] ^ (u << 5) ^ (u << 7) ^ (u << 10) ^ (u << 15);
  122. ulong x08 = z[8];
  123. ulong t = x08 >> 59;
  124. z[0] = v ^ t ^ (t << 2) ^ (t << 5) ^ (t << 10);
  125. z[8] = x08 & M59;
  126. }
  127. public static void Reduce5(ulong[] z, int zOff)
  128. {
  129. ulong z8 = z[zOff + 8], t = z8 >> 59;
  130. z[zOff ] ^= t ^ (t << 2) ^ (t << 5) ^ (t << 10);
  131. z[zOff + 8] = z8 & M59;
  132. }
  133. public static void Sqrt(ulong[] x, ulong[] z)
  134. {
  135. ulong[] evn = Nat576.Create64(), odd = Nat576.Create64();
  136. int pos = 0;
  137. for (int i = 0; i < 4; ++i)
  138. {
  139. ulong u0 = Interleave.Unshuffle(x[pos++]);
  140. ulong u1 = Interleave.Unshuffle(x[pos++]);
  141. evn[i] = (u0 & 0x00000000FFFFFFFFUL) | (u1 << 32);
  142. odd[i] = (u0 >> 32) | (u1 & 0xFFFFFFFF00000000UL);
  143. }
  144. {
  145. ulong u0 = Interleave.Unshuffle(x[pos]);
  146. evn[4] = (u0 & 0x00000000FFFFFFFFUL);
  147. odd[4] = (u0 >> 32);
  148. }
  149. Multiply(odd, ROOT_Z, z);
  150. Add(z, evn, z);
  151. }
  152. public static void Square(ulong[] x, ulong[] z)
  153. {
  154. ulong[] tt = Nat576.CreateExt64();
  155. ImplSquare(x, tt);
  156. Reduce(tt, z);
  157. }
  158. public static void SquareAddToExt(ulong[] x, ulong[] zz)
  159. {
  160. ulong[] tt = Nat576.CreateExt64();
  161. ImplSquare(x, tt);
  162. AddExt(zz, tt, zz);
  163. }
  164. public static void SquareN(ulong[] x, int n, ulong[] z)
  165. {
  166. Debug.Assert(n > 0);
  167. ulong[] tt = Nat576.CreateExt64();
  168. ImplSquare(x, tt);
  169. Reduce(tt, z);
  170. while (--n > 0)
  171. {
  172. ImplSquare(z, tt);
  173. Reduce(tt, z);
  174. }
  175. }
  176. public static uint Trace(ulong[] x)
  177. {
  178. // Non-zero-trace bits: 0, 561, 569
  179. return (uint)(x[0] ^ (x[8] >> 49) ^ (x[8] >> 57)) & 1U;
  180. }
  181. protected static void ImplMultiply(ulong[] x, ulong[] y, ulong[] zz)
  182. {
  183. //for (int i = 0; i < 9; ++i)
  184. //{
  185. // ImplMulwAcc(x, y[i], zz, i);
  186. //}
  187. /*
  188. * Precompute table of all 4-bit products of y
  189. */
  190. ulong[] T0 = new ulong[9 << 4];
  191. Array.Copy(y, 0, T0, 9, 9);
  192. // Reduce5(T0, 9);
  193. int tOff = 0;
  194. for (int i = 7; i > 0; --i)
  195. {
  196. tOff += 18;
  197. Nat.ShiftUpBit64(9, T0, tOff >> 1, 0UL, T0, tOff);
  198. Reduce5(T0, tOff);
  199. Add(T0, 9, T0, tOff, T0, tOff + 9);
  200. }
  201. /*
  202. * Second table with all 4-bit products of B shifted 4 bits
  203. */
  204. ulong[] T1 = new ulong[T0.Length];
  205. Nat.ShiftUpBits64(T0.Length, T0, 0, 4, 0L, T1, 0);
  206. uint MASK = 0xF;
  207. /*
  208. * Lopez-Dahab algorithm
  209. */
  210. for (int k = 56; k >= 0; k -= 8)
  211. {
  212. for (int j = 1; j < 9; j += 2)
  213. {
  214. uint aVal = (uint)(x[j] >> k);
  215. uint u = aVal & MASK;
  216. uint v = (aVal >> 4) & MASK;
  217. AddBothTo(T0, (int)(9 * u), T1, (int)(9 * v), zz, j - 1);
  218. }
  219. Nat.ShiftUpBits64(16, zz, 0, 8, 0L);
  220. }
  221. for (int k = 56; k >= 0; k -= 8)
  222. {
  223. for (int j = 0; j < 9; j += 2)
  224. {
  225. uint aVal = (uint)(x[j] >> k);
  226. uint u = aVal & MASK;
  227. uint v = (aVal >> 4) & MASK;
  228. AddBothTo(T0, (int)(9 * u), T1, (int)(9 * v), zz, j);
  229. }
  230. if (k > 0)
  231. {
  232. Nat.ShiftUpBits64(18, zz, 0, 8, 0L);
  233. }
  234. }
  235. }
  236. protected static void ImplMulwAcc(ulong[] xs, ulong y, ulong[] z, int zOff)
  237. {
  238. ulong[] u = new ulong[32];
  239. //u[0] = 0;
  240. u[1] = y;
  241. for (int i = 2; i < 32; i += 2)
  242. {
  243. u[i ] = u[i >> 1] << 1;
  244. u[i + 1] = u[i ] ^ y;
  245. }
  246. ulong l = 0;
  247. for (int i = 0; i < 9; ++i)
  248. {
  249. ulong x = xs[i];
  250. uint j = (uint)x;
  251. l ^= u[j & 31];
  252. ulong g, h = 0;
  253. int k = 60;
  254. do
  255. {
  256. j = (uint)(x >> k);
  257. g = u[j & 31];
  258. l ^= (g << k);
  259. h ^= (g >> -k);
  260. }
  261. while ((k -= 5) > 0);
  262. for (int p = 0; p < 4; ++p)
  263. {
  264. x = (x & RM) >> 1;
  265. h ^= x & (ulong)(((long)y << p) >> 63);
  266. }
  267. z[zOff + i] ^= l;
  268. l = h;
  269. }
  270. z[zOff + 9] ^= l;
  271. }
  272. protected static void ImplSquare(ulong[] x, ulong[] zz)
  273. {
  274. for (int i = 0; i < 9; ++i)
  275. {
  276. Interleave.Expand64To128(x[i], zz, i << 1);
  277. }
  278. }
  279. }
  280. }
  281. #endif