SecP160K1Point.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 SecP160K1Point
  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 SecP160K1Point(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 SecP160K1Point(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 SecP160K1Point(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs,
  48. bool withCompression)
  49. : base(curve, x, y, zs, withCompression)
  50. {
  51. }
  52. protected override ECPoint Detach()
  53. {
  54. return new SecP160K1Point(null, AffineXCoord, AffineYCoord);
  55. }
  56. // B.3 pg 62
  57. public override ECPoint Add(ECPoint b)
  58. {
  59. if (this.IsInfinity)
  60. return b;
  61. if (b.IsInfinity)
  62. return this;
  63. if (this == b)
  64. return Twice();
  65. ECCurve curve = this.Curve;
  66. SecP160R2FieldElement X1 = (SecP160R2FieldElement)this.RawXCoord, Y1 = (SecP160R2FieldElement)this.RawYCoord;
  67. SecP160R2FieldElement X2 = (SecP160R2FieldElement)b.RawXCoord, Y2 = (SecP160R2FieldElement)b.RawYCoord;
  68. SecP160R2FieldElement Z1 = (SecP160R2FieldElement)this.RawZCoords[0];
  69. SecP160R2FieldElement Z2 = (SecP160R2FieldElement)b.RawZCoords[0];
  70. uint c;
  71. uint[] tt1 = Nat160.CreateExt();
  72. uint[] t2 = Nat160.Create();
  73. uint[] t3 = Nat160.Create();
  74. uint[] t4 = Nat160.Create();
  75. bool Z1IsOne = Z1.IsOne;
  76. uint[] U2, S2;
  77. if (Z1IsOne)
  78. {
  79. U2 = X2.x;
  80. S2 = Y2.x;
  81. }
  82. else
  83. {
  84. S2 = t3;
  85. SecP160R2Field.Square(Z1.x, S2);
  86. U2 = t2;
  87. SecP160R2Field.Multiply(S2, X2.x, U2);
  88. SecP160R2Field.Multiply(S2, Z1.x, S2);
  89. SecP160R2Field.Multiply(S2, Y2.x, S2);
  90. }
  91. bool Z2IsOne = Z2.IsOne;
  92. uint[] U1, S1;
  93. if (Z2IsOne)
  94. {
  95. U1 = X1.x;
  96. S1 = Y1.x;
  97. }
  98. else
  99. {
  100. S1 = t4;
  101. SecP160R2Field.Square(Z2.x, S1);
  102. U1 = tt1;
  103. SecP160R2Field.Multiply(S1, X1.x, U1);
  104. SecP160R2Field.Multiply(S1, Z2.x, S1);
  105. SecP160R2Field.Multiply(S1, Y1.x, S1);
  106. }
  107. uint[] H = Nat160.Create();
  108. SecP160R2Field.Subtract(U1, U2, H);
  109. uint[] R = t2;
  110. SecP160R2Field.Subtract(S1, S2, R);
  111. // Check if b == this or b == -this
  112. if (Nat160.IsZero(H))
  113. {
  114. if (Nat160.IsZero(R))
  115. {
  116. // this == b, i.e. this must be doubled
  117. return this.Twice();
  118. }
  119. // this == -b, i.e. the result is the point at infinity
  120. return curve.Infinity;
  121. }
  122. uint[] HSquared = t3;
  123. SecP160R2Field.Square(H, HSquared);
  124. uint[] G = Nat160.Create();
  125. SecP160R2Field.Multiply(HSquared, H, G);
  126. uint[] V = t3;
  127. SecP160R2Field.Multiply(HSquared, U1, V);
  128. SecP160R2Field.Negate(G, G);
  129. Nat160.Mul(S1, G, tt1);
  130. c = Nat160.AddBothTo(V, V, G);
  131. SecP160R2Field.Reduce32(c, G);
  132. SecP160R2FieldElement X3 = new SecP160R2FieldElement(t4);
  133. SecP160R2Field.Square(R, X3.x);
  134. SecP160R2Field.Subtract(X3.x, G, X3.x);
  135. SecP160R2FieldElement Y3 = new SecP160R2FieldElement(G);
  136. SecP160R2Field.Subtract(V, X3.x, Y3.x);
  137. SecP160R2Field.MultiplyAddToExt(Y3.x, R, tt1);
  138. SecP160R2Field.Reduce(tt1, Y3.x);
  139. SecP160R2FieldElement Z3 = new SecP160R2FieldElement(H);
  140. if (!Z1IsOne)
  141. {
  142. SecP160R2Field.Multiply(Z3.x, Z1.x, Z3.x);
  143. }
  144. if (!Z2IsOne)
  145. {
  146. SecP160R2Field.Multiply(Z3.x, Z2.x, Z3.x);
  147. }
  148. ECFieldElement[] zs = new ECFieldElement[] { Z3 };
  149. return new SecP160K1Point(curve, X3, Y3, zs, IsCompressed);
  150. }
  151. // B.3 pg 62
  152. public override ECPoint Twice()
  153. {
  154. if (this.IsInfinity)
  155. return this;
  156. ECCurve curve = this.Curve;
  157. SecP160R2FieldElement Y1 = (SecP160R2FieldElement)this.RawYCoord;
  158. if (Y1.IsZero)
  159. return curve.Infinity;
  160. SecP160R2FieldElement X1 = (SecP160R2FieldElement)this.RawXCoord, Z1 = (SecP160R2FieldElement)this.RawZCoords[0];
  161. uint c;
  162. uint[] Y1Squared = Nat160.Create();
  163. SecP160R2Field.Square(Y1.x, Y1Squared);
  164. uint[] T = Nat160.Create();
  165. SecP160R2Field.Square(Y1Squared, T);
  166. uint[] M = Nat160.Create();
  167. SecP160R2Field.Square(X1.x, M);
  168. c = Nat160.AddBothTo(M, M, M);
  169. SecP160R2Field.Reduce32(c, M);
  170. uint[] S = Y1Squared;
  171. SecP160R2Field.Multiply(Y1Squared, X1.x, S);
  172. c = Nat.ShiftUpBits(5, S, 2, 0);
  173. SecP160R2Field.Reduce32(c, S);
  174. uint[] t1 = Nat160.Create();
  175. c = Nat.ShiftUpBits(5, T, 3, 0, t1);
  176. SecP160R2Field.Reduce32(c, t1);
  177. SecP160R2FieldElement X3 = new SecP160R2FieldElement(T);
  178. SecP160R2Field.Square(M, X3.x);
  179. SecP160R2Field.Subtract(X3.x, S, X3.x);
  180. SecP160R2Field.Subtract(X3.x, S, X3.x);
  181. SecP160R2FieldElement Y3 = new SecP160R2FieldElement(S);
  182. SecP160R2Field.Subtract(S, X3.x, Y3.x);
  183. SecP160R2Field.Multiply(Y3.x, M, Y3.x);
  184. SecP160R2Field.Subtract(Y3.x, t1, Y3.x);
  185. SecP160R2FieldElement Z3 = new SecP160R2FieldElement(M);
  186. SecP160R2Field.Twice(Y1.x, Z3.x);
  187. if (!Z1.IsOne)
  188. {
  189. SecP160R2Field.Multiply(Z3.x, Z1.x, Z3.x);
  190. }
  191. return new SecP160K1Point(curve, X3, Y3, new ECFieldElement[] { Z3 }, IsCompressed);
  192. }
  193. public override ECPoint TwicePlus(ECPoint b)
  194. {
  195. if (this == b)
  196. return ThreeTimes();
  197. if (this.IsInfinity)
  198. return b;
  199. if (b.IsInfinity)
  200. return Twice();
  201. ECFieldElement Y1 = this.RawYCoord;
  202. if (Y1.IsZero)
  203. return b;
  204. return Twice().Add(b);
  205. }
  206. public override ECPoint ThreeTimes()
  207. {
  208. if (this.IsInfinity || this.RawYCoord.IsZero)
  209. return this;
  210. // NOTE: Be careful about recursions between TwicePlus and threeTimes
  211. return Twice().Add(this);
  212. }
  213. public override ECPoint Negate()
  214. {
  215. if (IsInfinity)
  216. return this;
  217. return new SecP160K1Point(Curve, this.RawXCoord, this.RawYCoord.Negate(), this.RawZCoords, IsCompressed);
  218. }
  219. }
  220. }
  221. #pragma warning restore
  222. #endif