SecT233K1Point.cs 9.3 KB

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