Crypto++ 8.7
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
23NAMESPACE_BEGIN(CryptoPP)
24
25/// \brief Elliptic Curve over GF(p), where p is prime
26class CRYPTOPP_DLL ECP : public AbstractGroup<ECPPoint>, public EncodedPoint<ECPPoint>
27{
28public:
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
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
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
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
109private:
110 clonable_ptr<Field> m_fieldPtr;
111 FieldElement m_a, m_b;
112 mutable Point m_R;
113};
114
117
118/// \brief Elliptic Curve precomputation
119/// \tparam EC elliptic curve field
120template <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
126template<> class EcPrecomputation<ECP> : public DL_GroupPrecomputation<ECP::Point>
127{
128public:
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
157private:
158 value_ptr<ECP> m_ec, m_ecOriginal;
159};
160
161NAMESPACE_END
162
163#if CRYPTOPP_MSC_VERSION
164# pragma warning(pop)
165#endif
166
167#endif
Classes for performing mathematics over different fields.
bool operator==(const OID &lhs, const OID &rhs)
Compare two OIDs for equality.
Abstract group.
Definition: algebra.h:27
Interface for buffered transformations.
Definition: cryptlib.h:1652
DL_FixedBasePrecomputation adapter class.
Definition: eprecomp.h:127
Elliptic Curve over GF(p), where p is prime.
Definition: ecp.h:27
bool InversionIsFast() const
Determine if inversion is fast.
Definition: ecp.h:75
const Point & Double(const Point &P) const
Doubles an element in the group.
Point ScalarMultiply(const Point &P, const Integer &k) const
Performs a scalar multiplication.
void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const
Encodes an elliptic curve point.
void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const
Encodes an elliptic curve point.
ECP()
Construct an ECP.
Definition: ecp.h:36
bool Equal(const Point &P, const Point &Q) const
Compare two points.
void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const
DER Encodes an elliptic curve point.
ECP(const Integer &modulus, const FieldElement &a, const FieldElement &b)
Construct an ECP.
Definition: ecp.h:52
bool VerifyPoint(const Point &P) const
Verifies points on elliptic curve.
Point CascadeScalarMultiply(const Point &P, const Integer &k1, const Point &Q, const Integer &k2) const
TODO.
const Point & Inverse(const Point &P) const
Inverts the element in the group.
const Point & Identity() const
Provides the Identity element.
Point BERDecodePoint(BufferedTransformation &bt) const
BER Decodes an elliptic curve point.
ECP(BufferedTransformation &bt)
Construct an ECP from BER encoded parameters.
unsigned int EncodedPointSize(bool compressed=false) const
Determines encoded point size.
Definition: ecp.h:90
ECP(const ECP &ecp, bool convertToMontgomeryRepresentation)
Construct an ECP.
bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const
Decodes an elliptic curve point.
bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const
Decodes an elliptic curve point.
void DEREncode(BufferedTransformation &bt) const
DER Encode.
const Point & Add(const Point &P, const Point &Q) const
Adds elements in the group.
void SimultaneousMultiply(Point *results, const Point &base, const Integer *exponents, unsigned int exponentsCount) const
Multiplies a base to multiple exponents in a group.
void DEREncodeElement(BufferedTransformation &bt, const Element &v) const
Encodes element in DER format.
Definition: ecp.h:141
Element ConvertOut(const Element &P) const
Converts an element between representations.
Definition: ecp.h:137
Element ConvertIn(const Element &P) const
Converts an element between representations.
Definition: ecp.h:135
Element BERDecodeElement(BufferedTransformation &bt) const
Decodes element in DER format.
Definition: ecp.h:140
const AbstractGroup< Element > & GetGroup() const
Retrieves AbstractGroup interface.
Definition: ecp.h:139
void SetCurve(const ECP &ec)
Set the elliptic curve.
Definition: ecp.h:146
const ECP & GetCurve() const
Get the elliptic curve.
Definition: ecp.h:155
bool NeedConversions() const
Determines if elements needs conversion.
Definition: ecp.h:134
Elliptic Curve precomputation.
Definition: ec2n.h:99
Abstract class for encoding and decoding ellicptic curve points.
Definition: ecpoint.h:91
Multiple precision integer with arithmetic operations.
Definition: integer.h:50
Ring of congruence classes modulo n.
Definition: modarith.h:44
Interface for random number generators.
Definition: cryptlib.h:1435
A pointer which can be copied and cloned.
Definition: smartptr.h:105
#define CRYPTOPP_DLL_TEMPLATE_CLASS
Instantiate templates in a dynamic library.
Definition: config_dll.h:72
Abstract base classes that provide a uniform interface to this library.
Classes for Elliptic Curve points.
Classes for precomputation in a group.
Multiple precision integer with arithmetic operations.
Class file for performing modular arithmetic.
Crypto++ library namespace.
This file contains helper classes/functions for implementing public key algorithms.
Classes for automatic resource management.
Elliptical Curve Point over GF(p), where p is prime.
Definition: ecpoint.h:21