SecP160R1Field.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 SecP160R1Field
  11. {
  12. // 2^160 - 2^31 - 1
  13. internal static readonly uint[] P = new uint[]{ 0x7FFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
  14. private static readonly uint[] PExt = new uint[]{ 0x00000001, 0x40000001, 0x00000000, 0x00000000, 0x00000000,
  15. 0xFFFFFFFE, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
  16. private static readonly uint[] PExtInv = new uint[]{ 0xFFFFFFFF, 0xBFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF,
  17. 0xFFFFFFFF, 0x00000001, 0x00000001 };
  18. private const uint P4 = 0xFFFFFFFF;
  19. private const uint PExt9 = 0xFFFFFFFF;
  20. private const uint PInv = 0x80000001;
  21. public static void Add(uint[] x, uint[] y, uint[] z)
  22. {
  23. uint c = Nat160.Add(x, y, z);
  24. if (c != 0 || (z[4] == P4 && Nat160.Gte(z, P)))
  25. {
  26. Nat.AddWordTo(5, PInv, z);
  27. }
  28. }
  29. public static void AddExt(uint[] xx, uint[] yy, uint[] zz)
  30. {
  31. uint c = Nat.Add(10, xx, yy, zz);
  32. if (c != 0 || (zz[9] == PExt9 && Nat.Gte(10, zz, PExt)))
  33. {
  34. if (Nat.AddTo(PExtInv.Length, PExtInv, zz) != 0)
  35. {
  36. Nat.IncAt(10, zz, PExtInv.Length);
  37. }
  38. }
  39. }
  40. public static void AddOne(uint[] x, uint[] z)
  41. {
  42. uint c = Nat.Inc(5, x, z);
  43. if (c != 0 || (z[4] == P4 && Nat160.Gte(z, P)))
  44. {
  45. Nat.AddWordTo(5, PInv, z);
  46. }
  47. }
  48. public static uint[] FromBigInteger(BigInteger x)
  49. {
  50. uint[] z = Nat.FromBigInteger(160, x);
  51. if (z[4] == P4 && Nat160.Gte(z, P))
  52. {
  53. Nat160.SubFrom(P, z);
  54. }
  55. return z;
  56. }
  57. public static void Half(uint[] x, uint[] z)
  58. {
  59. if ((x[0] & 1) == 0)
  60. {
  61. Nat.ShiftDownBit(5, x, 0, z);
  62. }
  63. else
  64. {
  65. uint c = Nat160.Add(x, P, z);
  66. Nat.ShiftDownBit(5, z, c);
  67. }
  68. }
  69. public static void Inv(uint[] x, uint[] z)
  70. {
  71. Mod.CheckedModOddInverse(P, x, z);
  72. }
  73. public static int IsZero(uint[] x)
  74. {
  75. uint d = 0;
  76. for (int i = 0; i < 5; ++i)
  77. {
  78. d |= x[i];
  79. }
  80. d = (d >> 1) | (d & 1);
  81. return ((int)d - 1) >> 31;
  82. }
  83. public static void Multiply(uint[] x, uint[] y, uint[] z)
  84. {
  85. uint[] tt = Nat160.CreateExt();
  86. Nat160.Mul(x, y, tt);
  87. Reduce(tt, z);
  88. }
  89. public static void MultiplyAddToExt(uint[] x, uint[] y, uint[] zz)
  90. {
  91. uint c = Nat160.MulAddTo(x, y, zz);
  92. if (c != 0 || (zz[9] == PExt9 && Nat.Gte(10, zz, PExt)))
  93. {
  94. if (Nat.AddTo(PExtInv.Length, PExtInv, zz) != 0)
  95. {
  96. Nat.IncAt(10, zz, PExtInv.Length);
  97. }
  98. }
  99. }
  100. public static void Negate(uint[] x, uint[] z)
  101. {
  102. if (0 != IsZero(x))
  103. {
  104. Nat160.Sub(P, P, z);
  105. }
  106. else
  107. {
  108. Nat160.Sub(P, x, z);
  109. }
  110. }
  111. public static void Random(SecureRandom r, uint[] z)
  112. {
  113. byte[] bb = new byte[5 * 4];
  114. do
  115. {
  116. r.NextBytes(bb);
  117. Pack.LE_To_UInt32(bb, 0, z, 0, 5);
  118. }
  119. while (0 == Nat.LessThan(5, z, P));
  120. }
  121. public static void RandomMult(SecureRandom r, uint[] z)
  122. {
  123. do
  124. {
  125. Random(r, z);
  126. }
  127. while (0 != IsZero(z));
  128. }
  129. public static void Reduce(uint[] xx, uint[] z)
  130. {
  131. ulong x5 = xx[5], x6 = xx[6], x7 = xx[7], x8 = xx[8], x9 = xx[9];
  132. ulong c = 0;
  133. c += (ulong)xx[0] + x5 + (x5 << 31);
  134. z[0] = (uint)c; c >>= 32;
  135. c += (ulong)xx[1] + x6 + (x6 << 31);
  136. z[1] = (uint)c; c >>= 32;
  137. c += (ulong)xx[2] + x7 + (x7 << 31);
  138. z[2] = (uint)c; c >>= 32;
  139. c += (ulong)xx[3] + x8 + (x8 << 31);
  140. z[3] = (uint)c; c >>= 32;
  141. c += (ulong)xx[4] + x9 + (x9 << 31);
  142. z[4] = (uint)c; c >>= 32;
  143. Debug.Assert(c >> 32 == 0);
  144. Reduce32((uint)c, z);
  145. }
  146. public static void Reduce32(uint x, uint[] z)
  147. {
  148. if ((x != 0 && Nat160.MulWordsAdd(PInv, x, z, 0) != 0)
  149. || (z[4] == P4 && Nat160.Gte(z, P)))
  150. {
  151. Nat.AddWordTo(5, PInv, z);
  152. }
  153. }
  154. public static void Square(uint[] x, uint[] z)
  155. {
  156. uint[] tt = Nat160.CreateExt();
  157. Nat160.Square(x, tt);
  158. Reduce(tt, z);
  159. }
  160. public static void SquareN(uint[] x, int n, uint[] z)
  161. {
  162. Debug.Assert(n > 0);
  163. uint[] tt = Nat160.CreateExt();
  164. Nat160.Square(x, tt);
  165. Reduce(tt, z);
  166. while (--n > 0)
  167. {
  168. Nat160.Square(z, tt);
  169. Reduce(tt, z);
  170. }
  171. }
  172. public static void Subtract(uint[] x, uint[] y, uint[] z)
  173. {
  174. int c = Nat160.Sub(x, y, z);
  175. if (c != 0)
  176. {
  177. Nat.SubWordFrom(5, PInv, z);
  178. }
  179. }
  180. public static void SubtractExt(uint[] xx, uint[] yy, uint[] zz)
  181. {
  182. int c = Nat.Sub(10, xx, yy, zz);
  183. if (c != 0)
  184. {
  185. if (Nat.SubFrom(PExtInv.Length, PExtInv, zz) != 0)
  186. {
  187. Nat.DecAt(10, zz, PExtInv.Length);
  188. }
  189. }
  190. }
  191. public static void Twice(uint[] x, uint[] z)
  192. {
  193. uint c = Nat.ShiftUpBit(5, x, 0, z);
  194. if (c != 0 || (z[4] == P4 && Nat160.Gte(z, P)))
  195. {
  196. Nat.AddWordTo(5, PInv, z);
  197. }
  198. }
  199. }
  200. }
  201. #pragma warning restore
  202. #endif