SecT113Field.cs 6.5 KB

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