Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.2 KiB

  1. // ec2n.h - written and placed in the public domain by Wei Dai
  2. //! \file
  3. //! \headerfile ec2n.h
  4. //! \brief Classes for Elliptic Curves over binary fields
  5. #ifndef CRYPTOPP_EC2N_H
  6. #define CRYPTOPP_EC2N_H
  7. #include "cryptlib.h"
  8. #include "gf2n.h"
  9. #include "integer.h"
  10. #include "eprecomp.h"
  11. #include "smartptr.h"
  12. #include "pubkey.h"
  13. NAMESPACE_BEGIN(CryptoPP)
  14. //! Elliptic Curve Point
  15. struct CRYPTOPP_DLL EC2NPoint
  16. {
  17. EC2NPoint() : identity(true) {}
  18. EC2NPoint(const PolynomialMod2 &x, const PolynomialMod2 &y)
  19. : identity(false), x(x), y(y) {}
  20. bool operator==(const EC2NPoint &t) const
  21. {return (identity && t.identity) || (!identity && !t.identity && x==t.x && y==t.y);}
  22. bool operator< (const EC2NPoint &t) const
  23. {return identity ? !t.identity : (!t.identity && (x<t.x || (x==t.x && y<t.y)));}
  24. #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
  25. virtual ~EC2NPoint() {}
  26. #endif
  27. bool identity;
  28. PolynomialMod2 x, y;
  29. };
  30. CRYPTOPP_DLL_TEMPLATE_CLASS AbstractGroup<EC2NPoint>;
  31. //! Elliptic Curve over GF(2^n)
  32. class CRYPTOPP_DLL EC2N : public AbstractGroup<EC2NPoint>
  33. {
  34. public:
  35. typedef GF2NP Field;
  36. typedef Field::Element FieldElement;
  37. typedef EC2NPoint Point;
  38. EC2N() {}
  39. EC2N(const Field &field, const Field::Element &a, const Field::Element &b)
  40. : m_field(field), m_a(a), m_b(b) {}
  41. // construct from BER encoded parameters
  42. // this constructor will decode and extract the the fields fieldID and curve of the sequence ECParameters
  43. EC2N(BufferedTransformation &bt);
  44. // encode the fields fieldID and curve of the sequence ECParameters
  45. void DEREncode(BufferedTransformation &bt) const;
  46. bool Equal(const Point &P, const Point &Q) const;
  47. const Point& Identity() const;
  48. const Point& Inverse(const Point &P) const;
  49. bool InversionIsFast() const {return true;}
  50. const Point& Add(const Point &P, const Point &Q) const;
  51. const Point& Double(const Point &P) const;
  52. Point Multiply(const Integer &k, const Point &P) const
  53. {return ScalarMultiply(P, k);}
  54. Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const
  55. {return CascadeScalarMultiply(P, k1, Q, k2);}
  56. bool ValidateParameters(RandomNumberGenerator &rng, unsigned int level=3) const;
  57. bool VerifyPoint(const Point &P) const;
  58. unsigned int EncodedPointSize(bool compressed = false) const
  59. {return 1 + (compressed?1:2)*m_field->MaxElementByteLength();}
  60. // returns false if point is compressed and not valid (doesn't check if uncompressed)
  61. bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const;
  62. bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const;
  63. void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const;
  64. void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
  65. Point BERDecodePoint(BufferedTransformation &bt) const;
  66. void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
  67. Integer FieldSize() const {return Integer::Power2(m_field->MaxElementBitLength());}
  68. const Field & GetField() const {return *m_field;}
  69. const FieldElement & GetA() const {return m_a;}
  70. const FieldElement & GetB() const {return m_b;}
  71. bool operator==(const EC2N &rhs) const
  72. {return GetField() == rhs.GetField() && m_a == rhs.m_a && m_b == rhs.m_b;}
  73. #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
  74. virtual ~EC2N() {}
  75. #endif
  76. private:
  77. clonable_ptr<Field> m_field;
  78. FieldElement m_a, m_b;
  79. mutable Point m_R;
  80. };
  81. CRYPTOPP_DLL_TEMPLATE_CLASS DL_FixedBasePrecomputationImpl<EC2N::Point>;
  82. CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupPrecomputation<EC2N::Point>;
  83. template <class T> class EcPrecomputation;
  84. //! EC2N precomputation
  85. template<> class EcPrecomputation<EC2N> : public DL_GroupPrecomputation<EC2N::Point>
  86. {
  87. public:
  88. typedef EC2N EllipticCurve;
  89. // DL_GroupPrecomputation
  90. const AbstractGroup<Element> & GetGroup() const {return m_ec;}
  91. Element BERDecodeElement(BufferedTransformation &bt) const {return m_ec.BERDecodePoint(bt);}
  92. void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {m_ec.DEREncodePoint(bt, v, false);}
  93. // non-inherited
  94. void SetCurve(const EC2N &ec) {m_ec = ec;}
  95. const EC2N & GetCurve() const {return m_ec;}
  96. #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
  97. virtual ~EcPrecomputation() {}
  98. #endif
  99. private:
  100. EC2N m_ec;
  101. };
  102. NAMESPACE_END
  103. #endif