eprecomp.cpp
// eprecomp.cpp - originally written and placed in the public domain by Wei Dai
#include "pch.h"
#ifndef CRYPTOPP_IMPORTS
#include "eprecomp.h"
#include "integer.h"
#include "algebra.h"
#include "asn.h"
NAMESPACE_BEGIN(CryptoPP)
template <class T> void DL_FixedBasePrecomputationImpl<T>::SetBase(const DL_GroupPrecomputation<Element> &group, const Element &i_base)
{
m_base = group.NeedConversions() ? group.ConvertIn(i_base) : i_base;
if (m_bases.empty() || !(m_base == m_bases[0]))
{
m_bases.resize(1);
m_bases[0] = m_base;
}
if (group.NeedConversions())
m_base = i_base;
}
template <class T> void DL_FixedBasePrecomputationImpl<T>::Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage)
{
CRYPTOPP_ASSERT(m_bases.size() > 0);
CRYPTOPP_ASSERT(storage <= maxExpBits);
if (storage > 1)
{
m_windowSize = (maxExpBits+storage-1)/storage;
m_exponentBase = Integer::Power2(m_windowSize);
}
m_bases.resize(storage);
for (unsigned i=1; i<storage; i++)
m_bases[i] = group.GetGroup().ScalarMultiply(m_bases[i-1], m_exponentBase);
}
template <class T> void DL_FixedBasePrecomputationImpl<T>::Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &bt)
{
BERSequenceDecoder seq(bt);
word32 version;
BERDecodeUnsigned<word32>(seq, version, INTEGER, 1, 1);
m_exponentBase.BERDecode(seq);
m_windowSize = m_exponentBase.BitCount() - 1;
m_bases.clear();
while (!seq.EndReached())
m_bases.push_back(group.BERDecodeElement(seq));
if (!m_bases.empty() && group.NeedConversions())
m_base = group.ConvertOut(m_bases[0]);
seq.MessageEnd();
}
template <class T> void DL_FixedBasePrecomputationImpl<T>::Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &bt) const
{
DERSequenceEncoder seq(bt);
DEREncodeUnsigned<word32>(seq, 1); // version
m_exponentBase.DEREncode(seq);
for (unsigned i=0; i<m_bases.size(); i++)
group.DEREncodeElement(seq, m_bases[i]);
seq.MessageEnd();
}
template <class T> void DL_FixedBasePrecomputationImpl<T>::PrepareCascade(const DL_GroupPrecomputation<Element> &i_group, std::vector<BaseAndExponent<Element> > &eb, const Integer &exponent) const
{
const AbstractGroup<T> &group = i_group.GetGroup();
Integer r, q, e = exponent;
bool fastNegate = group.InversionIsFast() && m_windowSize > 1;
unsigned int i;
for (i=0; i+1<m_bases.size(); i++)
{
Integer::DivideByPowerOf2(r, q, e, m_windowSize);
std::swap(q, e);
if (fastNegate && r.GetBit(m_windowSize-1))
{
++e;
eb.push_back(BaseAndExponent<Element>(group.Inverse(m_bases[i]), m_exponentBase - r));
}
else
eb.push_back(BaseAndExponent<Element>(m_bases[i], r));
}
eb.push_back(BaseAndExponent<Element>(m_bases[i], e));
}
template <class T> T DL_FixedBasePrecomputationImpl<T>::Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const
{
std::vector<BaseAndExponent<Element> > eb; // array of segments of the exponent and precalculated bases
eb.reserve(m_bases.size());
PrepareCascade(group, eb, exponent);
return group.ConvertOut(GeneralCascadeMultiplication<Element>(group.GetGroup(), eb.begin(), eb.end()));
}
template <class T> T
DL_FixedBasePrecomputationImpl<T>::CascadeExponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent,
const DL_FixedBasePrecomputation<T> &i_pc2, const Integer &exponent2) const
{
std::vector<BaseAndExponent<Element> > eb; // array of segments of the exponent and precalculated bases
const DL_FixedBasePrecomputationImpl<T> &pc2 = static_cast<const DL_FixedBasePrecomputationImpl<T> &>(i_pc2);
eb.reserve(m_bases.size() + pc2.m_bases.size());
PrepareCascade(group, eb, exponent);
pc2.PrepareCascade(group, eb, exponent2);
return group.ConvertOut(GeneralCascadeMultiplication<Element>(group.GetGroup(), eb.begin(), eb.end()));
}
NAMESPACE_END
#endif
algebra.h
Classes for performing mathematics over different fields.
asn.h
Classes and functions for working with ANS.1 objects.
BERDecodeUnsigned
void BERDecodeUnsigned(BufferedTransformation &in, T &w, byte asnTag=INTEGER, T minValue=0, T maxValue=T(0xffffffff))
BER Decode unsigned value.
Definition asn.h:856
DEREncodeUnsigned
size_t DEREncodeUnsigned(BufferedTransformation &out, T w, byte asnTag=INTEGER)
DER Encode unsigned value.
Definition asn.h:820
INTEGER
@ INTEGER
ASN.1 Integer.
Definition asn.h:34
AbstractGroup
Abstract group.
Definition algebra.h:27
AbstractGroup::InversionIsFast
virtual bool InversionIsFast() const
Determine if inversion is fast.
Definition algebra.h:57
AbstractGroup::Inverse
virtual const Element & Inverse(const Element &a) const =0
Inverts the element in the group.
BERSequenceDecoder
BER Sequence Decoder.
Definition asn.h:525
BufferedTransformation
Interface for buffered transformations.
Definition cryptlib.h:1657
DERSequenceEncoder
DER Sequence Encoder.
Definition asn.h:557
Element
DL_FixedBasePrecomputation
DL_FixedBasePrecomputation interface.
Definition eprecomp.h:61
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::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_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
Integer::GetBit
bool GetBit(size_t i) const
Provides the i-th bit of the Integer.
Integer::DivideByPowerOf2
static void DivideByPowerOf2(Integer &r, Integer &q, const Integer &a, unsigned int n)
Extended Division.
Integer::Power2
static Integer Power2(size_t e)
Exponentiates to a power of 2.
word32
unsigned int word32
32-bit unsigned datatype
Definition config_int.h:72
eprecomp.h
Classes for precomputation in a group.
integer.h
Multiple precision integer with arithmetic operations.
CryptoPP
Crypto++ library namespace.
pch.h
Precompiled header file.
BaseAndExponent
Base and exponent.
Definition algebra.h:250
CRYPTOPP_ASSERT
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition trap.h:68