SecP521R1Point.cs 8.5 KB

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