1 | // eprecomp.h - written and placed in the public domain by Wei Dai |
---|
2 | |
---|
3 | //! \file eprecomp.h |
---|
4 | //! \brief Classes for precomputation in a group |
---|
5 | |
---|
6 | #ifndef CRYPTOPP_EPRECOMP_H |
---|
7 | #define CRYPTOPP_EPRECOMP_H |
---|
8 | |
---|
9 | #include "cryptlib.h" |
---|
10 | #include "integer.h" |
---|
11 | #include "algebra.h" |
---|
12 | #include "stdcpp.h" |
---|
13 | |
---|
14 | NAMESPACE_BEGIN(CryptoPP) |
---|
15 | |
---|
16 | template <class T> |
---|
17 | class DL_GroupPrecomputation |
---|
18 | { |
---|
19 | public: |
---|
20 | typedef T Element; |
---|
21 | |
---|
22 | virtual bool NeedConversions() const {return false;} |
---|
23 | virtual Element ConvertIn(const Element &v) const {return v;} |
---|
24 | virtual Element ConvertOut(const Element &v) const {return v;} |
---|
25 | virtual const AbstractGroup<Element> & GetGroup() const =0; |
---|
26 | virtual Element BERDecodeElement(BufferedTransformation &bt) const =0; |
---|
27 | virtual void DEREncodeElement(BufferedTransformation &bt, const Element &P) const =0; |
---|
28 | |
---|
29 | #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 |
---|
30 | virtual ~DL_GroupPrecomputation() {} |
---|
31 | #endif |
---|
32 | }; |
---|
33 | |
---|
34 | template <class T> |
---|
35 | class DL_FixedBasePrecomputation |
---|
36 | { |
---|
37 | public: |
---|
38 | typedef T Element; |
---|
39 | |
---|
40 | virtual bool IsInitialized() const =0; |
---|
41 | virtual void SetBase(const DL_GroupPrecomputation<Element> &group, const Element &base) =0; |
---|
42 | virtual const Element & GetBase(const DL_GroupPrecomputation<Element> &group) const =0; |
---|
43 | virtual void Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage) =0; |
---|
44 | virtual void Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) =0; |
---|
45 | virtual void Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) const =0; |
---|
46 | virtual Element Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const =0; |
---|
47 | virtual Element CascadeExponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent, const DL_FixedBasePrecomputation<Element> &pc2, const Integer &exponent2) const =0; |
---|
48 | |
---|
49 | #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 |
---|
50 | virtual ~DL_FixedBasePrecomputation() {} |
---|
51 | #endif |
---|
52 | }; |
---|
53 | |
---|
54 | template <class T> |
---|
55 | class DL_FixedBasePrecomputationImpl : public DL_FixedBasePrecomputation<T> |
---|
56 | { |
---|
57 | public: |
---|
58 | typedef T Element; |
---|
59 | |
---|
60 | DL_FixedBasePrecomputationImpl() : m_windowSize(0) {} |
---|
61 | |
---|
62 | // DL_FixedBasePrecomputation |
---|
63 | bool IsInitialized() const |
---|
64 | {return !m_bases.empty();} |
---|
65 | void SetBase(const DL_GroupPrecomputation<Element> &group, const Element &base); |
---|
66 | const Element & GetBase(const DL_GroupPrecomputation<Element> &group) const |
---|
67 | {return group.NeedConversions() ? m_base : m_bases[0];} |
---|
68 | void Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage); |
---|
69 | void Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation); |
---|
70 | void Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) const; |
---|
71 | Element Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const; |
---|
72 | Element CascadeExponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent, const DL_FixedBasePrecomputation<Element> &pc2, const Integer &exponent2) const; |
---|
73 | |
---|
74 | #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 |
---|
75 | virtual ~DL_FixedBasePrecomputationImpl() {} |
---|
76 | #endif |
---|
77 | |
---|
78 | private: |
---|
79 | void PrepareCascade(const DL_GroupPrecomputation<Element> &group, std::vector<BaseAndExponent<Element> > &eb, const Integer &exponent) const; |
---|
80 | |
---|
81 | Element m_base; |
---|
82 | unsigned int m_windowSize; |
---|
83 | Integer m_exponentBase; // what base to represent the exponent in |
---|
84 | std::vector<Element> m_bases; // precalculated bases |
---|
85 | }; |
---|
86 | |
---|
87 | NAMESPACE_END |
---|
88 | |
---|
89 | #ifdef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES |
---|
90 | #include "eprecomp.cpp" |
---|
91 | #endif |
---|
92 | |
---|
93 | #endif |
---|