SecP384R1Point.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 SecP384R1Point
  8. : AbstractFpPoint
  9. {
  10. /**
  11. * Create a point which encodes with point compression.
  12. *
  13. * @param curve
  14. * the curve to use
  15. * @param x
  16. * affine x co-ordinate
  17. * @param y
  18. * affine y co-ordinate
  19. *
  20. * @deprecated Use ECCurve.createPoint to construct points
  21. */
  22. public SecP384R1Point(ECCurve curve, ECFieldElement x, ECFieldElement y)
  23. : this(curve, x, y, false)
  24. {
  25. }
  26. /**
  27. * Create a point that encodes with or without point compresion.
  28. *
  29. * @param curve
  30. * the curve to use
  31. * @param x
  32. * affine x co-ordinate
  33. * @param y
  34. * affine y co-ordinate
  35. * @param withCompression
  36. * if true encode with point compression
  37. *
  38. * @deprecated per-point compression property will be removed, refer
  39. * {@link #getEncoded(bool)}
  40. */
  41. public SecP384R1Point(ECCurve curve, ECFieldElement x, ECFieldElement y, bool withCompression)
  42. : base(curve, x, y, withCompression)
  43. {
  44. if ((x == null) != (y == null))
  45. throw new ArgumentException("Exactly one of the field elements is null");
  46. }
  47. internal SecP384R1Point(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression)
  48. : base(curve, x, y, zs, withCompression)
  49. {
  50. }
  51. protected override ECPoint Detach()
  52. {
  53. return new SecP384R1Point(null, AffineXCoord, AffineYCoord);
  54. }
  55. public override ECPoint Add(ECPoint b)
  56. {
  57. if (this.IsInfinity)
  58. return b;
  59. if (b.IsInfinity)
  60. return this;
  61. if (this == b)
  62. return Twice();
  63. ECCurve curve = this.Curve;
  64. SecP384R1FieldElement X1 = (SecP384R1FieldElement)this.RawXCoord, Y1 = (SecP384R1FieldElement)this.RawYCoord;
  65. SecP384R1FieldElement X2 = (SecP384R1FieldElement)b.RawXCoord, Y2 = (SecP384R1FieldElement)b.RawYCoord;
  66. SecP384R1FieldElement Z1 = (SecP384R1FieldElement)this.RawZCoords[0];
  67. SecP384R1FieldElement Z2 = (SecP384R1FieldElement)b.RawZCoords[0];
  68. uint c;
  69. uint[] tt1 = Nat.Create(24);
  70. uint[] tt2 = Nat.Create(24);
  71. uint[] t3 = Nat.Create(12);
  72. uint[] t4 = Nat.Create(12);
  73. bool Z1IsOne = Z1.IsOne;
  74. uint[] U2, S2;
  75. if (Z1IsOne)
  76. {
  77. U2 = X2.x;
  78. S2 = Y2.x;
  79. }
  80. else
  81. {
  82. S2 = t3;
  83. SecP384R1Field.Square(Z1.x, S2);
  84. U2 = tt2;
  85. SecP384R1Field.Multiply(S2, X2.x, U2);
  86. SecP384R1Field.Multiply(S2, Z1.x, S2);
  87. SecP384R1Field.Multiply(S2, Y2.x, S2);
  88. }
  89. bool Z2IsOne = Z2.IsOne;
  90. uint[] U1, S1;
  91. if (Z2IsOne)
  92. {
  93. U1 = X1.x;
  94. S1 = Y1.x;
  95. }
  96. else
  97. {
  98. S1 = t4;
  99. SecP384R1Field.Square(Z2.x, S1);
  100. U1 = tt1;
  101. SecP384R1Field.Multiply(S1, X1.x, U1);
  102. SecP384R1Field.Multiply(S1, Z2.x, S1);
  103. SecP384R1Field.Multiply(S1, Y1.x, S1);
  104. }
  105. uint[] H = Nat.Create(12);
  106. SecP384R1Field.Subtract(U1, U2, H);
  107. uint[] R = Nat.Create(12);
  108. SecP384R1Field.Subtract(S1, S2, R);
  109. // Check if b == this or b == -this
  110. if (Nat.IsZero(12, H))
  111. {
  112. if (Nat.IsZero(12, R))
  113. {
  114. // this == b, i.e. this must be doubled
  115. return this.Twice();
  116. }
  117. // this == -b, i.e. the result is the point at infinity
  118. return curve.Infinity;
  119. }
  120. uint[] HSquared = t3;
  121. SecP384R1Field.Square(H, HSquared);
  122. uint[] G = Nat.Create(12);
  123. SecP384R1Field.Multiply(HSquared, H, G);
  124. uint[] V = t3;
  125. SecP384R1Field.Multiply(HSquared, U1, V);
  126. SecP384R1Field.Negate(G, G);
  127. Nat384.Mul(S1, G, tt1);
  128. c = Nat.AddBothTo(12, V, V, G);
  129. SecP384R1Field.Reduce32(c, G);
  130. SecP384R1FieldElement X3 = new SecP384R1FieldElement(t4);
  131. SecP384R1Field.Square(R, X3.x);
  132. SecP384R1Field.Subtract(X3.x, G, X3.x);
  133. SecP384R1FieldElement Y3 = new SecP384R1FieldElement(G);
  134. SecP384R1Field.Subtract(V, X3.x, Y3.x);
  135. Nat384.Mul(Y3.x, R, tt2);
  136. SecP384R1Field.AddExt(tt1, tt2, tt1);
  137. SecP384R1Field.Reduce(tt1, Y3.x);
  138. SecP384R1FieldElement Z3 = new SecP384R1FieldElement(H);
  139. if (!Z1IsOne)
  140. {
  141. SecP384R1Field.Multiply(Z3.x, Z1.x, Z3.x);
  142. }
  143. if (!Z2IsOne)
  144. {
  145. SecP384R1Field.Multiply(Z3.x, Z2.x, Z3.x);
  146. }
  147. ECFieldElement[] zs = new ECFieldElement[] { Z3 };
  148. return new SecP384R1Point(curve, X3, Y3, zs, IsCompressed);
  149. }
  150. public override ECPoint Twice()
  151. {
  152. if (this.IsInfinity)
  153. return this;
  154. ECCurve curve = this.Curve;
  155. SecP384R1FieldElement Y1 = (SecP384R1FieldElement)this.RawYCoord;
  156. if (Y1.IsZero)
  157. return curve.Infinity;
  158. SecP384R1FieldElement X1 = (SecP384R1FieldElement)this.RawXCoord, Z1 = (SecP384R1FieldElement)this.RawZCoords[0];
  159. uint c;
  160. uint[] t1 = Nat.Create(12);
  161. uint[] t2 = Nat.Create(12);
  162. uint[] Y1Squared = Nat.Create(12);
  163. SecP384R1Field.Square(Y1.x, Y1Squared);
  164. uint[] T = Nat.Create(12);
  165. SecP384R1Field.Square(Y1Squared, T);
  166. bool Z1IsOne = Z1.IsOne;
  167. uint[] Z1Squared = Z1.x;
  168. if (!Z1IsOne)
  169. {
  170. Z1Squared = t2;
  171. SecP384R1Field.Square(Z1.x, Z1Squared);
  172. }
  173. SecP384R1Field.Subtract(X1.x, Z1Squared, t1);
  174. uint[] M = t2;
  175. SecP384R1Field.Add(X1.x, Z1Squared, M);
  176. SecP384R1Field.Multiply(M, t1, M);
  177. c = Nat.AddBothTo(12, M, M, M);
  178. SecP384R1Field.Reduce32(c, M);
  179. uint[] S = Y1Squared;
  180. SecP384R1Field.Multiply(Y1Squared, X1.x, S);
  181. c = Nat.ShiftUpBits(12, S, 2, 0);
  182. SecP384R1Field.Reduce32(c, S);
  183. c = Nat.ShiftUpBits(12, T, 3, 0, t1);
  184. SecP384R1Field.Reduce32(c, t1);
  185. SecP384R1FieldElement X3 = new SecP384R1FieldElement(T);
  186. SecP384R1Field.Square(M, X3.x);
  187. SecP384R1Field.Subtract(X3.x, S, X3.x);
  188. SecP384R1Field.Subtract(X3.x, S, X3.x);
  189. SecP384R1FieldElement Y3 = new SecP384R1FieldElement(S);
  190. SecP384R1Field.Subtract(S, X3.x, Y3.x);
  191. SecP384R1Field.Multiply(Y3.x, M, Y3.x);
  192. SecP384R1Field.Subtract(Y3.x, t1, Y3.x);
  193. SecP384R1FieldElement Z3 = new SecP384R1FieldElement(M);
  194. SecP384R1Field.Twice(Y1.x, Z3.x);
  195. if (!Z1IsOne)
  196. {
  197. SecP384R1Field.Multiply(Z3.x, Z1.x, Z3.x);
  198. }
  199. return new SecP384R1Point(curve, X3, Y3, new ECFieldElement[] { Z3 }, IsCompressed);
  200. }
  201. public override ECPoint TwicePlus(ECPoint b)
  202. {
  203. if (this == b)
  204. return ThreeTimes();
  205. if (this.IsInfinity)
  206. return b;
  207. if (b.IsInfinity)
  208. return Twice();
  209. ECFieldElement Y1 = this.RawYCoord;
  210. if (Y1.IsZero)
  211. return b;
  212. return Twice().Add(b);
  213. }
  214. public override ECPoint ThreeTimes()
  215. {
  216. if (this.IsInfinity || this.RawYCoord.IsZero)
  217. return this;
  218. // NOTE: Be careful about recursions between TwicePlus and ThreeTimes
  219. return Twice().Add(this);
  220. }
  221. public override ECPoint Negate()
  222. {
  223. if (IsInfinity)
  224. return this;
  225. return new SecP384R1Point(Curve, RawXCoord, RawYCoord.Negate(), RawZCoords, IsCompressed);
  226. }
  227. }
  228. }
  229. #pragma warning restore
  230. #endif