Crypto++  5.6.5
Free C++ class library of cryptographic schemes
ec2n.h
Go to the documentation of this file.
1 // ec2n.h - 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 NAMESPACE_BEGIN(CryptoPP)
21 
22 //! \class EC2N
23 //! \brief Elliptic Curve over GF(2^n)
24 class CRYPTOPP_DLL EC2N : public AbstractGroup<EC2NPoint>, public EncodedPoint<EC2NPoint>
25 {
26 public:
27  typedef GF2NP Field;
28  typedef Field::Element FieldElement;
29  typedef EC2NPoint Point;
30 
31  virtual ~EC2N() {}
32 
33  //! \brief Construct an EC2N
34  EC2N() {}
35 
36  //! \brief Construct an EC2N
37  //! \param field Field, GF2NP derived class
38  //! \param a Field::Element
39  //! \param b Field::Element
40  EC2N(const Field &field, const Field::Element &a, const Field::Element &b)
41  : m_field(field), m_a(a), m_b(b) {}
42 
43  //! \brief Construct an EC2N from BER encoded parameters
44  //! \param bt BufferedTransformation derived object
45  //! \details This constructor will decode and extract the the fields fieldID and curve of the sequence ECParameters
47 
48  //! \brief Encode the fields fieldID and curve of the sequence ECParameters
49  //! \param bt BufferedTransformation derived object
50  void DEREncode(BufferedTransformation &bt) const;
51 
52  bool Equal(const Point &P, const Point &Q) const;
53  const Point& Identity() const;
54  const Point& Inverse(const Point &P) const;
55  bool InversionIsFast() const {return true;}
56  const Point& Add(const Point &P, const Point &Q) const;
57  const Point& Double(const Point &P) const;
58 
59  Point Multiply(const Integer &k, const Point &P) const
60  {return ScalarMultiply(P, k);}
61  Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const
62  {return CascadeScalarMultiply(P, k1, Q, k2);}
63 
64  bool ValidateParameters(RandomNumberGenerator &rng, unsigned int level=3) const;
65  bool VerifyPoint(const Point &P) const;
66 
67  unsigned int EncodedPointSize(bool compressed = false) const
68  {return 1 + (compressed?1:2)*m_field->MaxElementByteLength();}
69  // returns false if point is compressed and not valid (doesn't check if uncompressed)
70  bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const;
71  bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const;
72  void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const;
73  void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
74 
75  Point BERDecodePoint(BufferedTransformation &bt) const;
76  void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
77 
78  Integer FieldSize() const {return Integer::Power2(m_field->MaxElementBitLength());}
79  const Field & GetField() const {return *m_field;}
80  const FieldElement & GetA() const {return m_a;}
81  const FieldElement & GetB() const {return m_b;}
82 
83  bool operator==(const EC2N &rhs) const
84  {return GetField() == rhs.GetField() && m_a == rhs.m_a && m_b == rhs.m_b;}
85 
86 private:
87  clonable_ptr<Field> m_field;
88  FieldElement m_a, m_b;
89  mutable Point m_R;
90 };
91 
92 CRYPTOPP_DLL_TEMPLATE_CLASS DL_FixedBasePrecomputationImpl<EC2N::Point>;
93 CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupPrecomputation<EC2N::Point>;
94 
95 //! \class EcPrecomputation
96 //! \brief Elliptic Curve precomputation
97 //! \tparam EC elliptic curve field
98 template <class EC> class EcPrecomputation;
99 
100 //! \class EcPrecomputation<EC2N>
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  // non-inherited
117  void SetCurve(const EC2N &ec) {m_ec = ec;}
118  const EC2N & GetCurve() const {return m_ec;}
119 
120 private:
121  EC2N m_ec;
122 };
123 
124 NAMESPACE_END
125 
126 #endif
EC2N()
Construct an EC2N.
Definition: ec2n.h:34
This file contains helper classes/functions for implementing public key algorithms.
Abstract base classes that provide a uniform interface to this library.
Classes for automatic resource management.
Interface for random number generators.
Definition: cryptlib.h:1188
Classes for Elliptic Curve points.
Classes for performing mathematics over different fields.
Interface for buffered transformations.
Definition: cryptlib.h:1352
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:40
unsigned int EncodedPointSize(bool compressed=false) const
Determines encoded point size.
Definition: ec2n.h:67
A pointer which can be copied and cloned.
Definition: smartptr.h:108
static Integer Power2(size_t e)
Exponentiates to a power of 2.
Definition: integer.cpp:3008
Multiple precision integer with arithmetic operations.
Definition: integer.h:43
Elliptic Curve over GF(2^n)
Definition: ec2n.h:24
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:290
Abstract class for encoding and decoding ellicptic curve points.
Definition: ecpoint.h:93
bool InversionIsFast() const
Determine if inversion is fast.
Definition: ec2n.h:55
Elliptic Curve precomputation.
Definition: ec2n.h:98
Multiple precision integer with arithmetic operations.
Crypto++ library namespace.
Elliptical Curve Point over GF(2^n)
Definition: ecpoint.h:55