SecP224K1Curve.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
  7. {
  8. internal class SecP224K1Curve
  9. : AbstractFpCurve
  10. {
  11. public static readonly BigInteger q = new BigInteger(1,
  12. Hex.Decode("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFE56D"));
  13. private const int SECP224K1_DEFAULT_COORDS = COORD_JACOBIAN;
  14. private const int SECP224K1_FE_INTS = 7;
  15. protected readonly SecP224K1Point m_infinity;
  16. public SecP224K1Curve()
  17. : base(q)
  18. {
  19. this.m_infinity = new SecP224K1Point(this, null, null);
  20. this.m_a = FromBigInteger(BigInteger.Zero);
  21. this.m_b = FromBigInteger(BigInteger.ValueOf(5));
  22. this.m_order = new BigInteger(1, Hex.Decode("010000000000000000000000000001DCE8D2EC6184CAF0A971769FB1F7"));
  23. this.m_cofactor = BigInteger.One;
  24. this.m_coord = SECP224K1_DEFAULT_COORDS;
  25. }
  26. protected override ECCurve CloneCurve()
  27. {
  28. return new SecP224K1Curve();
  29. }
  30. public override bool SupportsCoordinateSystem(int coord)
  31. {
  32. switch (coord)
  33. {
  34. case COORD_JACOBIAN:
  35. return true;
  36. default:
  37. return false;
  38. }
  39. }
  40. public virtual BigInteger Q
  41. {
  42. get { return q; }
  43. }
  44. public override ECPoint Infinity
  45. {
  46. get { return m_infinity; }
  47. }
  48. public override int FieldSize
  49. {
  50. get { return q.BitLength; }
  51. }
  52. public override ECFieldElement FromBigInteger(BigInteger x)
  53. {
  54. return new SecP224K1FieldElement(x);
  55. }
  56. protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, bool withCompression)
  57. {
  58. return new SecP224K1Point(this, x, y, withCompression);
  59. }
  60. protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression)
  61. {
  62. return new SecP224K1Point(this, x, y, zs, withCompression);
  63. }
  64. public override ECLookupTable CreateCacheSafeLookupTable(ECPoint[] points, int off, int len)
  65. {
  66. uint[] table = new uint[len * SECP224K1_FE_INTS * 2];
  67. {
  68. int pos = 0;
  69. for (int i = 0; i < len; ++i)
  70. {
  71. ECPoint p = points[off + i];
  72. Nat224.Copy(((SecP224K1FieldElement)p.RawXCoord).x, 0, table, pos); pos += SECP224K1_FE_INTS;
  73. Nat224.Copy(((SecP224K1FieldElement)p.RawYCoord).x, 0, table, pos); pos += SECP224K1_FE_INTS;
  74. }
  75. }
  76. return new SecP224K1LookupTable(this, table, len);
  77. }
  78. private class SecP224K1LookupTable
  79. : ECLookupTable
  80. {
  81. private readonly SecP224K1Curve m_outer;
  82. private readonly uint[] m_table;
  83. private readonly int m_size;
  84. internal SecP224K1LookupTable(SecP224K1Curve outer, uint[] table, int size)
  85. {
  86. this.m_outer = outer;
  87. this.m_table = table;
  88. this.m_size = size;
  89. }
  90. public virtual int Size
  91. {
  92. get { return m_size; }
  93. }
  94. public virtual ECPoint Lookup(int index)
  95. {
  96. uint[] x = Nat224.Create(), y = Nat224.Create();
  97. int pos = 0;
  98. for (int i = 0; i < m_size; ++i)
  99. {
  100. uint MASK = (uint)(((i ^ index) - 1) >> 31);
  101. for (int j = 0; j < SECP224K1_FE_INTS; ++j)
  102. {
  103. x[j] ^= m_table[pos + j] & MASK;
  104. y[j] ^= m_table[pos + SECP224K1_FE_INTS + j] & MASK;
  105. }
  106. pos += (SECP224K1_FE_INTS * 2);
  107. }
  108. return m_outer.CreateRawPoint(new SecP224K1FieldElement(x), new SecP224K1FieldElement(y), false);
  109. }
  110. }
  111. }
  112. }
  113. #pragma warning restore
  114. #endif