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.

158 lines
5.2 KiB

  1. #ifndef CRYPTOPP_MODARITH_H
  2. #define CRYPTOPP_MODARITH_H
  3. // implementations are in integer.cpp
  4. #include "cryptlib.h"
  5. #include "misc.h"
  6. #include "integer.h"
  7. #include "algebra.h"
  8. NAMESPACE_BEGIN(CryptoPP)
  9. CRYPTOPP_DLL_TEMPLATE_CLASS AbstractGroup<Integer>;
  10. CRYPTOPP_DLL_TEMPLATE_CLASS AbstractRing<Integer>;
  11. CRYPTOPP_DLL_TEMPLATE_CLASS AbstractEuclideanDomain<Integer>;
  12. //! ring of congruence classes modulo n
  13. /*! \note this implementation represents each congruence class as the smallest non-negative integer in that class */
  14. class CRYPTOPP_DLL ModularArithmetic : public AbstractRing<Integer>
  15. {
  16. public:
  17. typedef int RandomizationParameter;
  18. typedef Integer Element;
  19. ModularArithmetic(const Integer &modulus = Integer::One())
  20. : m_modulus(modulus), m_result((word)0, modulus.reg.size()) {}
  21. ModularArithmetic(const ModularArithmetic &ma)
  22. : AbstractRing<Integer>(ma), m_modulus(ma.m_modulus), m_result((word)0, m_modulus.reg.size()) {}
  23. ModularArithmetic(BufferedTransformation &bt); // construct from BER encoded parameters
  24. virtual ModularArithmetic * Clone() const {return new ModularArithmetic(*this);}
  25. void DEREncode(BufferedTransformation &bt) const;
  26. void DEREncodeElement(BufferedTransformation &out, const Element &a) const;
  27. void BERDecodeElement(BufferedTransformation &in, Element &a) const;
  28. const Integer& GetModulus() const {return m_modulus;}
  29. void SetModulus(const Integer &newModulus) {m_modulus = newModulus; m_result.reg.resize(m_modulus.reg.size());}
  30. virtual bool IsMontgomeryRepresentation() const {return false;}
  31. virtual Integer ConvertIn(const Integer &a) const
  32. {return a%m_modulus;}
  33. virtual Integer ConvertOut(const Integer &a) const
  34. {return a;}
  35. const Integer& Half(const Integer &a) const;
  36. bool Equal(const Integer &a, const Integer &b) const
  37. {return a==b;}
  38. const Integer& Identity() const
  39. {return Integer::Zero();}
  40. const Integer& Add(const Integer &a, const Integer &b) const;
  41. Integer& Accumulate(Integer &a, const Integer &b) const;
  42. const Integer& Inverse(const Integer &a) const;
  43. const Integer& Subtract(const Integer &a, const Integer &b) const;
  44. Integer& Reduce(Integer &a, const Integer &b) const;
  45. const Integer& Double(const Integer &a) const
  46. {return Add(a, a);}
  47. const Integer& MultiplicativeIdentity() const
  48. {return Integer::One();}
  49. const Integer& Multiply(const Integer &a, const Integer &b) const
  50. {return m_result1 = a*b%m_modulus;}
  51. const Integer& Square(const Integer &a) const
  52. {return m_result1 = a.Squared()%m_modulus;}
  53. bool IsUnit(const Integer &a) const
  54. {return Integer::Gcd(a, m_modulus).IsUnit();}
  55. const Integer& MultiplicativeInverse(const Integer &a) const
  56. {return m_result1 = a.InverseMod(m_modulus);}
  57. const Integer& Divide(const Integer &a, const Integer &b) const
  58. {return Multiply(a, MultiplicativeInverse(b));}
  59. Integer CascadeExponentiate(const Integer &x, const Integer &e1, const Integer &y, const Integer &e2) const;
  60. void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const;
  61. unsigned int MaxElementBitLength() const
  62. {return (m_modulus-1).BitCount();}
  63. unsigned int MaxElementByteLength() const
  64. {return (m_modulus-1).ByteCount();}
  65. Element RandomElement( RandomNumberGenerator &rng , const RandomizationParameter &ignore_for_now = 0 ) const
  66. // left RandomizationParameter arg as ref in case RandomizationParameter becomes a more complicated struct
  67. {
  68. return Element( rng , Integer( (long) 0) , m_modulus - Integer( (long) 1 ) ) ;
  69. }
  70. bool operator==(const ModularArithmetic &rhs) const
  71. {return m_modulus == rhs.m_modulus;}
  72. static const RandomizationParameter DefaultRandomizationParameter ;
  73. protected:
  74. Integer m_modulus;
  75. mutable Integer m_result, m_result1;
  76. };
  77. // const ModularArithmetic::RandomizationParameter ModularArithmetic::DefaultRandomizationParameter = 0 ;
  78. //! do modular arithmetics in Montgomery representation for increased speed
  79. /*! \note the Montgomery representation represents each congruence class [a] as a*r%n, where r is a convenient power of 2 */
  80. class CRYPTOPP_DLL MontgomeryRepresentation : public ModularArithmetic
  81. {
  82. public:
  83. MontgomeryRepresentation(const Integer &modulus); // modulus must be odd
  84. virtual ModularArithmetic * Clone() const {return new MontgomeryRepresentation(*this);}
  85. bool IsMontgomeryRepresentation() const {return true;}
  86. Integer ConvertIn(const Integer &a) const
  87. {return (a<<(WORD_BITS*m_modulus.reg.size()))%m_modulus;}
  88. Integer ConvertOut(const Integer &a) const;
  89. const Integer& MultiplicativeIdentity() const
  90. {return m_result1 = Integer::Power2(WORD_BITS*m_modulus.reg.size())%m_modulus;}
  91. const Integer& Multiply(const Integer &a, const Integer &b) const;
  92. const Integer& Square(const Integer &a) const;
  93. const Integer& MultiplicativeInverse(const Integer &a) const;
  94. Integer CascadeExponentiate(const Integer &x, const Integer &e1, const Integer &y, const Integer &e2) const
  95. {return AbstractRing<Integer>::CascadeExponentiate(x, e1, y, e2);}
  96. void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const
  97. {AbstractRing<Integer>::SimultaneousExponentiate(results, base, exponents, exponentsCount);}
  98. private:
  99. Integer m_u;
  100. mutable IntegerSecBlock m_workspace;
  101. };
  102. NAMESPACE_END
  103. #endif