ec2n.h 4.4 KB

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