eprecomp.h
Go to the documentation of this file.
// eprecomp.h - originally written and placed in the public domain by Wei Dai
/// \file eprecomp.h
/// \brief Classes for precomputation in a group
#ifndef CRYPTOPP_EPRECOMP_H
#define CRYPTOPP_EPRECOMP_H
#include "cryptlib.h"
#include "integer.h"
#include "algebra.h"
#include "stdcpp.h"
NAMESPACE_BEGIN(CryptoPP)
/// \brief DL_GroupPrecomputation interface
/// \tparam T Field element
template <class T>
class DL_GroupPrecomputation
{
public:
typedef T Element;
virtual ~DL_GroupPrecomputation() {}
/// \brief Determines if elements needs conversion
/// \return true if the element needs conversion, false otherwise
/// \details NeedConversions determines if an element must convert between representations.
virtual bool NeedConversions() const {return false;}
/// \brief Converts an element between representations
/// \param v element to convert
/// \return an element converted to an alternate representation for internal use
/// \details ConvertIn is used when an element must convert between representations.
virtual Element ConvertIn(const Element &v) const {return v;}
/// \brief Converts an element between representations
/// \param v element to convert
/// \return an element converted from an alternate representation
virtual Element ConvertOut(const Element &v) const {return v;}
/// \brief Retrieves AbstractGroup interface
/// \return GetGroup() returns the AbstractGroup interface
virtual const AbstractGroup<Element> & GetGroup() const =0;
/// \brief Decodes element in DER format
/// \param bt BufferedTransformation object
/// \return element in the group
virtual Element BERDecodeElement(BufferedTransformation &bt) const =0;
/// \brief Encodes element in DER format
/// \param bt BufferedTransformation object
/// \param P Element to encode
virtual void DEREncodeElement(BufferedTransformation &bt, const Element &P) const =0;
};
/// \brief DL_FixedBasePrecomputation interface
/// \tparam T Field element
template <class T>
class DL_FixedBasePrecomputation
{
public:
typedef T Element;
virtual ~DL_FixedBasePrecomputation() {}
/// \brief Determines whether this object is initialized
/// \return true if this object is initialized, false otherwise
virtual bool IsInitialized() const =0;
/// \brief Set the base element
/// \param group the group
/// \param base element in the group
virtual void SetBase(const DL_GroupPrecomputation<Element> &group, const Element &base) =0;
/// \brief Get the base element
/// \param group the group
/// \return base element in the group
virtual const Element & GetBase(const DL_GroupPrecomputation<Element> &group) const =0;
/// \brief Perform precomputation
/// \param group the group
/// \param maxExpBits used to calculate the exponent base
/// \param storage the suggested number of objects for the precompute table
/// \details The exact semantics of Precompute() varies, but it typically means calculate
/// a table of n objects that can be used later to speed up computation.
/// \details If a derived class does not override Precompute(), then the base class throws
/// NotImplemented.
/// \sa SupportsPrecomputation(), LoadPrecomputation(), SavePrecomputation()
virtual void Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage) =0;
/// \brief Retrieve previously saved precomputation
/// \param group the group
/// \param storedPrecomputation BufferedTransformation with the saved precomputation
/// \throw NotImplemented
/// \sa SupportsPrecomputation(), Precompute()
virtual void Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) =0;
/// \brief Save precomputation for later use
/// \param group the group
/// \param storedPrecomputation BufferedTransformation to write the precomputation
/// \throw NotImplemented
/// \sa SupportsPrecomputation(), Precompute()
virtual void Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) const =0;
/// \brief Exponentiates an element
/// \param group the group
/// \param exponent the exponent
/// \return the result of the exponentiation
virtual Element Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const =0;
/// \brief Exponentiates an element
/// \param pc1 the first the group precomputation
/// \param exponent1 the first exponent
/// \param pc2 the second the group precomputation
/// \param exponent2 the first exponent2
/// \return the public element raised to the exponent
/// \details CascadeExponentiateBaseAndPublicElement raises the public element to
/// the base element and precomputation.
virtual Element CascadeExponentiate(const DL_GroupPrecomputation<Element> &pc1, const Integer &exponent1, const DL_FixedBasePrecomputation<Element> &pc2, const Integer &exponent2) const =0;
};
/// \brief DL_FixedBasePrecomputation adapter class
/// \tparam T Field element
template <class T>
class DL_FixedBasePrecomputationImpl : public DL_FixedBasePrecomputation<T>
{
public:
typedef T Element;
virtual ~DL_FixedBasePrecomputationImpl() {}
DL_FixedBasePrecomputationImpl() : m_windowSize(0) {}
// DL_FixedBasePrecomputation
bool IsInitialized() const
{return !m_bases.empty();}
void SetBase(const DL_GroupPrecomputation<Element> &group, const Element &base);
const Element & GetBase(const DL_GroupPrecomputation<Element> &group) const
{return group.NeedConversions() ? m_base : m_bases[0];}
void Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage);
void Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation);
void Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) const;
Element Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const;
Element CascadeExponentiate(const DL_GroupPrecomputation<Element> &pc1, const Integer &exponent1, const DL_FixedBasePrecomputation<Element> &pc2, const Integer &exponent2) const;
private:
void PrepareCascade(const DL_GroupPrecomputation<Element> &group, std::vector<BaseAndExponent<Element> > &eb, const Integer &exponent) const;
Element m_base;
unsigned int m_windowSize;
Integer m_exponentBase; // what base to represent the exponent in
std::vector<Element> m_bases; // precalculated bases
};
NAMESPACE_END
#ifdef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
#include "eprecomp.cpp"
#endif
#endif
algebra.h
Classes for performing mathematics over different fields.
AbstractGroup
Abstract group.
Definition algebra.h:27
BufferedTransformation
Interface for buffered transformations.
Definition cryptlib.h:1657
DL_FixedBasePrecomputation
DL_FixedBasePrecomputation interface.
Definition eprecomp.h:61
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_FixedBasePrecomputation::SetBase
virtual void SetBase(const DL_GroupPrecomputation< Element > &group, const Element &base)=0
Set the base element.
DL_FixedBasePrecomputation::Precompute
virtual void Precompute(const DL_GroupPrecomputation< Element > &group, unsigned int maxExpBits, unsigned int storage)=0
Perform precomputation.
DL_FixedBasePrecomputation::Exponentiate
virtual Element Exponentiate(const DL_GroupPrecomputation< Element > &group, const Integer &exponent) const =0
Exponentiates an element.
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_FixedBasePrecomputation::IsInitialized
virtual bool IsInitialized() const =0
Determines whether this object is initialized.
DL_FixedBasePrecomputation::Load
virtual void Load(const DL_GroupPrecomputation< Element > &group, BufferedTransformation &storedPrecomputation)=0
Retrieve previously saved precomputation.
DL_FixedBasePrecomputationImpl
DL_FixedBasePrecomputation adapter class.
Definition eprecomp.h:127
DL_FixedBasePrecomputationImpl::Load
void Load(const DL_GroupPrecomputation< Element > &group, BufferedTransformation &storedPrecomputation)
Retrieve previously saved precomputation.
DL_FixedBasePrecomputationImpl::Exponentiate
Element Exponentiate(const DL_GroupPrecomputation< Element > &group, const Integer &exponent) const
Exponentiates an element.
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.
DL_FixedBasePrecomputationImpl::IsInitialized
bool IsInitialized() const
Determines whether this object is initialized.
Definition eprecomp.h:136
DL_FixedBasePrecomputationImpl::SetBase
void SetBase(const DL_GroupPrecomputation< Element > &group, const Element &base)
Set the base element.
DL_FixedBasePrecomputationImpl::Precompute
void Precompute(const DL_GroupPrecomputation< Element > &group, unsigned int maxExpBits, unsigned int storage)
Perform precomputation.
DL_FixedBasePrecomputationImpl::Save
void Save(const DL_GroupPrecomputation< Element > &group, BufferedTransformation &storedPrecomputation) const
Save precomputation for later use.
DL_FixedBasePrecomputationImpl::GetBase
const Element & GetBase(const DL_GroupPrecomputation< Element > &group) const
Get the base element.
Definition eprecomp.h:139
DL_GroupPrecomputation
DL_GroupPrecomputation interface.
Definition eprecomp.h:20
DL_GroupPrecomputation::ConvertIn
virtual Element ConvertIn(const Element &v) const
Converts an element between representations.
Definition eprecomp.h:35
DL_GroupPrecomputation::BERDecodeElement
virtual Element BERDecodeElement(BufferedTransformation &bt) const =0
Decodes element in DER format.
DL_GroupPrecomputation::DEREncodeElement
virtual void DEREncodeElement(BufferedTransformation &bt, const Element &P) const =0
Encodes element in DER format.
DL_GroupPrecomputation::GetGroup
virtual const AbstractGroup< Element > & GetGroup() const =0
Retrieves AbstractGroup interface.
DL_GroupPrecomputation::ConvertOut
virtual Element ConvertOut(const Element &v) const
Converts an element between representations.
Definition eprecomp.h:40
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:50
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
integer.h
Multiple precision integer with arithmetic operations.
CryptoPP
Crypto++ library namespace.
stdcpp.h
Common C++ header files.
BaseAndExponent
Base and exponent.
Definition algebra.h:250