SecT239K1Curve.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Multiplier;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
  8. {
  9. internal class SecT239K1Curve
  10. : AbstractF2mCurve
  11. {
  12. private const int SECT239K1_DEFAULT_COORDS = COORD_LAMBDA_PROJECTIVE;
  13. private const int SECT239K1_FE_LONGS = 4;
  14. private static readonly ECFieldElement[] SECT239K1_AFFINE_ZS = new ECFieldElement[] { new SecT239FieldElement(BigInteger.One) };
  15. protected readonly SecT239K1Point m_infinity;
  16. public SecT239K1Curve()
  17. : base(239, 158, 0, 0)
  18. {
  19. this.m_infinity = new SecT239K1Point(this, null, null);
  20. this.m_a = FromBigInteger(BigInteger.Zero);
  21. this.m_b = FromBigInteger(BigInteger.One);
  22. this.m_order = new BigInteger(1, Hex.DecodeStrict("2000000000000000000000000000005A79FEC67CB6E91F1C1DA800E478A5"));
  23. this.m_cofactor = BigInteger.ValueOf(4);
  24. this.m_coord = SECT239K1_DEFAULT_COORDS;
  25. }
  26. protected override ECCurve CloneCurve()
  27. {
  28. return new SecT239K1Curve();
  29. }
  30. public override bool SupportsCoordinateSystem(int coord)
  31. {
  32. switch (coord)
  33. {
  34. case COORD_LAMBDA_PROJECTIVE:
  35. return true;
  36. default:
  37. return false;
  38. }
  39. }
  40. protected override ECMultiplier CreateDefaultMultiplier()
  41. {
  42. return new WTauNafMultiplier();
  43. }
  44. public override ECPoint Infinity
  45. {
  46. get { return m_infinity; }
  47. }
  48. public override int FieldSize
  49. {
  50. get { return 239; }
  51. }
  52. public override ECFieldElement FromBigInteger(BigInteger x)
  53. {
  54. return new SecT239FieldElement(x);
  55. }
  56. protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, bool withCompression)
  57. {
  58. return new SecT239K1Point(this, x, y, withCompression);
  59. }
  60. protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression)
  61. {
  62. return new SecT239K1Point(this, x, y, zs, withCompression);
  63. }
  64. public override bool IsKoblitz
  65. {
  66. get { return true; }
  67. }
  68. public virtual int M
  69. {
  70. get { return 239; }
  71. }
  72. public virtual bool IsTrinomial
  73. {
  74. get { return true; }
  75. }
  76. public virtual int K1
  77. {
  78. get { return 158; }
  79. }
  80. public virtual int K2
  81. {
  82. get { return 0; }
  83. }
  84. public virtual int K3
  85. {
  86. get { return 0; }
  87. }
  88. public override ECLookupTable CreateCacheSafeLookupTable(ECPoint[] points, int off, int len)
  89. {
  90. ulong[] table = new ulong[len * SECT239K1_FE_LONGS * 2];
  91. {
  92. int pos = 0;
  93. for (int i = 0; i < len; ++i)
  94. {
  95. ECPoint p = points[off + i];
  96. Nat256.Copy64(((SecT239FieldElement)p.RawXCoord).x, 0, table, pos); pos += SECT239K1_FE_LONGS;
  97. Nat256.Copy64(((SecT239FieldElement)p.RawYCoord).x, 0, table, pos); pos += SECT239K1_FE_LONGS;
  98. }
  99. }
  100. return new SecT239K1LookupTable(this, table, len);
  101. }
  102. private class SecT239K1LookupTable
  103. : AbstractECLookupTable
  104. {
  105. private readonly SecT239K1Curve m_outer;
  106. private readonly ulong[] m_table;
  107. private readonly int m_size;
  108. internal SecT239K1LookupTable(SecT239K1Curve outer, ulong[] table, int size)
  109. {
  110. this.m_outer = outer;
  111. this.m_table = table;
  112. this.m_size = size;
  113. }
  114. public override int Size
  115. {
  116. get { return m_size; }
  117. }
  118. public override ECPoint Lookup(int index)
  119. {
  120. ulong[] x = Nat256.Create64(), y = Nat256.Create64();
  121. int pos = 0;
  122. for (int i = 0; i < m_size; ++i)
  123. {
  124. ulong MASK = (ulong)(long)(((i ^ index) - 1) >> 31);
  125. for (int j = 0; j < SECT239K1_FE_LONGS; ++j)
  126. {
  127. x[j] ^= m_table[pos + j] & MASK;
  128. y[j] ^= m_table[pos + SECT239K1_FE_LONGS + j] & MASK;
  129. }
  130. pos += (SECT239K1_FE_LONGS * 2);
  131. }
  132. return CreatePoint(x, y);
  133. }
  134. public override ECPoint LookupVar(int index)
  135. {
  136. ulong[] x = Nat256.Create64(), y = Nat256.Create64();
  137. int pos = index * SECT239K1_FE_LONGS * 2;
  138. for (int j = 0; j < SECT239K1_FE_LONGS; ++j)
  139. {
  140. x[j] = m_table[pos + j];
  141. y[j] = m_table[pos + SECT239K1_FE_LONGS + j];
  142. }
  143. return CreatePoint(x, y);
  144. }
  145. private ECPoint CreatePoint(ulong[] x, ulong[] y)
  146. {
  147. return m_outer.CreateRawPoint(new SecT239FieldElement(x), new SecT239FieldElement(y), SECT239K1_AFFINE_ZS, false);
  148. }
  149. }
  150. }
  151. }
  152. #pragma warning restore
  153. #endif