SecP160R1Field.cs 5.4 KB

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