SecT113Field.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Diagnostics;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
  7. {
  8. internal class SecT113Field
  9. {
  10. private const ulong M49 = ulong.MaxValue >> 15;
  11. private const ulong M57 = ulong.MaxValue >> 7;
  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. }
  17. public static void AddExt(ulong[] xx, ulong[] yy, ulong[] zz)
  18. {
  19. zz[0] = xx[0] ^ yy[0];
  20. zz[1] = xx[1] ^ yy[1];
  21. zz[2] = xx[2] ^ yy[2];
  22. zz[3] = xx[3] ^ yy[3];
  23. }
  24. public static void AddOne(ulong[] x, ulong[] z)
  25. {
  26. z[0] = x[0] ^ 1UL;
  27. z[1] = x[1];
  28. }
  29. private static void AddTo(ulong[] x, ulong[] z)
  30. {
  31. z[0] ^= x[0];
  32. z[1] ^= x[1];
  33. }
  34. public static ulong[] FromBigInteger(BigInteger x)
  35. {
  36. return Nat.FromBigInteger64(113, x);
  37. }
  38. public static void HalfTrace(ulong[] x, ulong[] z)
  39. {
  40. ulong[] tt = Nat128.CreateExt64();
  41. Nat128.Copy64(x, z);
  42. for (int i = 1; i < 113; i += 2)
  43. {
  44. ImplSquare(z, tt);
  45. Reduce(tt, z);
  46. ImplSquare(z, tt);
  47. Reduce(tt, z);
  48. AddTo(x, z);
  49. }
  50. }
  51. public static void Invert(ulong[] x, ulong[] z)
  52. {
  53. if (Nat128.IsZero64(x))
  54. throw new InvalidOperationException();
  55. // Itoh-Tsujii inversion
  56. ulong[] t0 = Nat128.Create64();
  57. ulong[] t1 = Nat128.Create64();
  58. Square(x, t0);
  59. Multiply(t0, x, t0);
  60. Square(t0, t0);
  61. Multiply(t0, x, t0);
  62. SquareN(t0, 3, t1);
  63. Multiply(t1, t0, t1);
  64. Square(t1, t1);
  65. Multiply(t1, x, t1);
  66. SquareN(t1, 7, t0);
  67. Multiply(t0, t1, t0);
  68. SquareN(t0, 14, t1);
  69. Multiply(t1, t0, t1);
  70. SquareN(t1, 28, t0);
  71. Multiply(t0, t1, t0);
  72. SquareN(t0, 56, t1);
  73. Multiply(t1, t0, t1);
  74. Square(t1, z);
  75. }
  76. public static void Multiply(ulong[] x, ulong[] y, ulong[] z)
  77. {
  78. ulong[] tt = new ulong[8];
  79. ImplMultiply(x, y, tt);
  80. Reduce(tt, z);
  81. }
  82. public static void MultiplyAddToExt(ulong[] x, ulong[] y, ulong[] zz)
  83. {
  84. ulong[] tt = new ulong[8];
  85. ImplMultiply(x, y, tt);
  86. AddExt(zz, tt, zz);
  87. }
  88. public static void Reduce(ulong[] xx, ulong[] z)
  89. {
  90. ulong x0 = xx[0], x1 = xx[1], x2 = xx[2], x3 = xx[3];
  91. x1 ^= (x3 << 15) ^ (x3 << 24);
  92. x2 ^= (x3 >> 49) ^ (x3 >> 40);
  93. x0 ^= (x2 << 15) ^ (x2 << 24);
  94. x1 ^= (x2 >> 49) ^ (x2 >> 40);
  95. ulong t = x1 >> 49;
  96. z[0] = x0 ^ t ^ (t << 9);
  97. z[1] = x1 & M49;
  98. }
  99. public static void Reduce15(ulong[] z, int zOff)
  100. {
  101. ulong z1 = z[zOff + 1], t = z1 >> 49;
  102. z[zOff ] ^= t ^ (t << 9);
  103. z[zOff + 1] = z1 & M49;
  104. }
  105. public static void Sqrt(ulong[] x, ulong[] z)
  106. {
  107. ulong u0 = Interleave.Unshuffle(x[0]), u1 = Interleave.Unshuffle(x[1]);
  108. ulong e0 = (u0 & 0x00000000FFFFFFFFUL) | (u1 << 32);
  109. ulong c0 = (u0 >> 32) | (u1 & 0xFFFFFFFF00000000UL);
  110. z[0] = e0 ^ (c0 << 57) ^ (c0 << 5);
  111. z[1] = (c0 >> 7) ^ (c0 >> 59);
  112. }
  113. public static void Square(ulong[] x, ulong[] z)
  114. {
  115. ulong[] tt = Nat128.CreateExt64();
  116. ImplSquare(x, tt);
  117. Reduce(tt, z);
  118. }
  119. public static void SquareAddToExt(ulong[] x, ulong[] zz)
  120. {
  121. ulong[] tt = Nat128.CreateExt64();
  122. ImplSquare(x, tt);
  123. AddExt(zz, tt, zz);
  124. }
  125. public static void SquareN(ulong[] x, int n, ulong[] z)
  126. {
  127. Debug.Assert(n > 0);
  128. ulong[] tt = Nat128.CreateExt64();
  129. ImplSquare(x, tt);
  130. Reduce(tt, z);
  131. while (--n > 0)
  132. {
  133. ImplSquare(z, tt);
  134. Reduce(tt, z);
  135. }
  136. }
  137. public static uint Trace(ulong[] x)
  138. {
  139. // Non-zero-trace bits: 0
  140. return (uint)(x[0]) & 1U;
  141. }
  142. protected static void ImplMultiply(ulong[] x, ulong[] y, ulong[] zz)
  143. {
  144. /*
  145. * "Three-way recursion" as described in "Batch binary Edwards", Daniel J. Bernstein.
  146. */
  147. ulong f0 = x[0], f1 = x[1];
  148. f1 = ((f0 >> 57) ^ (f1 << 7)) & M57;
  149. f0 &= M57;
  150. ulong g0 = y[0], g1 = y[1];
  151. g1 = ((g0 >> 57) ^ (g1 << 7)) & M57;
  152. g0 &= M57;
  153. ulong[] u = zz;
  154. ulong[] H = new ulong[6];
  155. ImplMulw(u, f0, g0, H, 0); // H(0) 57/56 bits
  156. ImplMulw(u, f1, g1, H, 2); // H(INF) 57/54 bits
  157. ImplMulw(u, f0 ^ f1, g0 ^ g1, H, 4); // H(1) 57/56 bits
  158. ulong r = H[1] ^ H[2];
  159. ulong z0 = H[0],
  160. z3 = H[3],
  161. z1 = H[4] ^ z0 ^ r,
  162. z2 = H[5] ^ z3 ^ r;
  163. zz[0] = z0 ^ (z1 << 57);
  164. zz[1] = (z1 >> 7) ^ (z2 << 50);
  165. zz[2] = (z2 >> 14) ^ (z3 << 43);
  166. zz[3] = (z3 >> 21);
  167. }
  168. protected static void ImplMulw(ulong[] u, ulong x, ulong y, ulong[] z, int zOff)
  169. {
  170. Debug.Assert(x >> 57 == 0);
  171. Debug.Assert(y >> 57 == 0);
  172. //u[0] = 0;
  173. u[1] = y;
  174. u[2] = u[1] << 1;
  175. u[3] = u[2] ^ y;
  176. u[4] = u[2] << 1;
  177. u[5] = u[4] ^ y;
  178. u[6] = u[3] << 1;
  179. u[7] = u[6] ^ y;
  180. uint j = (uint)x;
  181. ulong g, h = 0, l = u[j & 7];
  182. int k = 48;
  183. do
  184. {
  185. j = (uint)(x >> k);
  186. g = u[j & 7]
  187. ^ u[(j >> 3) & 7] << 3
  188. ^ u[(j >> 6) & 7] << 6;
  189. l ^= (g << k);
  190. h ^= (g >> -k);
  191. }
  192. while ((k -= 9) > 0);
  193. h ^= ((x & 0x0100804020100800UL) & (ulong)(((long)y << 7) >> 63)) >> 8;
  194. Debug.Assert(h >> 49 == 0);
  195. z[zOff ] = l & M57;
  196. z[zOff + 1] = (l >> 57) ^ (h << 7);
  197. }
  198. protected static void ImplSquare(ulong[] x, ulong[] zz)
  199. {
  200. Interleave.Expand64To128(x, 0, 2, zz, 0);
  201. }
  202. }
  203. }
  204. #pragma warning restore
  205. #endif