00001
00002
00003
00004
00005
00006
00007
00008 #ifndef CRYPTOPP_EC2N_H
00009 #define CRYPTOPP_EC2N_H
00010
00011 #include "cryptlib.h"
00012 #include "gf2n.h"
00013 #include "integer.h"
00014 #include "eprecomp.h"
00015 #include "smartptr.h"
00016 #include "pubkey.h"
00017
00018 NAMESPACE_BEGIN(CryptoPP)
00019
00020
00021 struct CRYPTOPP_DLL EC2NPoint
00022 {
00023 EC2NPoint() : identity(true) {}
00024 EC2NPoint(const PolynomialMod2 &x, const PolynomialMod2 &y)
00025 : identity(false), x(x), y(y) {}
00026
00027 bool operator==(const EC2NPoint &t) const
00028 {return (identity && t.identity) || (!identity && !t.identity && x==t.x && y==t.y);}
00029 bool operator< (const EC2NPoint &t) const
00030 {return identity ? !t.identity : (!t.identity && (x<t.x || (x==t.x && y<t.y)));}
00031
00032 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
00033 virtual ~EC2NPoint() {}
00034 #endif
00035
00036 bool identity;
00037 PolynomialMod2 x, y;
00038 };
00039
00040 CRYPTOPP_DLL_TEMPLATE_CLASS AbstractGroup<EC2NPoint>;
00041
00042
00043 class CRYPTOPP_DLL EC2N : public AbstractGroup<EC2NPoint>
00044 {
00045 public:
00046 typedef GF2NP Field;
00047 typedef Field::Element FieldElement;
00048 typedef EC2NPoint Point;
00049
00050 EC2N() {}
00051 EC2N(const Field &field, const Field::Element &a, const Field::Element &b)
00052 : m_field(field), m_a(a), m_b(b) {}
00053
00054
00055 EC2N(BufferedTransformation &bt);
00056
00057
00058 void DEREncode(BufferedTransformation &bt) const;
00059
00060 bool Equal(const Point &P, const Point &Q) const;
00061 const Point& Identity() const;
00062 const Point& Inverse(const Point &P) const;
00063 bool InversionIsFast() const {return true;}
00064 const Point& Add(const Point &P, const Point &Q) const;
00065 const Point& Double(const Point &P) const;
00066
00067 Point Multiply(const Integer &k, const Point &P) const
00068 {return ScalarMultiply(P, k);}
00069 Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const
00070 {return CascadeScalarMultiply(P, k1, Q, k2);}
00071
00072 bool ValidateParameters(RandomNumberGenerator &rng, unsigned int level=3) const;
00073 bool VerifyPoint(const Point &P) const;
00074
00075 unsigned int EncodedPointSize(bool compressed = false) const
00076 {return 1 + (compressed?1:2)*m_field->MaxElementByteLength();}
00077
00078 bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const;
00079 bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const;
00080 void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const;
00081 void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
00082
00083 Point BERDecodePoint(BufferedTransformation &bt) const;
00084 void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
00085
00086 Integer FieldSize() const {return Integer::Power2(m_field->MaxElementBitLength());}
00087 const Field & GetField() const {return *m_field;}
00088 const FieldElement & GetA() const {return m_a;}
00089 const FieldElement & GetB() const {return m_b;}
00090
00091 bool operator==(const EC2N &rhs) const
00092 {return GetField() == rhs.GetField() && m_a == rhs.m_a && m_b == rhs.m_b;}
00093
00094 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
00095 virtual ~EC2N() {}
00096 #endif
00097
00098 private:
00099 clonable_ptr<Field> m_field;
00100 FieldElement m_a, m_b;
00101 mutable Point m_R;
00102 };
00103
00104 CRYPTOPP_DLL_TEMPLATE_CLASS DL_FixedBasePrecomputationImpl<EC2N::Point>;
00105 CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupPrecomputation<EC2N::Point>;
00106
00107 template <class T> class EcPrecomputation;
00108
00109
00110 template<> class EcPrecomputation<EC2N> : public DL_GroupPrecomputation<EC2N::Point>
00111 {
00112 public:
00113 typedef EC2N EllipticCurve;
00114
00115
00116 const AbstractGroup<Element> & GetGroup() const {return m_ec;}
00117 Element BERDecodeElement(BufferedTransformation &bt) const {return m_ec.BERDecodePoint(bt);}
00118 void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {m_ec.DEREncodePoint(bt, v, false);}
00119
00120
00121 void SetCurve(const EC2N &ec) {m_ec = ec;}
00122 const EC2N & GetCurve() const {return m_ec;}
00123
00124 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
00125 virtual ~EcPrecomputation() {}
00126 #endif
00127
00128 private:
00129 EC2N m_ec;
00130 };
00131
00132 NAMESPACE_END
00133
00134 #endif