<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>Crypto++: eprecomp.cpp Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.6.1 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul> </div> <h1>eprecomp.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// eprecomp.cpp - written and placed in the public domain by Wei Dai</span> <a name="l00002"></a>00002 <a name="l00003"></a>00003 <span class="preprocessor">#include "pch.h"</span> <a name="l00004"></a>00004 <a name="l00005"></a>00005 <span class="preprocessor">#ifndef CRYPTOPP_IMPORTS</span> <a name="l00006"></a>00006 <span class="preprocessor"></span> <a name="l00007"></a>00007 <span class="preprocessor">#include "eprecomp.h"</span> <a name="l00008"></a>00008 <span class="preprocessor">#include "asn.h"</span> <a name="l00009"></a>00009 <a name="l00010"></a>00010 NAMESPACE_BEGIN(CryptoPP) <a name="l00011"></a>00011 <a name="l00012"></a>00012 template <class T> <span class="keywordtype">void</span> <a class="code" href="class_d_l___fixed_base_precomputation_impl.html">DL_FixedBasePrecomputationImpl</a><T>::SetBase(const <a class="code" href="class_d_l___group_precomputation.html">DL_GroupPrecomputation</a><Element> &group, const Element &i_base) <a name="l00013"></a>00013 { <a name="l00014"></a>00014 m_base = group.NeedConversions() ? group.ConvertIn(i_base) : i_base; <a name="l00015"></a>00015 <a name="l00016"></a>00016 <span class="keywordflow">if</span> (m_bases.empty() || !(m_base == m_bases[0])) <a name="l00017"></a>00017 { <a name="l00018"></a>00018 m_bases.resize(1); <a name="l00019"></a>00019 m_bases[0] = m_base; <a name="l00020"></a>00020 } <a name="l00021"></a>00021 <a name="l00022"></a>00022 <span class="keywordflow">if</span> (group.NeedConversions()) <a name="l00023"></a>00023 m_base = i_base; <a name="l00024"></a>00024 } <a name="l00025"></a>00025 <a name="l00026"></a>00026 <span class="keyword">template</span> <<span class="keyword">class</span> T> <span class="keywordtype">void</span> <a class="code" href="class_d_l___fixed_base_precomputation_impl.html">DL_FixedBasePrecomputationImpl<T>::Precompute</a>(<span class="keyword">const</span> <a class="code" href="class_d_l___group_precomputation.html">DL_GroupPrecomputation<Element></a> &group, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> maxExpBits, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> storage) <a name="l00027"></a>00027 { <a name="l00028"></a>00028 assert(m_bases.size() > 0); <a name="l00029"></a>00029 assert(storage <= maxExpBits); <a name="l00030"></a>00030 <a name="l00031"></a>00031 <span class="keywordflow">if</span> (storage > 1) <a name="l00032"></a>00032 { <a name="l00033"></a>00033 m_windowSize = (maxExpBits+storage-1)/storage; <a name="l00034"></a>00034 m_exponentBase = <a class="code" href="class_integer.html#ade53248f5dbb520273a70856b975417c" title="return the integer 2**e">Integer::Power2</a>(m_windowSize); <a name="l00035"></a>00035 } <a name="l00036"></a>00036 <a name="l00037"></a>00037 m_bases.resize(storage); <a name="l00038"></a>00038 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i=1; i<storage; i++) <a name="l00039"></a>00039 m_bases[i] = group.GetGroup().ScalarMultiply(m_bases[i-1], m_exponentBase); <a name="l00040"></a>00040 } <a name="l00041"></a>00041 <a name="l00042"></a>00042 <span class="keyword">template</span> <<span class="keyword">class</span> T> <span class="keywordtype">void</span> <a class="code" href="class_d_l___fixed_base_precomputation_impl.html">DL_FixedBasePrecomputationImpl<T>::Load</a>(<span class="keyword">const</span> <a class="code" href="class_d_l___group_precomputation.html">DL_GroupPrecomputation<Element></a> &group, <a class="code" href="class_buffered_transformation.html" title="interface for buffered transformations">BufferedTransformation</a> &bt) <a name="l00043"></a>00043 { <a name="l00044"></a>00044 <a class="code" href="class_b_e_r_sequence_decoder.html" title="BER Sequence Decoder.">BERSequenceDecoder</a> seq(bt); <a name="l00045"></a>00045 word32 version; <a name="l00046"></a>00046 BERDecodeUnsigned<word32>(seq, version, INTEGER, 1, 1); <a name="l00047"></a>00047 m_exponentBase.BERDecode(seq); <a name="l00048"></a>00048 m_windowSize = m_exponentBase.BitCount() - 1; <a name="l00049"></a>00049 m_bases.clear(); <a name="l00050"></a>00050 <span class="keywordflow">while</span> (!seq.EndReached()) <a name="l00051"></a>00051 m_bases.push_back(group.BERDecodeElement(seq)); <a name="l00052"></a>00052 <span class="keywordflow">if</span> (!m_bases.empty() && group.NeedConversions()) <a name="l00053"></a>00053 m_base = group.ConvertOut(m_bases[0]); <a name="l00054"></a>00054 seq.MessageEnd(); <a name="l00055"></a>00055 } <a name="l00056"></a>00056 <a name="l00057"></a>00057 <span class="keyword">template</span> <<span class="keyword">class</span> T> <span class="keywordtype">void</span> <a class="code" href="class_d_l___fixed_base_precomputation_impl.html">DL_FixedBasePrecomputationImpl<T>::Save</a>(<span class="keyword">const</span> <a class="code" href="class_d_l___group_precomputation.html">DL_GroupPrecomputation<Element></a> &group, <a class="code" href="class_buffered_transformation.html" title="interface for buffered transformations">BufferedTransformation</a> &bt)<span class="keyword"> const</span> <a name="l00058"></a>00058 <span class="keyword"></span>{ <a name="l00059"></a>00059 <a class="code" href="class_d_e_r_sequence_encoder.html" title="DER Sequence Encoder.">DERSequenceEncoder</a> seq(bt); <a name="l00060"></a>00060 DEREncodeUnsigned<word32>(seq, 1); <span class="comment">// version</span> <a name="l00061"></a>00061 m_exponentBase.DEREncode(seq); <a name="l00062"></a>00062 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i=0; i<m_bases.size(); i++) <a name="l00063"></a>00063 group.DEREncodeElement(seq, m_bases[i]); <a name="l00064"></a>00064 seq.MessageEnd(); <a name="l00065"></a>00065 } <a name="l00066"></a>00066 <a name="l00067"></a>00067 <span class="keyword">template</span> <<span class="keyword">class</span> T> <span class="keywordtype">void</span> <a class="code" href="class_d_l___fixed_base_precomputation_impl.html">DL_FixedBasePrecomputationImpl<T>::PrepareCascade</a>(<span class="keyword">const</span> <a class="code" href="class_d_l___group_precomputation.html">DL_GroupPrecomputation<Element></a> &i_group, std::vector<<a class="code" href="struct_base_and_exponent.html" title="Base and Exponent.">BaseAndExponent<Element></a> > &eb, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &exponent)<span class="keyword"> const</span> <a name="l00068"></a>00068 <span class="keyword"></span>{ <a name="l00069"></a>00069 <span class="keyword">const</span> <a class="code" href="class_abstract_group.html" title="Abstract Group.">AbstractGroup<T></a> &group = i_group.GetGroup(); <a name="l00070"></a>00070 <a name="l00071"></a>00071 <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> r, q, e = exponent; <a name="l00072"></a>00072 <span class="keywordtype">bool</span> fastNegate = group.InversionIsFast() && m_windowSize > 1; <a name="l00073"></a>00073 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i; <a name="l00074"></a>00074 <a name="l00075"></a>00075 <span class="keywordflow">for</span> (i=0; i+1<m_bases.size(); i++) <a name="l00076"></a>00076 { <a name="l00077"></a>00077 <a class="code" href="class_integer.html#abf2934a6172b4cb5317874f0c993a420" title="returns same result as Divide(r, q, a, Power2(n)), but faster">Integer::DivideByPowerOf2</a>(r, q, e, m_windowSize); <a name="l00078"></a>00078 std::swap(q, e); <a name="l00079"></a>00079 <span class="keywordflow">if</span> (fastNegate && r.<a class="code" href="class_integer.html#a2814c3b82849bd8f6f44cc36974f1717" title="return the i-th bit, i=0 being the least significant bit">GetBit</a>(m_windowSize-1)) <a name="l00080"></a>00080 { <a name="l00081"></a>00081 ++e; <a name="l00082"></a>00082 eb.push_back(<a class="code" href="struct_base_and_exponent.html" title="Base and Exponent.">BaseAndExponent<Element></a>(group.Inverse(m_bases[i]), m_exponentBase - r)); <a name="l00083"></a>00083 } <a name="l00084"></a>00084 <span class="keywordflow">else</span> <a name="l00085"></a>00085 eb.push_back(<a class="code" href="struct_base_and_exponent.html" title="Base and Exponent.">BaseAndExponent<Element></a>(m_bases[i], r)); <a name="l00086"></a>00086 } <a name="l00087"></a>00087 eb.push_back(<a class="code" href="struct_base_and_exponent.html" title="Base and Exponent.">BaseAndExponent<Element></a>(m_bases[i], e)); <a name="l00088"></a>00088 } <a name="l00089"></a>00089 <a name="l00090"></a>00090 <span class="keyword">template</span> <<span class="keyword">class</span> T> T <a class="code" href="class_d_l___fixed_base_precomputation_impl.html">DL_FixedBasePrecomputationImpl<T>::Exponentiate</a>(<span class="keyword">const</span> <a class="code" href="class_d_l___group_precomputation.html">DL_GroupPrecomputation<Element></a> &group, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &exponent)<span class="keyword"> const</span> <a name="l00091"></a>00091 <span class="keyword"></span>{ <a name="l00092"></a>00092 std::vector<BaseAndExponent<Element> > eb; <span class="comment">// array of segments of the exponent and precalculated bases</span> <a name="l00093"></a>00093 eb.reserve(m_bases.size()); <a name="l00094"></a>00094 PrepareCascade(group, eb, exponent); <a name="l00095"></a>00095 <span class="keywordflow">return</span> group.ConvertOut(GeneralCascadeMultiplication<Element>(group.GetGroup(), eb.begin(), eb.end())); <a name="l00096"></a>00096 } <a name="l00097"></a>00097 <a name="l00098"></a>00098 <span class="keyword">template</span> <<span class="keyword">class</span> T> T <a name="l00099"></a>00099 <a class="code" href="class_d_l___fixed_base_precomputation_impl.html">DL_FixedBasePrecomputationImpl<T>::CascadeExponentiate</a>(<span class="keyword">const</span> <a class="code" href="class_d_l___group_precomputation.html">DL_GroupPrecomputation<Element></a> &group, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &exponent, <a name="l00100"></a>00100 <span class="keyword">const</span> <a class="code" href="class_d_l___fixed_base_precomputation.html">DL_FixedBasePrecomputation<T></a> &i_pc2, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &exponent2)<span class="keyword"> const</span> <a name="l00101"></a>00101 <span class="keyword"></span>{ <a name="l00102"></a>00102 std::vector<BaseAndExponent<Element> > eb; <span class="comment">// array of segments of the exponent and precalculated bases</span> <a name="l00103"></a>00103 <span class="keyword">const</span> <a class="code" href="class_d_l___fixed_base_precomputation_impl.html">DL_FixedBasePrecomputationImpl<T></a> &pc2 = <span class="keyword">static_cast<</span><span class="keyword">const </span><a class="code" href="class_d_l___fixed_base_precomputation_impl.html">DL_FixedBasePrecomputationImpl<T></a> &<span class="keyword">></span>(i_pc2); <a name="l00104"></a>00104 eb.reserve(m_bases.size() + pc2.m_bases.size()); <a name="l00105"></a>00105 PrepareCascade(group, eb, exponent); <a name="l00106"></a>00106 pc2.PrepareCascade(group, eb, exponent2); <a name="l00107"></a>00107 <span class="keywordflow">return</span> group.ConvertOut(GeneralCascadeMultiplication<Element>(group.GetGroup(), eb.begin(), eb.end())); <a name="l00108"></a>00108 } <a name="l00109"></a>00109 <a name="l00110"></a>00110 NAMESPACE_END <a name="l00111"></a>00111 <a name="l00112"></a>00112 <span class="preprocessor">#endif</span> </pre></div></div> <hr size="1"/><address style="text-align: right;"><small>Generated on 9 Dec 2009 for Crypto++ by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.1 </small></address> </body> </html>