SecT233Field.cs 9.3 KB

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