Crypto++  8.0
Free C++ class library of cryptographic schemes
ecp.h
Go to the documentation of this file.
1 // ecp.h - originally 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 "algebra.h"
12 #include "modarith.h"
13 #include "ecpoint.h"
14 #include "eprecomp.h"
15 #include "smartptr.h"
16 #include "pubkey.h"
17 
18 #if CRYPTOPP_MSC_VERSION
19 # pragma warning(push)
20 # pragma warning(disable: 4231 4275)
21 #endif
22 
23 NAMESPACE_BEGIN(CryptoPP)
24 
25 /// \brief Elliptic Curve over GF(p), where p is prime
26 class CRYPTOPP_DLL ECP : public AbstractGroup<ECPPoint>, public EncodedPoint<ECPPoint>
27 {
28 public:
29  typedef ModularArithmetic Field;
30  typedef Integer FieldElement;
31  typedef ECPPoint Point;
32 
33  virtual ~ECP() {}
34 
35  /// \brief Construct an ECP
36  ECP() {}
37 
38  /// \brief Copy construct an ECP
39  /// \param ecp the other ECP object
40  /// \param convertToMontgomeryRepresentation flag indicating if the curve should be converted to a MontgomeryRepresentation
41  /// \sa ModularArithmetic, MontgomeryRepresentation
42  ECP(const ECP &ecp, bool convertToMontgomeryRepresentation = false);
43 
44  /// \brief Construct an ECP
45  /// \param modulus the prime modulus
46  /// \param a Field::Element
47  /// \param b Field::Element
48  ECP(const Integer &modulus, const FieldElement &a, const FieldElement &b)
49  : m_fieldPtr(new Field(modulus)), m_a(a.IsNegative() ? modulus+a : a), m_b(b) {}
50 
51  /// \brief Construct an ECP from BER encoded parameters
52  /// \param bt BufferedTransformation derived object
53  /// \details This constructor will decode and extract the the fields fieldID and curve of the sequence ECParameters
55 
56  /// \brief Encode the fields fieldID and curve of the sequence ECParameters
57  /// \param bt BufferedTransformation derived object
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  Point ScalarMultiply(const Point &P, const Integer &k) const;
67  Point CascadeScalarMultiply(const Point &P, const Integer &k1, const Point &Q, const Integer &k2) const;
68  void SimultaneousMultiply(Point *results, const Point &base, const Integer *exponents, unsigned int exponentsCount) const;
69 
70  Point Multiply(const Integer &k, const Point &P) const
71  {return ScalarMultiply(P, k);}
72  Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const
73  {return CascadeScalarMultiply(P, k1, Q, k2);}
74 
75  bool ValidateParameters(RandomNumberGenerator &rng, unsigned int level=3) const;
76  bool VerifyPoint(const Point &P) const;
77 
78  unsigned int EncodedPointSize(bool compressed = false) const
79  {return 1 + (compressed?1:2)*GetField().MaxElementByteLength();}
80  // returns false if point is compressed and not valid (doesn't check if uncompressed)
81  bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const;
82  bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const;
83  void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const;
84  void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
85 
86  Point BERDecodePoint(BufferedTransformation &bt) const;
87  void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
88 
89  Integer FieldSize() const {return GetField().GetModulus();}
90  const Field & GetField() const {return *m_fieldPtr;}
91  const FieldElement & GetA() const {return m_a;}
92  const FieldElement & GetB() const {return m_b;}
93 
94  bool operator==(const ECP &rhs) const
95  {return GetField() == rhs.GetField() && m_a == rhs.m_a && m_b == rhs.m_b;}
96 
97 private:
98  clonable_ptr<Field> m_fieldPtr;
99  FieldElement m_a, m_b;
100  mutable Point m_R;
101 };
102 
103 CRYPTOPP_DLL_TEMPLATE_CLASS DL_FixedBasePrecomputationImpl<ECP::Point>;
104 CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupPrecomputation<ECP::Point>;
105 
106 /// \brief Elliptic Curve precomputation
107 /// \tparam EC elliptic curve field
108 template <class EC> class EcPrecomputation;
109 
110 /// \brief ECP precomputation specialization
111 /// \details Implementation of <tt>DL_GroupPrecomputation<ECP::Point></tt> with input and output
112 /// conversions for Montgomery modular multiplication.
113 /// \sa DL_GroupPrecomputation, ModularArithmetic, MontgomeryRepresentation
114 template<> class EcPrecomputation<ECP> : public DL_GroupPrecomputation<ECP::Point>
115 {
116 public:
117  typedef ECP EllipticCurve;
118 
119  virtual ~EcPrecomputation() {}
120 
121  // DL_GroupPrecomputation
122  bool NeedConversions() const {return true;}
123  Element ConvertIn(const Element &P) const
124  {return P.identity ? P : ECP::Point(m_ec->GetField().ConvertIn(P.x), m_ec->GetField().ConvertIn(P.y));};
125  Element ConvertOut(const Element &P) const
126  {return P.identity ? P : ECP::Point(m_ec->GetField().ConvertOut(P.x), m_ec->GetField().ConvertOut(P.y));}
127  const AbstractGroup<Element> & GetGroup() const {return *m_ec;}
128  Element BERDecodeElement(BufferedTransformation &bt) const {return m_ec->BERDecodePoint(bt);}
129  void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {m_ec->DEREncodePoint(bt, v, false);}
130 
131  /// \brief Set the elliptic curve
132  /// \param ec ECP derived class
133  /// \details SetCurve() is not inherited
134  void SetCurve(const ECP &ec)
135  {
136  m_ec.reset(new ECP(ec, true));
137  m_ecOriginal = ec;
138  }
139 
140  /// \brief Get the elliptic curve
141  /// \returns ECP curve
142  /// \details GetCurve() is not inherited
143  const ECP & GetCurve() const {return *m_ecOriginal;}
144 
145 private:
146  value_ptr<ECP> m_ec, m_ecOriginal;
147 };
148 
149 NAMESPACE_END
150 
151 #if CRYPTOPP_MSC_VERSION
152 # pragma warning(pop)
153 #endif
154 
155 #endif
Elliptical Curve Point over GF(p), where p is prime.
Definition: ecpoint.h:20
This file contains helper classes/functions for implementing public key algorithms.
const char * Identity()
ConstByteArrayParameter.
Definition: argnames.h:94
Elliptic Curve over GF(p), where p is prime.
Definition: ecp.h:26
Abstract base classes that provide a uniform interface to this library.
bool InversionIsFast() const
Determine if inversion is fast.
Definition: ecp.h:63
Classes for automatic resource management.
Ring of congruence classes modulo n.
Definition: modarith.h:38
Interface for random number generators.
Definition: cryptlib.h:1383
Element ConvertIn(const Element &P) const
Converts an element between representations.
Definition: ecp.h:123
Classes for Elliptic Curve points.
Classes for performing mathematics over different fields.
Interface for buffered transformations.
Definition: cryptlib.h:1598
bool operator==(const OID &lhs, const OID &rhs)
Compare two OIDs for equality.
void DEREncodeElement(BufferedTransformation &bt, const Element &v) const
Encodes element in DER format.
Definition: ecp.h:129
A pointer which can be copied and cloned.
Definition: smartptr.h:103
Multiple precision integer with arithmetic operations.
Definition: integer.h:49
const AbstractGroup< Element > & GetGroup() const
Retrieves AbstractGroup interface.
Definition: ecp.h:127
Element BERDecodeElement(BufferedTransformation &bt) const
Decodes element in DER format.
Definition: ecp.h:128
Abstract group.
Definition: algebra.h:26
Classes for precomputation in a group.
Abstract class for encoding and decoding ellicptic curve points.
Definition: ecpoint.h:90
Elliptic Curve precomputation.
Definition: ec2n.h:100
ECP()
Construct an ECP.
Definition: ecp.h:36
Multiple precision integer with arithmetic operations.
bool NeedConversions() const
Determines if elements needs conversion.
Definition: ecp.h:122
unsigned int EncodedPointSize(bool compressed=false) const
Determines encoded point size.
Definition: ecp.h:78
Class file for performing modular arithmetic.
Crypto++ library namespace.
ECP(const Integer &modulus, const FieldElement &a, const FieldElement &b)
Construct an ECP.
Definition: ecp.h:48
const ECP & GetCurve() const
Get the elliptic curve.
Definition: ecp.h:143
Element ConvertOut(const Element &P) const
Converts an element between representations.
Definition: ecp.h:125
DL_FixedBasePrecomputation adapter class.
Definition: eprecomp.h:126
void SetCurve(const ECP &ec)
Set the elliptic curve.
Definition: ecp.h:134