SecP256K1Point.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 SecP256K1Point
  8. : AbstractFpPoint
  9. {
  10. internal SecP256K1Point(ECCurve curve, ECFieldElement x, ECFieldElement y)
  11. : base(curve, x, y)
  12. {
  13. }
  14. internal SecP256K1Point(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 SecP256K1Point(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. SecP256K1FieldElement X1 = (SecP256K1FieldElement)this.RawXCoord, Y1 = (SecP256K1FieldElement)this.RawYCoord;
  32. SecP256K1FieldElement X2 = (SecP256K1FieldElement)b.RawXCoord, Y2 = (SecP256K1FieldElement)b.RawYCoord;
  33. SecP256K1FieldElement Z1 = (SecP256K1FieldElement)this.RawZCoords[0];
  34. SecP256K1FieldElement Z2 = (SecP256K1FieldElement)b.RawZCoords[0];
  35. uint c;
  36. uint[] tt0 = Nat256.CreateExt();
  37. uint[] tt1 = Nat256.CreateExt();
  38. uint[] t2 = Nat256.Create();
  39. uint[] t3 = Nat256.Create();
  40. uint[] t4 = Nat256.Create();
  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. SecP256K1Field.Square(Z1.x, S2, tt0);
  52. U2 = t2;
  53. SecP256K1Field.Multiply(S2, X2.x, U2, tt0);
  54. SecP256K1Field.Multiply(S2, Z1.x, S2, tt0);
  55. SecP256K1Field.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. SecP256K1Field.Square(Z2.x, S1, tt0);
  68. U1 = tt1;
  69. SecP256K1Field.Multiply(S1, X1.x, U1, tt0);
  70. SecP256K1Field.Multiply(S1, Z2.x, S1, tt0);
  71. SecP256K1Field.Multiply(S1, Y1.x, S1, tt0);
  72. }
  73. uint[] H = Nat256.Create();
  74. SecP256K1Field.Subtract(U1, U2, H);
  75. uint[] R = t2;
  76. SecP256K1Field.Subtract(S1, S2, R);
  77. // Check if b == this or b == -this
  78. if (Nat256.IsZero(H))
  79. {
  80. if (Nat256.IsZero(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. SecP256K1Field.Square(H, HSquared, tt0);
  90. uint[] G = Nat256.Create();
  91. SecP256K1Field.Multiply(HSquared, H, G, tt0);
  92. uint[] V = t3;
  93. SecP256K1Field.Multiply(HSquared, U1, V, tt0);
  94. SecP256K1Field.Negate(G, G);
  95. Nat256.Mul(S1, G, tt1);
  96. c = Nat256.AddBothTo(V, V, G);
  97. SecP256K1Field.Reduce32(c, G);
  98. SecP256K1FieldElement X3 = new SecP256K1FieldElement(t4);
  99. SecP256K1Field.Square(R, X3.x, tt0);
  100. SecP256K1Field.Subtract(X3.x, G, X3.x);
  101. SecP256K1FieldElement Y3 = new SecP256K1FieldElement(G);
  102. SecP256K1Field.Subtract(V, X3.x, Y3.x);
  103. SecP256K1Field.MultiplyAddToExt(Y3.x, R, tt1);
  104. SecP256K1Field.Reduce(tt1, Y3.x);
  105. SecP256K1FieldElement Z3 = new SecP256K1FieldElement(H);
  106. if (!Z1IsOne)
  107. {
  108. SecP256K1Field.Multiply(Z3.x, Z1.x, Z3.x, tt0);
  109. }
  110. if (!Z2IsOne)
  111. {
  112. SecP256K1Field.Multiply(Z3.x, Z2.x, Z3.x, tt0);
  113. }
  114. ECFieldElement[] zs = new ECFieldElement[] { Z3 };
  115. return new SecP256K1Point(curve, X3, Y3, zs);
  116. }
  117. public override ECPoint Twice()
  118. {
  119. if (this.IsInfinity)
  120. return this;
  121. ECCurve curve = this.Curve;
  122. SecP256K1FieldElement Y1 = (SecP256K1FieldElement)this.RawYCoord;
  123. if (Y1.IsZero)
  124. return curve.Infinity;
  125. SecP256K1FieldElement X1 = (SecP256K1FieldElement)this.RawXCoord, Z1 = (SecP256K1FieldElement)this.RawZCoords[0];
  126. uint c;
  127. uint[] tt0 = Nat256.CreateExt();
  128. uint[] Y1Squared = Nat256.Create();
  129. SecP256K1Field.Square(Y1.x, Y1Squared, tt0);
  130. uint[] T = Nat256.Create();
  131. SecP256K1Field.Square(Y1Squared, T, tt0);
  132. uint[] M = Nat256.Create();
  133. SecP256K1Field.Square(X1.x, M, tt0);
  134. c = Nat256.AddBothTo(M, M, M);
  135. SecP256K1Field.Reduce32(c, M);
  136. uint[] S = Y1Squared;
  137. SecP256K1Field.Multiply(Y1Squared, X1.x, S, tt0);
  138. c = Nat.ShiftUpBits(8, S, 2, 0);
  139. SecP256K1Field.Reduce32(c, S);
  140. uint[] t1 = Nat256.Create();
  141. c = Nat.ShiftUpBits(8, T, 3, 0, t1);
  142. SecP256K1Field.Reduce32(c, t1);
  143. SecP256K1FieldElement X3 = new SecP256K1FieldElement(T);
  144. SecP256K1Field.Square(M, X3.x, tt0);
  145. SecP256K1Field.Subtract(X3.x, S, X3.x);
  146. SecP256K1Field.Subtract(X3.x, S, X3.x);
  147. SecP256K1FieldElement Y3 = new SecP256K1FieldElement(S);
  148. SecP256K1Field.Subtract(S, X3.x, Y3.x);
  149. SecP256K1Field.Multiply(Y3.x, M, Y3.x, tt0);
  150. SecP256K1Field.Subtract(Y3.x, t1, Y3.x);
  151. SecP256K1FieldElement Z3 = new SecP256K1FieldElement(M);
  152. SecP256K1Field.Twice(Y1.x, Z3.x);
  153. if (!Z1.IsOne)
  154. {
  155. SecP256K1Field.Multiply(Z3.x, Z1.x, Z3.x, tt0);
  156. }
  157. return new SecP256K1Point(curve, X3, Y3, new ECFieldElement[] { Z3 });
  158. }
  159. public override ECPoint TwicePlus(ECPoint b)
  160. {
  161. if (this == b)
  162. return ThreeTimes();
  163. if (this.IsInfinity)
  164. return b;
  165. if (b.IsInfinity)
  166. return Twice();
  167. ECFieldElement Y1 = this.RawYCoord;
  168. if (Y1.IsZero)
  169. return b;
  170. return Twice().Add(b);
  171. }
  172. public override ECPoint ThreeTimes()
  173. {
  174. if (this.IsInfinity || this.RawYCoord.IsZero)
  175. return this;
  176. // NOTE: Be careful about recursions between TwicePlus and ThreeTimes
  177. return Twice().Add(this);
  178. }
  179. public override ECPoint Negate()
  180. {
  181. if (IsInfinity)
  182. return this;
  183. return new SecP256K1Point(Curve, RawXCoord, RawYCoord.Negate(), RawZCoords);
  184. }
  185. }
  186. }
  187. #pragma warning restore
  188. #endif