SecT571R1Curve.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 SecT571R1Curve
  9. : AbstractF2mCurve
  10. {
  11. private const int SECT571R1_DEFAULT_COORDS = COORD_LAMBDA_PROJECTIVE;
  12. private const int SECT571R1_FE_LONGS = 9;
  13. protected readonly SecT571R1Point m_infinity;
  14. internal static readonly SecT571FieldElement SecT571R1_B = new SecT571FieldElement(
  15. new BigInteger(1, Hex.Decode("02F40E7E2221F295DE297117B7F3D62F5C6A97FFCB8CEFF1CD6BA8CE4A9A18AD84FFABBD8EFA59332BE7AD6756A66E294AFD185A78FF12AA520E4DE739BACA0C7FFEFF7F2955727A")));
  16. internal static readonly SecT571FieldElement SecT571R1_B_SQRT = (SecT571FieldElement)SecT571R1_B.Sqrt();
  17. public SecT571R1Curve()
  18. : base(571, 2, 5, 10)
  19. {
  20. this.m_infinity = new SecT571R1Point(this, null, null);
  21. this.m_a = FromBigInteger(BigInteger.One);
  22. this.m_b = SecT571R1_B;
  23. this.m_order = new BigInteger(1, Hex.Decode("03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE661CE18FF55987308059B186823851EC7DD9CA1161DE93D5174D66E8382E9BB2FE84E47"));
  24. this.m_cofactor = BigInteger.Two;
  25. this.m_coord = SECT571R1_DEFAULT_COORDS;
  26. }
  27. protected override ECCurve CloneCurve()
  28. {
  29. return new SecT571R1Curve();
  30. }
  31. public override bool SupportsCoordinateSystem(int coord)
  32. {
  33. switch (coord)
  34. {
  35. case COORD_LAMBDA_PROJECTIVE:
  36. return true;
  37. default:
  38. return false;
  39. }
  40. }
  41. public override ECPoint Infinity
  42. {
  43. get { return m_infinity; }
  44. }
  45. public override int FieldSize
  46. {
  47. get { return 571; }
  48. }
  49. public override ECFieldElement FromBigInteger(BigInteger x)
  50. {
  51. return new SecT571FieldElement(x);
  52. }
  53. protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, bool withCompression)
  54. {
  55. return new SecT571R1Point(this, x, y, withCompression);
  56. }
  57. protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression)
  58. {
  59. return new SecT571R1Point(this, x, y, zs, withCompression);
  60. }
  61. public override bool IsKoblitz
  62. {
  63. get { return false; }
  64. }
  65. public virtual int M
  66. {
  67. get { return 571; }
  68. }
  69. public virtual bool IsTrinomial
  70. {
  71. get { return false; }
  72. }
  73. public virtual int K1
  74. {
  75. get { return 2; }
  76. }
  77. public virtual int K2
  78. {
  79. get { return 5; }
  80. }
  81. public virtual int K3
  82. {
  83. get { return 10; }
  84. }
  85. public override ECLookupTable CreateCacheSafeLookupTable(ECPoint[] points, int off, int len)
  86. {
  87. ulong[] table = new ulong[len * SECT571R1_FE_LONGS * 2];
  88. {
  89. int pos = 0;
  90. for (int i = 0; i < len; ++i)
  91. {
  92. ECPoint p = points[off + i];
  93. Nat576.Copy64(((SecT571FieldElement)p.RawXCoord).x, 0, table, pos); pos += SECT571R1_FE_LONGS;
  94. Nat576.Copy64(((SecT571FieldElement)p.RawYCoord).x, 0, table, pos); pos += SECT571R1_FE_LONGS;
  95. }
  96. }
  97. return new SecT571R1LookupTable(this, table, len);
  98. }
  99. private class SecT571R1LookupTable
  100. : ECLookupTable
  101. {
  102. private readonly SecT571R1Curve m_outer;
  103. private readonly ulong[] m_table;
  104. private readonly int m_size;
  105. internal SecT571R1LookupTable(SecT571R1Curve outer, ulong[] table, int size)
  106. {
  107. this.m_outer = outer;
  108. this.m_table = table;
  109. this.m_size = size;
  110. }
  111. public virtual int Size
  112. {
  113. get { return m_size; }
  114. }
  115. public virtual ECPoint Lookup(int index)
  116. {
  117. ulong[] x = Nat576.Create64(), y = Nat576.Create64();
  118. int pos = 0;
  119. for (int i = 0; i < m_size; ++i)
  120. {
  121. ulong MASK = (ulong)(long)(((i ^ index) - 1) >> 31);
  122. for (int j = 0; j < SECT571R1_FE_LONGS; ++j)
  123. {
  124. x[j] ^= m_table[pos + j] & MASK;
  125. y[j] ^= m_table[pos + SECT571R1_FE_LONGS + j] & MASK;
  126. }
  127. pos += (SECT571R1_FE_LONGS * 2);
  128. }
  129. return m_outer.CreateRawPoint(new SecT571FieldElement(x), new SecT571FieldElement(y), false);
  130. }
  131. }
  132. }
  133. }
  134. #pragma warning restore
  135. #endif