SecP521R1Point.cs 8.5 KB

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