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