SecP256R1Point.cs 7.6 KB

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