SecT571R1Curve.cs 5.8 KB

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