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