SecT113R2Point.cs 8.1 KB

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