Crypto++

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

ec2n.h

    00001 #ifndef CRYPTOPP_EC2N_H
    00002 #define CRYPTOPP_EC2N_H
    00003
    00004 #include "gf2n.h"
    00005 #include "eprecomp.h"
    00006 #include "smartptr.h"
    00007 #include "pubkey.h"
    00008
    00009 NAMESPACE_BEGIN(CryptoPP)
    00010
    00011 //! Elliptic Curve Point
    00012 struct CRYPTOPP_DLL EC2NPoint
    00013 {
    00014     EC2NPoint() : identity(true) {}
    00015     EC2NPoint(const PolynomialMod2 &x, const PolynomialMod2 &y)
    00016         : identity(false), x(x), y(y) {}
    00017
    00018     bool operator==(const EC2NPoint &t) const
    00019         {return (identity && t.identity) || (!identity && !t.identity && x==t.x && y==t.y);}
    00020     bool operator< (const EC2NPoint &t) const
    00021         {return identity ? !t.identity : (!t.identity && (x<t.x || (x==t.x && y<t.y)));}
    00022
    00023     bool identity;
    00024     PolynomialMod2 x, y;
    00025 };
    00026
    00027 CRYPTOPP_DLL_TEMPLATE_CLASS AbstractGroup<EC2NPoint>;
    00028
    00029 //! Elliptic Curve over GF(2^n)
    00030 class CRYPTOPP_DLL EC2N : public AbstractGroup<EC2NPoint>
    00031 {
    00032 public:
    00033     typedef GF2NP Field;
    00034     typedef Field::Element FieldElement;
    00035     typedef EC2NPoint Point;
    00036
    00037     EC2N() {}
    00038     EC2N(const Field &field, const Field::Element &a, const Field::Element &b)
    00039         : m_field(field), m_a(a), m_b(b) {}
    00040     // construct from BER encoded parameters
    00041     // this constructor will decode and extract the the fields fieldID and curve of the sequence ECParameters
    00042     EC2N(BufferedTransformation &bt);
    00043
    00044     // encode the fields fieldID and curve of the sequence ECParameters
    00045     void DEREncode(BufferedTransformation &bt) const;
    00046
    00047     bool Equal(const Point &P, const Point &Q) const;
    00048     const Point& Identity() const;
    00049     const Point& Inverse(const Point &P) const;
    00050     bool InversionIsFast() const {return true;}
    00051     const Point& Add(const Point &P, const Point &Q) const;
    00052     const Point& Double(const Point &P) const;
    00053
    00054     Point Multiply(const Integer &k, const Point &P) const
    00055         {return ScalarMultiply(P, k);}
    00056     Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const
    00057         {return CascadeScalarMultiply(P, k1, Q, k2);}
    00058
    00059     bool ValidateParameters(RandomNumberGenerator &rng, unsigned int level=3) const;
    00060     bool VerifyPoint(const Point &P) const;
    00061
    00062     unsigned int EncodedPointSize(bool compressed = false) const
    00063         {return 1 + (compressed?1:2)*m_field->MaxElementByteLength();}
    00064     // returns false if point is compressed and not valid (doesn't check if uncompressed)
    00065     bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const;
    00066     bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const;
    00067     void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const;
    00068     void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
    00069
    00070     Point BERDecodePoint(BufferedTransformation &bt) const;
    00071     void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
    00072
    00073     Integer FieldSize() const {return Integer::Power2(m_field->MaxElementBitLength());}
    00074     const Field & GetField() const {return *m_field;}
    00075     const FieldElement & GetA() const {return m_a;}
    00076     const FieldElement & GetB() const {return m_b;}
    00077
    00078     bool operator==(const EC2N &rhs) const
    00079         {return GetField() == rhs.GetField() && m_a == rhs.m_a && m_b == rhs.m_b;}
    00080
    00081 private:
    00082     clonable_ptr<Field> m_field;
    00083     FieldElement m_a, m_b;
    00084     mutable Point m_R;
    00085 };
    00086
    00087 CRYPTOPP_DLL_TEMPLATE_CLASS DL_FixedBasePrecomputationImpl<EC2N::Point>;
    00088 CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupPrecomputation<EC2N::Point>;
    00089
    00090 template <class T> class EcPrecomputation;
    00091
    00092 //! EC2N precomputation
    00093 template<> class EcPrecomputation<EC2N> : public DL_GroupPrecomputation<EC2N::Point>
    00094 {
    00095 public:
    00096     typedef EC2N EllipticCurve;
    00097
    00098     // DL_GroupPrecomputation
    00099     const AbstractGroup<Element> & GetGroup() const {return m_ec;}
    00100     Element BERDecodeElement(BufferedTransformation &bt) const {return m_ec.BERDecodePoint(bt);}
    00101     void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {m_ec.DEREncodePoint(bt, v, false);}
    00102
    00103     // non-inherited
    00104     void SetCurve(const EC2N &ec) {m_ec = ec;}
    00105     const EC2N & GetCurve() const {return m_ec;}
    00106
    00107 private:
    00108     EC2N m_ec;
    00109 };
    00110
    00111 NAMESPACE_END
    00112
    00113 #endif