Team Fortress 2 Source Code as on 22/4/2020
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.

177 lines
5.7 KiB

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