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
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 #ifndef SKIP_EXPLICIT_INSTANTIATION
00028 CRYPTOPP_DLL_TEMPLATE_CLASS AbstractGroup<EC2NPoint>;
00029 #endif // SKIP_EXPLICIT_INSTANTIATION
00030
00031
00032 class CRYPTOPP_DLL EC2N : public AbstractGroup<EC2NPoint>
00033 {
00034 public:
00035 typedef GF2NP Field;
00036 typedef Field::Element FieldElement;
00037 typedef EC2NPoint Point;
00038
00039 EC2N() {}
00040 EC2N(const Field &field, const Field::Element &a, const Field::Element &b)
00041 : m_field(field), m_a(a), m_b(b) {}
00042
00043
00044 EC2N(BufferedTransformation &bt);
00045
00046
00047 void DEREncode(BufferedTransformation &bt) const;
00048
00049 bool Equal(const Point &P, const Point &Q) const;
00050 const Point& Identity() const;
00051 const Point& Inverse(const Point &P) const;
00052 bool InversionIsFast() const {return true;}
00053 const Point& Add(const Point &P, const Point &Q) const;
00054 const Point& Double(const Point &P) const;
00055
00056 Point Multiply(const Integer &k, const Point &P) const
00057 {return ScalarMultiply(P, k);}
00058 Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const
00059 {return CascadeScalarMultiply(P, k1, Q, k2);}
00060
00061 bool ValidateParameters(RandomNumberGenerator &rng, unsigned int level=3) const;
00062 bool VerifyPoint(const Point &P) const;
00063
00064 unsigned int EncodedPointSize(bool compressed = false) const
00065 {return 1 + (compressed?1:2)*m_field->MaxElementByteLength();}
00066
00067 bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const;
00068 bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const;
00069 void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const;
00070 void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
00071
00072 Point BERDecodePoint(BufferedTransformation &bt) const;
00073 void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
00074
00075 Integer FieldSize() const {return Integer::Power2(m_field->MaxElementBitLength());}
00076 const Field & GetField() const {return *m_field;}
00077 const FieldElement & GetA() const {return m_a;}
00078 const FieldElement & GetB() const {return m_b;}
00079
00080 bool operator==(const EC2N &rhs) const
00081 {return GetField() == rhs.GetField() && m_a == rhs.m_a && m_b == rhs.m_b;}
00082
00083 private:
00084 clonable_ptr<Field> m_field;
00085 FieldElement m_a, m_b;
00086 mutable Point m_R;
00087 };
00088
00089 #ifndef SKIP_EXPLICIT_INSTANTIATION
00090 CRYPTOPP_DLL_TEMPLATE_CLASS DL_FixedBasePrecomputationImpl<EC2N::Point>;
00091 CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupPrecomputation<EC2N::Point>;
00092 #endif
00093
00094 template <class T> class EcPrecomputation;
00095
00096
00097 template<> class EcPrecomputation<EC2N> : public DL_GroupPrecomputation<EC2N::Point>
00098 {
00099 public:
00100 typedef EC2N EllipticCurve;
00101
00102
00103 const AbstractGroup<Element> & GetGroup() const {return m_ec;}
00104 Element BERDecodeElement(BufferedTransformation &bt) const {return m_ec.BERDecodePoint(bt);}
00105 void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {m_ec.DEREncodePoint(bt, v, false);}
00106
00107
00108 void SetCurve(const EC2N &ec) {m_ec = ec;}
00109 const EC2N & GetCurve() const {return m_ec;}
00110
00111 private:
00112 EC2N m_ec;
00113 };
00114
00115 NAMESPACE_END
00116
00117 #endif