Crypto++ 8.2
Free C&
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 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
97private:
98 clonable_ptr<Field> m_fieldPtr;
99 FieldElement m_a, m_b;
100 mutable Point m_R;
101};
102
103CRYPTOPP_DLL_TEMPLATE_CLASS DL_FixedBasePrecomputationImpl<ECP::Point>;
104CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupPrecomputation<ECP::Point>;
105
106/// \brief Elliptic Curve precomputation
107/// \tparam EC elliptic curve field
108template <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
114template<> class EcPrecomputation<ECP> : public DL_GroupPrecomputation<ECP::Point>
115{
116public:
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
145private:
146 value_ptr<ECP> m_ec, m_ecOriginal;
147};
148
149NAMESPACE_END
150
151#if CRYPTOPP_MSC_VERSION
152# pragma warning(pop)
153#endif
154
155#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:1599
DL_FixedBasePrecomputation adapter class.
Definition: eprecomp.h:127
DL_GroupPrecomputation interface.
Definition: eprecomp.h:20
Elliptic Curve over GF(p), where p is prime.
Definition: ecp.h:27
bool InversionIsFast() const
Determine if inversion is fast.
Definition: ecp.h:63
ECP()
Construct an ECP.
Definition: ecp.h:36
ECP(const Integer &modulus, const FieldElement &a, const FieldElement &b)
Construct an ECP.
Definition: ecp.h:48
unsigned int EncodedPointSize(bool compressed=false) const
Determines encoded point size.
Definition: ecp.h:78
void DEREncodeElement(BufferedTransformation &bt, const Element &v) const
Encodes element in DER format.
Definition: ecp.h:129
Element ConvertOut(const Element &P) const
Converts an element between representations.
Definition: ecp.h:125
Element ConvertIn(const Element &P) const
Converts an element between representations.
Definition: ecp.h:123
Element BERDecodeElement(BufferedTransformation &bt) const
Decodes element in DER format.
Definition: ecp.h:128
const AbstractGroup< Element > & GetGroup() const
Retrieves AbstractGroup interface.
Definition: ecp.h:127
void SetCurve(const ECP &ec)
Set the elliptic curve.
Definition: ecp.h:134
const ECP & GetCurve() const
Get the elliptic curve.
Definition: ecp.h:143
bool NeedConversions() const
Determines if elements needs conversion.
Definition: ecp.h:122
Elliptic Curve precomputation.
Definition: ec2n.h:100
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:39
Interface for random number generators.
Definition: cryptlib.h:1384
A pointer which can be copied and cloned.
Definition: smartptr.h:104
Value pointer.
Definition: smartptr.h:76
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