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