SecP256K1Curve.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.Security;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
  8. {
  9. internal class SecP256K1Curve
  10. : AbstractFpCurve
  11. {
  12. public static readonly BigInteger q = SecP256K1FieldElement.Q;
  13. private const int SECP256K1_DEFAULT_COORDS = COORD_JACOBIAN;
  14. private const int SECP256K1_FE_INTS = 8;
  15. private static readonly ECFieldElement[] SECP256K1_AFFINE_ZS = new ECFieldElement[] { new SecP256K1FieldElement(BigInteger.One) };
  16. protected readonly SecP256K1Point m_infinity;
  17. public SecP256K1Curve()
  18. : base(q)
  19. {
  20. this.m_infinity = new SecP256K1Point(this, null, null);
  21. this.m_a = FromBigInteger(BigInteger.Zero);
  22. this.m_b = FromBigInteger(BigInteger.ValueOf(7));
  23. this.m_order = new BigInteger(1, Hex.DecodeStrict("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141"));
  24. this.m_cofactor = BigInteger.One;
  25. this.m_coord = SECP256K1_DEFAULT_COORDS;
  26. }
  27. protected override ECCurve CloneCurve()
  28. {
  29. return new SecP256K1Curve();
  30. }
  31. public override bool SupportsCoordinateSystem(int coord)
  32. {
  33. switch (coord)
  34. {
  35. case COORD_JACOBIAN:
  36. return true;
  37. default:
  38. return false;
  39. }
  40. }
  41. public virtual BigInteger Q
  42. {
  43. get { return q; }
  44. }
  45. public override ECPoint Infinity
  46. {
  47. get { return m_infinity; }
  48. }
  49. public override int FieldSize
  50. {
  51. get { return q.BitLength; }
  52. }
  53. public override ECFieldElement FromBigInteger(BigInteger x)
  54. {
  55. return new SecP256K1FieldElement(x);
  56. }
  57. protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, bool withCompression)
  58. {
  59. return new SecP256K1Point(this, x, y, withCompression);
  60. }
  61. protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression)
  62. {
  63. return new SecP256K1Point(this, x, y, zs, withCompression);
  64. }
  65. public override ECLookupTable CreateCacheSafeLookupTable(ECPoint[] points, int off, int len)
  66. {
  67. uint[] table = new uint[len * SECP256K1_FE_INTS * 2];
  68. {
  69. int pos = 0;
  70. for (int i = 0; i < len; ++i)
  71. {
  72. ECPoint p = points[off + i];
  73. Nat256.Copy(((SecP256K1FieldElement)p.RawXCoord).x, 0, table, pos); pos += SECP256K1_FE_INTS;
  74. Nat256.Copy(((SecP256K1FieldElement)p.RawYCoord).x, 0, table, pos); pos += SECP256K1_FE_INTS;
  75. }
  76. }
  77. return new SecP256K1LookupTable(this, table, len);
  78. }
  79. public override ECFieldElement RandomFieldElement(SecureRandom r)
  80. {
  81. uint[] x = Nat256.Create();
  82. SecP256K1Field.Random(r, x);
  83. return new SecP256K1FieldElement(x);
  84. }
  85. public override ECFieldElement RandomFieldElementMult(SecureRandom r)
  86. {
  87. uint[] x = Nat256.Create();
  88. SecP256K1Field.RandomMult(r, x);
  89. return new SecP256K1FieldElement(x);
  90. }
  91. private class SecP256K1LookupTable
  92. : AbstractECLookupTable
  93. {
  94. private readonly SecP256K1Curve m_outer;
  95. private readonly uint[] m_table;
  96. private readonly int m_size;
  97. internal SecP256K1LookupTable(SecP256K1Curve outer, uint[] table, int size)
  98. {
  99. this.m_outer = outer;
  100. this.m_table = table;
  101. this.m_size = size;
  102. }
  103. public override int Size
  104. {
  105. get { return m_size; }
  106. }
  107. public override ECPoint Lookup(int index)
  108. {
  109. uint[] x = Nat256.Create(), y = Nat256.Create();
  110. int pos = 0;
  111. for (int i = 0; i < m_size; ++i)
  112. {
  113. uint MASK = (uint)(((i ^ index) - 1) >> 31);
  114. for (int j = 0; j < SECP256K1_FE_INTS; ++j)
  115. {
  116. x[j] ^= m_table[pos + j] & MASK;
  117. y[j] ^= m_table[pos + SECP256K1_FE_INTS + j] & MASK;
  118. }
  119. pos += (SECP256K1_FE_INTS * 2);
  120. }
  121. return CreatePoint(x, y);
  122. }
  123. public override ECPoint LookupVar(int index)
  124. {
  125. uint[] x = Nat256.Create(), y = Nat256.Create();
  126. int pos = index * SECP256K1_FE_INTS * 2;
  127. for (int j = 0; j < SECP256K1_FE_INTS; ++j)
  128. {
  129. x[j] = m_table[pos + j];
  130. y[j] = m_table[pos + SECP256K1_FE_INTS + j];
  131. }
  132. return CreatePoint(x, y);
  133. }
  134. private ECPoint CreatePoint(uint[] x, uint[] y)
  135. {
  136. return m_outer.CreateRawPoint(new SecP256K1FieldElement(x), new SecP256K1FieldElement(y), SECP256K1_AFFINE_ZS, false);
  137. }
  138. }
  139. }
  140. }
  141. #pragma warning restore
  142. #endif