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