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.

112 lines
3.7 KiB

  1. // eprecomp.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #ifndef CRYPTOPP_IMPORTS
  4. #include "eprecomp.h"
  5. #include "asn.h"
  6. NAMESPACE_BEGIN(CryptoPP)
  7. template <class T> void DL_FixedBasePrecomputationImpl<T>::SetBase(const DL_GroupPrecomputation<Element> &group, const Element &i_base)
  8. {
  9. m_base = group.NeedConversions() ? group.ConvertIn(i_base) : i_base;
  10. if (m_bases.empty() || !(m_base == m_bases[0]))
  11. {
  12. m_bases.resize(1);
  13. m_bases[0] = m_base;
  14. }
  15. if (group.NeedConversions())
  16. m_base = i_base;
  17. }
  18. template <class T> void DL_FixedBasePrecomputationImpl<T>::Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage)
  19. {
  20. assert(m_bases.size() > 0);
  21. assert(storage <= maxExpBits);
  22. if (storage > 1)
  23. {
  24. m_windowSize = (maxExpBits+storage-1)/storage;
  25. m_exponentBase = Integer::Power2(m_windowSize);
  26. }
  27. m_bases.resize(storage);
  28. for (unsigned i=1; i<storage; i++)
  29. m_bases[i] = group.GetGroup().ScalarMultiply(m_bases[i-1], m_exponentBase);
  30. }
  31. template <class T> void DL_FixedBasePrecomputationImpl<T>::Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &bt)
  32. {
  33. BERSequenceDecoder seq(bt);
  34. word32 version;
  35. BERDecodeUnsigned<word32>(seq, version, INTEGER, 1, 1);
  36. m_exponentBase.BERDecode(seq);
  37. m_windowSize = m_exponentBase.BitCount() - 1;
  38. m_bases.clear();
  39. while (!seq.EndReached())
  40. m_bases.push_back(group.BERDecodeElement(seq));
  41. if (!m_bases.empty() && group.NeedConversions())
  42. m_base = group.ConvertOut(m_bases[0]);
  43. seq.MessageEnd();
  44. }
  45. template <class T> void DL_FixedBasePrecomputationImpl<T>::Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &bt) const
  46. {
  47. DERSequenceEncoder seq(bt);
  48. DEREncodeUnsigned<word32>(seq, 1); // version
  49. m_exponentBase.DEREncode(seq);
  50. for (unsigned i=0; i<m_bases.size(); i++)
  51. group.DEREncodeElement(seq, m_bases[i]);
  52. seq.MessageEnd();
  53. }
  54. template <class T> void DL_FixedBasePrecomputationImpl<T>::PrepareCascade(const DL_GroupPrecomputation<Element> &i_group, std::vector<BaseAndExponent<Element> > &eb, const Integer &exponent) const
  55. {
  56. const AbstractGroup<T> &group = i_group.GetGroup();
  57. Integer r, q, e = exponent;
  58. bool fastNegate = group.InversionIsFast() && m_windowSize > 1;
  59. unsigned int i;
  60. for (i=0; i+1<m_bases.size(); i++)
  61. {
  62. Integer::DivideByPowerOf2(r, q, e, m_windowSize);
  63. std::swap(q, e);
  64. if (fastNegate && r.GetBit(m_windowSize-1))
  65. {
  66. ++e;
  67. eb.push_back(BaseAndExponent<Element>(group.Inverse(m_bases[i]), m_exponentBase - r));
  68. }
  69. else
  70. eb.push_back(BaseAndExponent<Element>(m_bases[i], r));
  71. }
  72. eb.push_back(BaseAndExponent<Element>(m_bases[i], e));
  73. }
  74. template <class T> T DL_FixedBasePrecomputationImpl<T>::Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const
  75. {
  76. std::vector<BaseAndExponent<Element> > eb; // array of segments of the exponent and precalculated bases
  77. eb.reserve(m_bases.size());
  78. PrepareCascade(group, eb, exponent);
  79. return group.ConvertOut(GeneralCascadeMultiplication<Element>(group.GetGroup(), eb.begin(), eb.end()));
  80. }
  81. template <class T> T
  82. DL_FixedBasePrecomputationImpl<T>::CascadeExponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent,
  83. const DL_FixedBasePrecomputation<T> &i_pc2, const Integer &exponent2) const
  84. {
  85. std::vector<BaseAndExponent<Element> > eb; // array of segments of the exponent and precalculated bases
  86. const DL_FixedBasePrecomputationImpl<T> &pc2 = static_cast<const DL_FixedBasePrecomputationImpl<T> &>(i_pc2);
  87. eb.reserve(m_bases.size() + pc2.m_bases.size());
  88. PrepareCascade(group, eb, exponent);
  89. pc2.PrepareCascade(group, eb, exponent2);
  90. return group.ConvertOut(GeneralCascadeMultiplication<Element>(group.GetGroup(), eb.begin(), eb.end()));
  91. }
  92. NAMESPACE_END
  93. #endif