eprecomp.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // eprecomp.h - originally written and placed in the public domain by Wei Dai
  2. /// \file eprecomp.h
  3. /// \brief Classes for precomputation in a group
  4. #ifndef CRYPTOPP_EPRECOMP_H
  5. #define CRYPTOPP_EPRECOMP_H
  6. #include "cryptlib.h"
  7. #include "integer.h"
  8. #include "algebra.h"
  9. #include "stdcpp.h"
  10. NAMESPACE_BEGIN(CryptoPP)
  11. /// \brief DL_GroupPrecomputation interface
  12. /// \tparam T Field element
  13. template <class T>
  14. class DL_GroupPrecomputation
  15. {
  16. public:
  17. typedef T Element;
  18. virtual ~DL_GroupPrecomputation() {}
  19. /// \brief Determines if elements needs conversion
  20. /// \return true if the element needs conversion, false otherwise
  21. /// \details NeedConversions determines if an element must convert between representations.
  22. virtual bool NeedConversions() const {return false;}
  23. /// \brief Converts an element between representations
  24. /// \param v element to convert
  25. /// \return an element converted to an alternate representation for internal use
  26. /// \details ConvertIn is used when an element must convert between representations.
  27. virtual Element ConvertIn(const Element &v) const {return v;}
  28. /// \brief Converts an element between representations
  29. /// \param v element to convert
  30. /// \return an element converted from an alternate representation
  31. virtual Element ConvertOut(const Element &v) const {return v;}
  32. /// \brief Retrieves AbstractGroup interface
  33. /// \return GetGroup() returns the AbstractGroup interface
  34. virtual const AbstractGroup<Element> & GetGroup() const =0;
  35. /// \brief Decodes element in DER format
  36. /// \param bt BufferedTransformation object
  37. /// \return element in the group
  38. virtual Element BERDecodeElement(BufferedTransformation &bt) const =0;
  39. /// \brief Encodes element in DER format
  40. /// \param bt BufferedTransformation object
  41. /// \param P Element to encode
  42. virtual void DEREncodeElement(BufferedTransformation &bt, const Element &P) const =0;
  43. };
  44. /// \brief DL_FixedBasePrecomputation interface
  45. /// \tparam T Field element
  46. template <class T>
  47. class DL_FixedBasePrecomputation
  48. {
  49. public:
  50. typedef T Element;
  51. virtual ~DL_FixedBasePrecomputation() {}
  52. /// \brief Determines whether this object is initialized
  53. /// \return true if this object is initialized, false otherwise
  54. virtual bool IsInitialized() const =0;
  55. /// \brief Set the base element
  56. /// \param group the group
  57. /// \param base element in the group
  58. virtual void SetBase(const DL_GroupPrecomputation<Element> &group, const Element &base) =0;
  59. /// \brief Get the base element
  60. /// \param group the group
  61. /// \return base element in the group
  62. virtual const Element & GetBase(const DL_GroupPrecomputation<Element> &group) const =0;
  63. /// \brief Perform precomputation
  64. /// \param group the group
  65. /// \param maxExpBits used to calculate the exponent base
  66. /// \param storage the suggested number of objects for the precompute table
  67. /// \details The exact semantics of Precompute() varies, but it typically means calculate
  68. /// a table of n objects that can be used later to speed up computation.
  69. /// \details If a derived class does not override Precompute(), then the base class throws
  70. /// NotImplemented.
  71. /// \sa SupportsPrecomputation(), LoadPrecomputation(), SavePrecomputation()
  72. virtual void Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage) =0;
  73. /// \brief Retrieve previously saved precomputation
  74. /// \param group the group
  75. /// \param storedPrecomputation BufferedTransformation with the saved precomputation
  76. /// \throw NotImplemented
  77. /// \sa SupportsPrecomputation(), Precompute()
  78. virtual void Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) =0;
  79. /// \brief Save precomputation for later use
  80. /// \param group the group
  81. /// \param storedPrecomputation BufferedTransformation to write the precomputation
  82. /// \throw NotImplemented
  83. /// \sa SupportsPrecomputation(), Precompute()
  84. virtual void Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) const =0;
  85. /// \brief Exponentiates an element
  86. /// \param group the group
  87. /// \param exponent the exponent
  88. /// \return the result of the exponentiation
  89. virtual Element Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const =0;
  90. /// \brief Exponentiates an element
  91. /// \param pc1 the first the group precomputation
  92. /// \param exponent1 the first exponent
  93. /// \param pc2 the second the group precomputation
  94. /// \param exponent2 the first exponent2
  95. /// \return the public element raised to the exponent
  96. /// \details CascadeExponentiateBaseAndPublicElement raises the public element to
  97. /// the base element and precomputation.
  98. virtual Element CascadeExponentiate(const DL_GroupPrecomputation<Element> &pc1, const Integer &exponent1, const DL_FixedBasePrecomputation<Element> &pc2, const Integer &exponent2) const =0;
  99. };
  100. /// \brief DL_FixedBasePrecomputation adapter class
  101. /// \tparam T Field element
  102. template <class T>
  103. class DL_FixedBasePrecomputationImpl : public DL_FixedBasePrecomputation<T>
  104. {
  105. public:
  106. typedef T Element;
  107. virtual ~DL_FixedBasePrecomputationImpl() {}
  108. DL_FixedBasePrecomputationImpl() : m_windowSize(0) {}
  109. // DL_FixedBasePrecomputation
  110. bool IsInitialized() const
  111. {return !m_bases.empty();}
  112. void SetBase(const DL_GroupPrecomputation<Element> &group, const Element &base);
  113. const Element & GetBase(const DL_GroupPrecomputation<Element> &group) const
  114. {return group.NeedConversions() ? m_base : m_bases[0];}
  115. void Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage);
  116. void Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation);
  117. void Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) const;
  118. Element Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const;
  119. Element CascadeExponentiate(const DL_GroupPrecomputation<Element> &pc1, const Integer &exponent1, const DL_FixedBasePrecomputation<Element> &pc2, const Integer &exponent2) const;
  120. private:
  121. void PrepareCascade(const DL_GroupPrecomputation<Element> &group, std::vector<BaseAndExponent<Element> > &eb, const Integer &exponent) const;
  122. Element m_base;
  123. unsigned int m_windowSize;
  124. Integer m_exponentBase; // what base to represent the exponent in
  125. std::vector<Element> m_bases; // precalculated bases
  126. };
  127. NAMESPACE_END
  128. #ifdef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
  129. #include "eprecomp.cpp"
  130. #endif
  131. #endif