Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:01

0001 // ec2n.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file ec2n.h

0004 /// \brief Classes for Elliptic Curves over binary fields

0005 
0006 #ifndef CRYPTOPP_EC2N_H
0007 #define CRYPTOPP_EC2N_H
0008 
0009 #include "cryptlib.h"
0010 #include "gf2n.h"
0011 #include "integer.h"
0012 #include "algebra.h"
0013 #include "ecpoint.h"
0014 #include "eprecomp.h"
0015 #include "smartptr.h"
0016 #include "pubkey.h"
0017 
0018 #if CRYPTOPP_MSC_VERSION
0019 # pragma warning(push)
0020 # pragma warning(disable: 4231 4275)
0021 #endif
0022 
0023 NAMESPACE_BEGIN(CryptoPP)
0024 
0025 /// \brief Elliptic Curve over GF(2^n)

0026 class CRYPTOPP_DLL EC2N : public AbstractGroup<EC2NPoint>, public EncodedPoint<EC2NPoint>
0027 {
0028 public:
0029     typedef GF2NP Field;
0030     typedef Field::Element FieldElement;
0031     typedef EC2NPoint Point;
0032 
0033     virtual ~EC2N() {}
0034 
0035     /// \brief Construct an EC2N

0036     EC2N() {}
0037 
0038     /// \brief Construct an EC2N

0039     /// \param field Field, GF2NP derived class

0040     /// \param a Field::Element

0041     /// \param b Field::Element

0042     EC2N(const Field &field, const Field::Element &a, const Field::Element &b)
0043         : m_field(field), m_a(a), m_b(b) {}
0044 
0045     /// \brief Construct an EC2N from BER encoded parameters

0046     /// \param bt BufferedTransformation derived object

0047     /// \details This constructor will decode and extract the fields fieldID and curve of the sequence ECParameters

0048     EC2N(BufferedTransformation &bt);
0049 
0050     /// \brief Encode the fields fieldID and curve of the sequence ECParameters

0051     /// \param bt BufferedTransformation derived object

0052     void DEREncode(BufferedTransformation &bt) const;
0053 
0054     bool Equal(const Point &P, const Point &Q) const;
0055     const Point& Identity() const;
0056     const Point& Inverse(const Point &P) const;
0057     bool InversionIsFast() const {return true;}
0058     const Point& Add(const Point &P, const Point &Q) const;
0059     const Point& Double(const Point &P) const;
0060 
0061     Point Multiply(const Integer &k, const Point &P) const
0062         {return ScalarMultiply(P, k);}
0063     Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const
0064         {return CascadeScalarMultiply(P, k1, Q, k2);}
0065 
0066     bool ValidateParameters(RandomNumberGenerator &rng, unsigned int level=3) const;
0067     bool VerifyPoint(const Point &P) const;
0068 
0069     unsigned int EncodedPointSize(bool compressed = false) const
0070         {return 1 + (compressed?1:2)*m_field->MaxElementByteLength();}
0071     // returns false if point is compressed and not valid (doesn't check if uncompressed)

0072     bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const;
0073     bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const;
0074     void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const;
0075     void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
0076 
0077     Point BERDecodePoint(BufferedTransformation &bt) const;
0078     void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
0079 
0080     Integer FieldSize() const {return Integer::Power2(m_field->MaxElementBitLength());}
0081     const Field & GetField() const {return *m_field;}
0082     const FieldElement & GetA() const {return m_a;}
0083     const FieldElement & GetB() const {return m_b;}
0084 
0085     bool operator==(const EC2N &rhs) const
0086         {return GetField() == rhs.GetField() && m_a == rhs.m_a && m_b == rhs.m_b;}
0087 
0088 private:
0089     clonable_ptr<Field> m_field;
0090     FieldElement m_a, m_b;
0091     mutable Point m_R;
0092 };
0093 
0094 CRYPTOPP_DLL_TEMPLATE_CLASS DL_FixedBasePrecomputationImpl<EC2N::Point>;
0095 CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupPrecomputation<EC2N::Point>;
0096 
0097 /// \brief Elliptic Curve precomputation

0098 /// \tparam EC elliptic curve field

0099 template <class EC> class EcPrecomputation;
0100 
0101 /// \brief EC2N precomputation specialization

0102 /// \details Implementation of <tt>DL_GroupPrecomputation<EC2N::Point></tt>

0103 /// \sa DL_GroupPrecomputation

0104 template<> class EcPrecomputation<EC2N> : public DL_GroupPrecomputation<EC2N::Point>
0105 {
0106 public:
0107     typedef EC2N EllipticCurve;
0108 
0109     virtual ~EcPrecomputation() {}
0110 
0111     // DL_GroupPrecomputation

0112     const AbstractGroup<Element> & GetGroup() const {return m_ec;}
0113     Element BERDecodeElement(BufferedTransformation &bt) const {return m_ec.BERDecodePoint(bt);}
0114     void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {m_ec.DEREncodePoint(bt, v, false);}
0115 
0116     /// \brief Set the elliptic curve

0117     /// \param ec ECP derived class

0118     /// \details SetCurve() is not inherited

0119     void SetCurve(const EC2N &ec) {m_ec = ec;}
0120 
0121     /// \brief Get the elliptic curve

0122     /// \return EC2N curve

0123     /// \details GetCurve() is not inherited

0124     const EC2N & GetCurve() const {return m_ec;}
0125 
0126 private:
0127     EC2N m_ec;
0128 };
0129 
0130 NAMESPACE_END
0131 
0132 #if CRYPTOPP_MSC_VERSION
0133 # pragma warning(pop)
0134 #endif
0135 
0136 #endif