Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

ecp.h

00001 #ifndef CRYPTOPP_ECP_H
00002 #define CRYPTOPP_ECP_H
00003 
00004 #include "modarith.h"
00005 #include "eprecomp.h"
00006 #include "smartptr.h"
00007 
00008 NAMESPACE_BEGIN(CryptoPP)
00009 
00011 struct ECPPoint
00012 {
00013         ECPPoint() : identity(true) {}
00014         ECPPoint(const Integer &x, const Integer &y)
00015                 : identity(false), x(x), y(y) {}
00016 
00017         bool operator==(const ECPPoint &t) const
00018                 {return (identity && t.identity) || (!identity && !t.identity && x==t.x && y==t.y);}
00019         bool operator< (const ECPPoint &t) const
00020                 {return identity ? !t.identity : (!t.identity && (x<t.x || (x==t.x && y<t.y)));}
00021 
00022         bool identity;
00023         Integer x, y;
00024 };
00025 
00027 class ECP : public AbstractGroup<ECPPoint>
00028 {
00029 public:
00030         typedef ModularArithmetic Field;
00031         typedef Integer FieldElement;
00032         typedef ECPPoint Point;
00033 
00034         ECP(const ECP &ecp)
00035                 : m_fieldPtr(new Field(ecp.m_field.GetModulus())), m_field(*m_fieldPtr), m_a(ecp.m_a), m_b(ecp.m_b) {}
00036         ECP(const Integer &modulus, const FieldElement &a, const FieldElement &b)
00037                 : m_fieldPtr(new Field(modulus)), m_field(*m_fieldPtr), m_a(a.IsNegative() ? modulus+a : a), m_b(b) {}
00038         ECP(const MontgomeryRepresentation &mr, const FieldElement &a, const FieldElement &b)
00039                 : m_field(mr), m_a(a), m_b(b) {}
00040         // construct from BER encoded parameters
00041         // this constructor will decode and extract the the fields fieldID and curve of the sequence ECParameters
00042         ECP(BufferedTransformation &bt);
00043 
00044         // encode the fields fieldID and curve of the sequence ECParameters
00045         void DEREncode(BufferedTransformation &bt) const;
00046 
00047         bool Equal(const Point &P, const Point &Q) const;
00048         const Point& Zero() const {static const Point zero; return zero;}
00049         const Point& Inverse(const Point &P) const;
00050         bool InversionIsFast() const {return true;}
00051         const Point& Add(const Point &P, const Point &Q) const;
00052         const Point& Double(const Point &P) const;
00053         Point ScalarMultiply(const Point &P, const Integer &k) const;
00054         Point CascadeScalarMultiply(const Point &P, const Integer &k1, const Point &Q, const Integer &k2) const;
00055         void SimultaneousMultiply(Point *results, const Point &base, const Integer *exponents, unsigned int exponentsCount) const;
00056 
00057         Point Multiply(const Integer &k, const Point &P) const
00058                 {return ScalarMultiply(P, k);}
00059         Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const
00060                 {return CascadeScalarMultiply(P, k1, Q, k2);}
00061 
00062         bool ValidateParameters(RandomNumberGenerator &rng) const;
00063         bool VerifyPoint(const Point &P) const;
00064 
00065         unsigned int EncodedPointSize(bool compressed = false) const
00066                 {return 1 + (compressed?1:2)*m_field.MaxElementByteLength();}
00067         // returns false if point is compressed and not valid (doesn't check if uncompressed)
00068         bool DecodePoint(Point &P, BufferedTransformation &bt, unsigned int len) const;
00069         bool DecodePoint(Point &P, const byte *encodedPoint, unsigned int len) const;
00070         void EncodePoint(byte *encodedPoint, const Point &P, bool compressed = false) const;
00071 
00072         Point BERDecodePoint(BufferedTransformation &bt) const;
00073         void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed = false) const;
00074 
00075         Integer FieldSize() const {return m_field.GetModulus();}
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 private:
00081         member_ptr<Field> m_fieldPtr;
00082         const Field &m_field;
00083         FieldElement m_a, m_b;
00084         mutable Point m_R;
00085 };
00086 
00087 template <class T> class EcPrecomputation;
00088 
00090 template<> class EcPrecomputation<ECP>
00091 {
00092 public:
00093         EcPrecomputation() {}
00094         EcPrecomputation(const EcPrecomputation &a)
00095                 {operator=(a);}
00096         EcPrecomputation(const ECP &ec, const ECP::Point &base)
00097                 {SetCurveAndBase(ec, base);}
00098 
00099         EcPrecomputation& operator=(const EcPrecomputation &rhs);
00100 
00101         void SetCurveAndBase(const ECP &ec, const ECP::Point &base);
00102         void Precompute(unsigned int maxExpBits, unsigned int storage);
00103         void Load(BufferedTransformation &storedPrecomputation);
00104         void Save(BufferedTransformation &storedPrecomputation) const;
00105 
00106         ECP::Point Multiply(const Integer &exponent) const;
00107         ECP::Point CascadeMultiply(const Integer &exponent, const EcPrecomputation<ECP> &pc2, const Integer &exponent2) const;
00108 
00109 private:
00110         value_ptr<MontgomeryRepresentation> m_mr;
00111         value_ptr<ECP> m_ec;
00112         ExponentiationPrecomputation<ECP::Point> m_ep;
00113 };
00114 
00115 NAMESPACE_END
00116 
00117 #endif

Generated at Mon Jan 15 01:16:31 2001 for Crypto++ by doxygen1.2.4 written by Dimitri van Heesch, © 1997-2000