Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:02

0001 // ecpoint.h - written and placed in the public domain by Jeffrey Walton

0002 //             Data structures moved from ecp.h and ec2n.h. Added EncodedPoint interface

0003 
0004 /// \file ecpoint.h

0005 /// \brief Classes for Elliptic Curve points

0006 /// \since Crypto++ 6.0

0007 
0008 #ifndef CRYPTOPP_ECPOINT_H
0009 #define CRYPTOPP_ECPOINT_H
0010 
0011 #include "cryptlib.h"
0012 #include "integer.h"
0013 #include "algebra.h"
0014 #include "gf2n.h"
0015 
0016 NAMESPACE_BEGIN(CryptoPP)
0017 
0018 /// \brief Elliptical Curve Point over GF(p), where p is prime

0019 /// \since Crypto++ 2.0

0020 struct CRYPTOPP_DLL ECPPoint
0021 {
0022     virtual ~ECPPoint() {}
0023 
0024     /// \brief Construct an ECPPoint

0025     /// \details identity is set to <tt>true</tt>

0026     ECPPoint() : identity(true) {}
0027 
0028     /// \brief Construct an ECPPoint from coordinates

0029     /// \details identity is set to <tt>false</tt>

0030     ECPPoint(const Integer &x, const Integer &y)
0031         : x(x), y(y), identity(false) {}
0032 
0033     /// \brief Tests points for equality

0034     /// \param t the other point

0035     /// \return true if the points are equal, false otherwise

0036     bool operator==(const ECPPoint &t) const
0037         {return (identity && t.identity) || (!identity && !t.identity && x==t.x && y==t.y);}
0038 
0039     /// \brief Tests points for ordering

0040     /// \param t the other point

0041     /// \return true if this point is less than other, false otherwise

0042     bool operator< (const ECPPoint &t) const
0043         {return identity ? !t.identity : (!t.identity && (x<t.x || (x==t.x && y<t.y)));}
0044 
0045     Integer x, y;
0046     bool identity;
0047 };
0048 
0049 CRYPTOPP_DLL_TEMPLATE_CLASS AbstractGroup<ECPPoint>;
0050 
0051 /// \brief Elliptical Curve Point over GF(2^n)

0052 /// \since Crypto++ 2.0

0053 struct CRYPTOPP_DLL EC2NPoint
0054 {
0055     virtual ~EC2NPoint() {}
0056 
0057     /// \brief Construct an EC2NPoint

0058     /// \details identity is set to <tt>true</tt>

0059     EC2NPoint() : identity(true) {}
0060 
0061     /// \brief Construct an EC2NPoint from coordinates

0062     /// \details identity is set to <tt>false</tt>

0063     EC2NPoint(const PolynomialMod2 &x, const PolynomialMod2 &y)
0064         : x(x), y(y), identity(false) {}
0065 
0066     /// \brief Tests points for equality

0067     /// \param t the other point

0068     /// \return true if the points are equal, false otherwise

0069     bool operator==(const EC2NPoint &t) const
0070         {return (identity && t.identity) || (!identity && !t.identity && x==t.x && y==t.y);}
0071 
0072     /// \brief Tests points for ordering

0073     /// \param t the other point

0074     /// \return true if this point is less than other, false otherwise

0075     bool operator< (const EC2NPoint &t) const
0076         {return identity ? !t.identity : (!t.identity && (x<t.x || (x==t.x && y<t.y)));}
0077 
0078     PolynomialMod2 x, y;
0079     bool identity;
0080 };
0081 
0082 CRYPTOPP_DLL_TEMPLATE_CLASS AbstractGroup<EC2NPoint>;
0083 
0084 /// \brief Abstract class for encoding and decoding ellicptic curve points

0085 /// \tparam Point ellicptic curve point

0086 /// \details EncodedPoint is an interface for encoding and decoding elliptic curve points.

0087 ///   The template parameter <tt>Point</tt> should be a class like ECP or EC2N.

0088 /// \since Crypto++ 6.0

0089 template <class Point>
0090 class EncodedPoint
0091 {
0092 public:
0093     virtual ~EncodedPoint() {}
0094 
0095     /// \brief Decodes an elliptic curve point

0096     /// \param P point which is decoded

0097     /// \param bt source BufferedTransformation

0098     /// \param len number of bytes to read from the BufferedTransformation

0099     /// \return true if a point was decoded, false otherwise

0100     virtual bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const =0;
0101 
0102     /// \brief Decodes an elliptic curve point

0103     /// \param P point which is decoded

0104     /// \param encodedPoint byte array with the encoded point

0105     /// \param len the size of the array

0106     /// \return true if a point was decoded, false otherwise

0107     virtual bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const =0;
0108 
0109     /// \brief Verifies points on elliptic curve

0110     /// \param P point to verify

0111     /// \return true if the point is valid, false otherwise

0112     virtual bool VerifyPoint(const Point &P) const =0;
0113 
0114     /// \brief Determines encoded point size

0115     /// \param compressed flag indicating if the point is compressed

0116     /// \return the minimum number of bytes required to encode the point

0117     virtual unsigned int EncodedPointSize(bool compressed = false) const =0;
0118 
0119     /// \brief Encodes an elliptic curve point

0120     /// \param P point which is decoded

0121     /// \param encodedPoint byte array for the encoded point

0122     /// \param compressed flag indicating if the point is compressed

0123     /// \details <tt>encodedPoint</tt> must be at least EncodedPointSize() in length

0124     virtual void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const =0;
0125 
0126     /// \brief Encodes an elliptic curve point

0127     /// \param bt target BufferedTransformation

0128     /// \param P point which is encoded

0129     /// \param compressed flag indicating if the point is compressed

0130     virtual void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const =0;
0131 
0132     /// \brief BER Decodes an elliptic curve point

0133     /// \param bt source BufferedTransformation

0134     /// \return the decoded elliptic curve point

0135     virtual Point BERDecodePoint(BufferedTransformation &bt) const =0;
0136 
0137     /// \brief DER Encodes an elliptic curve point

0138     /// \param bt target BufferedTransformation

0139     /// \param P point which is encoded

0140     /// \param compressed flag indicating if the point is compressed

0141     virtual void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const =0;
0142 };
0143 
0144 NAMESPACE_END
0145 
0146 #endif  // CRYPTOPP_ECPOINT_H