Crypto++  5.6.3
Free C++ class library of cryptographic schemes
ecp.h
Go to the documentation of this file.
1 // ecp.h - written and placed in the public domain by Wei Dai
2 
3 //! \file ecp.h
4 //! \brief Classes for Elliptic Curves over prime fields
5 
6 #ifndef CRYPTOPP_ECP_H
7 #define CRYPTOPP_ECP_H
8 
9 #include "cryptlib.h"
10 #include "integer.h"
11 #include "modarith.h"
12 #include "eprecomp.h"
13 #include "smartptr.h"
14 #include "pubkey.h"
15 
16 NAMESPACE_BEGIN(CryptoPP)
17 
18 //! Elliptical Curve Point
19 struct CRYPTOPP_DLL ECPPoint
20 {
21  ECPPoint() : identity(true) {}
22  ECPPoint(const Integer &x, const Integer &y)
23  : identity(false), x(x), y(y) {}
24 
25  bool operator==(const ECPPoint &t) const
26  {return (identity && t.identity) || (!identity && !t.identity && x==t.x && y==t.y);}
27  bool operator< (const ECPPoint &t) const
28  {return identity ? !t.identity : (!t.identity && (x<t.x || (x==t.x && y<t.y)));}
29 
30 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
31  virtual ~ECPPoint() {}
32 #endif
33 
34  bool identity;
35  Integer x, y;
36 };
37 
38 CRYPTOPP_DLL_TEMPLATE_CLASS AbstractGroup<ECPPoint>;
39 
40 //! Elliptic Curve over GF(p), where p is prime
41 class CRYPTOPP_DLL ECP : public AbstractGroup<ECPPoint>
42 {
43 public:
44  typedef ModularArithmetic Field;
45  typedef Integer FieldElement;
46  typedef ECPPoint Point;
47 
48  ECP() {}
49  ECP(const ECP &ecp, bool convertToMontgomeryRepresentation = false);
50  ECP(const Integer &modulus, const FieldElement &a, const FieldElement &b)
51  : m_fieldPtr(new Field(modulus)), m_a(a.IsNegative() ? modulus+a : a), m_b(b) {}
52  // construct from BER encoded parameters
53  // this constructor will decode and extract the the fields fieldID and curve of the sequence ECParameters
55 
56  // encode the fields fieldID and curve of the sequence ECParameters
57  void DEREncode(BufferedTransformation &bt) const;
58 
59  bool Equal(const Point &P, const Point &Q) const;
60  const Point& Identity() const;
61  const Point& Inverse(const Point &P) const;
62  bool InversionIsFast() const {return true;}
63  const Point& Add(const Point &P, const Point &Q) const;
64  const Point& Double(const Point &P) const;
65  Point ScalarMultiply(const Point &P, const Integer &k) const;
66  Point CascadeScalarMultiply(const Point &P, const Integer &k1, const Point &Q, const Integer &k2) const;
67  void SimultaneousMultiply(Point *results, const Point &base, const Integer *exponents, unsigned int exponentsCount) const;
68 
69  Point Multiply(const Integer &k, const Point &P) const
70  {return ScalarMultiply(P, k);}
71  Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const
72  {return CascadeScalarMultiply(P, k1, Q, k2);}
73 
74  bool ValidateParameters(RandomNumberGenerator &rng, unsigned int level=3) const;
75  bool VerifyPoint(const Point &P) const;
76 
77  unsigned int EncodedPointSize(bool compressed = false) const
78  {return 1 + (compressed?1:2)*GetField().MaxElementByteLength();}
79  // returns false if point is compressed and not valid (doesn't check if uncompressed)
80  bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const;
81  bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const;
82  void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const;
83  void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
84 
85  Point BERDecodePoint(BufferedTransformation &bt) const;
86  void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
87 
88  Integer FieldSize() const {return GetField().GetModulus();}
89  const Field & GetField() const {return *m_fieldPtr;}
90  const FieldElement & GetA() const {return m_a;}
91  const FieldElement & GetB() const {return m_b;}
92 
93  bool operator==(const ECP &rhs) const
94  {return GetField() == rhs.GetField() && m_a == rhs.m_a && m_b == rhs.m_b;}
95 
96 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
97  virtual ~ECP() {}
98 #endif
99 
100 private:
101  clonable_ptr<Field> m_fieldPtr;
102  FieldElement m_a, m_b;
103  mutable Point m_R;
104 };
105 
106 CRYPTOPP_DLL_TEMPLATE_CLASS DL_FixedBasePrecomputationImpl<ECP::Point>;
107 CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupPrecomputation<ECP::Point>;
108 
109 template <class T> class EcPrecomputation;
110 
111 //! ECP precomputation
112 template<> class EcPrecomputation<ECP> : public DL_GroupPrecomputation<ECP::Point>
113 {
114 public:
115  typedef ECP EllipticCurve;
116 
117  // DL_GroupPrecomputation
118  bool NeedConversions() const {return true;}
119  Element ConvertIn(const Element &P) const
120  {return P.identity ? P : ECP::Point(m_ec->GetField().ConvertIn(P.x), m_ec->GetField().ConvertIn(P.y));};
121  Element ConvertOut(const Element &P) const
122  {return P.identity ? P : ECP::Point(m_ec->GetField().ConvertOut(P.x), m_ec->GetField().ConvertOut(P.y));}
123  const AbstractGroup<Element> & GetGroup() const {return *m_ec;}
124  Element BERDecodeElement(BufferedTransformation &bt) const {return m_ec->BERDecodePoint(bt);}
125  void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {m_ec->DEREncodePoint(bt, v, false);}
126 
127  // non-inherited
128  void SetCurve(const ECP &ec)
129  {
130  m_ec.reset(new ECP(ec, true));
131  m_ecOriginal = ec;
132  }
133  const ECP & GetCurve() const {return *m_ecOriginal;}
134 
135 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
136  virtual ~EcPrecomputation() {}
137 #endif
138 
139 private:
140  value_ptr<ECP> m_ec, m_ecOriginal;
141 };
142 
143 NAMESPACE_END
144 
145 #endif
Elliptical Curve Point.
Definition: ecp.h:19
This file contains helper classes/functions for implementing public key algorithms.
Elliptic Curve over GF(p), where p is prime.
Definition: ecp.h:41
Abstract base classes that provide a uniform interface to this library.
Classes for automatic resource management.
Ring of congruence classes modulo n.
Definition: modarith.h:27
Interface for random number generators.
Definition: cryptlib.h:1085
Interface for buffered transformations.
Definition: cryptlib.h:1247
A pointer which can be copied and cloned.
Definition: smartptr.h:108
Multiple precision integer with arithmetic operations.
Definition: integer.h:31
bool operator<(const ::PolynomialMod2 &a, const ::PolynomialMod2 &b)
compares degree
Definition: gf2n.h:253
Classes for precomputation in a group.
Class file for performing modular arithmetic.
Crypto++ library namespace.