Security Scol plugin
eprecomp.h
Go to the documentation of this file.
1// eprecomp.h - originally written and placed in the public domain by Wei Dai
2
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
14NAMESPACE_BEGIN(CryptoPP)
15
16
18template <class T>
20{
21public:
22 typedef T Element;
23
24 virtual ~DL_GroupPrecomputation() {}
25
29 virtual bool NeedConversions() const {return false;}
30
35 virtual Element ConvertIn(const Element &v) const {return v;}
36
40 virtual Element ConvertOut(const Element &v) const {return v;}
41
44 virtual const AbstractGroup<Element> & GetGroup() const =0;
45
49 virtual Element BERDecodeElement(BufferedTransformation &bt) const =0;
50
54 virtual void DEREncodeElement(BufferedTransformation &bt, const Element &P) const =0;
55};
56
59template <class T>
61{
62public:
63 typedef T Element;
64
66
69 virtual bool IsInitialized() const =0;
70
74 virtual void SetBase(const DL_GroupPrecomputation<Element> &group, const Element &base) =0;
75
79 virtual const Element & GetBase(const DL_GroupPrecomputation<Element> &group) const =0;
80
90 virtual void Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage) =0;
91
97 virtual void Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) =0;
98
104 virtual void Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) const =0;
105
110 virtual Element Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const =0;
111
120 virtual Element CascadeExponentiate(const DL_GroupPrecomputation<Element> &pc1, const Integer &exponent1, const DL_FixedBasePrecomputation<Element> &pc2, const Integer &exponent2) const =0;
121};
122
125template <class T>
127{
128public:
129 typedef T Element;
130
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
147private:
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
156NAMESPACE_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:1652
DL_FixedBasePrecomputation interface.
Definition eprecomp.h:61
virtual const Element & GetBase(const DL_GroupPrecomputation< Element > &group) const =0
Get the base element.
virtual void Save(const DL_GroupPrecomputation< Element > &group, BufferedTransformation &storedPrecomputation) const =0
Save precomputation for later use.
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.
Definition eprecomp.cpp:44
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.
Definition eprecomp.cpp:14
void Precompute(const DL_GroupPrecomputation< Element > &group, unsigned int maxExpBits, unsigned int storage)
Perform precomputation.
Definition eprecomp.cpp:28
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
void Save(const DL_GroupPrecomputation< Element > &group, BufferedTransformation &storedPrecomputation) const
Save precomputation for later use.
Definition eprecomp.cpp:59
const Element & GetBase(const DL_GroupPrecomputation< Element > &group) const
Get the base element.
Definition eprecomp.h:139
Element Exponentiate(const DL_GroupPrecomputation< Element > &group, const Integer &exponent) const
Exponentiates an element.
Definition eprecomp.cpp:92
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.
Common C++ header files.
Base and exponent.
Definition algebra.h:250