SecP160K1Curve.cs 4.2 KB

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