1 // ecpoint.h - written and placed in the public domain by Jeffrey Walton
2 //             Data structures moved from ecp.h and ec2n.h. Added EncodedPoint interface
3 
4 /// \file ecpoint.h
5 /// \brief Classes for Elliptic Curve points
6 /// \since Crypto++ 6.0
7 
8 #ifndef CRYPTOPP_ECPOINT_H
9 #define CRYPTOPP_ECPOINT_H
10 
11 #include "cryptlib.h"
12 #include "integer.h"
13 #include "algebra.h"
14 #include "gf2n.h"
15 
16 NAMESPACE_BEGIN(CryptoPP)
17 
18 /// \brief Elliptical Curve Point over GF(p), where p is prime
19 /// \since Crypto++ 2.0
20 struct CRYPTOPP_DLL ECPPoint
21 {
~ECPPointECPPoint22 	virtual ~ECPPoint() {}
23 
24 	/// \brief Construct an ECPPoint
25 	/// \details identity is set to <tt>true</tt>
ECPPointECPPoint26 	ECPPoint() : identity(true) {}
27 
28 	/// \brief Construct an ECPPoint from coordinates
29 	/// \details identity is set to <tt>false</tt>
ECPPointECPPoint30 	ECPPoint(const Integer &x, const Integer &y)
31 		: x(x), y(y), identity(false) {}
32 
33 	/// \brief Tests points for equality
34 	/// \param t the other point
35 	/// \return true if the points are equal, false otherwise
36 	bool operator==(const ECPPoint &t) const
37 		{return (identity && t.identity) || (!identity && !t.identity && x==t.x && y==t.y);}
38 
39 	/// \brief Tests points for ordering
40 	/// \param t the other point
41 	/// \return true if this point is less than other, false otherwise
42 	bool operator< (const ECPPoint &t) const
43 		{return identity ? !t.identity : (!t.identity && (x<t.x || (x==t.x && y<t.y)));}
44 
45 	Integer x, y;
46 	bool identity;
47 };
48 
49 CRYPTOPP_DLL_TEMPLATE_CLASS AbstractGroup<ECPPoint>;
50 
51 /// \brief Elliptical Curve Point over GF(2^n)
52 /// \since Crypto++ 2.0
53 struct CRYPTOPP_DLL EC2NPoint
54 {
~EC2NPointEC2NPoint55 	virtual ~EC2NPoint() {}
56 
57 	/// \brief Construct an EC2NPoint
58 	/// \details identity is set to <tt>true</tt>
EC2NPointEC2NPoint59 	EC2NPoint() : identity(true) {}
60 
61 	/// \brief Construct an EC2NPoint from coordinates
62 	/// \details identity is set to <tt>false</tt>
EC2NPointEC2NPoint63 	EC2NPoint(const PolynomialMod2 &x, const PolynomialMod2 &y)
64 		: x(x), y(y), identity(false) {}
65 
66 	/// \brief Tests points for equality
67 	/// \param t the other point
68 	/// \return true if the points are equal, false otherwise
69 	bool operator==(const EC2NPoint &t) const
70 		{return (identity && t.identity) || (!identity && !t.identity && x==t.x && y==t.y);}
71 
72 	/// \brief Tests points for ordering
73 	/// \param t the other point
74 	/// \return true if this point is less than other, false otherwise
75 	bool operator< (const EC2NPoint &t) const
76 		{return identity ? !t.identity : (!t.identity && (x<t.x || (x==t.x && y<t.y)));}
77 
78 	PolynomialMod2 x, y;
79 	bool identity;
80 };
81 
82 CRYPTOPP_DLL_TEMPLATE_CLASS AbstractGroup<EC2NPoint>;
83 
84 /// \brief Abstract class for encoding and decoding ellicptic curve points
85 /// \tparam Point ellicptic curve point
86 /// \details EncodedPoint is an interface for encoding and decoding elliptic curve points.
87 ///   The template parameter <tt>Point</tt> should be a class like ECP or EC2N.
88 /// \since Crypto++ 6.0
89 template <class Point>
90 class EncodedPoint
91 {
92 public:
~EncodedPoint()93 	virtual ~EncodedPoint() {}
94 
95 	/// \brief Decodes an elliptic curve point
96 	/// \param P point which is decoded
97 	/// \param bt source BufferedTransformation
98 	/// \param len number of bytes to read from the BufferedTransformation
99 	/// \return true if a point was decoded, false otherwise
100 	virtual bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const =0;
101 
102 	/// \brief Decodes an elliptic curve point
103 	/// \param P point which is decoded
104 	/// \param encodedPoint byte array with the encoded point
105 	/// \param len the size of the array
106 	/// \return true if a point was decoded, false otherwise
107 	virtual bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const =0;
108 
109 	/// \brief Verifies points on elliptic curve
110 	/// \param P point to verify
111 	/// \return true if the point is valid, false otherwise
112 	virtual bool VerifyPoint(const Point &P) const =0;
113 
114 	/// \brief Determines encoded point size
115 	/// \param compressed flag indicating if the point is compressed
116 	/// \return the minimum number of bytes required to encode the point
117 	virtual unsigned int EncodedPointSize(bool compressed = false) const =0;
118 
119 	/// \brief Encodes an elliptic curve point
120 	/// \param P point which is decoded
121 	/// \param encodedPoint byte array for the encoded point
122 	/// \param compressed flag indicating if the point is compressed
123 	/// \details <tt>encodedPoint</tt> must be at least EncodedPointSize() in length
124 	virtual void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const =0;
125 
126 	/// \brief Encodes an elliptic curve point
127 	/// \param bt target BufferedTransformation
128 	/// \param P point which is encoded
129 	/// \param compressed flag indicating if the point is compressed
130 	virtual void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const =0;
131 
132 	/// \brief BER Decodes an elliptic curve point
133 	/// \param bt source BufferedTransformation
134 	/// \return the decoded elliptic curve point
135 	virtual Point BERDecodePoint(BufferedTransformation &bt) const =0;
136 
137 	/// \brief DER Encodes an elliptic curve point
138 	/// \param bt target BufferedTransformation
139 	/// \param P point which is encoded
140 	/// \param compressed flag indicating if the point is compressed
141 	virtual void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const =0;
142 };
143 
144 NAMESPACE_END
145 
146 #endif  // CRYPTOPP_ECPOINT_H
147