SecP384R1Point.cs 7.6 KB

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