Crypto++ 8.4 Free C++ class library of cryptographic schemes

ec2n.h
Go to the documentation of this file.
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 virtual ~EC2N() {}
34
35 /// \brief Construct an EC2N
36 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 : 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 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 {return ScalarMultiply(P, k);}
63 Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const
64 {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 {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 Integer FieldSize() const {return Integer::Power2(m_field->MaxElementBitLength());}
81 const Field & GetField() const {return *m_field;}
82 const FieldElement & GetA() const {return m_a;}
83 const FieldElement & GetB() const {return m_b;}
84
85 bool operator==(const EC2N &rhs) const
86 {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 virtual ~EcPrecomputation() {}
110
111 // DL_GroupPrecomputation
112 const AbstractGroup<Element> & GetGroup() const {return m_ec;}
113 Element BERDecodeElement(BufferedTransformation &bt) const {return m_ec.BERDecodePoint(bt);}
114 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 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 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
Integer::Power2
static Integer Power2(size_t e)
Exponentiates to a power of 2.
AbstractGroup
Abstract group.
Definition: algebra.h:26
EC2N::EC2N
EC2N(const Field &field, const Field::Element &a, const Field::Element &b)
Construct an EC2N.
Definition: ec2n.h:42
gf2n.h
Classes and functions for schemes over GF(2^n)
EncodedPoint
Abstract class for encoding and decoding ellicptic curve points.
Definition: ecpoint.h:90
BufferedTransformation
Interface for buffered transformations.
Definition: cryptlib.h:1634
EC2N
Elliptic Curve over GF(2^n)
Definition: ec2n.h:26
smartptr.h
Classes for automatic resource management.
pubkey.h
This file contains helper classes/functions for implementing public key algorithms.
EC2NPoint
Elliptical Curve Point over GF(2^n)
Definition: ecpoint.h:53
RandomNumberGenerator
Interface for random number generators.
Definition: cryptlib.h:1417
EcPrecomputation< EC2N >::GetCurve
const EC2N & GetCurve() const
Get the elliptic curve.
Definition: ec2n.h:124
DL_GroupPrecomputation< EC2N::Point >
operator==
bool operator==(const OID &lhs, const OID &rhs)
Compare two OIDs for equality.
EcPrecomputation
Elliptic Curve precomputation.
Definition: ec2n.h:99
eprecomp.h
Classes for precomputation in a group.
EC2N::EC2N
EC2N()
Construct an EC2N.
Definition: ec2n.h:36
EcPrecomputation< EC2N >::SetCurve
void SetCurve(const EC2N &ec)
Set the elliptic curve.
Definition: ec2n.h:119
GF2NP
GF(2^n) with Polynomial Basis.
Definition: gf2n.h:296
EcPrecomputation< EC2N >::BERDecodeElement
Element BERDecodeElement(BufferedTransformation &bt) const
Decodes element in DER format.
Definition: ec2n.h:113
clonable_ptr
A pointer which can be copied and cloned.
Definition: smartptr.h:104
DL_FixedBasePrecomputationImpl
DL_FixedBasePrecomputation adapter class.
Definition: eprecomp.h:126
EcPrecomputation< EC2N >::DEREncodeElement
void DEREncodeElement(BufferedTransformation &bt, const Element &v) const
Encodes element in DER format.
Definition: ec2n.h:114
algebra.h
Classes for performing mathematics over different fields.
CryptoPP
Crypto++ library namespace.
CRYPTOPP_DLL_TEMPLATE_CLASS
#define CRYPTOPP_DLL_TEMPLATE_CLASS
Instantiate templates in a dynamic library.
Definition: config_dll.h:72
ecpoint.h
Classes for Elliptic Curve points.
EC2N::EncodedPointSize
unsigned int EncodedPointSize(bool compressed=false) const
Determines encoded point size.
Definition: ec2n.h:69
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
integer.h
Multiple precision integer with arithmetic operations.
Integer
Multiple precision integer with arithmetic operations.
Definition: integer.h:49