ecpoint.h 5.1 KB

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