Security Scol plugin
ecp.h
Go to the documentation of this file.
1// ecp.h - originally written and placed in the public domain by Wei Dai
2
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
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
36 ECP() {}
37
46 ECP(const ECP &ecp, bool convertToMontgomeryRepresentation);
47
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
60
65 void DEREncode(BufferedTransformation &bt) const;
66
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
109private:
110 clonable_ptr<Field> m_fieldPtr;
111 FieldElement m_a, m_b;
112 mutable Point m_R;
113};
114
115CRYPTOPP_DLL_TEMPLATE_CLASS DL_FixedBasePrecomputationImpl<ECP::Point>;
116CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupPrecomputation<ECP::Point>;
117
120template <class EC> class EcPrecomputation;
121
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
146 void SetCurve(const ECP &ec)
147 {
148 m_ec.reset(new ECP(ec, true));
149 m_ecOriginal = ec;
150 }
151
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.
Abstract group.
Definition algebra.h:27
Interface for buffered transformations.
Definition cryptlib.h:1652
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:75
ECP()
Construct an ECP.
Definition ecp.h:36
ECP(const Integer &modulus, const FieldElement &a, const FieldElement &b)
Construct an ECP.
Definition ecp.h:52
unsigned int EncodedPointSize(bool compressed=false) const
Determines encoded point size.
Definition ecp.h:90
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
Value pointer.
Definition smartptr.h:77
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.
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