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.

126 lines
4.6 KiB

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