Crypto++ 8.9 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 Construct an ECP
39 /// \param ecp the other ECP object
40 /// \param convertToMontgomeryRepresentation flag indicating if the curve
41 /// should be converted to a MontgomeryRepresentation.
42 /// \details Prior to Crypto++ 8.3 the default value for
43 /// convertToMontgomeryRepresentation was false. it was changed due to
44 /// two audit tools finding, "Signature-compatible with a copy constructor".
45 /// \sa ModularArithmetic, MontgomeryRepresentation
46 ECP(const ECP &ecp, bool convertToMontgomeryRepresentation);
47
48 /// \brief Construct an ECP
49 /// \param modulus the prime modulus
50 /// \param a Field::Element
51 /// \param b Field::Element
52 ECP(const Integer &modulus, const FieldElement &a, const FieldElement &b)
53 : m_fieldPtr(new Field(modulus)), m_a(a.IsNegative() ? modulus+a : a), m_b(b) {}
54
55 /// \brief Construct an ECP from BER encoded parameters
56 /// \param bt BufferedTransformation derived object
57 /// \details This constructor will decode and extract the fields
58 /// fieldID and curve of the sequence ECParameters
59 ECP(BufferedTransformation &bt);
60
61 /// \brief DER Encode
62 /// \param bt BufferedTransformation derived object
63 /// \details DEREncode encode the fields fieldID and curve of the sequence
64 /// ECParameters
65 void DEREncode(BufferedTransformation &bt) const;
66
67 /// \brief Compare two points
68 /// \param P the first point
69 /// \param Q the second point
70 /// \return true if equal, false otherwise
71 bool Equal(const Point &P, const Point &Q) const;
72
73 const Point& Identity() const;
74 const Point& Inverse(const Point &P) const;
75 bool InversionIsFast() const {return true;}
76 const Point& Add(const Point &P, const Point &Q) const;
77 const Point& Double(const Point &P) const;
78 Point ScalarMultiply(const Point &P, const Integer &k) const;
79 Point CascadeScalarMultiply(const Point &P, const Integer &k1, const Point &Q, const Integer &k2) const;
80 void SimultaneousMultiply(Point *results, const Point &base, const Integer *exponents, unsigned int exponentsCount) const;
81
82 Point Multiply(const Integer &k, const Point &P) const
83 {return ScalarMultiply(P, k);}
84 Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const
85 {return CascadeScalarMultiply(P, k1, Q, k2);}
86
87 bool ValidateParameters(RandomNumberGenerator &rng, unsigned int level=3) const;
88 bool VerifyPoint(const Point &P) const;
89
90 unsigned int EncodedPointSize(bool compressed = false) const
91 {return 1 + (compressed?1:2)*GetField().MaxElementByteLength();}
92 // returns false if point is compressed and not valid (doesn't check if uncompressed)
93 bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const;
94 bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const;
95 void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const;
96 void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
97
98 Point BERDecodePoint(BufferedTransformation &bt) const;
99 void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
100
101 Integer FieldSize() const {return GetField().GetModulus();}
102 const Field & GetField() const {return *m_fieldPtr;}
103 const FieldElement & GetA() const {return m_a;}
104 const FieldElement & GetB() const {return m_b;}
105
106 bool operator==(const ECP &rhs) const
107 {return GetField() == rhs.GetField() && m_a == rhs.m_a && m_b == rhs.m_b;}
108
109 private:
110 clonable_ptr<Field> m_fieldPtr;
111 FieldElement m_a, m_b;
112 mutable Point m_R;
113 };
114
115 CRYPTOPP_DLL_TEMPLATE_CLASS DL_FixedBasePrecomputationImpl<ECP::Point>;
116 CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupPrecomputation<ECP::Point>;
117
118 /// \brief Elliptic Curve precomputation
119 /// \tparam EC elliptic curve field
120 template <class EC> class EcPrecomputation;
121
122 /// \brief ECP precomputation specialization
123 /// \details Implementation of <tt>DL_GroupPrecomputation<ECP::Point></tt> with input and output
124 /// conversions for Montgomery modular multiplication.
125 /// \sa DL_GroupPrecomputation, ModularArithmetic, MontgomeryRepresentation
126 template<> class EcPrecomputation<ECP> : public DL_GroupPrecomputation<ECP::Point>
127 {
128 public:
129 typedef ECP EllipticCurve;
130
131 virtual ~EcPrecomputation() {}
132
133 // DL_GroupPrecomputation
134 bool NeedConversions() const {return true;}
135 Element ConvertIn(const Element &P) const
136 {return P.identity ? P : ECP::Point(m_ec->GetField().ConvertIn(P.x), m_ec->GetField().ConvertIn(P.y));};
137 Element ConvertOut(const Element &P) const
138 {return P.identity ? P : ECP::Point(m_ec->GetField().ConvertOut(P.x), m_ec->GetField().ConvertOut(P.y));}
139 const AbstractGroup<Element> & GetGroup() const {return *m_ec;}
140 Element BERDecodeElement(BufferedTransformation &bt) const {return m_ec->BERDecodePoint(bt);}
141 void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {m_ec->DEREncodePoint(bt, v, false);}
142
143 /// \brief Set the elliptic curve
144 /// \param ec ECP derived class
145 /// \details SetCurve() is not inherited
146 void SetCurve(const ECP &ec)
147 {
148 m_ec.reset(new ECP(ec, true));
149 m_ecOriginal = ec;
150 }
151
152 /// \brief Get the elliptic curve
153 /// \return ECP curve
154 /// \details GetCurve() is not inherited
155 const ECP & GetCurve() const {return *m_ecOriginal;}
156
157 private:
158 value_ptr<ECP> m_ec, m_ecOriginal;
159 };
160
161 NAMESPACE_END
162
163 #if CRYPTOPP_MSC_VERSION
164 # pragma warning(pop)
165 #endif
166
167 #endif
algebra.h
Classes for performing mathematics over different fields.
operator==
bool operator==(const OID &lhs, const OID &rhs)
Compare two OIDs for equality.
AbstractGroup
Abstract group.
Definition: algebra.h:27
BufferedTransformation
Interface for buffered transformations.
Definition: cryptlib.h:1657
DL_FixedBasePrecomputationImpl
DL_FixedBasePrecomputation adapter class.
Definition: eprecomp.h:127
DL_GroupPrecomputation< ECP::Point >
ECP
Elliptic Curve over GF(p), where p is prime.
Definition: ecp.h:27
ECP::InversionIsFast
bool InversionIsFast() const
Determine if inversion is fast.
Definition: ecp.h:75
ECP::Inverse
const Point & Inverse(const Point &P) const
Inverts the element in the group.
ECP::EncodePoint
void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const
Encodes an elliptic curve point.
ECP::EncodePoint
void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const
Encodes an elliptic curve point.
ECP::ECP
ECP()
Construct an ECP.
Definition: ecp.h:36
ECP::Equal
bool Equal(const Point &P, const Point &Q) const
Compare two points.
ECP::DEREncodePoint
void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const
DER Encodes an elliptic curve point.
ECP::ECP
ECP(const Integer &modulus, const FieldElement &a, const FieldElement &b)
Construct an ECP.
Definition: ecp.h:52
ECP::VerifyPoint
bool VerifyPoint(const Point &P) const
Verifies points on elliptic curve.
ECP::Identity
const Point & Identity() const
Provides the Identity element.
ECP::BERDecodePoint
Point BERDecodePoint(BufferedTransformation &bt) const
BER Decodes an elliptic curve point.
ECP::ECP
ECP(BufferedTransformation &bt)
Construct an ECP from BER encoded parameters.
ECP::EncodedPointSize
unsigned int EncodedPointSize(bool compressed=false) const
Determines encoded point size.
Definition: ecp.h:90
ECP::ECP
ECP(const ECP &ecp, bool convertToMontgomeryRepresentation)
Construct an ECP.
ECP::DecodePoint
bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const
Decodes an elliptic curve point.
ECP::DecodePoint
bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const
Decodes an elliptic curve point.
ECP::DEREncode
void DEREncode(BufferedTransformation &bt) const
DER Encode.
ECP::Add
const Point & Add(const Point &P, const Point &Q) const
Adds elements in the group.
EcPrecomputation< ECP >::DEREncodeElement
void DEREncodeElement(BufferedTransformation &bt, const Element &v) const
Encodes element in DER format.
Definition: ecp.h:141
EcPrecomputation< ECP >::GetCurve
const ECP & GetCurve() const
Get the elliptic curve.
Definition: ecp.h:155
EcPrecomputation< ECP >::GetGroup
const AbstractGroup< Element > & GetGroup() const
Retrieves AbstractGroup interface.
Definition: ecp.h:139
EcPrecomputation< ECP >::ConvertOut
Element ConvertOut(const Element &P) const
Converts an element between representations.
Definition: ecp.h:137
EcPrecomputation< ECP >::ConvertIn
Element ConvertIn(const Element &P) const
Converts an element between representations.
Definition: ecp.h:135
EcPrecomputation< ECP >::BERDecodeElement
Element BERDecodeElement(BufferedTransformation &bt) const
Decodes element in DER format.
Definition: ecp.h:140
EcPrecomputation< ECP >::SetCurve
void SetCurve(const ECP &ec)
Set the elliptic curve.
Definition: ecp.h:146
EcPrecomputation< ECP >::NeedConversions
bool NeedConversions() const
Determines if elements needs conversion.
Definition: ecp.h:134
EcPrecomputation
Elliptic Curve precomputation.
Definition: ec2n.h:99
EncodedPoint
Abstract class for encoding and decoding ellicptic curve points.
Definition: ecpoint.h:91
Integer
Multiple precision integer with arithmetic operations.
Definition: integer.h:50
ModularArithmetic
Ring of congruence classes modulo n.
Definition: modarith.h:44
RandomNumberGenerator
Interface for random number generators.
Definition: cryptlib.h:1440
clonable_ptr
A pointer which can be copied and cloned.
Definition: smartptr.h:105
value_ptr< ECP >
CRYPTOPP_DLL_TEMPLATE_CLASS
#define CRYPTOPP_DLL_TEMPLATE_CLASS
Instantiate templates in a dynamic library.
Definition: config_dll.h:72
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
ecpoint.h
Classes for Elliptic Curve points.
eprecomp.h
Classes for precomputation in a group.
integer.h
Multiple precision integer with arithmetic operations.
modarith.h
Class file for performing modular arithmetic.
CryptoPP
Crypto++ library namespace.
pubkey.h
This file contains helper classes/functions for implementing public key algorithms.
smartptr.h
Classes for automatic resource management.
ECPPoint
Elliptical Curve Point over GF(p), where p is prime.
Definition: ecpoint.h:21