SecP256K1Point.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 SecP256K1Point
  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 SecP256K1Point(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 SecP256K1Point(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 SecP256K1Point(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs,
  48. bool withCompression)
  49. : base(curve, x, y, zs, withCompression)
  50. {
  51. }
  52. protected override ECPoint Detach()
  53. {
  54. return new SecP256K1Point(null, AffineXCoord, AffineYCoord);
  55. }
  56. public override ECPoint Add(ECPoint b)
  57. {
  58. if (this.IsInfinity)
  59. return b;
  60. if (b.IsInfinity)
  61. return this;
  62. if (this == b)
  63. return Twice();
  64. ECCurve curve = this.Curve;
  65. SecP256K1FieldElement X1 = (SecP256K1FieldElement)this.RawXCoord, Y1 = (SecP256K1FieldElement)this.RawYCoord;
  66. SecP256K1FieldElement X2 = (SecP256K1FieldElement)b.RawXCoord, Y2 = (SecP256K1FieldElement)b.RawYCoord;
  67. SecP256K1FieldElement Z1 = (SecP256K1FieldElement)this.RawZCoords[0];
  68. SecP256K1FieldElement Z2 = (SecP256K1FieldElement)b.RawZCoords[0];
  69. uint c;
  70. uint[] tt1 = Nat256.CreateExt();
  71. uint[] t2 = Nat256.Create();
  72. uint[] t3 = Nat256.Create();
  73. uint[] t4 = Nat256.Create();
  74. bool Z1IsOne = Z1.IsOne;
  75. uint[] U2, S2;
  76. if (Z1IsOne)
  77. {
  78. U2 = X2.x;
  79. S2 = Y2.x;
  80. }
  81. else
  82. {
  83. S2 = t3;
  84. SecP256K1Field.Square(Z1.x, S2);
  85. U2 = t2;
  86. SecP256K1Field.Multiply(S2, X2.x, U2);
  87. SecP256K1Field.Multiply(S2, Z1.x, S2);
  88. SecP256K1Field.Multiply(S2, Y2.x, S2);
  89. }
  90. bool Z2IsOne = Z2.IsOne;
  91. uint[] U1, S1;
  92. if (Z2IsOne)
  93. {
  94. U1 = X1.x;
  95. S1 = Y1.x;
  96. }
  97. else
  98. {
  99. S1 = t4;
  100. SecP256K1Field.Square(Z2.x, S1);
  101. U1 = tt1;
  102. SecP256K1Field.Multiply(S1, X1.x, U1);
  103. SecP256K1Field.Multiply(S1, Z2.x, S1);
  104. SecP256K1Field.Multiply(S1, Y1.x, S1);
  105. }
  106. uint[] H = Nat256.Create();
  107. SecP256K1Field.Subtract(U1, U2, H);
  108. uint[] R = t2;
  109. SecP256K1Field.Subtract(S1, S2, R);
  110. // Check if b == this or b == -this
  111. if (Nat256.IsZero(H))
  112. {
  113. if (Nat256.IsZero(R))
  114. {
  115. // this == b, i.e. this must be doubled
  116. return this.Twice();
  117. }
  118. // this == -b, i.e. the result is the point at infinity
  119. return curve.Infinity;
  120. }
  121. uint[] HSquared = t3;
  122. SecP256K1Field.Square(H, HSquared);
  123. uint[] G = Nat256.Create();
  124. SecP256K1Field.Multiply(HSquared, H, G);
  125. uint[] V = t3;
  126. SecP256K1Field.Multiply(HSquared, U1, V);
  127. SecP256K1Field.Negate(G, G);
  128. Nat256.Mul(S1, G, tt1);
  129. c = Nat256.AddBothTo(V, V, G);
  130. SecP256K1Field.Reduce32(c, G);
  131. SecP256K1FieldElement X3 = new SecP256K1FieldElement(t4);
  132. SecP256K1Field.Square(R, X3.x);
  133. SecP256K1Field.Subtract(X3.x, G, X3.x);
  134. SecP256K1FieldElement Y3 = new SecP256K1FieldElement(G);
  135. SecP256K1Field.Subtract(V, X3.x, Y3.x);
  136. SecP256K1Field.MultiplyAddToExt(Y3.x, R, tt1);
  137. SecP256K1Field.Reduce(tt1, Y3.x);
  138. SecP256K1FieldElement Z3 = new SecP256K1FieldElement(H);
  139. if (!Z1IsOne)
  140. {
  141. SecP256K1Field.Multiply(Z3.x, Z1.x, Z3.x);
  142. }
  143. if (!Z2IsOne)
  144. {
  145. SecP256K1Field.Multiply(Z3.x, Z2.x, Z3.x);
  146. }
  147. ECFieldElement[] zs = new ECFieldElement[] { Z3 };
  148. return new SecP256K1Point(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. SecP256K1FieldElement Y1 = (SecP256K1FieldElement)this.RawYCoord;
  156. if (Y1.IsZero)
  157. return curve.Infinity;
  158. SecP256K1FieldElement X1 = (SecP256K1FieldElement)this.RawXCoord, Z1 = (SecP256K1FieldElement)this.RawZCoords[0];
  159. uint c;
  160. uint[] Y1Squared = Nat256.Create();
  161. SecP256K1Field.Square(Y1.x, Y1Squared);
  162. uint[] T = Nat256.Create();
  163. SecP256K1Field.Square(Y1Squared, T);
  164. uint[] M = Nat256.Create();
  165. SecP256K1Field.Square(X1.x, M);
  166. c = Nat256.AddBothTo(M, M, M);
  167. SecP256K1Field.Reduce32(c, M);
  168. uint[] S = Y1Squared;
  169. SecP256K1Field.Multiply(Y1Squared, X1.x, S);
  170. c = Nat.ShiftUpBits(8, S, 2, 0);
  171. SecP256K1Field.Reduce32(c, S);
  172. uint[] t1 = Nat256.Create();
  173. c = Nat.ShiftUpBits(8, T, 3, 0, t1);
  174. SecP256K1Field.Reduce32(c, t1);
  175. SecP256K1FieldElement X3 = new SecP256K1FieldElement(T);
  176. SecP256K1Field.Square(M, X3.x);
  177. SecP256K1Field.Subtract(X3.x, S, X3.x);
  178. SecP256K1Field.Subtract(X3.x, S, X3.x);
  179. SecP256K1FieldElement Y3 = new SecP256K1FieldElement(S);
  180. SecP256K1Field.Subtract(S, X3.x, Y3.x);
  181. SecP256K1Field.Multiply(Y3.x, M, Y3.x);
  182. SecP256K1Field.Subtract(Y3.x, t1, Y3.x);
  183. SecP256K1FieldElement Z3 = new SecP256K1FieldElement(M);
  184. SecP256K1Field.Twice(Y1.x, Z3.x);
  185. if (!Z1.IsOne)
  186. {
  187. SecP256K1Field.Multiply(Z3.x, Z1.x, Z3.x);
  188. }
  189. return new SecP256K1Point(curve, X3, Y3, new ECFieldElement[] { Z3 }, IsCompressed);
  190. }
  191. public override ECPoint TwicePlus(ECPoint b)
  192. {
  193. if (this == b)
  194. return ThreeTimes();
  195. if (this.IsInfinity)
  196. return b;
  197. if (b.IsInfinity)
  198. return Twice();
  199. ECFieldElement Y1 = this.RawYCoord;
  200. if (Y1.IsZero)
  201. return b;
  202. return Twice().Add(b);
  203. }
  204. public override ECPoint ThreeTimes()
  205. {
  206. if (this.IsInfinity || this.RawYCoord.IsZero)
  207. return this;
  208. // NOTE: Be careful about recursions between TwicePlus and ThreeTimes
  209. return Twice().Add(this);
  210. }
  211. public override ECPoint Negate()
  212. {
  213. if (IsInfinity)
  214. return this;
  215. return new SecP256K1Point(Curve, RawXCoord, RawYCoord.Negate(), RawZCoords, IsCompressed);
  216. }
  217. }
  218. }
  219. #pragma warning restore
  220. #endif