SecP160R1Field.cs 5.5 KB

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