SecT131R1Curve.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 SecT131R1Curve
  9. : AbstractF2mCurve
  10. {
  11. private const int SECT131R1_DEFAULT_COORDS = COORD_LAMBDA_PROJECTIVE;
  12. private const int SECT131R1_FE_LONGS = 3;
  13. private static readonly ECFieldElement[] SECT131R1_AFFINE_ZS = new ECFieldElement[] { new SecT131FieldElement(BigInteger.One) };
  14. protected readonly SecT131R1Point m_infinity;
  15. public SecT131R1Curve()
  16. : base(131, 2, 3, 8)
  17. {
  18. this.m_infinity = new SecT131R1Point(this, null, null);
  19. this.m_a = FromBigInteger(new BigInteger(1, Hex.DecodeStrict("07A11B09A76B562144418FF3FF8C2570B8")));
  20. this.m_b = FromBigInteger(new BigInteger(1, Hex.DecodeStrict("0217C05610884B63B9C6C7291678F9D341")));
  21. this.m_order = new BigInteger(1, Hex.DecodeStrict("0400000000000000023123953A9464B54D"));
  22. this.m_cofactor = BigInteger.Two;
  23. this.m_coord = SECT131R1_DEFAULT_COORDS;
  24. }
  25. protected override ECCurve CloneCurve()
  26. {
  27. return new SecT131R1Curve();
  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. public override ECPoint Infinity
  40. {
  41. get { return m_infinity; }
  42. }
  43. public override int FieldSize
  44. {
  45. get { return 131; }
  46. }
  47. public override ECFieldElement FromBigInteger(BigInteger x)
  48. {
  49. return new SecT131FieldElement(x);
  50. }
  51. protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, bool withCompression)
  52. {
  53. return new SecT131R1Point(this, x, y, withCompression);
  54. }
  55. protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression)
  56. {
  57. return new SecT131R1Point(this, x, y, zs, withCompression);
  58. }
  59. public override bool IsKoblitz
  60. {
  61. get { return false; }
  62. }
  63. public virtual int M
  64. {
  65. get { return 131; }
  66. }
  67. public virtual bool IsTrinomial
  68. {
  69. get { return false; }
  70. }
  71. public virtual int K1
  72. {
  73. get { return 2; }
  74. }
  75. public virtual int K2
  76. {
  77. get { return 3; }
  78. }
  79. public virtual int K3
  80. {
  81. get { return 8; }
  82. }
  83. public override ECLookupTable CreateCacheSafeLookupTable(ECPoint[] points, int off, int len)
  84. {
  85. ulong[] table = new ulong[len * SECT131R1_FE_LONGS * 2];
  86. {
  87. int pos = 0;
  88. for (int i = 0; i < len; ++i)
  89. {
  90. ECPoint p = points[off + i];
  91. Nat192.Copy64(((SecT131FieldElement)p.RawXCoord).x, 0, table, pos); pos += SECT131R1_FE_LONGS;
  92. Nat192.Copy64(((SecT131FieldElement)p.RawYCoord).x, 0, table, pos); pos += SECT131R1_FE_LONGS;
  93. }
  94. }
  95. return new SecT131R1LookupTable(this, table, len);
  96. }
  97. private class SecT131R1LookupTable
  98. : AbstractECLookupTable
  99. {
  100. private readonly SecT131R1Curve m_outer;
  101. private readonly ulong[] m_table;
  102. private readonly int m_size;
  103. internal SecT131R1LookupTable(SecT131R1Curve outer, ulong[] table, int size)
  104. {
  105. this.m_outer = outer;
  106. this.m_table = table;
  107. this.m_size = size;
  108. }
  109. public override int Size
  110. {
  111. get { return m_size; }
  112. }
  113. public override ECPoint Lookup(int index)
  114. {
  115. ulong[] x = Nat192.Create64(), y = Nat192.Create64();
  116. int pos = 0;
  117. for (int i = 0; i < m_size; ++i)
  118. {
  119. ulong MASK = (ulong)(long)(((i ^ index) - 1) >> 31);
  120. for (int j = 0; j < SECT131R1_FE_LONGS; ++j)
  121. {
  122. x[j] ^= m_table[pos + j] & MASK;
  123. y[j] ^= m_table[pos + SECT131R1_FE_LONGS + j] & MASK;
  124. }
  125. pos += (SECT131R1_FE_LONGS * 2);
  126. }
  127. return CreatePoint(x, y);
  128. }
  129. public override ECPoint LookupVar(int index)
  130. {
  131. ulong[] x = Nat192.Create64(), y = Nat192.Create64();
  132. int pos = index * SECT131R1_FE_LONGS * 2;
  133. for (int j = 0; j < SECT131R1_FE_LONGS; ++j)
  134. {
  135. x[j] = m_table[pos + j];
  136. y[j] = m_table[pos + SECT131R1_FE_LONGS + j];
  137. }
  138. return CreatePoint(x, y);
  139. }
  140. private ECPoint CreatePoint(ulong[] x, ulong[] y)
  141. {
  142. return m_outer.CreateRawPoint(new SecT131FieldElement(x), new SecT131FieldElement(y), SECT131R1_AFFINE_ZS, false);
  143. }
  144. }
  145. }
  146. }
  147. #pragma warning restore
  148. #endif