SecT113R1Curve.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Utilities.Encoders;
  4. namespace Org.BouncyCastle.Math.EC.Custom.Sec
  5. {
  6. internal class SecT113R1Curve
  7. : AbstractF2mCurve
  8. {
  9. private const int SecT113R1_DEFAULT_COORDS = COORD_LAMBDA_PROJECTIVE;
  10. protected readonly SecT113R1Point m_infinity;
  11. public SecT113R1Curve()
  12. : base(113, 9, 0, 0)
  13. {
  14. this.m_infinity = new SecT113R1Point(this, null, null);
  15. this.m_a = FromBigInteger(new BigInteger(1, Hex.Decode("003088250CA6E7C7FE649CE85820F7")));
  16. this.m_b = FromBigInteger(new BigInteger(1, Hex.Decode("00E8BEE4D3E2260744188BE0E9C723")));
  17. this.m_order = new BigInteger(1, Hex.Decode("0100000000000000D9CCEC8A39E56F"));
  18. this.m_cofactor = BigInteger.Two;
  19. this.m_coord = SecT113R1_DEFAULT_COORDS;
  20. }
  21. protected override ECCurve CloneCurve()
  22. {
  23. return new SecT113R1Curve();
  24. }
  25. public override bool SupportsCoordinateSystem(int coord)
  26. {
  27. switch (coord)
  28. {
  29. case COORD_LAMBDA_PROJECTIVE:
  30. return true;
  31. default:
  32. return false;
  33. }
  34. }
  35. public override ECPoint Infinity
  36. {
  37. get { return m_infinity; }
  38. }
  39. public override int FieldSize
  40. {
  41. get { return 113; }
  42. }
  43. public override ECFieldElement FromBigInteger(BigInteger x)
  44. {
  45. return new SecT113FieldElement(x);
  46. }
  47. protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, bool withCompression)
  48. {
  49. return new SecT113R1Point(this, x, y, withCompression);
  50. }
  51. protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression)
  52. {
  53. return new SecT113R1Point(this, x, y, zs, withCompression);
  54. }
  55. public override bool IsKoblitz
  56. {
  57. get { return false; }
  58. }
  59. /**
  60. * Decompresses a compressed point P = (xp, yp) (X9.62 s 4.2.2).
  61. *
  62. * @param yTilde
  63. * ~yp, an indication bit for the decompression of yp.
  64. * @param X1
  65. * The field element xp.
  66. * @return the decompressed point.
  67. */
  68. protected override ECPoint DecompressPoint(int yTilde, BigInteger X1)
  69. {
  70. ECFieldElement x = FromBigInteger(X1), y = null;
  71. if (x.IsZero)
  72. {
  73. y = B.Sqrt();
  74. }
  75. else
  76. {
  77. ECFieldElement beta = x.Square().Invert().Multiply(B).Add(A).Add(x);
  78. ECFieldElement z = SolveQuadraticEquation(beta);
  79. if (z != null)
  80. {
  81. if (z.TestBitZero() != (yTilde == 1))
  82. {
  83. z = z.AddOne();
  84. }
  85. switch (this.CoordinateSystem)
  86. {
  87. case COORD_LAMBDA_AFFINE:
  88. case COORD_LAMBDA_PROJECTIVE:
  89. {
  90. y = z.Add(x);
  91. break;
  92. }
  93. default:
  94. {
  95. y = z.Multiply(x);
  96. break;
  97. }
  98. }
  99. }
  100. }
  101. if (y == null)
  102. throw new ArgumentException("Invalid point compression");
  103. return this.CreateRawPoint(x, y, true);
  104. }
  105. /**
  106. * Solves a quadratic equation <code>z<sup>2</sup> + z = beta</code>(X9.62
  107. * D.1.6) The other solution is <code>z + 1</code>.
  108. *
  109. * @param beta
  110. * The value to solve the quadratic equation for.
  111. * @return the solution for <code>z<sup>2</sup> + z = beta</code> or
  112. * <code>null</code> if no solution exists.
  113. */
  114. private ECFieldElement SolveQuadraticEquation(ECFieldElement beta)
  115. {
  116. if (beta.IsZero)
  117. return beta;
  118. ECFieldElement zeroElement = FromBigInteger(BigInteger.Zero);
  119. ECFieldElement z = null;
  120. ECFieldElement gamma = null;
  121. Random rand = new Random();
  122. do
  123. {
  124. ECFieldElement t = FromBigInteger(new BigInteger(113, rand));
  125. z = zeroElement;
  126. ECFieldElement w = beta;
  127. for (int i = 1; i < 113; i++)
  128. {
  129. ECFieldElement w2 = w.Square();
  130. z = z.Square().Add(w2.Multiply(t));
  131. w = w2.Add(beta);
  132. }
  133. if (!w.IsZero)
  134. return null;
  135. gamma = z.Square().Add(z);
  136. }
  137. while (gamma.IsZero);
  138. return z;
  139. }
  140. public virtual int M
  141. {
  142. get { return 113; }
  143. }
  144. public virtual bool IsTrinomial
  145. {
  146. get { return true; }
  147. }
  148. public virtual int K1
  149. {
  150. get { return 9; }
  151. }
  152. public virtual int K2
  153. {
  154. get { return 0; }
  155. }
  156. public virtual int K3
  157. {
  158. get { return 0; }
  159. }
  160. }
  161. }
  162. #endif