Coverage Report

Created: 2024-11-21 07:03

/src/cryptopp/eprecomp.h
Line
Count
Source (jump to first uncovered line)
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>
19
class DL_GroupPrecomputation
20
{
21
public:
22
  typedef T Element;
23
24
30.6k
  virtual ~DL_GroupPrecomputation() {}
CryptoPP::DL_GroupPrecomputation<CryptoPP::ECPPoint>::~DL_GroupPrecomputation()
Line
Count
Source
24
30.6k
  virtual ~DL_GroupPrecomputation() {}
Unexecuted instantiation: CryptoPP::DL_GroupPrecomputation<CryptoPP::EC2NPoint>::~DL_GroupPrecomputation()
Unexecuted instantiation: CryptoPP::DL_GroupPrecomputation<CryptoPP::Integer>::~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
0
  virtual bool NeedConversions() const {return false;}
Unexecuted instantiation: CryptoPP::DL_GroupPrecomputation<CryptoPP::Integer>::NeedConversions() const
Unexecuted instantiation: CryptoPP::DL_GroupPrecomputation<CryptoPP::EC2NPoint>::NeedConversions() const
Unexecuted instantiation: CryptoPP::DL_GroupPrecomputation<CryptoPP::ECPPoint>::NeedConversions() const
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
0
  virtual Element ConvertIn(const Element &v) const {return v;}
Unexecuted instantiation: CryptoPP::DL_GroupPrecomputation<CryptoPP::Integer>::ConvertIn(CryptoPP::Integer const&) const
Unexecuted instantiation: CryptoPP::DL_GroupPrecomputation<CryptoPP::EC2NPoint>::ConvertIn(CryptoPP::EC2NPoint const&) const
Unexecuted instantiation: CryptoPP::DL_GroupPrecomputation<CryptoPP::ECPPoint>::ConvertIn(CryptoPP::ECPPoint const&) const
36
37
  /// \brief Converts an element between representations
38
  /// \param v element to convert
39
  /// \return an element converted from an alternate representation
40
0
  virtual Element ConvertOut(const Element &v) const {return v;}
Unexecuted instantiation: CryptoPP::DL_GroupPrecomputation<CryptoPP::Integer>::ConvertOut(CryptoPP::Integer const&) const
Unexecuted instantiation: CryptoPP::DL_GroupPrecomputation<CryptoPP::EC2NPoint>::ConvertOut(CryptoPP::EC2NPoint const&) const
Unexecuted instantiation: CryptoPP::DL_GroupPrecomputation<CryptoPP::ECPPoint>::ConvertOut(CryptoPP::ECPPoint const&) const
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>
60
class DL_FixedBasePrecomputation
61
{
62
public:
63
  typedef T Element;
64
65
31.9k
  virtual ~DL_FixedBasePrecomputation() {}
CryptoPP::DL_FixedBasePrecomputation<CryptoPP::ECPPoint>::~DL_FixedBasePrecomputation()
Line
Count
Source
65
31.9k
  virtual ~DL_FixedBasePrecomputation() {}
Unexecuted instantiation: CryptoPP::DL_FixedBasePrecomputation<CryptoPP::Integer>::~DL_FixedBasePrecomputation()
Unexecuted instantiation: CryptoPP::DL_FixedBasePrecomputation<CryptoPP::EC2NPoint>::~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>
126
class DL_FixedBasePrecomputationImpl : public DL_FixedBasePrecomputation<T>
127
{
128
public:
129
  typedef T Element;
130
131
31.9k
  virtual ~DL_FixedBasePrecomputationImpl() {}
CryptoPP::DL_FixedBasePrecomputationImpl<CryptoPP::ECPPoint>::~DL_FixedBasePrecomputationImpl()
Line
Count
Source
131
31.9k
  virtual ~DL_FixedBasePrecomputationImpl() {}
Unexecuted instantiation: CryptoPP::DL_FixedBasePrecomputationImpl<CryptoPP::Integer>::~DL_FixedBasePrecomputationImpl()
Unexecuted instantiation: CryptoPP::DL_FixedBasePrecomputationImpl<CryptoPP::EC2NPoint>::~DL_FixedBasePrecomputationImpl()
132
133
31.9k
  DL_FixedBasePrecomputationImpl() : m_windowSize(0) {}
CryptoPP::DL_FixedBasePrecomputationImpl<CryptoPP::ECPPoint>::DL_FixedBasePrecomputationImpl()
Line
Count
Source
133
31.9k
  DL_FixedBasePrecomputationImpl() : m_windowSize(0) {}
Unexecuted instantiation: CryptoPP::DL_FixedBasePrecomputationImpl<CryptoPP::Integer>::DL_FixedBasePrecomputationImpl()
Unexecuted instantiation: CryptoPP::DL_FixedBasePrecomputationImpl<CryptoPP::EC2NPoint>::DL_FixedBasePrecomputationImpl()
134
135
  // DL_FixedBasePrecomputation
136
  bool IsInitialized() const
137
0
    {return !m_bases.empty();}
Unexecuted instantiation: CryptoPP::DL_FixedBasePrecomputationImpl<CryptoPP::Integer>::IsInitialized() const
Unexecuted instantiation: CryptoPP::DL_FixedBasePrecomputationImpl<CryptoPP::EC2NPoint>::IsInitialized() const
Unexecuted instantiation: CryptoPP::DL_FixedBasePrecomputationImpl<CryptoPP::ECPPoint>::IsInitialized() const
138
  void SetBase(const DL_GroupPrecomputation<Element> &group, const Element &base);
139
  const Element & GetBase(const DL_GroupPrecomputation<Element> &group) const
140
0
    {return group.NeedConversions() ? m_base : m_bases[0];}
Unexecuted instantiation: CryptoPP::DL_FixedBasePrecomputationImpl<CryptoPP::Integer>::GetBase(CryptoPP::DL_GroupPrecomputation<CryptoPP::Integer> const&) const
Unexecuted instantiation: CryptoPP::DL_FixedBasePrecomputationImpl<CryptoPP::EC2NPoint>::GetBase(CryptoPP::DL_GroupPrecomputation<CryptoPP::EC2NPoint> const&) const
Unexecuted instantiation: CryptoPP::DL_FixedBasePrecomputationImpl<CryptoPP::ECPPoint>::GetBase(CryptoPP::DL_GroupPrecomputation<CryptoPP::ECPPoint> const&) const
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