SecT113R2Point.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
  5. {
  6. internal class SecT113R2Point
  7. : AbstractF2mPoint
  8. {
  9. /**
  10. * @deprecated Use ECCurve.createPoint to construct points
  11. */
  12. public SecT113R2Point(ECCurve curve, ECFieldElement x, ECFieldElement y)
  13. : this(curve, x, y, false)
  14. {
  15. }
  16. /**
  17. * @deprecated per-point compression property will be removed, refer {@link #getEncoded(bool)}
  18. */
  19. public SecT113R2Point(ECCurve curve, ECFieldElement x, ECFieldElement y, bool withCompression)
  20. : base(curve, x, y, withCompression)
  21. {
  22. if ((x == null) != (y == null))
  23. throw new ArgumentException("Exactly one of the field elements is null");
  24. }
  25. internal SecT113R2Point(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression)
  26. : base(curve, x, y, zs, withCompression)
  27. {
  28. }
  29. protected override ECPoint Detach()
  30. {
  31. return new SecT113R2Point(null, AffineXCoord, AffineYCoord);
  32. }
  33. public override ECFieldElement YCoord
  34. {
  35. get
  36. {
  37. ECFieldElement X = RawXCoord, L = RawYCoord;
  38. if (this.IsInfinity || X.IsZero)
  39. return L;
  40. // Y is actually Lambda (X + Y/X) here; convert to affine value on the fly
  41. ECFieldElement Y = L.Add(X).Multiply(X);
  42. ECFieldElement Z = RawZCoords[0];
  43. if (!Z.IsOne)
  44. {
  45. Y = Y.Divide(Z);
  46. }
  47. return Y;
  48. }
  49. }
  50. protected internal override bool CompressionYTilde
  51. {
  52. get
  53. {
  54. ECFieldElement X = this.RawXCoord;
  55. if (X.IsZero)
  56. return false;
  57. ECFieldElement Y = this.RawYCoord;
  58. // Y is actually Lambda (X + Y/X) here
  59. return Y.TestBitZero() != X.TestBitZero();
  60. }
  61. }
  62. public override ECPoint Add(ECPoint b)
  63. {
  64. if (this.IsInfinity)
  65. {
  66. return b;
  67. }
  68. if (b.IsInfinity)
  69. {
  70. return this;
  71. }
  72. ECCurve curve = this.Curve;
  73. ECFieldElement X1 = this.RawXCoord;
  74. ECFieldElement X2 = b.RawXCoord;
  75. if (X1.IsZero)
  76. {
  77. if (X2.IsZero)
  78. return curve.Infinity;
  79. return b.Add(this);
  80. }
  81. ECFieldElement L1 = this.RawYCoord, Z1 = this.RawZCoords[0];
  82. ECFieldElement L2 = b.RawYCoord, Z2 = b.RawZCoords[0];
  83. bool Z1IsOne = Z1.IsOne;
  84. ECFieldElement U2 = X2, S2 = L2;
  85. if (!Z1IsOne)
  86. {
  87. U2 = U2.Multiply(Z1);
  88. S2 = S2.Multiply(Z1);
  89. }
  90. bool Z2IsOne = Z2.IsOne;
  91. ECFieldElement U1 = X1, S1 = L1;
  92. if (!Z2IsOne)
  93. {
  94. U1 = U1.Multiply(Z2);
  95. S1 = S1.Multiply(Z2);
  96. }
  97. ECFieldElement A = S1.Add(S2);
  98. ECFieldElement B = U1.Add(U2);
  99. if (B.IsZero)
  100. {
  101. if (A.IsZero)
  102. return Twice();
  103. return curve.Infinity;
  104. }
  105. ECFieldElement X3, L3, Z3;
  106. if (X2.IsZero)
  107. {
  108. // TODO This can probably be optimized quite a bit
  109. ECPoint p = this.Normalize();
  110. X1 = p.XCoord;
  111. ECFieldElement Y1 = p.YCoord;
  112. ECFieldElement Y2 = L2;
  113. ECFieldElement L = Y1.Add(Y2).Divide(X1);
  114. X3 = L.Square().Add(L).Add(X1).Add(curve.A);
  115. if (X3.IsZero)
  116. {
  117. return new SecT113R2Point(curve, X3, curve.B.Sqrt(), IsCompressed);
  118. }
  119. ECFieldElement Y3 = L.Multiply(X1.Add(X3)).Add(X3).Add(Y1);
  120. L3 = Y3.Divide(X3).Add(X3);
  121. Z3 = curve.FromBigInteger(BigInteger.One);
  122. }
  123. else
  124. {
  125. B = B.Square();
  126. ECFieldElement AU1 = A.Multiply(U1);
  127. ECFieldElement AU2 = A.Multiply(U2);
  128. X3 = AU1.Multiply(AU2);
  129. if (X3.IsZero)
  130. {
  131. return new SecT113R2Point(curve, X3, curve.B.Sqrt(), IsCompressed);
  132. }
  133. ECFieldElement ABZ2 = A.Multiply(B);
  134. if (!Z2IsOne)
  135. {
  136. ABZ2 = ABZ2.Multiply(Z2);
  137. }
  138. L3 = AU2.Add(B).SquarePlusProduct(ABZ2, L1.Add(Z1));
  139. Z3 = ABZ2;
  140. if (!Z1IsOne)
  141. {
  142. Z3 = Z3.Multiply(Z1);
  143. }
  144. }
  145. return new SecT113R2Point(curve, X3, L3, new ECFieldElement[]{ Z3 }, IsCompressed);
  146. }
  147. public override ECPoint Twice()
  148. {
  149. if (this.IsInfinity)
  150. {
  151. return this;
  152. }
  153. ECCurve curve = this.Curve;
  154. ECFieldElement X1 = this.RawXCoord;
  155. if (X1.IsZero)
  156. {
  157. // A point with X == 0 is its own additive inverse
  158. return curve.Infinity;
  159. }
  160. ECFieldElement L1 = this.RawYCoord, Z1 = this.RawZCoords[0];
  161. bool Z1IsOne = Z1.IsOne;
  162. ECFieldElement L1Z1 = Z1IsOne ? L1 : L1.Multiply(Z1);
  163. ECFieldElement Z1Sq = Z1IsOne ? Z1 : Z1.Square();
  164. ECFieldElement a = curve.A;
  165. ECFieldElement aZ1Sq = Z1IsOne ? a : a.Multiply(Z1Sq);
  166. ECFieldElement T = L1.Square().Add(L1Z1).Add(aZ1Sq);
  167. if (T.IsZero)
  168. {
  169. return new SecT113R2Point(curve, T, curve.B.Sqrt(), IsCompressed);
  170. }
  171. ECFieldElement X3 = T.Square();
  172. ECFieldElement Z3 = Z1IsOne ? T : T.Multiply(Z1Sq);
  173. ECFieldElement X1Z1 = Z1IsOne ? X1 : X1.Multiply(Z1);
  174. ECFieldElement L3 = X1Z1.SquarePlusProduct(T, L1Z1).Add(X3).Add(Z3);
  175. return new SecT113R2Point(curve, X3, L3, new ECFieldElement[]{ Z3 }, IsCompressed);
  176. }
  177. public override ECPoint TwicePlus(ECPoint b)
  178. {
  179. if (this.IsInfinity)
  180. {
  181. return b;
  182. }
  183. if (b.IsInfinity)
  184. {
  185. return Twice();
  186. }
  187. ECCurve curve = this.Curve;
  188. ECFieldElement X1 = this.RawXCoord;
  189. if (X1.IsZero)
  190. {
  191. // A point with X == 0 is its own additive inverse
  192. return b;
  193. }
  194. ECFieldElement X2 = b.RawXCoord, Z2 = b.RawZCoords[0];
  195. if (X2.IsZero || !Z2.IsOne)
  196. {
  197. return Twice().Add(b);
  198. }
  199. ECFieldElement L1 = this.RawYCoord, Z1 = this.RawZCoords[0];
  200. ECFieldElement L2 = b.RawYCoord;
  201. ECFieldElement X1Sq = X1.Square();
  202. ECFieldElement L1Sq = L1.Square();
  203. ECFieldElement Z1Sq = Z1.Square();
  204. ECFieldElement L1Z1 = L1.Multiply(Z1);
  205. ECFieldElement T = curve.A.Multiply(Z1Sq).Add(L1Sq).Add(L1Z1);
  206. ECFieldElement L2plus1 = L2.AddOne();
  207. ECFieldElement A = curve.A.Add(L2plus1).Multiply(Z1Sq).Add(L1Sq).MultiplyPlusProduct(T, X1Sq, Z1Sq);
  208. ECFieldElement X2Z1Sq = X2.Multiply(Z1Sq);
  209. ECFieldElement B = X2Z1Sq.Add(T).Square();
  210. if (B.IsZero)
  211. {
  212. if (A.IsZero)
  213. return b.Twice();
  214. return curve.Infinity;
  215. }
  216. if (A.IsZero)
  217. {
  218. return new SecT113R2Point(curve, A, curve.B.Sqrt(), IsCompressed);
  219. }
  220. ECFieldElement X3 = A.Square().Multiply(X2Z1Sq);
  221. ECFieldElement Z3 = A.Multiply(B).Multiply(Z1Sq);
  222. ECFieldElement L3 = A.Add(B).Square().MultiplyPlusProduct(T, L2plus1, Z3);
  223. return new SecT113R2Point(curve, X3, L3, new ECFieldElement[]{ Z3 }, IsCompressed);
  224. }
  225. public override ECPoint Negate()
  226. {
  227. if (IsInfinity)
  228. return this;
  229. ECFieldElement X = this.RawXCoord;
  230. if (X.IsZero)
  231. return this;
  232. // L is actually Lambda (X + Y/X) here
  233. ECFieldElement L = this.RawYCoord, Z = this.RawZCoords[0];
  234. return new SecT113R2Point(Curve, X, L.Add(Z), new ECFieldElement[]{ Z }, IsCompressed);
  235. }
  236. }
  237. }
  238. #pragma warning restore
  239. #endif