1 | // ecp.h - written and placed in the public domain by Wei Dai |
---|
2 | |
---|
3 | //! \file ecp.h |
---|
4 | //! \brief Classes for Elliptic Curves over prime fields |
---|
5 | |
---|
6 | #ifndef CRYPTOPP_ECP_H |
---|
7 | #define CRYPTOPP_ECP_H |
---|
8 | |
---|
9 | #include "cryptlib.h" |
---|
10 | #include "integer.h" |
---|
11 | #include "algebra.h" |
---|
12 | #include "modarith.h" |
---|
13 | #include "eprecomp.h" |
---|
14 | #include "smartptr.h" |
---|
15 | #include "pubkey.h" |
---|
16 | |
---|
17 | NAMESPACE_BEGIN(CryptoPP) |
---|
18 | |
---|
19 | //! Elliptical Curve Point |
---|
20 | struct CRYPTOPP_DLL ECPPoint |
---|
21 | { |
---|
22 | ECPPoint() : identity(true) {} |
---|
23 | ECPPoint(const Integer &x, const Integer &y) |
---|
24 | : identity(false), x(x), y(y) {} |
---|
25 | |
---|
26 | bool operator==(const ECPPoint &t) const |
---|
27 | {return (identity && t.identity) || (!identity && !t.identity && x==t.x && y==t.y);} |
---|
28 | bool operator< (const ECPPoint &t) const |
---|
29 | {return identity ? !t.identity : (!t.identity && (x<t.x || (x==t.x && y<t.y)));} |
---|
30 | |
---|
31 | #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 |
---|
32 | virtual ~ECPPoint() {} |
---|
33 | #endif |
---|
34 | |
---|
35 | bool identity; |
---|
36 | Integer x, y; |
---|
37 | }; |
---|
38 | |
---|
39 | CRYPTOPP_DLL_TEMPLATE_CLASS AbstractGroup<ECPPoint>; |
---|
40 | |
---|
41 | //! Elliptic Curve over GF(p), where p is prime |
---|
42 | class CRYPTOPP_DLL ECP : public AbstractGroup<ECPPoint> |
---|
43 | { |
---|
44 | public: |
---|
45 | typedef ModularArithmetic Field; |
---|
46 | typedef Integer FieldElement; |
---|
47 | typedef ECPPoint Point; |
---|
48 | |
---|
49 | ECP() {} |
---|
50 | ECP(const ECP &ecp, bool convertToMontgomeryRepresentation = false); |
---|
51 | ECP(const Integer &modulus, const FieldElement &a, const FieldElement &b) |
---|
52 | : m_fieldPtr(new Field(modulus)), m_a(a.IsNegative() ? modulus+a : a), m_b(b) {} |
---|
53 | // construct from BER encoded parameters |
---|
54 | // this constructor will decode and extract the the fields fieldID and curve of the sequence ECParameters |
---|
55 | ECP(BufferedTransformation &bt); |
---|
56 | |
---|
57 | // encode the fields fieldID and curve of the sequence ECParameters |
---|
58 | void DEREncode(BufferedTransformation &bt) const; |
---|
59 | |
---|
60 | bool Equal(const Point &P, const Point &Q) const; |
---|
61 | const Point& Identity() const; |
---|
62 | const Point& Inverse(const Point &P) const; |
---|
63 | bool InversionIsFast() const {return true;} |
---|
64 | const Point& Add(const Point &P, const Point &Q) const; |
---|
65 | const Point& Double(const Point &P) const; |
---|
66 | Point ScalarMultiply(const Point &P, const Integer &k) const; |
---|
67 | Point CascadeScalarMultiply(const Point &P, const Integer &k1, const Point &Q, const Integer &k2) const; |
---|
68 | void SimultaneousMultiply(Point *results, const Point &base, const Integer *exponents, unsigned int exponentsCount) const; |
---|
69 | |
---|
70 | Point Multiply(const Integer &k, const Point &P) const |
---|
71 | {return ScalarMultiply(P, k);} |
---|
72 | Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const |
---|
73 | {return CascadeScalarMultiply(P, k1, Q, k2);} |
---|
74 | |
---|
75 | bool ValidateParameters(RandomNumberGenerator &rng, unsigned int level=3) const; |
---|
76 | bool VerifyPoint(const Point &P) const; |
---|
77 | |
---|
78 | unsigned int EncodedPointSize(bool compressed = false) const |
---|
79 | {return 1 + (compressed?1:2)*GetField().MaxElementByteLength();} |
---|
80 | // returns false if point is compressed and not valid (doesn't check if uncompressed) |
---|
81 | bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const; |
---|
82 | bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const; |
---|
83 | void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const; |
---|
84 | void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const; |
---|
85 | |
---|
86 | Point BERDecodePoint(BufferedTransformation &bt) const; |
---|
87 | void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const; |
---|
88 | |
---|
89 | Integer FieldSize() const {return GetField().GetModulus();} |
---|
90 | const Field & GetField() const {return *m_fieldPtr;} |
---|
91 | const FieldElement & GetA() const {return m_a;} |
---|
92 | const FieldElement & GetB() const {return m_b;} |
---|
93 | |
---|
94 | bool operator==(const ECP &rhs) const |
---|
95 | {return GetField() == rhs.GetField() && m_a == rhs.m_a && m_b == rhs.m_b;} |
---|
96 | |
---|
97 | #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 |
---|
98 | virtual ~ECP() {} |
---|
99 | #endif |
---|
100 | |
---|
101 | private: |
---|
102 | clonable_ptr<Field> m_fieldPtr; |
---|
103 | FieldElement m_a, m_b; |
---|
104 | mutable Point m_R; |
---|
105 | }; |
---|
106 | |
---|
107 | CRYPTOPP_DLL_TEMPLATE_CLASS DL_FixedBasePrecomputationImpl<ECP::Point>; |
---|
108 | CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupPrecomputation<ECP::Point>; |
---|
109 | |
---|
110 | template <class T> class EcPrecomputation; |
---|
111 | |
---|
112 | //! ECP precomputation |
---|
113 | template<> class EcPrecomputation<ECP> : public DL_GroupPrecomputation<ECP::Point> |
---|
114 | { |
---|
115 | public: |
---|
116 | typedef ECP EllipticCurve; |
---|
117 | |
---|
118 | // DL_GroupPrecomputation |
---|
119 | bool NeedConversions() const {return true;} |
---|
120 | Element ConvertIn(const Element &P) const |
---|
121 | {return P.identity ? P : ECP::Point(m_ec->GetField().ConvertIn(P.x), m_ec->GetField().ConvertIn(P.y));}; |
---|
122 | Element ConvertOut(const Element &P) const |
---|
123 | {return P.identity ? P : ECP::Point(m_ec->GetField().ConvertOut(P.x), m_ec->GetField().ConvertOut(P.y));} |
---|
124 | const AbstractGroup<Element> & GetGroup() const {return *m_ec;} |
---|
125 | Element BERDecodeElement(BufferedTransformation &bt) const {return m_ec->BERDecodePoint(bt);} |
---|
126 | void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {m_ec->DEREncodePoint(bt, v, false);} |
---|
127 | |
---|
128 | // non-inherited |
---|
129 | void SetCurve(const ECP &ec) |
---|
130 | { |
---|
131 | m_ec.reset(new ECP(ec, true)); |
---|
132 | m_ecOriginal = ec; |
---|
133 | } |
---|
134 | const ECP & GetCurve() const {return *m_ecOriginal;} |
---|
135 | |
---|
136 | #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 |
---|
137 | virtual ~EcPrecomputation() {} |
---|
138 | #endif |
---|
139 | |
---|
140 | private: |
---|
141 | value_ptr<ECP> m_ec, m_ecOriginal; |
---|
142 | }; |
---|
143 | |
---|
144 | NAMESPACE_END |
---|
145 | |
---|
146 | #endif |
---|