SecT113R1Point.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 SecT113R1Point
  7. : AbstractF2mPoint
  8. {
  9. /**
  10. * @deprecated Use ECCurve.createPoint to construct points
  11. */
  12. public SecT113R1Point(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 SecT113R1Point(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 SecT113R1Point(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 SecT113R1Point(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. return b;
  66. if (b.IsInfinity)
  67. return this;
  68. ECCurve curve = this.Curve;
  69. ECFieldElement X1 = this.RawXCoord;
  70. ECFieldElement X2 = b.RawXCoord;
  71. if (X1.IsZero)
  72. {
  73. if (X2.IsZero)
  74. return curve.Infinity;
  75. return b.Add(this);
  76. }
  77. ECFieldElement L1 = this.RawYCoord, Z1 = this.RawZCoords[0];
  78. ECFieldElement L2 = b.RawYCoord, Z2 = b.RawZCoords[0];
  79. bool Z1IsOne = Z1.IsOne;
  80. ECFieldElement U2 = X2, S2 = L2;
  81. if (!Z1IsOne)
  82. {
  83. U2 = U2.Multiply(Z1);
  84. S2 = S2.Multiply(Z1);
  85. }
  86. bool Z2IsOne = Z2.IsOne;
  87. ECFieldElement U1 = X1, S1 = L1;
  88. if (!Z2IsOne)
  89. {
  90. U1 = U1.Multiply(Z2);
  91. S1 = S1.Multiply(Z2);
  92. }
  93. ECFieldElement A = S1.Add(S2);
  94. ECFieldElement B = U1.Add(U2);
  95. if (B.IsZero)
  96. {
  97. if (A.IsZero)
  98. return Twice();
  99. return curve.Infinity;
  100. }
  101. ECFieldElement X3, L3, Z3;
  102. if (X2.IsZero)
  103. {
  104. // TODO This can probably be optimized quite a bit
  105. ECPoint p = this.Normalize();
  106. X1 = p.XCoord;
  107. ECFieldElement Y1 = p.YCoord;
  108. ECFieldElement Y2 = L2;
  109. ECFieldElement L = Y1.Add(Y2).Divide(X1);
  110. X3 = L.Square().Add(L).Add(X1).Add(curve.A);
  111. if (X3.IsZero)
  112. {
  113. return new SecT113R1Point(curve, X3, curve.B.Sqrt(), IsCompressed);
  114. }
  115. ECFieldElement Y3 = L.Multiply(X1.Add(X3)).Add(X3).Add(Y1);
  116. L3 = Y3.Divide(X3).Add(X3);
  117. Z3 = curve.FromBigInteger(BigInteger.One);
  118. }
  119. else
  120. {
  121. B = B.Square();
  122. ECFieldElement AU1 = A.Multiply(U1);
  123. ECFieldElement AU2 = A.Multiply(U2);
  124. X3 = AU1.Multiply(AU2);
  125. if (X3.IsZero)
  126. {
  127. return new SecT113R1Point(curve, X3, curve.B.Sqrt(), IsCompressed);
  128. }
  129. ECFieldElement ABZ2 = A.Multiply(B);
  130. if (!Z2IsOne)
  131. {
  132. ABZ2 = ABZ2.Multiply(Z2);
  133. }
  134. L3 = AU2.Add(B).SquarePlusProduct(ABZ2, L1.Add(Z1));
  135. Z3 = ABZ2;
  136. if (!Z1IsOne)
  137. {
  138. Z3 = Z3.Multiply(Z1);
  139. }
  140. }
  141. return new SecT113R1Point(curve, X3, L3, new ECFieldElement[]{ Z3 }, IsCompressed);
  142. }
  143. public override ECPoint Twice()
  144. {
  145. if (this.IsInfinity)
  146. return this;
  147. ECCurve curve = this.Curve;
  148. ECFieldElement X1 = this.RawXCoord;
  149. if (X1.IsZero)
  150. {
  151. // A point with X == 0 is its own additive inverse
  152. return curve.Infinity;
  153. }
  154. ECFieldElement L1 = this.RawYCoord, Z1 = this.RawZCoords[0];
  155. bool Z1IsOne = Z1.IsOne;
  156. ECFieldElement L1Z1 = Z1IsOne ? L1 : L1.Multiply(Z1);
  157. ECFieldElement Z1Sq = Z1IsOne ? Z1 : Z1.Square();
  158. ECFieldElement a = curve.A;
  159. ECFieldElement aZ1Sq = Z1IsOne ? a : a.Multiply(Z1Sq);
  160. ECFieldElement T = L1.Square().Add(L1Z1).Add(aZ1Sq);
  161. if (T.IsZero)
  162. {
  163. return new SecT113R1Point(curve, T, curve.B.Sqrt(), IsCompressed);
  164. }
  165. ECFieldElement X3 = T.Square();
  166. ECFieldElement Z3 = Z1IsOne ? T : T.Multiply(Z1Sq);
  167. ECFieldElement X1Z1 = Z1IsOne ? X1 : X1.Multiply(Z1);
  168. ECFieldElement L3 = X1Z1.SquarePlusProduct(T, L1Z1).Add(X3).Add(Z3);
  169. return new SecT113R1Point(curve, X3, L3, new ECFieldElement[]{ Z3 }, IsCompressed);
  170. }
  171. public override ECPoint TwicePlus(ECPoint b)
  172. {
  173. if (this.IsInfinity)
  174. return b;
  175. if (b.IsInfinity)
  176. return Twice();
  177. ECCurve curve = this.Curve;
  178. ECFieldElement X1 = this.RawXCoord;
  179. if (X1.IsZero)
  180. {
  181. // A point with X == 0 is its own additive inverse
  182. return b;
  183. }
  184. ECFieldElement X2 = b.RawXCoord, Z2 = b.RawZCoords[0];
  185. if (X2.IsZero || !Z2.IsOne)
  186. {
  187. return Twice().Add(b);
  188. }
  189. ECFieldElement L1 = this.RawYCoord, Z1 = this.RawZCoords[0];
  190. ECFieldElement L2 = b.RawYCoord;
  191. ECFieldElement X1Sq = X1.Square();
  192. ECFieldElement L1Sq = L1.Square();
  193. ECFieldElement Z1Sq = Z1.Square();
  194. ECFieldElement L1Z1 = L1.Multiply(Z1);
  195. ECFieldElement T = curve.A.Multiply(Z1Sq).Add(L1Sq).Add(L1Z1);
  196. ECFieldElement L2plus1 = L2.AddOne();
  197. ECFieldElement A = curve.A.Add(L2plus1).Multiply(Z1Sq).Add(L1Sq).MultiplyPlusProduct(T, X1Sq, Z1Sq);
  198. ECFieldElement X2Z1Sq = X2.Multiply(Z1Sq);
  199. ECFieldElement B = X2Z1Sq.Add(T).Square();
  200. if (B.IsZero)
  201. {
  202. if (A.IsZero)
  203. return b.Twice();
  204. return curve.Infinity;
  205. }
  206. if (A.IsZero)
  207. {
  208. return new SecT113R1Point(curve, A, curve.B.Sqrt(), IsCompressed);
  209. }
  210. ECFieldElement X3 = A.Square().Multiply(X2Z1Sq);
  211. ECFieldElement Z3 = A.Multiply(B).Multiply(Z1Sq);
  212. ECFieldElement L3 = A.Add(B).Square().MultiplyPlusProduct(T, L2plus1, Z3);
  213. return new SecT113R1Point(curve, X3, L3, new ECFieldElement[]{ Z3 }, IsCompressed);
  214. }
  215. public override ECPoint Negate()
  216. {
  217. if (IsInfinity)
  218. return this;
  219. ECFieldElement X = this.RawXCoord;
  220. if (X.IsZero)
  221. return this;
  222. // L is actually Lambda (X + Y/X) here
  223. ECFieldElement L = this.RawYCoord, Z = this.RawZCoords[0];
  224. return new SecT113R1Point(Curve, X, L.Add(Z), new ECFieldElement[]{ Z }, IsCompressed);
  225. }
  226. }
  227. }
  228. #pragma warning restore
  229. #endif