SecP160K1Point.cs 7.1 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 SecP160K1Point
  8. : AbstractFpPoint
  9. {
  10. internal SecP160K1Point(ECCurve curve, ECFieldElement x, ECFieldElement y)
  11. : base(curve, x, y)
  12. {
  13. }
  14. internal SecP160K1Point(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 SecP160K1Point(null, AffineXCoord, AffineYCoord);
  21. }
  22. // B.3 pg 62
  23. public override ECPoint Add(ECPoint b)
  24. {
  25. if (this.IsInfinity)
  26. return b;
  27. if (b.IsInfinity)
  28. return this;
  29. if (this == b)
  30. return Twice();
  31. ECCurve curve = this.Curve;
  32. SecP160R2FieldElement X1 = (SecP160R2FieldElement)this.RawXCoord, Y1 = (SecP160R2FieldElement)this.RawYCoord;
  33. SecP160R2FieldElement X2 = (SecP160R2FieldElement)b.RawXCoord, Y2 = (SecP160R2FieldElement)b.RawYCoord;
  34. SecP160R2FieldElement Z1 = (SecP160R2FieldElement)this.RawZCoords[0];
  35. SecP160R2FieldElement Z2 = (SecP160R2FieldElement)b.RawZCoords[0];
  36. uint c;
  37. uint[] tt1 = Nat160.CreateExt();
  38. uint[] t2 = Nat160.Create();
  39. uint[] t3 = Nat160.Create();
  40. uint[] t4 = Nat160.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. SecP160R2Field.Square(Z1.x, S2);
  52. U2 = t2;
  53. SecP160R2Field.Multiply(S2, X2.x, U2);
  54. SecP160R2Field.Multiply(S2, Z1.x, S2);
  55. SecP160R2Field.Multiply(S2, Y2.x, S2);
  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. SecP160R2Field.Square(Z2.x, S1);
  68. U1 = tt1;
  69. SecP160R2Field.Multiply(S1, X1.x, U1);
  70. SecP160R2Field.Multiply(S1, Z2.x, S1);
  71. SecP160R2Field.Multiply(S1, Y1.x, S1);
  72. }
  73. uint[] H = Nat160.Create();
  74. SecP160R2Field.Subtract(U1, U2, H);
  75. uint[] R = t2;
  76. SecP160R2Field.Subtract(S1, S2, R);
  77. // Check if b == this or b == -this
  78. if (Nat160.IsZero(H))
  79. {
  80. if (Nat160.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. SecP160R2Field.Square(H, HSquared);
  90. uint[] G = Nat160.Create();
  91. SecP160R2Field.Multiply(HSquared, H, G);
  92. uint[] V = t3;
  93. SecP160R2Field.Multiply(HSquared, U1, V);
  94. SecP160R2Field.Negate(G, G);
  95. Nat160.Mul(S1, G, tt1);
  96. c = Nat160.AddBothTo(V, V, G);
  97. SecP160R2Field.Reduce32(c, G);
  98. SecP160R2FieldElement X3 = new SecP160R2FieldElement(t4);
  99. SecP160R2Field.Square(R, X3.x);
  100. SecP160R2Field.Subtract(X3.x, G, X3.x);
  101. SecP160R2FieldElement Y3 = new SecP160R2FieldElement(G);
  102. SecP160R2Field.Subtract(V, X3.x, Y3.x);
  103. SecP160R2Field.MultiplyAddToExt(Y3.x, R, tt1);
  104. SecP160R2Field.Reduce(tt1, Y3.x);
  105. SecP160R2FieldElement Z3 = new SecP160R2FieldElement(H);
  106. if (!Z1IsOne)
  107. {
  108. SecP160R2Field.Multiply(Z3.x, Z1.x, Z3.x);
  109. }
  110. if (!Z2IsOne)
  111. {
  112. SecP160R2Field.Multiply(Z3.x, Z2.x, Z3.x);
  113. }
  114. ECFieldElement[] zs = new ECFieldElement[] { Z3 };
  115. return new SecP160K1Point(curve, X3, Y3, zs);
  116. }
  117. // B.3 pg 62
  118. public override ECPoint Twice()
  119. {
  120. if (this.IsInfinity)
  121. return this;
  122. ECCurve curve = this.Curve;
  123. SecP160R2FieldElement Y1 = (SecP160R2FieldElement)this.RawYCoord;
  124. if (Y1.IsZero)
  125. return curve.Infinity;
  126. SecP160R2FieldElement X1 = (SecP160R2FieldElement)this.RawXCoord, Z1 = (SecP160R2FieldElement)this.RawZCoords[0];
  127. uint c;
  128. uint[] Y1Squared = Nat160.Create();
  129. SecP160R2Field.Square(Y1.x, Y1Squared);
  130. uint[] T = Nat160.Create();
  131. SecP160R2Field.Square(Y1Squared, T);
  132. uint[] M = Nat160.Create();
  133. SecP160R2Field.Square(X1.x, M);
  134. c = Nat160.AddBothTo(M, M, M);
  135. SecP160R2Field.Reduce32(c, M);
  136. uint[] S = Y1Squared;
  137. SecP160R2Field.Multiply(Y1Squared, X1.x, S);
  138. c = Nat.ShiftUpBits(5, S, 2, 0);
  139. SecP160R2Field.Reduce32(c, S);
  140. uint[] t1 = Nat160.Create();
  141. c = Nat.ShiftUpBits(5, T, 3, 0, t1);
  142. SecP160R2Field.Reduce32(c, t1);
  143. SecP160R2FieldElement X3 = new SecP160R2FieldElement(T);
  144. SecP160R2Field.Square(M, X3.x);
  145. SecP160R2Field.Subtract(X3.x, S, X3.x);
  146. SecP160R2Field.Subtract(X3.x, S, X3.x);
  147. SecP160R2FieldElement Y3 = new SecP160R2FieldElement(S);
  148. SecP160R2Field.Subtract(S, X3.x, Y3.x);
  149. SecP160R2Field.Multiply(Y3.x, M, Y3.x);
  150. SecP160R2Field.Subtract(Y3.x, t1, Y3.x);
  151. SecP160R2FieldElement Z3 = new SecP160R2FieldElement(M);
  152. SecP160R2Field.Twice(Y1.x, Z3.x);
  153. if (!Z1.IsOne)
  154. {
  155. SecP160R2Field.Multiply(Z3.x, Z1.x, Z3.x);
  156. }
  157. return new SecP160K1Point(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 SecP160K1Point(Curve, this.RawXCoord, this.RawYCoord.Negate(), this.RawZCoords);
  184. }
  185. }
  186. }
  187. #pragma warning restore
  188. #endif