SecP256R1Point.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 SecP256R1Point
  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 SecP256R1Point(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 SecP256R1Point(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 SecP256R1Point(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 SecP256R1Point(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. SecP256R1FieldElement X1 = (SecP256R1FieldElement)this.RawXCoord, Y1 = (SecP256R1FieldElement)this.RawYCoord;
  64. SecP256R1FieldElement X2 = (SecP256R1FieldElement)b.RawXCoord, Y2 = (SecP256R1FieldElement)b.RawYCoord;
  65. SecP256R1FieldElement Z1 = (SecP256R1FieldElement)this.RawZCoords[0];
  66. SecP256R1FieldElement Z2 = (SecP256R1FieldElement)b.RawZCoords[0];
  67. uint c;
  68. uint[] tt1 = Nat256.CreateExt();
  69. uint[] t2 = Nat256.Create();
  70. uint[] t3 = Nat256.Create();
  71. uint[] t4 = Nat256.Create();
  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. SecP256R1Field.Square(Z1.x, S2);
  83. U2 = t2;
  84. SecP256R1Field.Multiply(S2, X2.x, U2);
  85. SecP256R1Field.Multiply(S2, Z1.x, S2);
  86. SecP256R1Field.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. SecP256R1Field.Square(Z2.x, S1);
  99. U1 = tt1;
  100. SecP256R1Field.Multiply(S1, X1.x, U1);
  101. SecP256R1Field.Multiply(S1, Z2.x, S1);
  102. SecP256R1Field.Multiply(S1, Y1.x, S1);
  103. }
  104. uint[] H = Nat256.Create();
  105. SecP256R1Field.Subtract(U1, U2, H);
  106. uint[] R = t2;
  107. SecP256R1Field.Subtract(S1, S2, R);
  108. // Check if b == this or b == -this
  109. if (Nat256.IsZero(H))
  110. {
  111. if (Nat256.IsZero(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. SecP256R1Field.Square(H, HSquared);
  121. uint[] G = Nat256.Create();
  122. SecP256R1Field.Multiply(HSquared, H, G);
  123. uint[] V = t3;
  124. SecP256R1Field.Multiply(HSquared, U1, V);
  125. SecP256R1Field.Negate(G, G);
  126. Nat256.Mul(S1, G, tt1);
  127. c = Nat256.AddBothTo(V, V, G);
  128. SecP256R1Field.Reduce32(c, G);
  129. SecP256R1FieldElement X3 = new SecP256R1FieldElement(t4);
  130. SecP256R1Field.Square(R, X3.x);
  131. SecP256R1Field.Subtract(X3.x, G, X3.x);
  132. SecP256R1FieldElement Y3 = new SecP256R1FieldElement(G);
  133. SecP256R1Field.Subtract(V, X3.x, Y3.x);
  134. SecP256R1Field.MultiplyAddToExt(Y3.x, R, tt1);
  135. SecP256R1Field.Reduce(tt1, Y3.x);
  136. SecP256R1FieldElement Z3 = new SecP256R1FieldElement(H);
  137. if (!Z1IsOne)
  138. {
  139. SecP256R1Field.Multiply(Z3.x, Z1.x, Z3.x);
  140. }
  141. if (!Z2IsOne)
  142. {
  143. SecP256R1Field.Multiply(Z3.x, Z2.x, Z3.x);
  144. }
  145. ECFieldElement[] zs = new ECFieldElement[]{ Z3 };
  146. return new SecP256R1Point(curve, X3, Y3, zs, IsCompressed);
  147. }
  148. public override ECPoint Twice()
  149. {
  150. if (this.IsInfinity)
  151. return this;
  152. ECCurve curve = this.Curve;
  153. SecP256R1FieldElement Y1 = (SecP256R1FieldElement)this.RawYCoord;
  154. if (Y1.IsZero)
  155. return curve.Infinity;
  156. SecP256R1FieldElement X1 = (SecP256R1FieldElement)this.RawXCoord, Z1 = (SecP256R1FieldElement)this.RawZCoords[0];
  157. uint c;
  158. uint[] t1 = Nat256.Create();
  159. uint[] t2 = Nat256.Create();
  160. uint[] Y1Squared = Nat256.Create();
  161. SecP256R1Field.Square(Y1.x, Y1Squared);
  162. uint[] T = Nat256.Create();
  163. SecP256R1Field.Square(Y1Squared, T);
  164. bool Z1IsOne = Z1.IsOne;
  165. uint[] Z1Squared = Z1.x;
  166. if (!Z1IsOne)
  167. {
  168. Z1Squared = t2;
  169. SecP256R1Field.Square(Z1.x, Z1Squared);
  170. }
  171. SecP256R1Field.Subtract(X1.x, Z1Squared, t1);
  172. uint[] M = t2;
  173. SecP256R1Field.Add(X1.x, Z1Squared, M);
  174. SecP256R1Field.Multiply(M, t1, M);
  175. c = Nat256.AddBothTo(M, M, M);
  176. SecP256R1Field.Reduce32(c, M);
  177. uint[] S = Y1Squared;
  178. SecP256R1Field.Multiply(Y1Squared, X1.x, S);
  179. c = Nat.ShiftUpBits(8, S, 2, 0);
  180. SecP256R1Field.Reduce32(c, S);
  181. c = Nat.ShiftUpBits(8, T, 3, 0, t1);
  182. SecP256R1Field.Reduce32(c, t1);
  183. SecP256R1FieldElement X3 = new SecP256R1FieldElement(T);
  184. SecP256R1Field.Square(M, X3.x);
  185. SecP256R1Field.Subtract(X3.x, S, X3.x);
  186. SecP256R1Field.Subtract(X3.x, S, X3.x);
  187. SecP256R1FieldElement Y3 = new SecP256R1FieldElement(S);
  188. SecP256R1Field.Subtract(S, X3.x, Y3.x);
  189. SecP256R1Field.Multiply(Y3.x, M, Y3.x);
  190. SecP256R1Field.Subtract(Y3.x, t1, Y3.x);
  191. SecP256R1FieldElement Z3 = new SecP256R1FieldElement(M);
  192. SecP256R1Field.Twice(Y1.x, Z3.x);
  193. if (!Z1IsOne)
  194. {
  195. SecP256R1Field.Multiply(Z3.x, Z1.x, Z3.x);
  196. }
  197. return new SecP256R1Point(curve, X3, Y3, new ECFieldElement[]{ Z3 }, IsCompressed);
  198. }
  199. public override ECPoint TwicePlus(ECPoint b)
  200. {
  201. if (this == b)
  202. return ThreeTimes();
  203. if (this.IsInfinity)
  204. return b;
  205. if (b.IsInfinity)
  206. return Twice();
  207. ECFieldElement Y1 = this.RawYCoord;
  208. if (Y1.IsZero)
  209. return b;
  210. return Twice().Add(b);
  211. }
  212. public override ECPoint ThreeTimes()
  213. {
  214. if (this.IsInfinity || this.RawYCoord.IsZero)
  215. return this;
  216. // NOTE: Be careful about recursions between TwicePlus and ThreeTimes
  217. return Twice().Add(this);
  218. }
  219. public override ECPoint Negate()
  220. {
  221. if (IsInfinity)
  222. return this;
  223. return new SecP256R1Point(Curve, RawXCoord, RawYCoord.Negate(), RawZCoords, IsCompressed);
  224. }
  225. }
  226. }
  227. #endif