Crypto++ 8.0 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
7 #ifndef CRYPTOPP_EC2N_H
8 #define CRYPTOPP_EC2N_H
9
10 #include "cryptlib.h"
11 #include "gf2n.h"
12 #include "integer.h"
13 #include "algebra.h"
14 #include "ecpoint.h"
15 #include "eprecomp.h"
16 #include "smartptr.h"
17 #include "pubkey.h"
18
19 #if CRYPTOPP_MSC_VERSION
20 # pragma warning(push)
21 # pragma warning(disable: 4231 4275)
22 #endif
23
24 NAMESPACE_BEGIN(CryptoPP)
25
26 /// \brief Elliptic Curve over GF(2^n)
27 class CRYPTOPP_DLL EC2N : public AbstractGroup<EC2NPoint>, public EncodedPoint<EC2NPoint>
28 {
29 public:
30 typedef GF2NP Field;
31 typedef Field::Element FieldElement;
32 typedef EC2NPoint Point;
33
34 virtual ~EC2N() {}
35
36 /// \brief Construct an EC2N
37 EC2N() {}
38
39 /// \brief Construct an EC2N
40 /// \param field Field, GF2NP derived class
41 /// \param a Field::Element
42 /// \param b Field::Element
43 EC2N(const Field &field, const Field::Element &a, const Field::Element &b)
44 : m_field(field), m_a(a), m_b(b) {}
45
46 /// \brief Construct an EC2N from BER encoded parameters
47 /// \param bt BufferedTransformation derived object
48 /// \details This constructor will decode and extract the the fields fieldID and curve of the sequence ECParameters
49 EC2N(BufferedTransformation &bt);
50
51 /// \brief Encode the fields fieldID and curve of the sequence ECParameters
52 /// \param bt BufferedTransformation derived object
53 void DEREncode(BufferedTransformation &bt) const;
54
55 bool Equal(const Point &P, const Point &Q) const;
56 const Point& Identity() const;
57 const Point& Inverse(const Point &P) const;
58 bool InversionIsFast() const {return true;}
59 const Point& Add(const Point &P, const Point &Q) const;
60 const Point& Double(const Point &P) const;
61
62 Point Multiply(const Integer &k, const Point &P) const
63 {return ScalarMultiply(P, k);}
64 Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const
65 {return CascadeScalarMultiply(P, k1, Q, k2);}
66
67 bool ValidateParameters(RandomNumberGenerator &rng, unsigned int level=3) const;
68 bool VerifyPoint(const Point &P) const;
69
70 unsigned int EncodedPointSize(bool compressed = false) const
71 {return 1 + (compressed?1:2)*m_field->MaxElementByteLength();}
72 // returns false if point is compressed and not valid (doesn't check if uncompressed)
73 bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const;
74 bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const;
75 void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const;
76 void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
77
78 Point BERDecodePoint(BufferedTransformation &bt) const;
79 void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
80
81 Integer FieldSize() const {return Integer::Power2(m_field->MaxElementBitLength());}
82 const Field & GetField() const {return *m_field;}
83 const FieldElement & GetA() const {return m_a;}
84 const FieldElement & GetB() const {return m_b;}
85
86 bool operator==(const EC2N &rhs) const
87 {return GetField() == rhs.GetField() && m_a == rhs.m_a && m_b == rhs.m_b;}
88
89 private:
90 clonable_ptr<Field> m_field;
91 FieldElement m_a, m_b;
92 mutable Point m_R;
93 };
94
95 CRYPTOPP_DLL_TEMPLATE_CLASS DL_FixedBasePrecomputationImpl<EC2N::Point>;
96 CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupPrecomputation<EC2N::Point>;
97
98 /// \brief Elliptic Curve precomputation
99 /// \tparam EC elliptic curve field
100 template <class EC> class EcPrecomputation;
101
102 /// \brief EC2N precomputation specialization
103 /// \details Implementation of <tt>DL_GroupPrecomputation<EC2N::Point></tt>
104 /// \sa DL_GroupPrecomputation
105 template<> class EcPrecomputation<EC2N> : public DL_GroupPrecomputation<EC2N::Point>
106 {
107 public:
108 typedef EC2N EllipticCurve;
109
110 virtual ~EcPrecomputation() {}
111
112 // DL_GroupPrecomputation
113 const AbstractGroup<Element> & GetGroup() const {return m_ec;}
114 Element BERDecodeElement(BufferedTransformation &bt) const {return m_ec.BERDecodePoint(bt);}
115 void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {m_ec.DEREncodePoint(bt, v, false);}
116
117 /// \brief Set the elliptic curve
118 /// \param ec ECP derived class
119 /// \details SetCurve() is not inherited
120 void SetCurve(const EC2N &ec) {m_ec = ec;}
121
122 /// \brief Get the elliptic curve
123 /// \returns EC2N curve
124 /// \details GetCurve() is not inherited
125 const EC2N & GetCurve() const {return m_ec;}
126
127 private:
128 EC2N m_ec;
129 };
130
131 NAMESPACE_END
132
133 #if CRYPTOPP_MSC_VERSION
134 # pragma warning(pop)
135 #endif
136
137 #endif
EcPrecomputation< EC2N >::BERDecodeElement
Element BERDecodeElement(BufferedTransformation &bt) const
Decodes element in DER format.
Definition: ec2n.h:114
EC2N::EC2N
EC2N()
Construct an EC2N.
Definition: ec2n.h:37
DL_GroupPrecomputation< EC2N::Point >
pubkey.h
This file contains helper classes/functions for implementing public key algorithms.
EcPrecomputation< EC2N >::GetGroup
const AbstractGroup< Element > & GetGroup() const
Retrieves AbstractGroup interface.
Definition: ec2n.h:113
Name::Identity
const char * Identity()
ConstByteArrayParameter.
Definition: argnames.h:94
EC2N::EncodedPointSize
unsigned int EncodedPointSize(bool compressed=false) const
Determines encoded point size.
Definition: ec2n.h:70
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
smartptr.h
Classes for automatic resource management.
RandomNumberGenerator
Interface for random number generators.
Definition: cryptlib.h:1383
EC2N::InversionIsFast
bool InversionIsFast() const
Determine if inversion is fast.
Definition: ec2n.h:58
ecpoint.h
Classes for Elliptic Curve points.
algebra.h
Classes for performing mathematics over different fields.
BufferedTransformation
Interface for buffered transformations.
Definition: cryptlib.h:1598
operator==
bool operator==(const OID &lhs, const OID &rhs)
Compare two OIDs for equality.
EcPrecomputation< EC2N >::SetCurve
void SetCurve(const EC2N &ec)
Set the elliptic curve.
Definition: ec2n.h:120
EC2N::EC2N
EC2N(const Field &field, const Field::Element &a, const Field::Element &b)
Construct an EC2N.
Definition: ec2n.h:43
clonable_ptr
A pointer which can be copied and cloned.
Definition: smartptr.h:103
Integer::Power2
static Integer Power2(size_t e)
Exponentiates to a power of 2.
Definition: integer.cpp:3079
Integer
Multiple precision integer with arithmetic operations.
Definition: integer.h:49
EC2N
Elliptic Curve over GF(2^n)
Definition: ec2n.h:27
gf2n.h
Classes and functions for schemes over GF(2^n)
AbstractGroup
Abstract group.
Definition: algebra.h:26
eprecomp.h
Classes for precomputation in a group.
GF2NP
GF(2^n) with Polynomial Basis.
Definition: gf2n.h:296
EncodedPoint
Abstract class for encoding and decoding ellicptic curve points.
Definition: ecpoint.h:90
EcPrecomputation
Elliptic Curve precomputation.
Definition: ec2n.h:100
integer.h
Multiple precision integer with arithmetic operations.
EcPrecomputation< EC2N >::GetCurve
const EC2N & GetCurve() const
Get the elliptic curve.
Definition: ec2n.h:125
CryptoPP
Crypto++ library namespace.
EC2NPoint
Elliptical Curve Point over GF(2^n)
Definition: ecpoint.h:53
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:115