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
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 "eprecomp.h"
16 #include "smartptr.h"
17 #include "pubkey.h"
18
19 NAMESPACE_BEGIN(CryptoPP)
20
21 //! \class EC2NPoint
22 //! \brief Elliptical Curve Point over GF(2^n)
23 struct CRYPTOPP_DLL EC2NPoint
24 {
25 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
26  virtual ~EC2NPoint() {}
27 #endif
28
29  EC2NPoint() : identity(true) {}
30  EC2NPoint(const PolynomialMod2 &x, const PolynomialMod2 &y)
31  : x(x), y(y), identity(false) {}
32
33  bool operator==(const EC2NPoint &t) const
34  {return (identity && t.identity) || (!identity && !t.identity && x==t.x && y==t.y);}
35  bool operator< (const EC2NPoint &t) const
36  {return identity ? !t.identity : (!t.identity && (x<t.x || (x==t.x && y<t.y)));}
37
38  PolynomialMod2 x, y;
39  bool identity;
40 };
41
42 CRYPTOPP_DLL_TEMPLATE_CLASS AbstractGroup<EC2NPoint>;
43
44 //! \class EC2N
45 //! \brief Elliptic Curve over GF(2^n)
46 class CRYPTOPP_DLL EC2N : public AbstractGroup<EC2NPoint>
47 {
48 public:
49  typedef GF2NP Field;
50  typedef Field::Element FieldElement;
51  typedef EC2NPoint Point;
52
53 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
54  virtual ~EC2N() {}
55 #endif
56
57  //! \brief Construct an EC2N
58  EC2N() {}
59
60  //! \brief Construct an EC2N
61  //! \param field Field, GF2NP derived class
62  //! \param a Field::Element
63  //! \param b Field::Element
64  EC2N(const Field &field, const Field::Element &a, const Field::Element &b)
65  : m_field(field), m_a(a), m_b(b) {}
66
67  //! \brief Construct an EC2N from BER encoded parameters
68  //! \param bt BufferedTransformation derived object
69  //! \details This constructor will decode and extract the the fields fieldID and curve of the sequence ECParameters
71
72  //! \brief Encode the fields fieldID and curve of the sequence ECParameters
73  //! \param bt BufferedTransformation derived object
74  void DEREncode(BufferedTransformation &bt) const;
75
76  bool Equal(const Point &P, const Point &Q) const;
77  const Point& Identity() const;
78  const Point& Inverse(const Point &P) const;
79  bool InversionIsFast() const {return true;}
80  const Point& Add(const Point &P, const Point &Q) const;
81  const Point& Double(const Point &P) const;
82
83  Point Multiply(const Integer &k, const Point &P) const
84  {return ScalarMultiply(P, k);}
85  Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const
86  {return CascadeScalarMultiply(P, k1, Q, k2);}
87
88  bool ValidateParameters(RandomNumberGenerator &rng, unsigned int level=3) const;
89  bool VerifyPoint(const Point &P) const;
90
91  unsigned int EncodedPointSize(bool compressed = false) const
92  {return 1 + (compressed?1:2)*m_field->MaxElementByteLength();}
93  // returns false if point is compressed and not valid (doesn't check if uncompressed)
94  bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const;
95  bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const;
96  void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const;
97  void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
98
99  Point BERDecodePoint(BufferedTransformation &bt) const;
100  void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
101
102  Integer FieldSize() const {return Integer::Power2(m_field->MaxElementBitLength());}
103  const Field & GetField() const {return *m_field;}
104  const FieldElement & GetA() const {return m_a;}
105  const FieldElement & GetB() const {return m_b;}
106
107  bool operator==(const EC2N &rhs) const
108  {return GetField() == rhs.GetField() && m_a == rhs.m_a && m_b == rhs.m_b;}
109
110 private:
111  clonable_ptr<Field> m_field;
112  FieldElement m_a, m_b;
113  mutable Point m_R;
114 };
115
116 CRYPTOPP_DLL_TEMPLATE_CLASS DL_FixedBasePrecomputationImpl<EC2N::Point>;
117 CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupPrecomputation<EC2N::Point>;
118
119 //! \class EcPrecomputation
120 //! \brief Elliptic Curve precomputation
121 //! \tparam EC elliptic curve field
122 template <class EC> class EcPrecomputation;
123
124 //! \class EcPrecomputation<EC2N>
125 //! \brief EC2N precomputation specialization
126 //! \details Implementation of <tt>DL_GroupPrecomputation<EC2N::Point></tt>
127 //! \sa DL_GroupPrecomputation
128 template<> class EcPrecomputation<EC2N> : public DL_GroupPrecomputation<EC2N::Point>
129 {
130 public:
131  typedef EC2N EllipticCurve;
132
133 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
134  virtual ~EcPrecomputation() {}
135 #endif
136
137  // DL_GroupPrecomputation
138  const AbstractGroup<Element> & GetGroup() const {return m_ec;}
139  Element BERDecodeElement(BufferedTransformation &bt) const {return m_ec.BERDecodePoint(bt);}
140  void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {m_ec.DEREncodePoint(bt, v, false);}
141
142  // non-inherited
143  void SetCurve(const EC2N &ec) {m_ec = ec;}
144  const EC2N & GetCurve() const {return m_ec;}
145
146 private:
147  EC2N m_ec;
148 };
149
150 NAMESPACE_END
151
152 #endif
EC2N()
Construct an EC2N.
Definition: ec2n.h:58
This file contains helper classes/functions for implementing public key algorithms.
virtual Element ScalarMultiply(const Element &a, const Integer &e) const
Performs a scalar multiplication.
Definition: algebra.cpp:90
Abstract base classes that provide a uniform interface to this library.
Classes for automatic resource management.
Interface for random number generators.
Definition: cryptlib.h:1201
Classes for performing mathematics over different fields.
Interface for buffered transformations.
Definition: cryptlib.h:1367
bool operator==(const OID &lhs, const OID &rhs)
Compare two OIDs for equality.
virtual const Element & Identity() const =0
Provides the Identity element.
Polynomial with Coefficients in GF(2)
Definition: gf2n.h:21
EC2N(const Field &field, const Field::Element &a, const Field::Element &b)
Construct an EC2N.
Definition: ec2n.h:64
virtual const Element & Double(const Element &a) const
Doubles an element in the group.
Definition: algebra.cpp:15
bool operator<(const OID &lhs, const OID &rhs)
Compare two OIDs for ordering.
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:3013
Multiple precision integer with arithmetic operations.
Definition: integer.h:45
Elliptic Curve over GF(2^n)
Definition: ec2n.h:46
virtual const Element & Add(const Element &a, const Element &b) const =0
Classes and functions for schemes over GF(2^n)
virtual bool Equal(const Element &a, const Element &b) const =0
Compare two elements for equality.
Classes for precomputation in a group.
GF(2^n) with Polynomial Basis.
Definition: gf2n.h:290
bool InversionIsFast() const
Determine if inversion is fast.
Definition: ec2n.h:79
virtual const Element & Inverse(const Element &a) const =0
Inverts the element in the group.
Elliptic Curve precomputation.
Definition: ec2n.h:122
Multiple precision integer with arithmetic operations.
Crypto++ library namespace.
Elliptical Curve Point over GF(2^n)
Definition: ec2n.h:23
virtual Element CascadeScalarMultiply(const Element &x, const Integer &e1, const Element &y, const Integer &e2) const
TODO.
Definition: algebra.cpp:97