SecP521R1Field.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Diagnostics;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
  8. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
  9. {
  10. internal class SecP521R1Field
  11. {
  12. // 2^521 - 1
  13. internal static readonly uint[] P = new uint[]{ 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  14. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
  15. 0xFFFFFFFF, 0xFFFFFFFF, 0x1FF };
  16. private const uint P16 = 0x1FFU;
  17. public static void Add(uint[] x, uint[] y, uint[] z)
  18. {
  19. uint c = Nat.Add(16, x, y, z) + x[16] + y[16];
  20. if (c > P16 || (c == P16 && Nat.Eq(16, z, P)))
  21. {
  22. c += Nat.Inc(16, z);
  23. c &= P16;
  24. }
  25. z[16] = c;
  26. }
  27. public static void AddOne(uint[] x, uint[] z)
  28. {
  29. uint c = Nat.Inc(16, x, z) + x[16];
  30. if (c > P16 || (c == P16 && Nat.Eq(16, z, P)))
  31. {
  32. c += Nat.Inc(16, z);
  33. c &= P16;
  34. }
  35. z[16] = c;
  36. }
  37. public static uint[] FromBigInteger(BigInteger x)
  38. {
  39. uint[] z = Nat.FromBigInteger(521, x);
  40. if (Nat.Eq(17, z, P))
  41. {
  42. Nat.Zero(17, z);
  43. }
  44. return z;
  45. }
  46. public static void Half(uint[] x, uint[] z)
  47. {
  48. uint x16 = x[16];
  49. uint c = Nat.ShiftDownBit(16, x, x16, z);
  50. z[16] = (x16 >> 1) | (c >> 23);
  51. }
  52. public static void Inv(uint[] x, uint[] z)
  53. {
  54. Mod.CheckedModOddInverse(P, x, z);
  55. }
  56. public static int IsZero(uint[] x)
  57. {
  58. uint d = 0;
  59. for (int i = 0; i < 17; ++i)
  60. {
  61. d |= x[i];
  62. }
  63. d = (d >> 1) | (d & 1);
  64. return ((int)d - 1) >> 31;
  65. }
  66. public static void Multiply(uint[] x, uint[] y, uint[] z)
  67. {
  68. uint[] tt = Nat.Create(33);
  69. ImplMultiply(x, y, tt);
  70. Reduce(tt, z);
  71. }
  72. public static void Multiply(uint[] x, uint[] y, uint[] z, uint[] tt)
  73. {
  74. ImplMultiply(x, y, tt);
  75. Reduce(tt, z);
  76. }
  77. public static void Negate(uint[] x, uint[] z)
  78. {
  79. if (0 != IsZero(x))
  80. {
  81. Nat.Sub(17, P, P, z);
  82. }
  83. else
  84. {
  85. Nat.Sub(17, P, x, z);
  86. }
  87. }
  88. public static void Random(SecureRandom r, uint[] z)
  89. {
  90. byte[] bb = new byte[17 * 4];
  91. do
  92. {
  93. r.NextBytes(bb);
  94. Pack.LE_To_UInt32(bb, 0, z, 0, 17);
  95. z[16] &= P16;
  96. }
  97. while (0 == Nat.LessThan(17, z, P));
  98. }
  99. public static void RandomMult(SecureRandom r, uint[] z)
  100. {
  101. do
  102. {
  103. Random(r, z);
  104. }
  105. while (0 != IsZero(z));
  106. }
  107. public static void Reduce(uint[] xx, uint[] z)
  108. {
  109. Debug.Assert(xx[32] >> 18 == 0);
  110. uint xx32 = xx[32];
  111. uint c = Nat.ShiftDownBits(16, xx, 16, 9, xx32, z, 0) >> 23;
  112. c += xx32 >> 9;
  113. c += Nat.AddTo(16, xx, z);
  114. if (c > P16 || (c == P16 && Nat.Eq(16, z, P)))
  115. {
  116. c += Nat.Inc(16, z);
  117. c &= P16;
  118. }
  119. z[16] = c;
  120. }
  121. public static void Reduce23(uint[] z)
  122. {
  123. uint z16 = z[16];
  124. uint c = Nat.AddWordTo(16, z16 >> 9, z) + (z16 & P16);
  125. if (c > P16 || (c == P16 && Nat.Eq(16, z, P)))
  126. {
  127. c += Nat.Inc(16, z);
  128. c &= P16;
  129. }
  130. z[16] = c;
  131. }
  132. public static void Square(uint[] x, uint[] z)
  133. {
  134. uint[] tt = Nat.Create(33);
  135. ImplSquare(x, tt);
  136. Reduce(tt, z);
  137. }
  138. public static void Square(uint[] x, uint[] z, uint[] tt)
  139. {
  140. ImplSquare(x, tt);
  141. Reduce(tt, z);
  142. }
  143. public static void SquareN(uint[] x, int n, uint[] z)
  144. {
  145. Debug.Assert(n > 0);
  146. uint[] tt = Nat.Create(33);
  147. ImplSquare(x, tt);
  148. Reduce(tt, z);
  149. while (--n > 0)
  150. {
  151. ImplSquare(z, tt);
  152. Reduce(tt, z);
  153. }
  154. }
  155. public static void SquareN(uint[] x, int n, uint[] z, uint[] tt)
  156. {
  157. Debug.Assert(n > 0);
  158. ImplSquare(x, tt);
  159. Reduce(tt, z);
  160. while (--n > 0)
  161. {
  162. ImplSquare(z, tt);
  163. Reduce(tt, z);
  164. }
  165. }
  166. public static void Subtract(uint[] x, uint[] y, uint[] z)
  167. {
  168. int c = Nat.Sub(16, x, y, z) + (int)(x[16] - y[16]);
  169. if (c < 0)
  170. {
  171. c += Nat.Dec(16, z);
  172. c &= (int)P16;
  173. }
  174. z[16] = (uint)c;
  175. }
  176. public static void Twice(uint[] x, uint[] z)
  177. {
  178. uint x16 = x[16];
  179. uint c = Nat.ShiftUpBit(16, x, x16 << 23, z) | (x16 << 1);
  180. z[16] = c & P16;
  181. }
  182. protected static void ImplMultiply(uint[] x, uint[] y, uint[] zz)
  183. {
  184. Nat512.Mul(x, y, zz);
  185. uint x16 = x[16], y16 = y[16];
  186. zz[32] = Nat.Mul31BothAdd(16, x16, y, y16, x, zz, 16) + (x16 * y16);
  187. }
  188. protected static void ImplSquare(uint[] x, uint[] zz)
  189. {
  190. Nat512.Square(x, zz);
  191. uint x16 = x[16];
  192. zz[32] = Nat.MulWordAddTo(16, x16 << 1, x, 0, zz, 16) + (x16 * x16);
  193. }
  194. }
  195. }
  196. #pragma warning restore
  197. #endif