SecT131Field.cs 9.6 KB

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