SecT233K1Curve.cs 4.8 KB

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