Crypto++ 8.7
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
23NAMESPACE_BEGIN(CryptoPP)
24
25/// \brief Elliptic Curve over GF(2^n)
26class CRYPTOPP_DLL EC2N : public AbstractGroup<EC2NPoint>, public EncodedPoint<EC2NPoint>
27{
28public:
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
49
50 /// \brief Encode the fields fieldID and curve of the sequence ECParameters
51 /// \param bt BufferedTransformation derived object
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
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
88private:
89 clonable_ptr<Field> m_field;
90 FieldElement m_a, m_b;
91 mutable Point m_R;
92};
93
96
97/// \brief Elliptic Curve precomputation
98/// \tparam EC elliptic curve field
99template <class EC> class EcPrecomputation;
100
101/// \brief EC2N precomputation specialization
102/// \details Implementation of <tt>DL_GroupPrecomputation<EC2N::Point></tt>
103/// \sa DL_GroupPrecomputation
104template<> class EcPrecomputation<EC2N> : public DL_GroupPrecomputation<EC2N::Point>
105{
106public:
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
126private:
127 EC2N m_ec;
128};
129
130NAMESPACE_END
131
132#if CRYPTOPP_MSC_VERSION
133# pragma warning(pop)
134#endif
135
136#endif
Classes for performing mathematics over different fields.
bool operator==(const OID &lhs, const OID &rhs)
Compare two OIDs for equality.
Abstract group.
Definition: algebra.h:27
Interface for buffered transformations.
Definition: cryptlib.h:1652
DL_FixedBasePrecomputation adapter class.
Definition: eprecomp.h:127
Elliptic Curve over GF(2^n)
Definition: ec2n.h:27
bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const
Decodes an elliptic curve point.
bool VerifyPoint(const Point &P) const
Verifies points on elliptic curve.
const Point & Identity() const
Provides the Identity element.
const Point & Double(const Point &P) const
Doubles an element in the group.
void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const
Encodes an elliptic curve point.
unsigned int EncodedPointSize(bool compressed=false) const
Determines encoded point size.
Definition: ec2n.h:69
bool Equal(const Point &P, const Point &Q) const
Compare two elements for equality.
bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const
Decodes an elliptic curve point.
void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const
DER Encodes an elliptic curve point.
bool InversionIsFast() const
Determine if inversion is fast.
Definition: ec2n.h:57
EC2N(const Field &field, const Field::Element &a, const Field::Element &b)
Construct an EC2N.
Definition: ec2n.h:42
void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const
Encodes an elliptic curve point.
EC2N(BufferedTransformation &bt)
Construct an EC2N from BER encoded parameters.
void DEREncode(BufferedTransformation &bt) const
Encode the fields fieldID and curve of the sequence ECParameters.
Point BERDecodePoint(BufferedTransformation &bt) const
BER Decodes an elliptic curve point.
EC2N()
Construct an EC2N.
Definition: ec2n.h:36
const Point & Inverse(const Point &P) const
Inverts the element in the group.
const Point & Add(const Point &P, const Point &Q) const
Adds elements in the group.
const AbstractGroup< Element > & GetGroup() const
Retrieves AbstractGroup interface.
Definition: ec2n.h:112
Element BERDecodeElement(BufferedTransformation &bt) const
Decodes element in DER format.
Definition: ec2n.h:113
void SetCurve(const EC2N &ec)
Set the elliptic curve.
Definition: ec2n.h:119
const EC2N & GetCurve() const
Get the elliptic curve.
Definition: ec2n.h:124
void DEREncodeElement(BufferedTransformation &bt, const Element &v) const
Encodes element in DER format.
Definition: ec2n.h:114
Elliptic Curve precomputation.
Definition: ec2n.h:99
Abstract class for encoding and decoding ellicptic curve points.
Definition: ecpoint.h:91
GF(2^n) with Polynomial Basis.
Definition: gf2n.h:297
Multiple precision integer with arithmetic operations.
Definition: integer.h:50
static Integer Power2(size_t e)
Exponentiates to a power of 2.
Interface for random number generators.
Definition: cryptlib.h:1435
A pointer which can be copied and cloned.
Definition: smartptr.h:105
#define CRYPTOPP_DLL_TEMPLATE_CLASS
Instantiate templates in a dynamic library.
Definition: config_dll.h:72
Abstract base classes that provide a uniform interface to this library.
Classes for Elliptic Curve points.
Classes for precomputation in a group.
Classes and functions for schemes over GF(2^n)
Multiple precision integer with arithmetic operations.
Crypto++ library namespace.
This file contains helper classes/functions for implementing public key algorithms.
Classes for automatic resource management.
Elliptical Curve Point over GF(2^n)
Definition: ecpoint.h:54