SecT571K1Point.cs 10 KB

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