monotone

monotone Mtn Source Tree

Root/cryptopp/eprecomp.cpp

1// eprecomp.cpp - written and placed in the public domain by Wei Dai
2
3#include "pch.h"
4#include "eprecomp.h"
5#include "asn.h"
6
7NAMESPACE_BEGIN(CryptoPP)
8
9template <class T> void DL_FixedBasePrecomputationImpl<T>::SetBase(const DL_GroupPrecomputation<Element> &group, const Element &i_base)
10{
11m_base = group.NeedConversions() ? group.ConvertIn(i_base) : i_base;
12
13if (m_bases.empty() || !(m_base == m_bases[0]))
14{
15m_bases.resize(1);
16m_bases[0] = m_base;
17}
18
19if (group.NeedConversions())
20m_base = i_base;
21}
22
23template <class T> void DL_FixedBasePrecomputationImpl<T>::Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage)
24{
25assert(m_bases.size() > 0);
26assert(storage <= maxExpBits);
27
28if (storage > 1)
29{
30m_windowSize = (maxExpBits+storage-1)/storage;
31m_exponentBase = Integer::Power2(m_windowSize);
32}
33
34m_bases.resize(storage);
35for (unsigned i=1; i<storage; i++)
36m_bases[i] = group.GetGroup().ScalarMultiply(m_bases[i-1], m_exponentBase);
37}
38
39template <class T> void DL_FixedBasePrecomputationImpl<T>::Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &bt)
40{
41BERSequenceDecoder seq(bt);
42word32 version;
43BERDecodeUnsigned<word32>(seq, version, INTEGER, 1, 1);
44m_exponentBase.BERDecode(seq);
45m_windowSize = m_exponentBase.BitCount() - 1;
46m_bases.clear();
47while (!seq.EndReached())
48m_bases.push_back(group.BERDecodeElement(seq));
49if (!m_bases.empty() && group.NeedConversions())
50m_base = group.ConvertOut(m_bases[0]);
51seq.MessageEnd();
52}
53
54template <class T> void DL_FixedBasePrecomputationImpl<T>::Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &bt) const
55{
56DERSequenceEncoder seq(bt);
57DEREncodeUnsigned<word32>(seq, 1);// version
58m_exponentBase.DEREncode(seq);
59for (unsigned i=0; i<m_bases.size(); i++)
60group.DEREncodeElement(seq, m_bases[i]);
61seq.MessageEnd();
62}
63
64template <class T> void DL_FixedBasePrecomputationImpl<T>::PrepareCascade(const DL_GroupPrecomputation<Element> &i_group, std::vector<BaseAndExponent<Element> > &eb, const Integer &exponent) const
65{
66const AbstractGroup<T> &group = i_group.GetGroup();
67
68Integer r, q, e = exponent;
69bool fastNegate = group.InversionIsFast() && m_windowSize > 1;
70unsigned int i;
71
72for (i=0; i+1<m_bases.size(); i++)
73{
74Integer::DivideByPowerOf2(r, q, e, m_windowSize);
75std::swap(q, e);
76if (fastNegate && r.GetBit(m_windowSize-1))
77{
78++e;
79eb.push_back(BaseAndExponent<Element>(group.Inverse(m_bases[i]), m_exponentBase - r));
80}
81else
82eb.push_back(BaseAndExponent<Element>(m_bases[i], r));
83}
84eb.push_back(BaseAndExponent<Element>(m_bases[i], e));
85}
86
87template <class T> T DL_FixedBasePrecomputationImpl<T>::Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const
88{
89std::vector<BaseAndExponent<Element> > eb;// array of segments of the exponent and precalculated bases
90eb.reserve(m_bases.size());
91PrepareCascade(group, eb, exponent);
92return group.ConvertOut(GeneralCascadeMultiplication<Element>(group.GetGroup(), eb.begin(), eb.end()));
93}
94
95template <class T> T
96DL_FixedBasePrecomputationImpl<T>::CascadeExponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent,
97const DL_FixedBasePrecomputation<T> &i_pc2, const Integer &exponent2) const
98{
99std::vector<BaseAndExponent<Element> > eb;// array of segments of the exponent and precalculated bases
100const DL_FixedBasePrecomputationImpl<T> &pc2 = static_cast<const DL_FixedBasePrecomputationImpl<T> &>(i_pc2);
101eb.reserve(m_bases.size() + pc2.m_bases.size());
102PrepareCascade(group, eb, exponent);
103pc2.PrepareCascade(group, eb, exponent2);
104return group.ConvertOut(GeneralCascadeMultiplication<Element>(group.GetGroup(), eb.begin(), eb.end()));
105}
106
107NAMESPACE_END

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status