1#ifndef CRYPTOPP_EC2N_H 2#define CRYPTOPP_EC2N_H 3 4#include "gf2n.h" 5#include "eprecomp.h" 6#include "smartptr.h" 7#include "pubkey.h" 8 9NAMESPACE_BEGIN(CryptoPP) 10 11//! Elliptic Curve Point 12struct CRYPTOPP_DLL EC2NPoint 13{ 14 EC2NPoint() : identity(true) {} 15 EC2NPoint(const PolynomialMod2 &x, const PolynomialMod2 &y) 16 : identity(false), x(x), y(y) {} 17 18 bool operator==(const EC2NPoint &t) const 19 {return (identity && t.identity) || (!identity && !t.identity && x==t.x && y==t.y);} 20 bool operator< (const EC2NPoint &t) const 21 {return identity ? !t.identity : (!t.identity && (x<t.x || (x==t.x && y<t.y)));} 22 23 bool identity; 24 PolynomialMod2 x, y; 25}; 26 27CRYPTOPP_DLL_TEMPLATE_CLASS AbstractGroup<EC2NPoint>; 28 29//! Elliptic Curve over GF(2^n) 30class CRYPTOPP_DLL EC2N : public AbstractGroup<EC2NPoint> 31{ 32public: 33 typedef GF2NP Field; 34 typedef Field::Element FieldElement; 35 typedef EC2NPoint Point; 36 37 EC2N() {} 38 EC2N(const Field &field, const Field::Element &a, const Field::Element &b) 39 : m_field(field), m_a(a), m_b(b) {} 40 // construct from BER encoded parameters 41 // this constructor will decode and extract the the fields fieldID and curve of the sequence ECParameters 42 EC2N(BufferedTransformation &bt); 43 44 // encode the fields fieldID and curve of the sequence ECParameters 45 void DEREncode(BufferedTransformation &bt) const; 46 47 bool Equal(const Point &P, const Point &Q) const; 48 const Point& Identity() const; 49 const Point& Inverse(const Point &P) const; 50 bool InversionIsFast() const {return true;} 51 const Point& Add(const Point &P, const Point &Q) const; 52 const Point& Double(const Point &P) const; 53 54 Point Multiply(const Integer &k, const Point &P) const 55 {return ScalarMultiply(P, k);} 56 Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const 57 {return CascadeScalarMultiply(P, k1, Q, k2);} 58 59 bool ValidateParameters(RandomNumberGenerator &rng, unsigned int level=3) const; 60 bool VerifyPoint(const Point &P) const; 61 62 unsigned int EncodedPointSize(bool compressed = false) const 63 {return 1 + (compressed?1:2)*m_field->MaxElementByteLength();} 64 // returns false if point is compressed and not valid (doesn't check if uncompressed) 65 bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const; 66 bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const; 67 void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const; 68 void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const; 69 70 Point BERDecodePoint(BufferedTransformation &bt) const; 71 void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const; 72 73 Integer FieldSize() const {return Integer::Power2(m_field->MaxElementBitLength());} 74 const Field & GetField() const {return *m_field;} 75 const FieldElement & GetA() const {return m_a;} 76 const FieldElement & GetB() const {return m_b;} 77 78 bool operator==(const EC2N &rhs) const 79 {return GetField() == rhs.GetField() && m_a == rhs.m_a && m_b == rhs.m_b;} 80 81private: 82 clonable_ptr<Field> m_field; 83 FieldElement m_a, m_b; 84 mutable Point m_R; 85}; 86 87CRYPTOPP_DLL_TEMPLATE_CLASS DL_FixedBasePrecomputationImpl<EC2N::Point>; 88CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupPrecomputation<EC2N::Point>; 89 90template <class T> class EcPrecomputation; 91 92//! EC2N precomputation 93template<> class EcPrecomputation<EC2N> : public DL_GroupPrecomputation<EC2N::Point> 94{ 95public: 96 typedef EC2N EllipticCurve; 97 98 // DL_GroupPrecomputation 99 const AbstractGroup<Element> & GetGroup() const {return m_ec;} 100 Element BERDecodeElement(BufferedTransformation &bt) const {return m_ec.BERDecodePoint(bt);} 101 void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {m_ec.DEREncodePoint(bt, v, false);} 102 103 // non-inherited 104 void SetCurve(const EC2N &ec) {m_ec = ec;} 105 const EC2N & GetCurve() const {return m_ec;} 106 107private: 108 EC2N m_ec; 109}; 110 111NAMESPACE_END 112 113#endif 114