SecP521R1Field.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.Crypto.Utilities;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  8. namespace BestHTTP.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 Negate(uint[] x, uint[] z)
  73. {
  74. if (0 != IsZero(x))
  75. {
  76. Nat.Sub(17, P, P, z);
  77. }
  78. else
  79. {
  80. Nat.Sub(17, P, x, z);
  81. }
  82. }
  83. public static void Random(SecureRandom r, uint[] z)
  84. {
  85. byte[] bb = new byte[17 * 4];
  86. do
  87. {
  88. r.NextBytes(bb);
  89. Pack.LE_To_UInt32(bb, 0, z, 0, 17);
  90. z[16] &= P16;
  91. }
  92. while (0 == Nat.LessThan(17, z, P));
  93. }
  94. public static void RandomMult(SecureRandom r, uint[] z)
  95. {
  96. do
  97. {
  98. Random(r, z);
  99. }
  100. while (0 != IsZero(z));
  101. }
  102. public static void Reduce(uint[] xx, uint[] z)
  103. {
  104. Debug.Assert(xx[32] >> 18 == 0);
  105. uint xx32 = xx[32];
  106. uint c = Nat.ShiftDownBits(16, xx, 16, 9, xx32, z, 0) >> 23;
  107. c += xx32 >> 9;
  108. c += Nat.AddTo(16, xx, z);
  109. if (c > P16 || (c == P16 && Nat.Eq(16, z, P)))
  110. {
  111. c += Nat.Inc(16, z);
  112. c &= P16;
  113. }
  114. z[16] = c;
  115. }
  116. public static void Reduce23(uint[] z)
  117. {
  118. uint z16 = z[16];
  119. uint c = Nat.AddWordTo(16, z16 >> 9, z) + (z16 & P16);
  120. if (c > P16 || (c == P16 && Nat.Eq(16, z, P)))
  121. {
  122. c += Nat.Inc(16, z);
  123. c &= P16;
  124. }
  125. z[16] = c;
  126. }
  127. public static void Square(uint[] x, uint[] z)
  128. {
  129. uint[] tt = Nat.Create(33);
  130. ImplSquare(x, tt);
  131. Reduce(tt, z);
  132. }
  133. public static void SquareN(uint[] x, int n, uint[] z)
  134. {
  135. Debug.Assert(n > 0);
  136. uint[] tt = Nat.Create(33);
  137. ImplSquare(x, tt);
  138. Reduce(tt, z);
  139. while (--n > 0)
  140. {
  141. ImplSquare(z, tt);
  142. Reduce(tt, z);
  143. }
  144. }
  145. public static void Subtract(uint[] x, uint[] y, uint[] z)
  146. {
  147. int c = Nat.Sub(16, x, y, z) + (int)(x[16] - y[16]);
  148. if (c < 0)
  149. {
  150. c += Nat.Dec(16, z);
  151. c &= (int)P16;
  152. }
  153. z[16] = (uint)c;
  154. }
  155. public static void Twice(uint[] x, uint[] z)
  156. {
  157. uint x16 = x[16];
  158. uint c = Nat.ShiftUpBit(16, x, x16 << 23, z) | (x16 << 1);
  159. z[16] = c & P16;
  160. }
  161. protected static void ImplMultiply(uint[] x, uint[] y, uint[] zz)
  162. {
  163. Nat512.Mul(x, y, zz);
  164. uint x16 = x[16], y16 = y[16];
  165. zz[32] = Nat.Mul31BothAdd(16, x16, y, y16, x, zz, 16) + (x16 * y16);
  166. }
  167. protected static void ImplSquare(uint[] x, uint[] zz)
  168. {
  169. Nat512.Square(x, zz);
  170. uint x16 = x[16];
  171. zz[32] = Nat.MulWordAddTo(16, x16 << 1, x, 0, zz, 16) + (x16 * x16);
  172. }
  173. }
  174. }
  175. #pragma warning restore
  176. #endif