SecP192K1Point.cs 7.0 KB

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