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