Crypto++  8.8
Free C++ class library of cryptographic schemes
eprecomp.h
Go to the documentation of this file.
1 // eprecomp.h - originally 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 /// \brief DL_GroupPrecomputation interface
17 /// \tparam T Field element
18 template <class T>
20 {
21 public:
22  typedef T Element;
23 
24  virtual ~DL_GroupPrecomputation() {}
25 
26  /// \brief Determines if elements needs conversion
27  /// \return true if the element needs conversion, false otherwise
28  /// \details NeedConversions determines if an element must convert between representations.
29  virtual bool NeedConversions() const {return false;}
30 
31  /// \brief Converts an element between representations
32  /// \param v element to convert
33  /// \return an element converted to an alternate representation for internal use
34  /// \details ConvertIn is used when an element must convert between representations.
35  virtual Element ConvertIn(const Element &v) const {return v;}
36 
37  /// \brief Converts an element between representations
38  /// \param v element to convert
39  /// \return an element converted from an alternate representation
40  virtual Element ConvertOut(const Element &v) const {return v;}
41 
42  /// \brief Retrieves AbstractGroup interface
43  /// \return GetGroup() returns the AbstractGroup interface
44  virtual const AbstractGroup<Element> & GetGroup() const =0;
45 
46  /// \brief Decodes element in DER format
47  /// \param bt BufferedTransformation object
48  /// \return element in the group
49  virtual Element BERDecodeElement(BufferedTransformation &bt) const =0;
50 
51  /// \brief Encodes element in DER format
52  /// \param bt BufferedTransformation object
53  /// \param P Element to encode
54  virtual void DEREncodeElement(BufferedTransformation &bt, const Element &P) const =0;
55 };
56 
57 /// \brief DL_FixedBasePrecomputation interface
58 /// \tparam T Field element
59 template <class T>
61 {
62 public:
63  typedef T Element;
64 
65  virtual ~DL_FixedBasePrecomputation() {}
66 
67  /// \brief Determines whether this object is initialized
68  /// \return true if this object is initialized, false otherwise
69  virtual bool IsInitialized() const =0;
70 
71  /// \brief Set the base element
72  /// \param group the group
73  /// \param base element in the group
74  virtual void SetBase(const DL_GroupPrecomputation<Element> &group, const Element &base) =0;
75 
76  /// \brief Get the base element
77  /// \param group the group
78  /// \return base element in the group
79  virtual const Element & GetBase(const DL_GroupPrecomputation<Element> &group) const =0;
80 
81  /// \brief Perform precomputation
82  /// \param group the group
83  /// \param maxExpBits used to calculate the exponent base
84  /// \param storage the suggested number of objects for the precompute table
85  /// \details The exact semantics of Precompute() varies, but it typically means calculate
86  /// a table of n objects that can be used later to speed up computation.
87  /// \details If a derived class does not override Precompute(), then the base class throws
88  /// NotImplemented.
89  /// \sa SupportsPrecomputation(), LoadPrecomputation(), SavePrecomputation()
90  virtual void Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage) =0;
91 
92  /// \brief Retrieve previously saved precomputation
93  /// \param group the group
94  /// \param storedPrecomputation BufferedTransformation with the saved precomputation
95  /// \throw NotImplemented
96  /// \sa SupportsPrecomputation(), Precompute()
97  virtual void Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) =0;
98 
99  /// \brief Save precomputation for later use
100  /// \param group the group
101  /// \param storedPrecomputation BufferedTransformation to write the precomputation
102  /// \throw NotImplemented
103  /// \sa SupportsPrecomputation(), Precompute()
104  virtual void Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) const =0;
105 
106  /// \brief Exponentiates an element
107  /// \param group the group
108  /// \param exponent the exponent
109  /// \return the result of the exponentiation
110  virtual Element Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const =0;
111 
112  /// \brief Exponentiates an element
113  /// \param pc1 the first the group precomputation
114  /// \param exponent1 the first exponent
115  /// \param pc2 the second the group precomputation
116  /// \param exponent2 the first exponent2
117  /// \return the public element raised to the exponent
118  /// \details CascadeExponentiateBaseAndPublicElement raises the public element to
119  /// the base element and precomputation.
120  virtual Element CascadeExponentiate(const DL_GroupPrecomputation<Element> &pc1, const Integer &exponent1, const DL_FixedBasePrecomputation<Element> &pc2, const Integer &exponent2) const =0;
121 };
122 
123 /// \brief DL_FixedBasePrecomputation adapter class
124 /// \tparam T Field element
125 template <class T>
127 {
128 public:
129  typedef T Element;
130 
131  virtual ~DL_FixedBasePrecomputationImpl() {}
132 
133  DL_FixedBasePrecomputationImpl() : m_windowSize(0) {}
134 
135  // DL_FixedBasePrecomputation
136  bool IsInitialized() const
137  {return !m_bases.empty();}
138  void SetBase(const DL_GroupPrecomputation<Element> &group, const Element &base);
139  const Element & GetBase(const DL_GroupPrecomputation<Element> &group) const
140  {return group.NeedConversions() ? m_base : m_bases[0];}
141  void Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage);
142  void Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation);
143  void Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) const;
144  Element Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const;
145  Element CascadeExponentiate(const DL_GroupPrecomputation<Element> &pc1, const Integer &exponent1, const DL_FixedBasePrecomputation<Element> &pc2, const Integer &exponent2) const;
146 
147 private:
148  void PrepareCascade(const DL_GroupPrecomputation<Element> &group, std::vector<BaseAndExponent<Element> > &eb, const Integer &exponent) const;
149 
150  Element m_base;
151  unsigned int m_windowSize;
152  Integer m_exponentBase; // what base to represent the exponent in
153  std::vector<Element> m_bases; // precalculated bases
154 };
155 
156 NAMESPACE_END
157 
158 #ifdef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
159 #include "eprecomp.cpp"
160 #endif
161 
162 #endif
Classes for performing mathematics over different fields.
Abstract group.
Definition: algebra.h:27
Interface for buffered transformations.
Definition: cryptlib.h:1657
DL_FixedBasePrecomputation interface.
Definition: eprecomp.h:61
virtual void Save(const DL_GroupPrecomputation< Element > &group, BufferedTransformation &storedPrecomputation) const =0
Save precomputation for later use.
virtual const Element & GetBase(const DL_GroupPrecomputation< Element > &group) const =0
Get the base element.
virtual void SetBase(const DL_GroupPrecomputation< Element > &group, const Element &base)=0
Set the base element.
virtual void Precompute(const DL_GroupPrecomputation< Element > &group, unsigned int maxExpBits, unsigned int storage)=0
Perform precomputation.
virtual Element Exponentiate(const DL_GroupPrecomputation< Element > &group, const Integer &exponent) const =0
Exponentiates an element.
virtual Element CascadeExponentiate(const DL_GroupPrecomputation< Element > &pc1, const Integer &exponent1, const DL_FixedBasePrecomputation< Element > &pc2, const Integer &exponent2) const =0
Exponentiates an element.
virtual bool IsInitialized() const =0
Determines whether this object is initialized.
virtual void Load(const DL_GroupPrecomputation< Element > &group, BufferedTransformation &storedPrecomputation)=0
Retrieve previously saved precomputation.
DL_FixedBasePrecomputation adapter class.
Definition: eprecomp.h:127
void Load(const DL_GroupPrecomputation< Element > &group, BufferedTransformation &storedPrecomputation)
Retrieve previously saved precomputation.
Element Exponentiate(const DL_GroupPrecomputation< Element > &group, const Integer &exponent) const
Exponentiates an element.
Element CascadeExponentiate(const DL_GroupPrecomputation< Element > &pc1, const Integer &exponent1, const DL_FixedBasePrecomputation< Element > &pc2, const Integer &exponent2) const
Exponentiates an element.
bool IsInitialized() const
Determines whether this object is initialized.
Definition: eprecomp.h:136
void SetBase(const DL_GroupPrecomputation< Element > &group, const Element &base)
Set the base element.
void Precompute(const DL_GroupPrecomputation< Element > &group, unsigned int maxExpBits, unsigned int storage)
Perform precomputation.
void Save(const DL_GroupPrecomputation< Element > &group, BufferedTransformation &storedPrecomputation) const
Save precomputation for later use.
const Element & GetBase(const DL_GroupPrecomputation< Element > &group) const
Get the base element.
Definition: eprecomp.h:139
DL_GroupPrecomputation interface.
Definition: eprecomp.h:20
virtual Element ConvertIn(const Element &v) const
Converts an element between representations.
Definition: eprecomp.h:35
virtual Element BERDecodeElement(BufferedTransformation &bt) const =0
Decodes element in DER format.
virtual void DEREncodeElement(BufferedTransformation &bt, const Element &P) const =0
Encodes element in DER format.
virtual const AbstractGroup< Element > & GetGroup() const =0
Retrieves AbstractGroup interface.
virtual Element ConvertOut(const Element &v) const
Converts an element between representations.
Definition: eprecomp.h:40
virtual bool NeedConversions() const
Determines if elements needs conversion.
Definition: eprecomp.h:29
Multiple precision integer with arithmetic operations.
Definition: integer.h:50
Abstract base classes that provide a uniform interface to this library.
Multiple precision integer with arithmetic operations.
Crypto++ library namespace.
Common C++ header files.
Base and exponent.
Definition: algebra.h:250