SecT571R1Point.cs 10 KB

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