Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.8 KiB

  1. #ifndef CRYPTOPP_EC2N_H
  2. #define CRYPTOPP_EC2N_H
  3. #include "gf2n.h"
  4. #include "eprecomp.h"
  5. #include "smartptr.h"
  6. #include "pubkey.h"
  7. NAMESPACE_BEGIN(CryptoPP)
  8. //! Elliptic Curve Point
  9. struct CRYPTOPP_DLL EC2NPoint
  10. {
  11. EC2NPoint() : identity(true) {}
  12. EC2NPoint(const PolynomialMod2 &x, const PolynomialMod2 &y)
  13. : identity(false), x(x), y(y) {}
  14. bool operator==(const EC2NPoint &t) const
  15. {return (identity && t.identity) || (!identity && !t.identity && x==t.x && y==t.y);}
  16. bool operator< (const EC2NPoint &t) const
  17. {return identity ? !t.identity : (!t.identity && (x<t.x || (x==t.x && y<t.y)));}
  18. bool identity;
  19. PolynomialMod2 x, y;
  20. };
  21. CRYPTOPP_DLL_TEMPLATE_CLASS AbstractGroup<EC2NPoint>;
  22. //! Elliptic Curve over GF(2^n)
  23. class CRYPTOPP_DLL EC2N : public AbstractGroup<EC2NPoint>
  24. {
  25. public:
  26. typedef GF2NP Field;
  27. typedef Field::Element FieldElement;
  28. typedef EC2NPoint Point;
  29. EC2N() {}
  30. EC2N(const Field &field, const Field::Element &a, const Field::Element &b)
  31. : m_field(field), m_a(a), m_b(b) {}
  32. // construct from BER encoded parameters
  33. // this constructor will decode and extract the the fields fieldID and curve of the sequence ECParameters
  34. EC2N(BufferedTransformation &bt);
  35. // encode the fields fieldID and curve of the sequence ECParameters
  36. void DEREncode(BufferedTransformation &bt) const;
  37. bool Equal(const Point &P, const Point &Q) const;
  38. const Point& Identity() const;
  39. const Point& Inverse(const Point &P) const;
  40. bool InversionIsFast() const {return true;}
  41. const Point& Add(const Point &P, const Point &Q) const;
  42. const Point& Double(const Point &P) const;
  43. Point Multiply(const Integer &k, const Point &P) const
  44. {return ScalarMultiply(P, k);}
  45. Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const
  46. {return CascadeScalarMultiply(P, k1, Q, k2);}
  47. bool ValidateParameters(RandomNumberGenerator &rng, unsigned int level=3) const;
  48. bool VerifyPoint(const Point &P) const;
  49. unsigned int EncodedPointSize(bool compressed = false) const
  50. {return 1 + (compressed?1:2)*m_field->MaxElementByteLength();}
  51. // returns false if point is compressed and not valid (doesn't check if uncompressed)
  52. bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const;
  53. bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const;
  54. void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const;
  55. void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
  56. Point BERDecodePoint(BufferedTransformation &bt) const;
  57. void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
  58. Integer FieldSize() const {return Integer::Power2(m_field->MaxElementBitLength());}
  59. const Field & GetField() const {return *m_field;}
  60. const FieldElement & GetA() const {return m_a;}
  61. const FieldElement & GetB() const {return m_b;}
  62. bool operator==(const EC2N &rhs) const
  63. {return GetField() == rhs.GetField() && m_a == rhs.m_a && m_b == rhs.m_b;}
  64. private:
  65. clonable_ptr<Field> m_field;
  66. FieldElement m_a, m_b;
  67. mutable Point m_R;
  68. };
  69. CRYPTOPP_DLL_TEMPLATE_CLASS DL_FixedBasePrecomputationImpl<EC2N::Point>;
  70. CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupPrecomputation<EC2N::Point>;
  71. template <class T> class EcPrecomputation;
  72. //! EC2N precomputation
  73. template<> class EcPrecomputation<EC2N> : public DL_GroupPrecomputation<EC2N::Point>
  74. {
  75. public:
  76. typedef EC2N EllipticCurve;
  77. // DL_GroupPrecomputation
  78. const AbstractGroup<Element> & GetGroup() const {return m_ec;}
  79. Element BERDecodeElement(BufferedTransformation &bt) const {return m_ec.BERDecodePoint(bt);}
  80. void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {m_ec.DEREncodePoint(bt, v, false);}
  81. // non-inherited
  82. void SetCurve(const EC2N &ec) {m_ec = ec;}
  83. const EC2N & GetCurve() const {return m_ec;}
  84. private:
  85. EC2N m_ec;
  86. };
  87. NAMESPACE_END
  88. #endif