Crypto++  5.6.5
Free C++ class library of cryptographic schemes
eprecomp.h
Go to the documentation of this file.
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>
18 {
19 public:
20  typedef T Element;
21 
22  virtual ~DL_GroupPrecomputation() {}
23 
24  virtual bool NeedConversions() const {return false;}
25  virtual Element ConvertIn(const Element &v) const {return v;}
26  virtual Element ConvertOut(const Element &v) const {return v;}
27  virtual const AbstractGroup<Element> & GetGroup() const =0;
28  virtual Element BERDecodeElement(BufferedTransformation &bt) const =0;
29  virtual void DEREncodeElement(BufferedTransformation &bt, const Element &P) const =0;
30 };
31 
32 template <class T>
34 {
35 public:
36  typedef T Element;
37 
38  virtual ~DL_FixedBasePrecomputation() {}
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 
50 template <class T>
52 {
53 public:
54  typedef T Element;
55 
56  virtual ~DL_FixedBasePrecomputationImpl() {}
57 
58  DL_FixedBasePrecomputationImpl() : m_windowSize(0) {}
59 
60  // DL_FixedBasePrecomputation
61  bool IsInitialized() const
62  {return !m_bases.empty();}
63  void SetBase(const DL_GroupPrecomputation<Element> &group, const Element &base);
64  const Element & GetBase(const DL_GroupPrecomputation<Element> &group) const
65  {return group.NeedConversions() ? m_base : m_bases[0];}
66  void Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage);
67  void Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation);
68  void Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) const;
69  Element Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const;
70  Element CascadeExponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent, const DL_FixedBasePrecomputation<Element> &pc2, const Integer &exponent2) const;
71 
72 private:
73  void PrepareCascade(const DL_GroupPrecomputation<Element> &group, std::vector<BaseAndExponent<Element> > &eb, const Integer &exponent) const;
74 
75  Element m_base;
76  unsigned int m_windowSize;
77  Integer m_exponentBase; // what base to represent the exponent in
78  std::vector<Element> m_bases; // precalculated bases
79 };
80 
81 NAMESPACE_END
82 
83 #ifdef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
84 #include "eprecomp.cpp"
85 #endif
86 
87 #endif
Abstract base classes that provide a uniform interface to this library.
Classes for performing mathematics over different fields.
Interface for buffered transformations.
Definition: cryptlib.h:1352
Multiple precision integer with arithmetic operations.
Definition: integer.h:43
Abstract group.
Definition: algebra.h:26
Multiple precision integer with arithmetic operations.
Base and exponent.
Definition: algebra.h:249
Crypto++ library namespace.