SecP384R1Point.cs 8.6 KB

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