SecP224K1Point.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 SecP224K1Point
  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 SecP224K1Point(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 SecP224K1Point(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 SecP224K1Point(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 SecP224K1Point(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. SecP224K1FieldElement X1 = (SecP224K1FieldElement)this.RawXCoord, Y1 = (SecP224K1FieldElement)this.RawYCoord;
  65. SecP224K1FieldElement X2 = (SecP224K1FieldElement)b.RawXCoord, Y2 = (SecP224K1FieldElement)b.RawYCoord;
  66. SecP224K1FieldElement Z1 = (SecP224K1FieldElement)this.RawZCoords[0];
  67. SecP224K1FieldElement Z2 = (SecP224K1FieldElement)b.RawZCoords[0];
  68. uint c;
  69. uint[] tt1 = Nat224.CreateExt();
  70. uint[] t2 = Nat224.Create();
  71. uint[] t3 = Nat224.Create();
  72. uint[] t4 = Nat224.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. SecP224K1Field.Square(Z1.x, S2);
  84. U2 = t2;
  85. SecP224K1Field.Multiply(S2, X2.x, U2);
  86. SecP224K1Field.Multiply(S2, Z1.x, S2);
  87. SecP224K1Field.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. SecP224K1Field.Square(Z2.x, S1);
  100. U1 = tt1;
  101. SecP224K1Field.Multiply(S1, X1.x, U1);
  102. SecP224K1Field.Multiply(S1, Z2.x, S1);
  103. SecP224K1Field.Multiply(S1, Y1.x, S1);
  104. }
  105. uint[] H = Nat224.Create();
  106. SecP224K1Field.Subtract(U1, U2, H);
  107. uint[] R = t2;
  108. SecP224K1Field.Subtract(S1, S2, R);
  109. // Check if b == this or b == -this
  110. if (Nat224.IsZero(H))
  111. {
  112. if (Nat224.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. SecP224K1Field.Square(H, HSquared);
  122. uint[] G = Nat224.Create();
  123. SecP224K1Field.Multiply(HSquared, H, G);
  124. uint[] V = t3;
  125. SecP224K1Field.Multiply(HSquared, U1, V);
  126. SecP224K1Field.Negate(G, G);
  127. Nat224.Mul(S1, G, tt1);
  128. c = Nat224.AddBothTo(V, V, G);
  129. SecP224K1Field.Reduce32(c, G);
  130. SecP224K1FieldElement X3 = new SecP224K1FieldElement(t4);
  131. SecP224K1Field.Square(R, X3.x);
  132. SecP224K1Field.Subtract(X3.x, G, X3.x);
  133. SecP224K1FieldElement Y3 = new SecP224K1FieldElement(G);
  134. SecP224K1Field.Subtract(V, X3.x, Y3.x);
  135. SecP224K1Field.MultiplyAddToExt(Y3.x, R, tt1);
  136. SecP224K1Field.Reduce(tt1, Y3.x);
  137. SecP224K1FieldElement Z3 = new SecP224K1FieldElement(H);
  138. if (!Z1IsOne)
  139. {
  140. SecP224K1Field.Multiply(Z3.x, Z1.x, Z3.x);
  141. }
  142. if (!Z2IsOne)
  143. {
  144. SecP224K1Field.Multiply(Z3.x, Z2.x, Z3.x);
  145. }
  146. ECFieldElement[] zs = new ECFieldElement[] { Z3 };
  147. return new SecP224K1Point(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. SecP224K1FieldElement Y1 = (SecP224K1FieldElement)this.RawYCoord;
  155. if (Y1.IsZero)
  156. return curve.Infinity;
  157. SecP224K1FieldElement X1 = (SecP224K1FieldElement)this.RawXCoord, Z1 = (SecP224K1FieldElement)this.RawZCoords[0];
  158. uint c;
  159. uint[] Y1Squared = Nat224.Create();
  160. SecP224K1Field.Square(Y1.x, Y1Squared);
  161. uint[] T = Nat224.Create();
  162. SecP224K1Field.Square(Y1Squared, T);
  163. uint[] M = Nat224.Create();
  164. SecP224K1Field.Square(X1.x, M);
  165. c = Nat224.AddBothTo(M, M, M);
  166. SecP224K1Field.Reduce32(c, M);
  167. uint[] S = Y1Squared;
  168. SecP224K1Field.Multiply(Y1Squared, X1.x, S);
  169. c = Nat.ShiftUpBits(7, S, 2, 0);
  170. SecP224K1Field.Reduce32(c, S);
  171. uint[] t1 = Nat224.Create();
  172. c = Nat.ShiftUpBits(7, T, 3, 0, t1);
  173. SecP224K1Field.Reduce32(c, t1);
  174. SecP224K1FieldElement X3 = new SecP224K1FieldElement(T);
  175. SecP224K1Field.Square(M, X3.x);
  176. SecP224K1Field.Subtract(X3.x, S, X3.x);
  177. SecP224K1Field.Subtract(X3.x, S, X3.x);
  178. SecP224K1FieldElement Y3 = new SecP224K1FieldElement(S);
  179. SecP224K1Field.Subtract(S, X3.x, Y3.x);
  180. SecP224K1Field.Multiply(Y3.x, M, Y3.x);
  181. SecP224K1Field.Subtract(Y3.x, t1, Y3.x);
  182. SecP224K1FieldElement Z3 = new SecP224K1FieldElement(M);
  183. SecP224K1Field.Twice(Y1.x, Z3.x);
  184. if (!Z1.IsOne)
  185. {
  186. SecP224K1Field.Multiply(Z3.x, Z1.x, Z3.x);
  187. }
  188. return new SecP224K1Point(curve, X3, Y3, new ECFieldElement[] { Z3 }, IsCompressed);
  189. }
  190. public override ECPoint TwicePlus(ECPoint b)
  191. {
  192. if (this == b)
  193. return ThreeTimes();
  194. if (this.IsInfinity)
  195. return b;
  196. if (b.IsInfinity)
  197. return Twice();
  198. ECFieldElement Y1 = this.RawYCoord;
  199. if (Y1.IsZero)
  200. return b;
  201. return Twice().Add(b);
  202. }
  203. public override ECPoint ThreeTimes()
  204. {
  205. if (this.IsInfinity || this.RawYCoord.IsZero)
  206. return this;
  207. // NOTE: Be careful about recursions between TwicePlus and ThreeTimes
  208. return Twice().Add(this);
  209. }
  210. public override ECPoint Negate()
  211. {
  212. if (IsInfinity)
  213. return this;
  214. return new SecP224K1Point(Curve, RawXCoord, RawYCoord.Negate(), RawZCoords, IsCompressed);
  215. }
  216. }
  217. }
  218. #endif