File indexing completed on 2025-01-18 09:55:02
0001
0002
0003
0004
0005
0006 #ifndef CRYPTOPP_EPRECOMP_H
0007 #define CRYPTOPP_EPRECOMP_H
0008
0009 #include "cryptlib.h"
0010 #include "integer.h"
0011 #include "algebra.h"
0012 #include "stdcpp.h"
0013
0014 NAMESPACE_BEGIN(CryptoPP)
0015
0016
0017
0018 template <class T>
0019 class DL_GroupPrecomputation
0020 {
0021 public:
0022 typedef T Element;
0023
0024 virtual ~DL_GroupPrecomputation() {}
0025
0026
0027
0028
0029 virtual bool NeedConversions() const {return false;}
0030
0031
0032
0033
0034
0035 virtual Element ConvertIn(const Element &v) const {return v;}
0036
0037
0038
0039
0040 virtual Element ConvertOut(const Element &v) const {return v;}
0041
0042
0043
0044 virtual const AbstractGroup<Element> & GetGroup() const =0;
0045
0046
0047
0048
0049 virtual Element BERDecodeElement(BufferedTransformation &bt) const =0;
0050
0051
0052
0053
0054 virtual void DEREncodeElement(BufferedTransformation &bt, const Element &P) const =0;
0055 };
0056
0057
0058
0059 template <class T>
0060 class DL_FixedBasePrecomputation
0061 {
0062 public:
0063 typedef T Element;
0064
0065 virtual ~DL_FixedBasePrecomputation() {}
0066
0067
0068
0069 virtual bool IsInitialized() const =0;
0070
0071
0072
0073
0074 virtual void SetBase(const DL_GroupPrecomputation<Element> &group, const Element &base) =0;
0075
0076
0077
0078
0079 virtual const Element & GetBase(const DL_GroupPrecomputation<Element> &group) const =0;
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090 virtual void Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage) =0;
0091
0092
0093
0094
0095
0096
0097 virtual void Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) =0;
0098
0099
0100
0101
0102
0103
0104 virtual void Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) const =0;
0105
0106
0107
0108
0109
0110 virtual Element Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const =0;
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120 virtual Element CascadeExponentiate(const DL_GroupPrecomputation<Element> &pc1, const Integer &exponent1, const DL_FixedBasePrecomputation<Element> &pc2, const Integer &exponent2) const =0;
0121 };
0122
0123
0124
0125 template <class T>
0126 class DL_FixedBasePrecomputationImpl : public DL_FixedBasePrecomputation<T>
0127 {
0128 public:
0129 typedef T Element;
0130
0131 virtual ~DL_FixedBasePrecomputationImpl() {}
0132
0133 DL_FixedBasePrecomputationImpl() : m_windowSize(0) {}
0134
0135
0136 bool IsInitialized() const
0137 {return !m_bases.empty();}
0138 void SetBase(const DL_GroupPrecomputation<Element> &group, const Element &base);
0139 const Element & GetBase(const DL_GroupPrecomputation<Element> &group) const
0140 {return group.NeedConversions() ? m_base : m_bases[0];}
0141 void Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage);
0142 void Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation);
0143 void Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) const;
0144 Element Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const;
0145 Element CascadeExponentiate(const DL_GroupPrecomputation<Element> &pc1, const Integer &exponent1, const DL_FixedBasePrecomputation<Element> &pc2, const Integer &exponent2) const;
0146
0147 private:
0148 void PrepareCascade(const DL_GroupPrecomputation<Element> &group, std::vector<BaseAndExponent<Element> > &eb, const Integer &exponent) const;
0149
0150 Element m_base;
0151 unsigned int m_windowSize;
0152 Integer m_exponentBase;
0153 std::vector<Element> m_bases;
0154 };
0155
0156 NAMESPACE_END
0157
0158 #ifdef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
0159 #include "eprecomp.cpp"
0160 #endif
0161
0162 #endif