SecP160R2Point.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
  6. {
  7. internal class SecP160R2Point
  8. : AbstractFpPoint
  9. {
  10. /**
  11. * Create a point which encodes with point compression.
  12. *
  13. * @param curve
  14. * the curve to use
  15. * @param x
  16. * affine x co-ordinate
  17. * @param y
  18. * affine y co-ordinate
  19. *
  20. * @deprecated Use ECCurve.CreatePoint to construct points
  21. */
  22. public SecP160R2Point(ECCurve curve, ECFieldElement x, ECFieldElement y)
  23. : this(curve, x, y, false)
  24. {
  25. }
  26. /**
  27. * Create a point that encodes with or without point compresion.
  28. *
  29. * @param curve
  30. * the curve to use
  31. * @param x
  32. * affine x co-ordinate
  33. * @param y
  34. * affine y co-ordinate
  35. * @param withCompression
  36. * if true encode with point compression
  37. *
  38. * @deprecated per-point compression property will be removed, refer
  39. * {@link #getEncoded(bool)}
  40. */
  41. public SecP160R2Point(ECCurve curve, ECFieldElement x, ECFieldElement y, bool withCompression)
  42. : base(curve, x, y, withCompression)
  43. {
  44. if ((x == null) != (y == null))
  45. throw new ArgumentException("Exactly one of the field elements is null");
  46. }
  47. internal SecP160R2Point(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression)
  48. : base(curve, x, y, zs, withCompression)
  49. {
  50. }
  51. protected override ECPoint Detach()
  52. {
  53. return new SecP160R2Point(null, AffineXCoord, AffineYCoord);
  54. }
  55. public override ECPoint Add(ECPoint b)
  56. {
  57. if (this.IsInfinity)
  58. return b;
  59. if (b.IsInfinity)
  60. return this;
  61. if (this == b)
  62. return Twice();
  63. ECCurve curve = this.Curve;
  64. SecP160R2FieldElement X1 = (SecP160R2FieldElement)this.RawXCoord, Y1 = (SecP160R2FieldElement)this.RawYCoord;
  65. SecP160R2FieldElement X2 = (SecP160R2FieldElement)b.RawXCoord, Y2 = (SecP160R2FieldElement)b.RawYCoord;
  66. SecP160R2FieldElement Z1 = (SecP160R2FieldElement)this.RawZCoords[0];
  67. SecP160R2FieldElement Z2 = (SecP160R2FieldElement)b.RawZCoords[0];
  68. uint c;
  69. uint[] tt1 = Nat160.CreateExt();
  70. uint[] t2 = Nat160.Create();
  71. uint[] t3 = Nat160.Create();
  72. uint[] t4 = Nat160.Create();
  73. bool Z1IsOne = Z1.IsOne;
  74. uint[] U2, S2;
  75. if (Z1IsOne)
  76. {
  77. U2 = X2.x;
  78. S2 = Y2.x;
  79. }
  80. else
  81. {
  82. S2 = t3;
  83. SecP160R2Field.Square(Z1.x, S2);
  84. U2 = t2;
  85. SecP160R2Field.Multiply(S2, X2.x, U2);
  86. SecP160R2Field.Multiply(S2, Z1.x, S2);
  87. SecP160R2Field.Multiply(S2, Y2.x, S2);
  88. }
  89. bool Z2IsOne = Z2.IsOne;
  90. uint[] U1, S1;
  91. if (Z2IsOne)
  92. {
  93. U1 = X1.x;
  94. S1 = Y1.x;
  95. }
  96. else
  97. {
  98. S1 = t4;
  99. SecP160R2Field.Square(Z2.x, S1);
  100. U1 = tt1;
  101. SecP160R2Field.Multiply(S1, X1.x, U1);
  102. SecP160R2Field.Multiply(S1, Z2.x, S1);
  103. SecP160R2Field.Multiply(S1, Y1.x, S1);
  104. }
  105. uint[] H = Nat160.Create();
  106. SecP160R2Field.Subtract(U1, U2, H);
  107. uint[] R = t2;
  108. SecP160R2Field.Subtract(S1, S2, R);
  109. // Check if b == this or b == -this
  110. if (Nat160.IsZero(H))
  111. {
  112. if (Nat160.IsZero(R))
  113. {
  114. // this == b, i.e. this must be doubled
  115. return this.Twice();
  116. }
  117. // this == -b, i.e. the result is the point at infinity
  118. return curve.Infinity;
  119. }
  120. uint[] HSquared = t3;
  121. SecP160R2Field.Square(H, HSquared);
  122. uint[] G = Nat160.Create();
  123. SecP160R2Field.Multiply(HSquared, H, G);
  124. uint[] V = t3;
  125. SecP160R2Field.Multiply(HSquared, U1, V);
  126. SecP160R2Field.Negate(G, G);
  127. Nat160.Mul(S1, G, tt1);
  128. c = Nat160.AddBothTo(V, V, G);
  129. SecP160R2Field.Reduce32(c, G);
  130. SecP160R2FieldElement X3 = new SecP160R2FieldElement(t4);
  131. SecP160R2Field.Square(R, X3.x);
  132. SecP160R2Field.Subtract(X3.x, G, X3.x);
  133. SecP160R2FieldElement Y3 = new SecP160R2FieldElement(G);
  134. SecP160R2Field.Subtract(V, X3.x, Y3.x);
  135. SecP160R2Field.MultiplyAddToExt(Y3.x, R, tt1);
  136. SecP160R2Field.Reduce(tt1, Y3.x);
  137. SecP160R2FieldElement Z3 = new SecP160R2FieldElement(H);
  138. if (!Z1IsOne)
  139. {
  140. SecP160R2Field.Multiply(Z3.x, Z1.x, Z3.x);
  141. }
  142. if (!Z2IsOne)
  143. {
  144. SecP160R2Field.Multiply(Z3.x, Z2.x, Z3.x);
  145. }
  146. ECFieldElement[] zs = new ECFieldElement[]{ Z3 };
  147. return new SecP160R2Point(curve, X3, Y3, zs, IsCompressed);
  148. }
  149. public override ECPoint Twice()
  150. {
  151. if (this.IsInfinity)
  152. return this;
  153. ECCurve curve = this.Curve;
  154. SecP160R2FieldElement Y1 = (SecP160R2FieldElement)this.RawYCoord;
  155. if (Y1.IsZero)
  156. return curve.Infinity;
  157. SecP160R2FieldElement X1 = (SecP160R2FieldElement)this.RawXCoord, Z1 = (SecP160R2FieldElement)this.RawZCoords[0];
  158. uint c;
  159. uint[] t1 = Nat160.Create();
  160. uint[] t2 = Nat160.Create();
  161. uint[] Y1Squared = Nat160.Create();
  162. SecP160R2Field.Square(Y1.x, Y1Squared);
  163. uint[] T = Nat160.Create();
  164. SecP160R2Field.Square(Y1Squared, T);
  165. bool Z1IsOne = Z1.IsOne;
  166. uint[] Z1Squared = Z1.x;
  167. if (!Z1IsOne)
  168. {
  169. Z1Squared = t2;
  170. SecP160R2Field.Square(Z1.x, Z1Squared);
  171. }
  172. SecP160R2Field.Subtract(X1.x, Z1Squared, t1);
  173. uint[] M = t2;
  174. SecP160R2Field.Add(X1.x, Z1Squared, M);
  175. SecP160R2Field.Multiply(M, t1, M);
  176. c = Nat160.AddBothTo(M, M, M);
  177. SecP160R2Field.Reduce32(c, M);
  178. uint[] S = Y1Squared;
  179. SecP160R2Field.Multiply(Y1Squared, X1.x, S);
  180. c = Nat.ShiftUpBits(5, S, 2, 0);
  181. SecP160R2Field.Reduce32(c, S);
  182. c = Nat.ShiftUpBits(5, T, 3, 0, t1);
  183. SecP160R2Field.Reduce32(c, t1);
  184. SecP160R2FieldElement X3 = new SecP160R2FieldElement(T);
  185. SecP160R2Field.Square(M, X3.x);
  186. SecP160R2Field.Subtract(X3.x, S, X3.x);
  187. SecP160R2Field.Subtract(X3.x, S, X3.x);
  188. SecP160R2FieldElement Y3 = new SecP160R2FieldElement(S);
  189. SecP160R2Field.Subtract(S, X3.x, Y3.x);
  190. SecP160R2Field.Multiply(Y3.x, M, Y3.x);
  191. SecP160R2Field.Subtract(Y3.x, t1, Y3.x);
  192. SecP160R2FieldElement Z3 = new SecP160R2FieldElement(M);
  193. SecP160R2Field.Twice(Y1.x, Z3.x);
  194. if (!Z1IsOne)
  195. {
  196. SecP160R2Field.Multiply(Z3.x, Z1.x, Z3.x);
  197. }
  198. return new SecP160R2Point(curve, X3, Y3, new ECFieldElement[]{ Z3 }, IsCompressed);
  199. }
  200. public override ECPoint TwicePlus(ECPoint b)
  201. {
  202. if (this == b)
  203. return ThreeTimes();
  204. if (this.IsInfinity)
  205. return b;
  206. if (b.IsInfinity)
  207. return Twice();
  208. ECFieldElement Y1 = this.RawYCoord;
  209. if (Y1.IsZero)
  210. return b;
  211. return Twice().Add(b);
  212. }
  213. public override ECPoint ThreeTimes()
  214. {
  215. if (this.IsInfinity || this.RawYCoord.IsZero)
  216. return this;
  217. // NOTE: Be careful about recursions between TwicePlus and ThreeTimes
  218. return Twice().Add(this);
  219. }
  220. public override ECPoint Negate()
  221. {
  222. if (IsInfinity)
  223. return this;
  224. return new SecP160R2Point(Curve, this.RawXCoord, this.RawYCoord.Negate(), this.RawZCoords, IsCompressed);
  225. }
  226. }
  227. }
  228. #pragma warning restore
  229. #endif