SecP160K1Point.cs 8.3 KB

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