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