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