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