00001 #ifndef CRYPTOPP_EC2N_H
00002 #define CRYPTOPP_EC2N_H
00003
00004 #include "gf2n.h"
00005 #include "eprecomp.h"
00006 #include "smartptr.h"
00007
00008 NAMESPACE_BEGIN(CryptoPP)
00009
00011 struct EC2NPoint
00012 {
00013 EC2NPoint() : identity(true) {}
00014 EC2NPoint(const PolynomialMod2 &x, const PolynomialMod2 &y)
00015 : identity(false), x(x), y(y) {}
00016
00017 bool operator==(const EC2NPoint &t) const
00018 {return (identity && t.identity) || (!identity && !t.identity && x==t.x && y==t.y);}
00019 bool operator< (const EC2NPoint &t) const
00020 {return identity ? !t.identity : (!t.identity && (x<t.x || (x==t.x && y<t.y)));}
00021
00022 bool identity;
00023 PolynomialMod2 x, y;
00024 };
00025
00027 class EC2N : public AbstractGroup<EC2NPoint>
00028 {
00029 public:
00030 typedef GF2NP Field;
00031 typedef Field::Element FieldElement;
00032 typedef EC2NPoint Point;
00033
00034 EC2N(const Field &field, const Field::Element &a, const Field::Element &b)
00035 : m_field(field), m_a(a), m_b(b) {}
00036
00037
00038 EC2N(BufferedTransformation &bt);
00039
00040
00041 void DEREncode(BufferedTransformation &bt) const;
00042
00043 bool Equal(const Point &P, const Point &Q) const;
00044 const Point& Zero() const {static const Point zero; return zero;}
00045 const Point& Inverse(const Point &P) const;
00046 bool InversionIsFast() const {return true;}
00047 const Point& Add(const Point &P, const Point &Q) const;
00048 const Point& Double(const Point &P) const;
00049
00050 Point Multiply(const Integer &k, const Point &P) const
00051 {return ScalarMultiply(P, k);}
00052 Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const
00053 {return CascadeScalarMultiply(P, k1, Q, k2);}
00054
00055 bool ValidateParameters(RandomNumberGenerator &rng) const;
00056 bool VerifyPoint(const Point &P) const;
00057
00058 unsigned int EncodedPointSize(bool compressed = false) const
00059 {return 1 + (compressed?1:2)*m_field->MaxElementByteLength();}
00060
00061 bool DecodePoint(Point &P, BufferedTransformation &bt, unsigned int len) const;
00062 bool DecodePoint(Point &P, const byte *encodedPoint, unsigned int len) const;
00063 void EncodePoint(byte *encodedPoint, const Point &P, bool compressed = false) const;
00064
00065 Point BERDecodePoint(BufferedTransformation &bt) const;
00066 void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed = false) const;
00067
00068 Integer FieldSize() const {return Integer::Power2(m_field->MaxElementBitLength());}
00069 const Field & GetField() const {return *m_field;}
00070 const FieldElement & GetA() const {return m_a;}
00071 const FieldElement & GetB() const {return m_b;}
00072
00073 private:
00074 clonable_ptr<Field> m_field;
00075 FieldElement m_a, m_b;
00076 mutable Point m_R;
00077 };
00078
00079 template <class T> class EcPrecomputation;
00080
00082 template<> class EcPrecomputation<EC2N>
00083 {
00084 public:
00085 EcPrecomputation() : m_ec(NULL) {}
00086 EcPrecomputation(const EcPrecomputation &a)
00087 {operator=(a);}
00088 EcPrecomputation(const EC2N &ec, const EC2N::Point &base)
00089 {SetCurveAndBase(ec, base);}
00090
00091 EcPrecomputation& operator=(const EcPrecomputation &rhs);
00092
00093 void SetCurveAndBase(const EC2N &ec, const EC2N::Point &base);
00094 void Precompute(unsigned int maxExpBits, unsigned int storage);
00095 void Load(BufferedTransformation &storedPrecomputation);
00096 void Save(BufferedTransformation &storedPrecomputation) const;
00097
00098 EC2N::Point Multiply(const Integer &exponent) const;
00099 EC2N::Point CascadeMultiply(const Integer &exponent, const EcPrecomputation<EC2N> &pc2, const Integer &exponent2) const;
00100
00101 private:
00102 value_ptr<EC2N> m_ec;
00103 ExponentiationPrecomputation<EC2N::Point> m_ep;
00104 };
00105
00106 NAMESPACE_END
00107
00108 #endif