Crypto++  5.6.3
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 "eprecomp.h"
15 #include "smartptr.h"
16 #include "pubkey.h"
17 
18 NAMESPACE_BEGIN(CryptoPP)
19 
20 //! Elliptic Curve Point
21 struct CRYPTOPP_DLL EC2NPoint
22 {
23  EC2NPoint() : identity(true) {}
24  EC2NPoint(const PolynomialMod2 &x, const PolynomialMod2 &y)
25  : identity(false), x(x), y(y) {}
26 
27  bool operator==(const EC2NPoint &t) const
28  {return (identity && t.identity) || (!identity && !t.identity && x==t.x && y==t.y);}
29  bool operator< (const EC2NPoint &t) const
30  {return identity ? !t.identity : (!t.identity && (x<t.x || (x==t.x && y<t.y)));}
31 
32 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
33  virtual ~EC2NPoint() {}
34 #endif
35 
36  bool identity;
37  PolynomialMod2 x, y;
38 };
39 
40 CRYPTOPP_DLL_TEMPLATE_CLASS AbstractGroup<EC2NPoint>;
41 
42 //! Elliptic Curve over GF(2^n)
43 class CRYPTOPP_DLL EC2N : public AbstractGroup<EC2NPoint>
44 {
45 public:
46  typedef GF2NP Field;
47  typedef Field::Element FieldElement;
48  typedef EC2NPoint Point;
49 
50  EC2N() {}
51  EC2N(const Field &field, const Field::Element &a, const Field::Element &b)
52  : m_field(field), m_a(a), m_b(b) {}
53  // construct from BER encoded parameters
54  // this constructor will decode and extract the the fields fieldID and curve of the sequence ECParameters
56 
57  // encode the fields fieldID and curve of the sequence ECParameters
58  void DEREncode(BufferedTransformation &bt) const;
59 
60  bool Equal(const Point &P, const Point &Q) const;
61  const Point& Identity() const;
62  const Point& Inverse(const Point &P) const;
63  bool InversionIsFast() const {return true;}
64  const Point& Add(const Point &P, const Point &Q) const;
65  const Point& Double(const Point &P) const;
66 
67  Point Multiply(const Integer &k, const Point &P) const
68  {return ScalarMultiply(P, k);}
69  Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const
70  {return CascadeScalarMultiply(P, k1, Q, k2);}
71 
72  bool ValidateParameters(RandomNumberGenerator &rng, unsigned int level=3) const;
73  bool VerifyPoint(const Point &P) const;
74 
75  unsigned int EncodedPointSize(bool compressed = false) const
76  {return 1 + (compressed?1:2)*m_field->MaxElementByteLength();}
77  // returns false if point is compressed and not valid (doesn't check if uncompressed)
78  bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const;
79  bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const;
80  void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const;
81  void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
82 
83  Point BERDecodePoint(BufferedTransformation &bt) const;
84  void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
85 
86  Integer FieldSize() const {return Integer::Power2(m_field->MaxElementBitLength());}
87  const Field & GetField() const {return *m_field;}
88  const FieldElement & GetA() const {return m_a;}
89  const FieldElement & GetB() const {return m_b;}
90 
91  bool operator==(const EC2N &rhs) const
92  {return GetField() == rhs.GetField() && m_a == rhs.m_a && m_b == rhs.m_b;}
93 
94 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
95  virtual ~EC2N() {}
96 #endif
97 
98 private:
99  clonable_ptr<Field> m_field;
100  FieldElement m_a, m_b;
101  mutable Point m_R;
102 };
103 
104 CRYPTOPP_DLL_TEMPLATE_CLASS DL_FixedBasePrecomputationImpl<EC2N::Point>;
105 CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupPrecomputation<EC2N::Point>;
106 
107 template <class T> class EcPrecomputation;
108 
109 //! EC2N precomputation
110 template<> class EcPrecomputation<EC2N> : public DL_GroupPrecomputation<EC2N::Point>
111 {
112 public:
113  typedef EC2N EllipticCurve;
114 
115  // DL_GroupPrecomputation
116  const AbstractGroup<Element> & GetGroup() const {return m_ec;}
117  Element BERDecodeElement(BufferedTransformation &bt) const {return m_ec.BERDecodePoint(bt);}
118  void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {m_ec.DEREncodePoint(bt, v, false);}
119 
120  // non-inherited
121  void SetCurve(const EC2N &ec) {m_ec = ec;}
122  const EC2N & GetCurve() const {return m_ec;}
123 
124 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
125  virtual ~EcPrecomputation() {}
126 #endif
127 
128 private:
129  EC2N m_ec;
130 };
131 
132 NAMESPACE_END
133 
134 #endif
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:1085
Interface for buffered transformations.
Definition: cryptlib.h:1247
Polynomial with Coefficients in GF(2)
Definition: gf2n.h:18
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:2910
Multiple precision integer with arithmetic operations.
Definition: integer.h:31
Elliptic Curve over GF(2^n)
Definition: ec2n.h:43
bool operator<(const ::PolynomialMod2 &a, const ::PolynomialMod2 &b)
compares degree
Definition: gf2n.h:253
Classes for precomputation in a group.
GF(2^n) with Polynomial Basis.
Definition: gf2n.h:282
Crypto++ library namespace.
Elliptic Curve Point.
Definition: ec2n.h:21