9
NAMESPACE_BEGIN(CryptoPP)
11
//! Elliptical Curve Point
12
struct CRYPTOPP_DLL ECPPoint
14
ECPPoint() : identity(true) {}
15
ECPPoint(const Integer &x, const Integer &y)
16
: identity(false), x(x), y(y) {}
18
bool operator==(const ECPPoint &t) const
19
{return (identity && t.identity) || (!identity && !t.identity && x==t.x && y==t.y);}
20
bool operator< (const ECPPoint &t) const
21
{return identity ? !t.identity : (!t.identity && (x<t.x || (x==t.x && y<t.y)));}
27
CRYPTOPP_DLL_TEMPLATE_CLASS AbstractGroup<ECPPoint>;
29
//! Elliptic Curve over GF(p), where p is prime
30
class CRYPTOPP_DLL ECP : public AbstractGroup<ECPPoint>
33
typedef ModularArithmetic Field;
34
typedef Integer FieldElement;
35
typedef ECPPoint Point;
38
ECP(const ECP &ecp, bool convertToMontgomeryRepresentation = false);
39
ECP(const Integer &modulus, const FieldElement &a, const FieldElement &b)
40
: m_fieldPtr(new Field(modulus)), m_a(a.IsNegative() ? modulus+a : a), m_b(b) {}
41
// construct from BER encoded parameters
42
// this constructor will decode and extract the the fields fieldID and curve of the sequence ECParameters
43
ECP(BufferedTransformation &bt);
45
// encode the fields fieldID and curve of the sequence ECParameters
46
void DEREncode(BufferedTransformation &bt) const;
48
bool Equal(const Point &P, const Point &Q) const;
49
const Point& Identity() const;
50
const Point& Inverse(const Point &P) const;
51
bool InversionIsFast() const {return true;}
52
const Point& Add(const Point &P, const Point &Q) const;
53
const Point& Double(const Point &P) const;
54
Point ScalarMultiply(const Point &P, const Integer &k) const;
55
Point CascadeScalarMultiply(const Point &P, const Integer &k1, const Point &Q, const Integer &k2) const;
56
void SimultaneousMultiply(Point *results, const Point &base, const Integer *exponents, unsigned int exponentsCount) const;
58
Point Multiply(const Integer &k, const Point &P) const
59
{return ScalarMultiply(P, k);}
60
Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const
61
{return CascadeScalarMultiply(P, k1, Q, k2);}
63
bool ValidateParameters(RandomNumberGenerator &rng, unsigned int level=3) const;
64
bool VerifyPoint(const Point &P) const;
66
unsigned int EncodedPointSize(bool compressed = false) const
67
{return 1 + (compressed?1:2)*GetField().MaxElementByteLength();}
68
// returns false if point is compressed and not valid (doesn't check if uncompressed)
69
bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const;
70
bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const;
71
void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const;
72
void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
74
Point BERDecodePoint(BufferedTransformation &bt) const;
75
void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
77
Integer FieldSize() const {return GetField().GetModulus();}
78
const Field & GetField() const {return *m_fieldPtr;}
79
const FieldElement & GetA() const {return m_a;}
80
const FieldElement & GetB() const {return m_b;}
82
bool operator==(const ECP &rhs) const
83
{return GetField() == rhs.GetField() && m_a == rhs.m_a && m_b == rhs.m_b;}
86
clonable_ptr<Field> m_fieldPtr;
87
FieldElement m_a, m_b;
91
CRYPTOPP_DLL_TEMPLATE_CLASS DL_FixedBasePrecomputationImpl<ECP::Point>;
92
CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupPrecomputation<ECP::Point>;
94
template <class T> class EcPrecomputation;
96
//! ECP precomputation
97
template<> class EcPrecomputation<ECP> : public DL_GroupPrecomputation<ECP::Point>
100
typedef ECP EllipticCurve;
102
// DL_GroupPrecomputation
103
bool NeedConversions() const {return true;}
104
Element ConvertIn(const Element &P) const
105
{return P.identity ? P : ECP::Point(m_ec->GetField().ConvertIn(P.x), m_ec->GetField().ConvertIn(P.y));};
106
Element ConvertOut(const Element &P) const
107
{return P.identity ? P : ECP::Point(m_ec->GetField().ConvertOut(P.x), m_ec->GetField().ConvertOut(P.y));}
108
const AbstractGroup<Element> & GetGroup() const {return *m_ec;}
109
Element BERDecodeElement(BufferedTransformation &bt) const {return m_ec->BERDecodePoint(bt);}
110
void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {m_ec->DEREncodePoint(bt, v, false);}
113
void SetCurve(const ECP &ec)
115
m_ec.reset(new ECP(ec, true));
118
const ECP & GetCurve() const {return *m_ecOriginal;}
121
value_ptr<ECP> m_ec, m_ecOriginal;