SecP521R1Point.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 SecP521R1Point
  8. : AbstractFpPoint
  9. {
  10. internal SecP521R1Point(ECCurve curve, ECFieldElement x, ECFieldElement y)
  11. : base(curve, x, y)
  12. {
  13. }
  14. internal SecP521R1Point(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 SecP521R1Point(null, AffineXCoord, AffineYCoord);
  21. }
  22. public override ECPoint Add(ECPoint b)
  23. {
  24. if (this.IsInfinity)
  25. return b;
  26. if (b.IsInfinity)
  27. return this;
  28. if (this == b)
  29. return Twice();
  30. ECCurve curve = this.Curve;
  31. SecP521R1FieldElement X1 = (SecP521R1FieldElement)this.RawXCoord, Y1 = (SecP521R1FieldElement)this.RawYCoord;
  32. SecP521R1FieldElement X2 = (SecP521R1FieldElement)b.RawXCoord, Y2 = (SecP521R1FieldElement)b.RawYCoord;
  33. SecP521R1FieldElement Z1 = (SecP521R1FieldElement)this.RawZCoords[0];
  34. SecP521R1FieldElement Z2 = (SecP521R1FieldElement)b.RawZCoords[0];
  35. uint[] tt0 = Nat.Create(33);
  36. uint[] t1 = Nat.Create(17);
  37. uint[] t2 = Nat.Create(17);
  38. uint[] t3 = Nat.Create(17);
  39. uint[] t4 = Nat.Create(17);
  40. bool Z1IsOne = Z1.IsOne;
  41. uint[] U2, S2;
  42. if (Z1IsOne)
  43. {
  44. U2 = X2.x;
  45. S2 = Y2.x;
  46. }
  47. else
  48. {
  49. S2 = t3;
  50. SecP521R1Field.Square(Z1.x, S2, tt0);
  51. U2 = t2;
  52. SecP521R1Field.Multiply(S2, X2.x, U2, tt0);
  53. SecP521R1Field.Multiply(S2, Z1.x, S2, tt0);
  54. SecP521R1Field.Multiply(S2, Y2.x, S2, tt0);
  55. }
  56. bool Z2IsOne = Z2.IsOne;
  57. uint[] U1, S1;
  58. if (Z2IsOne)
  59. {
  60. U1 = X1.x;
  61. S1 = Y1.x;
  62. }
  63. else
  64. {
  65. S1 = t4;
  66. SecP521R1Field.Square(Z2.x, S1, tt0);
  67. U1 = t1;
  68. SecP521R1Field.Multiply(S1, X1.x, U1, tt0);
  69. SecP521R1Field.Multiply(S1, Z2.x, S1, tt0);
  70. SecP521R1Field.Multiply(S1, Y1.x, S1, tt0);
  71. }
  72. uint[] H = Nat.Create(17);
  73. SecP521R1Field.Subtract(U1, U2, H);
  74. uint[] R = t2;
  75. SecP521R1Field.Subtract(S1, S2, R);
  76. // Check if b == this or b == -this
  77. if (Nat.IsZero(17, H))
  78. {
  79. if (Nat.IsZero(17, R))
  80. {
  81. // this == b, i.e. this must be doubled
  82. return this.Twice();
  83. }
  84. // this == -b, i.e. the result is the point at infinity
  85. return curve.Infinity;
  86. }
  87. uint[] HSquared = t3;
  88. SecP521R1Field.Square(H, HSquared, tt0);
  89. uint[] G = Nat.Create(17);
  90. SecP521R1Field.Multiply(HSquared, H, G, tt0);
  91. uint[] V = t3;
  92. SecP521R1Field.Multiply(HSquared, U1, V, tt0);
  93. SecP521R1Field.Multiply(S1, G, t1, tt0);
  94. SecP521R1FieldElement X3 = new SecP521R1FieldElement(t4);
  95. SecP521R1Field.Square(R, X3.x, tt0);
  96. SecP521R1Field.Add(X3.x, G, X3.x);
  97. SecP521R1Field.Subtract(X3.x, V, X3.x);
  98. SecP521R1Field.Subtract(X3.x, V, X3.x);
  99. SecP521R1FieldElement Y3 = new SecP521R1FieldElement(G);
  100. SecP521R1Field.Subtract(V, X3.x, Y3.x);
  101. SecP521R1Field.Multiply(Y3.x, R, t2, tt0);
  102. SecP521R1Field.Subtract(t2, t1, Y3.x);
  103. SecP521R1FieldElement Z3 = new SecP521R1FieldElement(H);
  104. if (!Z1IsOne)
  105. {
  106. SecP521R1Field.Multiply(Z3.x, Z1.x, Z3.x, tt0);
  107. }
  108. if (!Z2IsOne)
  109. {
  110. SecP521R1Field.Multiply(Z3.x, Z2.x, Z3.x, tt0);
  111. }
  112. ECFieldElement[] zs = new ECFieldElement[] { Z3 };
  113. return new SecP521R1Point(curve, X3, Y3, zs);
  114. }
  115. public override ECPoint Twice()
  116. {
  117. if (this.IsInfinity)
  118. return this;
  119. ECCurve curve = this.Curve;
  120. SecP521R1FieldElement Y1 = (SecP521R1FieldElement)this.RawYCoord;
  121. if (Y1.IsZero)
  122. return curve.Infinity;
  123. SecP521R1FieldElement X1 = (SecP521R1FieldElement)this.RawXCoord, Z1 = (SecP521R1FieldElement)this.RawZCoords[0];
  124. uint[] tt0 = Nat.Create(33);
  125. uint[] t1 = Nat.Create(17);
  126. uint[] t2 = Nat.Create(17);
  127. uint[] Y1Squared = Nat.Create(17);
  128. SecP521R1Field.Square(Y1.x, Y1Squared, tt0);
  129. uint[] T = Nat.Create(17);
  130. SecP521R1Field.Square(Y1Squared, T, tt0);
  131. bool Z1IsOne = Z1.IsOne;
  132. uint[] Z1Squared = Z1.x;
  133. if (!Z1IsOne)
  134. {
  135. Z1Squared = t2;
  136. SecP521R1Field.Square(Z1.x, Z1Squared, tt0);
  137. }
  138. SecP521R1Field.Subtract(X1.x, Z1Squared, t1);
  139. uint[] M = t2;
  140. SecP521R1Field.Add(X1.x, Z1Squared, M);
  141. SecP521R1Field.Multiply(M, t1, M, tt0);
  142. Nat.AddBothTo(17, M, M, M);
  143. SecP521R1Field.Reduce23(M);
  144. uint[] S = Y1Squared;
  145. SecP521R1Field.Multiply(Y1Squared, X1.x, S, tt0);
  146. Nat.ShiftUpBits(17, S, 2, 0);
  147. SecP521R1Field.Reduce23(S);
  148. Nat.ShiftUpBits(17, T, 3, 0, t1);
  149. SecP521R1Field.Reduce23(t1);
  150. SecP521R1FieldElement X3 = new SecP521R1FieldElement(T);
  151. SecP521R1Field.Square(M, X3.x, tt0);
  152. SecP521R1Field.Subtract(X3.x, S, X3.x);
  153. SecP521R1Field.Subtract(X3.x, S, X3.x);
  154. SecP521R1FieldElement Y3 = new SecP521R1FieldElement(S);
  155. SecP521R1Field.Subtract(S, X3.x, Y3.x);
  156. SecP521R1Field.Multiply(Y3.x, M, Y3.x, tt0);
  157. SecP521R1Field.Subtract(Y3.x, t1, Y3.x);
  158. SecP521R1FieldElement Z3 = new SecP521R1FieldElement(M);
  159. SecP521R1Field.Twice(Y1.x, Z3.x);
  160. if (!Z1IsOne)
  161. {
  162. SecP521R1Field.Multiply(Z3.x, Z1.x, Z3.x, tt0);
  163. }
  164. return new SecP521R1Point(curve, X3, Y3, new ECFieldElement[] { Z3 });
  165. }
  166. public override ECPoint TwicePlus(ECPoint b)
  167. {
  168. if (this == b)
  169. return ThreeTimes();
  170. if (this.IsInfinity)
  171. return b;
  172. if (b.IsInfinity)
  173. return Twice();
  174. ECFieldElement Y1 = this.RawYCoord;
  175. if (Y1.IsZero)
  176. return b;
  177. return Twice().Add(b);
  178. }
  179. public override ECPoint ThreeTimes()
  180. {
  181. if (this.IsInfinity || this.RawYCoord.IsZero)
  182. return this;
  183. // NOTE: Be careful about recursions between TwicePlus and ThreeTimes
  184. return Twice().Add(this);
  185. }
  186. public override ECPoint Negate()
  187. {
  188. if (IsInfinity)
  189. return this;
  190. return new SecP521R1Point(Curve, RawXCoord, RawYCoord.Negate(), RawZCoords);
  191. }
  192. }
  193. }
  194. #pragma warning restore
  195. #endif