Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | 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 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 //! Elliptic Curve over GF(2^n)
00028 class EC2N : public AbstractGroup<EC2NPoint>
00029 {
00030 public:
00031         typedef GF2NP Field;
00032         typedef Field::Element FieldElement;
00033         typedef EC2NPoint Point;
00034 
00035         EC2N() {}
00036         EC2N(const Field &field, const Field::Element &a, const Field::Element &b)
00037                 : m_field(field), m_a(a), m_b(b) {}
00038         // construct from BER encoded parameters
00039         // this constructor will decode and extract the the fields fieldID and curve of the sequence ECParameters
00040         EC2N(BufferedTransformation &bt);
00041 
00042         // encode the fields fieldID and curve of the sequence ECParameters
00043         void DEREncode(BufferedTransformation &bt) const;
00044 
00045         bool Equal(const Point &P, const Point &Q) const;
00046         const Point& Identity() const;
00047         const Point& Inverse(const Point &P) const;
00048         bool InversionIsFast() const {return true;}
00049         const Point& Add(const Point &P, const Point &Q) const;
00050         const Point& Double(const Point &P) const;
00051 
00052         Point Multiply(const Integer &k, const Point &P) const
00053                 {return ScalarMultiply(P, k);}
00054         Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const
00055                 {return CascadeScalarMultiply(P, k1, Q, k2);}
00056 
00057         bool ValidateParameters(RandomNumberGenerator &rng, unsigned int level=3) const;
00058         bool VerifyPoint(const Point &P) const;
00059 
00060         unsigned int EncodedPointSize(bool compressed = false) const
00061                 {return 1 + (compressed?1:2)*m_field->MaxElementByteLength();}
00062         // returns false if point is compressed and not valid (doesn't check if uncompressed)
00063         bool DecodePoint(Point &P, BufferedTransformation &bt, unsigned int len) const;
00064         bool DecodePoint(Point &P, const byte *encodedPoint, unsigned int len) const;
00065         void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const;
00066         void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
00067 
00068         Point BERDecodePoint(BufferedTransformation &bt) const;
00069         void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
00070 
00071         Integer FieldSize() const {return Integer::Power2(m_field->MaxElementBitLength());}
00072         const Field & GetField() const {return *m_field;}
00073         const FieldElement & GetA() const {return m_a;}
00074         const FieldElement & GetB() const {return m_b;}
00075 
00076 private:
00077         clonable_ptr<Field> m_field;
00078         FieldElement m_a, m_b;
00079         mutable Point m_R;
00080 };
00081 
00082 template <class T> class EcPrecomputation;
00083 
00084 //! .
00085 template<> class EcPrecomputation<EC2N> : public DL_GroupPrecomputation<EC2N::Point>
00086 {
00087 public:
00088         typedef EC2N EllipticCurve;
00089 
00090         // DL_GroupPrecomputation
00091         const AbstractGroup<Element> & GetGroup() const {return m_ec;}
00092         Element BERDecodeElement(BufferedTransformation &bt) const {return m_ec.BERDecodePoint(bt);}
00093         void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {m_ec.DEREncodePoint(bt, v, false);}
00094 
00095         // non-inherited
00096         void SetCurve(const EC2N &ec) {m_ec = ec;}
00097         const EC2N & GetCurve() const {return m_ec;}
00098 
00099 private:
00100         EC2N m_ec;
00101 };
00102 
00103 NAMESPACE_END
00104 
00105 #endif

Generated on Sun Mar 14 20:44:25 2004 for Crypto++ by doxygen 1.3.6-20040222