SecT113Field.cs 6.4 KB

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