- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# eprecomp.h

    00001 #ifndef CRYPTOPP_EPRECOMP_H
    00002 #define CRYPTOPP_EPRECOMP_H
    00003
    00004 #include "integer.h"
    00005 #include "algebra.h"
    00006 #include <vector>
    00007
    00008 NAMESPACE_BEGIN(CryptoPP)
    00009
    00010 template <class T>
    00011 class DL_GroupPrecomputation
    00012 {
    00013 public:
    00014         typedef T Element;
    00015
    00016         virtual bool NeedConversions() const {return false;}
    00017         virtual Element ConvertIn(const Element &v) const {return v;}
    00018         virtual Element ConvertOut(const Element &v) const {return v;}
    00019         virtual const AbstractGroup<Element> & GetGroup() const =0;
    00020         virtual Element BERDecodeElement(BufferedTransformation &bt) const =0;
    00021         virtual void DEREncodeElement(BufferedTransformation &bt, const Element &P) const =0;
    00022 };
    00023
    00024 template <class T>
    00025 class DL_FixedBasePrecomputation
    00026 {
    00027 public:
    00028         typedef T Element;
    00029
    00030         virtual bool IsInitialized() const =0;
    00031         virtual void SetBase(const DL_GroupPrecomputation<Element> &group, const Element &base) =0;
    00032         virtual const Element & GetBase(const DL_GroupPrecomputation<Element> &group) const =0;
    00033         virtual void Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage) =0;
    00034         virtual void Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) =0;
    00035         virtual void Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) const =0;
    00036         virtual Element Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const =0;
    00037         virtual Element CascadeExponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent, const DL_FixedBasePrecomputation<Element> &pc2, const Integer &exponent2) const =0;
    00038 };
    00039
    00040 template <class T>
    00041 class DL_FixedBasePrecomputationImpl : public DL_FixedBasePrecomputation<T>
    00042 {
    00043 public:
    00044         typedef T Element;
    00045
    00046         // DL_FixedBasePrecomputation
    00047         bool IsInitialized() const
    00048                 {return !m_bases.empty();}
    00049         void SetBase(const DL_GroupPrecomputation<Element> &group, const Element &base);
    00050         const Element & GetBase(const DL_GroupPrecomputation<Element> &group) const
    00051                 {return group.NeedConversions() ? m_base : m_bases[0];}
    00052         void Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage);
    00053         void Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation);
    00054         void Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) const;
    00055         Element Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const;
    00056         Element CascadeExponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent, const DL_FixedBasePrecomputation<Element> &pc2, const Integer &exponent2) const;
    00057
    00058 private:
    00059         void PrepareCascade(const DL_GroupPrecomputation<Element> &group, std::vector<BaseAndExponent<Element> > &eb, const Integer &exponent) const;
    00060
    00061         Element m_base;
    00062         unsigned int m_windowSize;
    00063         Integer m_exponentBase;                 // what base to represent the exponent in
    00064         std::vector<Element> m_bases;   // precalculated bases
    00065 };
    00066
    00067 NAMESPACE_END
    00068
    00069 #ifdef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
    00070 #include "eprecomp.cpp"
    00071 #endif
    00072
    00073 #endif