SecT409Field.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 SecT409Field
  8. {
  9. private const ulong M25 = ulong.MaxValue >> 39;
  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. z[4] = x[4] ^ y[4];
  18. z[5] = x[5] ^ y[5];
  19. z[6] = x[6] ^ y[6];
  20. }
  21. public static void AddExt(ulong[] xx, ulong[] yy, ulong[] zz)
  22. {
  23. for (int i = 0; i < 13; ++i)
  24. {
  25. zz[i] = xx[i] ^ yy[i];
  26. }
  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. z[4] = x[4];
  35. z[5] = x[5];
  36. z[6] = x[6];
  37. }
  38. public static ulong[] FromBigInteger(BigInteger x)
  39. {
  40. ulong[] z = Nat448.FromBigInteger64(x);
  41. Reduce39(z, 0);
  42. return z;
  43. }
  44. public static void Invert(ulong[] x, ulong[] z)
  45. {
  46. if (Nat448.IsZero64(x))
  47. throw new InvalidOperationException();
  48. // Itoh-Tsujii inversion with bases { 2, 3 }
  49. ulong[] t0 = Nat448.Create64();
  50. ulong[] t1 = Nat448.Create64();
  51. ulong[] t2 = Nat448.Create64();
  52. Square(x, t0);
  53. // 3 | 408
  54. SquareN(t0, 1, t1);
  55. Multiply(t0, t1, t0);
  56. SquareN(t1, 1, t1);
  57. Multiply(t0, t1, t0);
  58. // 2 | 136
  59. SquareN(t0, 3, t1);
  60. Multiply(t0, t1, t0);
  61. // 2 | 68
  62. SquareN(t0, 6, t1);
  63. Multiply(t0, t1, t0);
  64. // 2 | 34
  65. SquareN(t0, 12, t1);
  66. Multiply(t0, t1, t2);
  67. // ! {2,3} | 17
  68. SquareN(t2, 24, t0);
  69. SquareN(t0, 24, t1);
  70. Multiply(t0, t1, t0);
  71. // 2 | 8
  72. SquareN(t0, 48, t1);
  73. Multiply(t0, t1, t0);
  74. // 2 | 4
  75. SquareN(t0, 96, t1);
  76. Multiply(t0, t1, t0);
  77. // 2 | 2
  78. SquareN(t0, 192, t1);
  79. Multiply(t0, t1, t0);
  80. Multiply(t0, t2, z);
  81. }
  82. public static void Multiply(ulong[] x, ulong[] y, ulong[] z)
  83. {
  84. ulong[] tt = Nat448.CreateExt64();
  85. ImplMultiply(x, y, tt);
  86. Reduce(tt, z);
  87. }
  88. public static void MultiplyAddToExt(ulong[] x, ulong[] y, ulong[] zz)
  89. {
  90. ulong[] tt = Nat448.CreateExt64();
  91. ImplMultiply(x, y, tt);
  92. AddExt(zz, tt, zz);
  93. }
  94. public static void Reduce(ulong[] xx, ulong[] z)
  95. {
  96. ulong x00 = xx[0], x01 = xx[1], x02 = xx[2], x03 = xx[3];
  97. ulong x04 = xx[4], x05 = xx[5], x06 = xx[6], x07 = xx[7];
  98. ulong u = xx[12];
  99. x05 ^= (u << 39);
  100. x06 ^= (u >> 25) ^ (u << 62);
  101. x07 ^= (u >> 2);
  102. u = xx[11];
  103. x04 ^= (u << 39);
  104. x05 ^= (u >> 25) ^ (u << 62);
  105. x06 ^= (u >> 2);
  106. u = xx[10];
  107. x03 ^= (u << 39);
  108. x04 ^= (u >> 25) ^ (u << 62);
  109. x05 ^= (u >> 2);
  110. u = xx[9];
  111. x02 ^= (u << 39);
  112. x03 ^= (u >> 25) ^ (u << 62);
  113. x04 ^= (u >> 2);
  114. u = xx[8];
  115. x01 ^= (u << 39);
  116. x02 ^= (u >> 25) ^ (u << 62);
  117. x03 ^= (u >> 2);
  118. u = x07;
  119. x00 ^= (u << 39);
  120. x01 ^= (u >> 25) ^ (u << 62);
  121. x02 ^= (u >> 2);
  122. ulong t = x06 >> 25;
  123. z[0] = x00 ^ t;
  124. z[1] = x01 ^ (t << 23);
  125. z[2] = x02;
  126. z[3] = x03;
  127. z[4] = x04;
  128. z[5] = x05;
  129. z[6] = x06 & M25;
  130. }
  131. public static void Reduce39(ulong[] z, int zOff)
  132. {
  133. ulong z6 = z[zOff + 6], t = z6 >> 25;
  134. z[zOff ] ^= t;
  135. z[zOff + 1] ^= (t << 23);
  136. z[zOff + 6] = z6 & M25;
  137. }
  138. public static void Sqrt(ulong[] x, ulong[] z)
  139. {
  140. ulong u0, u1;
  141. u0 = Interleave.Unshuffle(x[0]); u1 = Interleave.Unshuffle(x[1]);
  142. ulong e0 = (u0 & 0x00000000FFFFFFFFUL) | (u1 << 32);
  143. ulong c0 = (u0 >> 32) | (u1 & 0xFFFFFFFF00000000UL);
  144. u0 = Interleave.Unshuffle(x[2]); u1 = Interleave.Unshuffle(x[3]);
  145. ulong e1 = (u0 & 0x00000000FFFFFFFFUL) | (u1 << 32);
  146. ulong c1 = (u0 >> 32) | (u1 & 0xFFFFFFFF00000000UL);
  147. u0 = Interleave.Unshuffle(x[4]); u1 = Interleave.Unshuffle(x[5]);
  148. ulong e2 = (u0 & 0x00000000FFFFFFFFUL) | (u1 << 32);
  149. ulong c2 = (u0 >> 32) | (u1 & 0xFFFFFFFF00000000UL);
  150. u0 = Interleave.Unshuffle(x[6]);
  151. ulong e3 = (u0 & 0x00000000FFFFFFFFUL);
  152. ulong c3 = (u0 >> 32);
  153. z[0] = e0 ^ (c0 << 44);
  154. z[1] = e1 ^ (c1 << 44) ^ (c0 >> 20);
  155. z[2] = e2 ^ (c2 << 44) ^ (c1 >> 20);
  156. z[3] = e3 ^ (c3 << 44) ^ (c2 >> 20) ^ (c0 << 13);
  157. z[4] = (c3 >> 20) ^ (c1 << 13) ^ (c0 >> 51);
  158. z[5] = (c2 << 13) ^ (c1 >> 51);
  159. z[6] = (c3 << 13) ^ (c2 >> 51);
  160. Debug.Assert((c3 >> 51) == 0);
  161. }
  162. public static void Square(ulong[] x, ulong[] z)
  163. {
  164. ulong[] tt = Nat.Create64(13);
  165. ImplSquare(x, tt);
  166. Reduce(tt, z);
  167. }
  168. public static void SquareAddToExt(ulong[] x, ulong[] zz)
  169. {
  170. ulong[] tt = Nat.Create64(13);
  171. ImplSquare(x, tt);
  172. AddExt(zz, tt, zz);
  173. }
  174. public static void SquareN(ulong[] x, int n, ulong[] z)
  175. {
  176. Debug.Assert(n > 0);
  177. ulong[] tt = Nat.Create64(13);
  178. ImplSquare(x, tt);
  179. Reduce(tt, z);
  180. while (--n > 0)
  181. {
  182. ImplSquare(z, tt);
  183. Reduce(tt, z);
  184. }
  185. }
  186. public static uint Trace(ulong[] x)
  187. {
  188. // Non-zero-trace bits: 0
  189. return (uint)(x[0]) & 1U;
  190. }
  191. protected static void ImplCompactExt(ulong[] zz)
  192. {
  193. ulong z00 = zz[ 0], z01 = zz[ 1], z02 = zz[ 2], z03 = zz[ 3], z04 = zz[ 4], z05 = zz[ 5], z06 = zz[ 6];
  194. ulong z07 = zz[ 7], z08 = zz[ 8], z09 = zz[ 9], z10 = zz[10], z11 = zz[11], z12 = zz[12], z13 = zz[13];
  195. zz[ 0] = z00 ^ (z01 << 59);
  196. zz[ 1] = (z01 >> 5) ^ (z02 << 54);
  197. zz[ 2] = (z02 >> 10) ^ (z03 << 49);
  198. zz[ 3] = (z03 >> 15) ^ (z04 << 44);
  199. zz[ 4] = (z04 >> 20) ^ (z05 << 39);
  200. zz[ 5] = (z05 >> 25) ^ (z06 << 34);
  201. zz[ 6] = (z06 >> 30) ^ (z07 << 29);
  202. zz[ 7] = (z07 >> 35) ^ (z08 << 24);
  203. zz[ 8] = (z08 >> 40) ^ (z09 << 19);
  204. zz[ 9] = (z09 >> 45) ^ (z10 << 14);
  205. zz[10] = (z10 >> 50) ^ (z11 << 9);
  206. zz[11] = (z11 >> 55) ^ (z12 << 4)
  207. ^ (z13 << 63);
  208. zz[12] = (z12 >> 60)
  209. ^ (z13 >> 1);
  210. zz[13] = 0;
  211. }
  212. protected static void ImplExpand(ulong[] x, ulong[] z)
  213. {
  214. ulong x0 = x[0], x1 = x[1], x2 = x[2], x3 = x[3], x4 = x[4], x5 = x[5], x6 = x[6];
  215. z[0] = x0 & M59;
  216. z[1] = ((x0 >> 59) ^ (x1 << 5)) & M59;
  217. z[2] = ((x1 >> 54) ^ (x2 << 10)) & M59;
  218. z[3] = ((x2 >> 49) ^ (x3 << 15)) & M59;
  219. z[4] = ((x3 >> 44) ^ (x4 << 20)) & M59;
  220. z[5] = ((x4 >> 39) ^ (x5 << 25)) & M59;
  221. z[6] = ((x5 >> 34) ^ (x6 << 30));
  222. }
  223. protected static void ImplMultiply(ulong[] x, ulong[] y, ulong[] zz)
  224. {
  225. ulong[] a = new ulong[7], b = new ulong[7];
  226. ImplExpand(x, a);
  227. ImplExpand(y, b);
  228. for (int i = 0; i < 7; ++i)
  229. {
  230. ImplMulwAcc(a, b[i], zz, i);
  231. }
  232. ImplCompactExt(zz);
  233. }
  234. protected static void ImplMulwAcc(ulong[] xs, ulong y, ulong[] z, int zOff)
  235. {
  236. Debug.Assert(y >> 59 == 0);
  237. ulong[] u = new ulong[8];
  238. //u[0] = 0;
  239. u[1] = y;
  240. u[2] = u[1] << 1;
  241. u[3] = u[2] ^ y;
  242. u[4] = u[2] << 1;
  243. u[5] = u[4] ^ y;
  244. u[6] = u[3] << 1;
  245. u[7] = u[6] ^ y;
  246. for (int i = 0; i < 7; ++i)
  247. {
  248. ulong x = xs[i];
  249. Debug.Assert(x >> 59 == 0);
  250. uint j = (uint)x;
  251. ulong g, h = 0, l = u[j & 7]
  252. ^ (u[(j >> 3) & 7] << 3);
  253. int k = 54;
  254. do
  255. {
  256. j = (uint)(x >> k);
  257. g = u[j & 7]
  258. ^ u[(j >> 3) & 7] << 3;
  259. l ^= (g << k);
  260. h ^= (g >> -k);
  261. }
  262. while ((k -= 6) > 0);
  263. Debug.Assert(h >> 53 == 0);
  264. z[zOff + i ] ^= l & M59;
  265. z[zOff + i + 1] ^= (l >> 59) ^ (h << 5);
  266. }
  267. }
  268. protected static void ImplSquare(ulong[] x, ulong[] zz)
  269. {
  270. for (int i = 0; i < 6; ++i)
  271. {
  272. Interleave.Expand64To128(x[i], zz, i << 1);
  273. }
  274. zz[12] = Interleave.Expand32to64((uint)x[6]);
  275. }
  276. }
  277. }
  278. #endif