Curve25519.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Math.Raw;
  4. using Org.BouncyCastle.Utilities.Encoders;
  5. namespace Org.BouncyCastle.Math.EC.Custom.Djb
  6. {
  7. internal class Curve25519
  8. : AbstractFpCurve
  9. {
  10. public static readonly BigInteger q = Nat256.ToBigInteger(Curve25519Field.P);
  11. private const int Curve25519_DEFAULT_COORDS = COORD_JACOBIAN_MODIFIED;
  12. protected readonly Curve25519Point m_infinity;
  13. public Curve25519()
  14. : base(q)
  15. {
  16. this.m_infinity = new Curve25519Point(this, null, null);
  17. this.m_a = FromBigInteger(new BigInteger(1,
  18. Hex.Decode("2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA984914A144")));
  19. this.m_b = FromBigInteger(new BigInteger(1,
  20. Hex.Decode("7B425ED097B425ED097B425ED097B425ED097B425ED097B4260B5E9C7710C864")));
  21. this.m_order = new BigInteger(1, Hex.Decode("1000000000000000000000000000000014DEF9DEA2F79CD65812631A5CF5D3ED"));
  22. this.m_cofactor = BigInteger.ValueOf(8);
  23. this.m_coord = Curve25519_DEFAULT_COORDS;
  24. }
  25. protected override ECCurve CloneCurve()
  26. {
  27. return new Curve25519();
  28. }
  29. public override bool SupportsCoordinateSystem(int coord)
  30. {
  31. switch (coord)
  32. {
  33. case COORD_JACOBIAN_MODIFIED:
  34. return true;
  35. default:
  36. return false;
  37. }
  38. }
  39. public virtual BigInteger Q
  40. {
  41. get { return q; }
  42. }
  43. public override ECPoint Infinity
  44. {
  45. get { return m_infinity; }
  46. }
  47. public override int FieldSize
  48. {
  49. get { return q.BitLength; }
  50. }
  51. public override ECFieldElement FromBigInteger(BigInteger x)
  52. {
  53. return new Curve25519FieldElement(x);
  54. }
  55. protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, bool withCompression)
  56. {
  57. return new Curve25519Point(this, x, y, withCompression);
  58. }
  59. protected internal override ECPoint CreateRawPoint(ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression)
  60. {
  61. return new Curve25519Point(this, x, y, zs, withCompression);
  62. }
  63. }
  64. }
  65. #endif