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