SecP160R2Field.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 SecP160R2Field
  11. {
  12. // 2^160 - 2^32 - 2^14 - 2^12 - 2^9 - 2^8 - 2^7 - 2^3 - 2^2 - 1
  13. internal static readonly uint[] P = new uint[]{ 0xFFFFAC73, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
  14. private static readonly uint[] PExt = new uint[]{ 0x1B44BBA9, 0x0000A71A, 0x00000001, 0x00000000, 0x00000000,
  15. 0xFFFF58E6, 0xFFFFFFFD, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
  16. private static readonly uint[] PExtInv = new uint[]{ 0xE4BB4457, 0xFFFF58E5, 0xFFFFFFFE, 0xFFFFFFFF,
  17. 0xFFFFFFFF, 0x0000A719, 0x00000002 };
  18. private const uint P4 = 0xFFFFFFFF;
  19. private const uint PExt9 = 0xFFFFFFFF;
  20. private const uint PInv33 = 0x538D;
  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.Add33To(5, PInv33, 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.Add33To(5, PInv33, 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 cc = Nat160.Mul33Add(PInv33, xx, 5, xx, 0, z, 0);
  132. uint c = Nat160.Mul33DWordAdd(PInv33, cc, z, 0);
  133. Debug.Assert(c == 0 || c == 1);
  134. if (c != 0 || (z[4] == P4 && Nat160.Gte(z, P)))
  135. {
  136. Nat.Add33To(5, PInv33, z);
  137. }
  138. }
  139. public static void Reduce32(uint x, uint[] z)
  140. {
  141. if ((x != 0 && Nat160.Mul33WordAdd(PInv33, x, z, 0) != 0)
  142. || (z[4] == P4 && Nat160.Gte(z, P)))
  143. {
  144. Nat.Add33To(5, PInv33, z);
  145. }
  146. }
  147. public static void Square(uint[] x, uint[] z)
  148. {
  149. uint[] tt = Nat160.CreateExt();
  150. Nat160.Square(x, tt);
  151. Reduce(tt, z);
  152. }
  153. public static void SquareN(uint[] x, int n, uint[] z)
  154. {
  155. Debug.Assert(n > 0);
  156. uint[] tt = Nat160.CreateExt();
  157. Nat160.Square(x, tt);
  158. Reduce(tt, z);
  159. while (--n > 0)
  160. {
  161. Nat160.Square(z, tt);
  162. Reduce(tt, z);
  163. }
  164. }
  165. public static void Subtract(uint[] x, uint[] y, uint[] z)
  166. {
  167. int c = Nat160.Sub(x, y, z);
  168. if (c != 0)
  169. {
  170. Nat.Sub33From(5, PInv33, z);
  171. }
  172. }
  173. public static void SubtractExt(uint[] xx, uint[] yy, uint[] zz)
  174. {
  175. int c = Nat.Sub(10, xx, yy, zz);
  176. if (c != 0)
  177. {
  178. if (Nat.SubFrom(PExtInv.Length, PExtInv, zz) != 0)
  179. {
  180. Nat.DecAt(10, zz, PExtInv.Length);
  181. }
  182. }
  183. }
  184. public static void Twice(uint[] x, uint[] z)
  185. {
  186. uint c = Nat.ShiftUpBit(5, x, 0, z);
  187. if (c != 0 || (z[4] == P4 && Nat160.Gte(z, P)))
  188. {
  189. Nat.Add33To(5, PInv33, z);
  190. }
  191. }
  192. }
  193. }
  194. #pragma warning restore
  195. #endif