Crypto++  8.2
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 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
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  /// \returns 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
Element BERDecodeElement(BufferedTransformation &bt) const
Decodes element in DER format.
Definition: ec2n.h:113
EC2N()
Construct an EC2N.
Definition: ec2n.h:36
This file contains helper classes/functions for implementing public key algorithms.
const AbstractGroup< Element > & GetGroup() const
Retrieves AbstractGroup interface.
Definition: ec2n.h:112
const char * Identity()
ConstByteArrayParameter.
Definition: argnames.h:94
unsigned int EncodedPointSize(bool compressed=false) const
Determines encoded point size.
Definition: ec2n.h:69
Abstract base classes that provide a uniform interface to this library.
Classes for automatic resource management.
Interface for random number generators.
Definition: cryptlib.h:1412
bool InversionIsFast() const
Determine if inversion is fast.
Definition: ec2n.h:57
Classes for Elliptic Curve points.
Classes for performing mathematics over different fields.
Interface for buffered transformations.
Definition: cryptlib.h:1627
bool operator==(const OID &lhs, const OID &rhs)
Compare two OIDs for equality.
void SetCurve(const EC2N &ec)
Set the elliptic curve.
Definition: ec2n.h:119
EC2N(const Field &field, const Field::Element &a, const Field::Element &b)
Construct an EC2N.
Definition: ec2n.h:42
A pointer which can be copied and cloned.
Definition: smartptr.h:103
static Integer CRYPTOPP_API Power2(size_t e)
Exponentiates to a power of 2.
Definition: integer.cpp:3093
Multiple precision integer with arithmetic operations.
Definition: integer.h:49
Elliptic Curve over GF(2^n)
Definition: ec2n.h:26
Classes and functions for schemes over GF(2^n)
Abstract group.
Definition: algebra.h:26
Classes for precomputation in a group.
GF(2^n) with Polynomial Basis.
Definition: gf2n.h:296
Abstract class for encoding and decoding ellicptic curve points.
Definition: ecpoint.h:90
Elliptic Curve precomputation.
Definition: ec2n.h:99
Multiple precision integer with arithmetic operations.
const EC2N & GetCurve() const
Get the elliptic curve.
Definition: ec2n.h:124
Crypto++ library namespace.
Elliptical Curve Point over GF(2^n)
Definition: ecpoint.h:53
DL_FixedBasePrecomputation adapter class.
Definition: eprecomp.h:126
void DEREncodeElement(BufferedTransformation &bt, const Element &v) const
Encodes element in DER format.
Definition: ec2n.h:114