Crypto++ 8.0 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>
19 class DL_GroupPrecomputation
20 {
21 public:
22 typedef T Element;
23
24 virtual ~DL_GroupPrecomputation() {}
25
26 /// \brief Determines if elements needs conversion
27 /// \returns 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 /// \returns 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 /// \returns an element converted from an alternate representation
40 virtual Element ConvertOut(const Element &v) const {return v;}
41
42 /// \brief Retrieves AbstractGroup interface
43 /// \returns 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 /// \returns 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 virtual ~DL_FixedBasePrecomputation() {}
66
67 /// \brief Determines whether this object is initialized
68 /// \returns 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 /// \returns 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 the group
94 /// \param storedPrecomputation BufferedTransformation with the saved precomputation
95 /// \throws 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 the group
101 /// \param storedPrecomputation BufferedTransformation to write the precomputation
102 /// \throws 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 /// \returns 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 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
DL_FixedBasePrecomputation::Exponentiate
virtual Element Exponentiate(const DL_GroupPrecomputation< Element > &group, const Integer &exponent) const =0
Exponentiates an element.
ECPPoint
Elliptical Curve Point over GF(p), where p is prime.
Definition: ecpoint.h:20
DL_GroupPrecomputation
DL_GroupPrecomputation interface.
Definition: eprecomp.h:19
DL_FixedBasePrecomputationImpl::SetBase
void SetBase(const DL_GroupPrecomputation< Element > &group, const Element &base)
Set the base element.
Definition: eprecomp.cpp:14
DL_FixedBasePrecomputationImpl::Precompute
void Precompute(const DL_GroupPrecomputation< Element > &group, unsigned int maxExpBits, unsigned int storage)
Perform precomputation.
Definition: eprecomp.cpp:28
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
DL_FixedBasePrecomputation::Load
virtual void Load(const DL_GroupPrecomputation< Element > &group, BufferedTransformation &storedPrecomputation)=0
Retrieve previously saved precomputation.
stdcpp.h
Common C++ header files.
algebra.h
Classes for performing mathematics over different fields.
BufferedTransformation
Interface for buffered transformations.
Definition: cryptlib.h:1598
DL_GroupPrecomputation::GetGroup
virtual const AbstractGroup< Element > & GetGroup() const =0
Retrieves AbstractGroup interface.
DL_FixedBasePrecomputationImpl::GetBase
const Element & GetBase(const DL_GroupPrecomputation< Element > &group) const
Get the base element.
Definition: eprecomp.h:139
DL_FixedBasePrecomputationImpl::CascadeExponentiate
Element CascadeExponentiate(const DL_GroupPrecomputation< Element > &pc1, const Integer &exponent1, const DL_FixedBasePrecomputation< Element > &pc2, const Integer &exponent2) const
Exponentiates an element.
Definition: eprecomp.cpp:101
DL_GroupPrecomputation::ConvertIn
virtual Element ConvertIn(const Element &v) const
Converts an element between representations.
Definition: eprecomp.h:35
DL_FixedBasePrecomputation::SetBase
virtual void SetBase(const DL_GroupPrecomputation< Element > &group, const Element &base)=0
Set the base element.
DL_FixedBasePrecomputation::IsInitialized
virtual bool IsInitialized() const =0
Determines whether this object is initialized.
DL_FixedBasePrecomputationImpl::Load
void Load(const DL_GroupPrecomputation< Element > &group, BufferedTransformation &storedPrecomputation)
Retrieve previously saved precomputation.
Definition: eprecomp.cpp:44
DL_FixedBasePrecomputation::GetBase
virtual const Element & GetBase(const DL_GroupPrecomputation< Element > &group) const =0
Get the base element.
DL_FixedBasePrecomputation::Save
virtual void Save(const DL_GroupPrecomputation< Element > &group, BufferedTransformation &storedPrecomputation) const =0
Save precomputation for later use.
DL_GroupPrecomputation::BERDecodeElement
virtual Element BERDecodeElement(BufferedTransformation &bt) const =0
Decodes element in DER format.
DL_GroupPrecomputation::NeedConversions
virtual bool NeedConversions() const
Determines if elements needs conversion.
Definition: eprecomp.h:29
Integer
Multiple precision integer with arithmetic operations.
Definition: integer.h:49
AbstractGroup
Abstract group.
Definition: algebra.h:26
DL_FixedBasePrecomputationImpl::Exponentiate
Element Exponentiate(const DL_GroupPrecomputation< Element > &group, const Integer &exponent) const
Exponentiates an element.
Definition: eprecomp.cpp:92
DL_FixedBasePrecomputation
DL_FixedBasePrecomputation interface.
Definition: eprecomp.h:60
DL_FixedBasePrecomputationImpl::IsInitialized
bool IsInitialized() const
Determines whether this object is initialized.
Definition: eprecomp.h:136
integer.h
Multiple precision integer with arithmetic operations.
BaseAndExponent
Base and exponent.
Definition: algebra.h:249
CryptoPP
Crypto++ library namespace.
DL_FixedBasePrecomputation::Precompute
virtual void Precompute(const DL_GroupPrecomputation< Element > &group, unsigned int maxExpBits, unsigned int storage)=0
Perform precomputation.
DL_FixedBasePrecomputationImpl::Save
void Save(const DL_GroupPrecomputation< Element > &group, BufferedTransformation &storedPrecomputation) const
Save precomputation for later use.
Definition: eprecomp.cpp:59
DL_FixedBasePrecomputationImpl
DL_FixedBasePrecomputation adapter class.
Definition: eprecomp.h:126
DL_GroupPrecomputation::DEREncodeElement
virtual void DEREncodeElement(BufferedTransformation &bt, const Element &P) const =0
Encodes element in DER format.
DL_FixedBasePrecomputation::CascadeExponentiate
virtual Element CascadeExponentiate(const DL_GroupPrecomputation< Element > &pc1, const Integer &exponent1, const DL_FixedBasePrecomputation< Element > &pc2, const Integer &exponent2) const =0
Exponentiates an element.
DL_GroupPrecomputation::ConvertOut
virtual Element ConvertOut(const Element &v) const
Converts an element between representations.
Definition: eprecomp.h:40