Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:02

0001 // eprecomp.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file eprecomp.h

0004 /// \brief Classes for precomputation in a group

0005 
0006 #ifndef CRYPTOPP_EPRECOMP_H
0007 #define CRYPTOPP_EPRECOMP_H
0008 
0009 #include "cryptlib.h"
0010 #include "integer.h"
0011 #include "algebra.h"
0012 #include "stdcpp.h"
0013 
0014 NAMESPACE_BEGIN(CryptoPP)
0015 
0016 /// \brief DL_GroupPrecomputation interface

0017 /// \tparam T Field element

0018 template <class T>
0019 class DL_GroupPrecomputation
0020 {
0021 public:
0022     typedef T Element;
0023 
0024     virtual ~DL_GroupPrecomputation() {}
0025 
0026     /// \brief Determines if elements needs conversion

0027     /// \return true if the element needs conversion, false otherwise

0028     /// \details NeedConversions determines if an element must convert between representations.

0029     virtual bool NeedConversions() const {return false;}
0030 
0031     /// \brief Converts an element between representations

0032     /// \param v element to convert

0033     /// \return an element converted to an alternate representation for internal use

0034     /// \details ConvertIn is used when an element must convert between representations.

0035     virtual Element ConvertIn(const Element &v) const {return v;}
0036 
0037     /// \brief Converts an element between representations

0038     /// \param v element to convert

0039     /// \return an element converted from an alternate representation

0040     virtual Element ConvertOut(const Element &v) const {return v;}
0041 
0042     /// \brief Retrieves AbstractGroup interface

0043     /// \return GetGroup() returns the AbstractGroup interface

0044     virtual const AbstractGroup<Element> & GetGroup() const =0;
0045 
0046     /// \brief Decodes element in DER format

0047     /// \param bt BufferedTransformation object

0048     /// \return element in the group

0049     virtual Element BERDecodeElement(BufferedTransformation &bt) const =0;
0050 
0051     /// \brief Encodes element in DER format

0052     /// \param bt BufferedTransformation object

0053     /// \param P Element to encode

0054     virtual void DEREncodeElement(BufferedTransformation &bt, const Element &P) const =0;
0055 };
0056 
0057 /// \brief DL_FixedBasePrecomputation interface

0058 /// \tparam T Field element

0059 template <class T>
0060 class DL_FixedBasePrecomputation
0061 {
0062 public:
0063     typedef T Element;
0064 
0065     virtual ~DL_FixedBasePrecomputation() {}
0066 
0067     /// \brief Determines whether this object is initialized

0068     /// \return true if this object is initialized, false otherwise

0069     virtual bool IsInitialized() const =0;
0070 
0071     /// \brief Set the base element

0072     /// \param group the group

0073     /// \param base element in the group

0074     virtual void SetBase(const DL_GroupPrecomputation<Element> &group, const Element &base) =0;
0075 
0076     /// \brief Get the base element

0077     /// \param group the group

0078     /// \return base element in the group

0079     virtual const Element & GetBase(const DL_GroupPrecomputation<Element> &group) const =0;
0080 
0081     /// \brief Perform precomputation

0082     /// \param group the group

0083     /// \param maxExpBits used to calculate the exponent base

0084     /// \param storage the suggested number of objects for the precompute table

0085     /// \details The exact semantics of Precompute() varies, but it typically means calculate

0086     ///   a table of n objects that can be used later to speed up computation.

0087     /// \details If a derived class does not override Precompute(), then the base class throws

0088     ///   NotImplemented.

0089     /// \sa SupportsPrecomputation(), LoadPrecomputation(), SavePrecomputation()

0090     virtual void Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage) =0;
0091 
0092     /// \brief Retrieve previously saved precomputation

0093     /// \param group the group

0094     /// \param storedPrecomputation BufferedTransformation with the saved precomputation

0095     /// \throw NotImplemented

0096     /// \sa SupportsPrecomputation(), Precompute()

0097     virtual void Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) =0;
0098 
0099     /// \brief Save precomputation for later use

0100     /// \param group the group

0101     /// \param storedPrecomputation BufferedTransformation to write the precomputation

0102     /// \throw NotImplemented

0103     /// \sa SupportsPrecomputation(), Precompute()

0104     virtual void Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) const =0;
0105 
0106     /// \brief Exponentiates an element

0107     /// \param group the group

0108     /// \param exponent the exponent

0109     /// \return the result of the exponentiation

0110     virtual Element Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const =0;
0111 
0112     /// \brief Exponentiates an element

0113     /// \param pc1 the first the group precomputation

0114     /// \param exponent1 the first exponent

0115     /// \param pc2 the second the group precomputation

0116     /// \param exponent2 the first exponent2

0117     /// \return the public element raised to the exponent

0118     /// \details CascadeExponentiateBaseAndPublicElement raises the public element to

0119     ///   the base element and precomputation.

0120     virtual Element CascadeExponentiate(const DL_GroupPrecomputation<Element> &pc1, const Integer &exponent1, const DL_FixedBasePrecomputation<Element> &pc2, const Integer &exponent2) const =0;
0121 };
0122 
0123 /// \brief DL_FixedBasePrecomputation adapter class

0124 /// \tparam T Field element

0125 template <class T>
0126 class DL_FixedBasePrecomputationImpl : public DL_FixedBasePrecomputation<T>
0127 {
0128 public:
0129     typedef T Element;
0130 
0131     virtual ~DL_FixedBasePrecomputationImpl() {}
0132 
0133     DL_FixedBasePrecomputationImpl() : m_windowSize(0) {}
0134 
0135     // DL_FixedBasePrecomputation

0136     bool IsInitialized() const
0137         {return !m_bases.empty();}
0138     void SetBase(const DL_GroupPrecomputation<Element> &group, const Element &base);
0139     const Element & GetBase(const DL_GroupPrecomputation<Element> &group) const
0140         {return group.NeedConversions() ? m_base : m_bases[0];}
0141     void Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage);
0142     void Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation);
0143     void Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) const;
0144     Element Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const;
0145     Element CascadeExponentiate(const DL_GroupPrecomputation<Element> &pc1, const Integer &exponent1, const DL_FixedBasePrecomputation<Element> &pc2, const Integer &exponent2) const;
0146 
0147 private:
0148     void PrepareCascade(const DL_GroupPrecomputation<Element> &group, std::vector<BaseAndExponent<Element> > &eb, const Integer &exponent) const;
0149 
0150     Element m_base;
0151     unsigned int m_windowSize;
0152     Integer m_exponentBase;         // what base to represent the exponent in

0153     std::vector<Element> m_bases;   // precalculated bases

0154 };
0155 
0156 NAMESPACE_END
0157 
0158 #ifdef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
0159 #include "eprecomp.cpp"
0160 #endif
0161 
0162 #endif