⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ecp_8cpp-source.html

📁 著名的密码库Crypto++的文档 C++语言的杰作。程序员必备。
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<a name="l00230"></a>00230         m_R.<a class="code" href="struct_e_c_p_point.html#24bbcc7eb375ebe2b46d2ac9ee6bb0a8">x</a>.<a class="code" href="class_integer.html#bf1e9c170e56841ce73d137c12675f9b">swap</a>(x);<a name="l00231"></a>00231         m_R.<a class="code" href="struct_e_c_p_point.html#806dedcf077e22de7904ccfd7987bcf9">identity</a> = <span class="keyword">false</span>;<a name="l00232"></a>00232         <span class="keywordflow">return</span> m_R;<a name="l00233"></a>00233 }<a name="l00234"></a>00234 <a name="l00235"></a>00235 <span class="keyword">const</span> <a class="code" href="struct_e_c_p_point.html" title="Elliptical Curve Point.">ECP::Point</a>&amp; ECP::Double(<span class="keyword">const</span> <a class="code" href="class_e_c_p.html#99c34a437007f32af4e6c4ae275358ea">Point</a> &amp;P)<span class="keyword"> const</span><a name="l00236"></a>00236 <span class="keyword"></span>{<a name="l00237"></a>00237         <span class="keywordflow">if</span> (P.identity || P.y==<a class="code" href="class_e_c_p.html#e996fce212244df79b83a587317f7423">GetField</a>().Identity()) <span class="keywordflow">return</span> Identity();<a name="l00238"></a>00238 <a name="l00239"></a>00239         <a class="code" href="class_e_c_p.html#337e68ddf10edab098c55474d52c4d04">FieldElement</a> t = <a class="code" href="class_e_c_p.html#e996fce212244df79b83a587317f7423">GetField</a>().<a class="code" href="class_modular_arithmetic.html#c378a2527fe2107d3379bc35d7cd0487">Square</a>(P.x);<a name="l00240"></a>00240         t = <a class="code" href="class_e_c_p.html#e996fce212244df79b83a587317f7423">GetField</a>().<a class="code" href="class_modular_arithmetic.html#f840f9421d210579fb9b526a90e857fe">Add</a>(<a class="code" href="class_e_c_p.html#e996fce212244df79b83a587317f7423">GetField</a>().Add(<a class="code" href="class_e_c_p.html#e996fce212244df79b83a587317f7423">GetField</a>().Double(t), t), m_a);<a name="l00241"></a>00241         t = <a class="code" href="class_e_c_p.html#e996fce212244df79b83a587317f7423">GetField</a>().<a class="code" href="class_modular_arithmetic.html#584d214676856d1463e14c6407d1cd31">Divide</a>(t, <a class="code" href="class_e_c_p.html#e996fce212244df79b83a587317f7423">GetField</a>().Double(P.y));<a name="l00242"></a>00242         <a class="code" href="class_e_c_p.html#337e68ddf10edab098c55474d52c4d04">FieldElement</a> x = <a class="code" href="class_e_c_p.html#e996fce212244df79b83a587317f7423">GetField</a>().<a class="code" href="class_modular_arithmetic.html#e4705633e8ca4308894f9a26c6f2881c">Subtract</a>(<a class="code" href="class_e_c_p.html#e996fce212244df79b83a587317f7423">GetField</a>().<a class="code" href="class_abstract_group.html#a19e1bef00198fb30eb01df3f7076717">Subtract</a>(<a class="code" href="class_e_c_p.html#e996fce212244df79b83a587317f7423">GetField</a>().<a class="code" href="class_square.html" title="Square">Square</a>(t), P.x), P.<a class="code" href="struct_e_c_p_point.html#24bbcc7eb375ebe2b46d2ac9ee6bb0a8">x</a>);<a name="l00243"></a>00243         m_R.<a class="code" href="struct_e_c_p_point.html#b47b82046f543e809bd90d78bf2c85bf">y</a> = <a class="code" href="class_e_c_p.html#e996fce212244df79b83a587317f7423">GetField</a>().<a class="code" href="class_modular_arithmetic.html#e4705633e8ca4308894f9a26c6f2881c">Subtract</a>(<a class="code" href="class_e_c_p.html#e996fce212244df79b83a587317f7423">GetField</a>().<a class="code" href="class_e_c_p.html#81862b8619979835884f0239012e2ce9">Multiply</a>(t, <a class="code" href="class_e_c_p.html#e996fce212244df79b83a587317f7423">GetField</a>().<a class="code" href="class_abstract_group.html#a19e1bef00198fb30eb01df3f7076717">Subtract</a>(P.x, x)), P.<a class="code" href="struct_e_c_p_point.html#b47b82046f543e809bd90d78bf2c85bf">y</a>);<a name="l00244"></a>00244 <a name="l00245"></a>00245         m_R.<a class="code" href="struct_e_c_p_point.html#24bbcc7eb375ebe2b46d2ac9ee6bb0a8">x</a>.<a class="code" href="class_integer.html#bf1e9c170e56841ce73d137c12675f9b">swap</a>(x);<a name="l00246"></a>00246         m_R.<a class="code" href="struct_e_c_p_point.html#806dedcf077e22de7904ccfd7987bcf9">identity</a> = <span class="keyword">false</span>;<a name="l00247"></a>00247         <span class="keywordflow">return</span> m_R;<a name="l00248"></a>00248 }<a name="l00249"></a>00249 <a name="l00250"></a>00250 <span class="keyword">template</span> &lt;<span class="keyword">class</span> T, <span class="keyword">class</span> Iterator&gt; <span class="keywordtype">void</span> ParallelInvert(<span class="keyword">const</span> <a class="code" href="class_abstract_ring.html" title="Abstract Ring.">AbstractRing&lt;T&gt;</a> &amp;ring, Iterator begin, Iterator end)<a name="l00251"></a>00251 {<a name="l00252"></a>00252         <span class="keywordtype">size_t</span> n = end-begin;<a name="l00253"></a>00253         <span class="keywordflow">if</span> (n == 1)<a name="l00254"></a>00254                 *begin = ring.<a class="code" href="class_abstract_ring.html#ba109f4e7932d77a8251c1ec20627571">MultiplicativeInverse</a>(*begin);<a name="l00255"></a>00255         <span class="keywordflow">else</span> <span class="keywordflow">if</span> (n &gt; 1)<a name="l00256"></a>00256         {<a name="l00257"></a>00257                 std::vector&lt;T&gt; vec((n+1)/2);<a name="l00258"></a>00258                 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i;<a name="l00259"></a>00259                 Iterator it;<a name="l00260"></a>00260 <a name="l00261"></a>00261                 <span class="keywordflow">for</span> (i=0, it=begin; i&lt;n/2; i++, it+=2)<a name="l00262"></a>00262                         vec[i] = ring.<a class="code" href="class_abstract_ring.html#7402d5b81b64771adb9cbed50fbbd158">Multiply</a>(*it, *(it+1));<a name="l00263"></a>00263                 <span class="keywordflow">if</span> (n%2 == 1)<a name="l00264"></a>00264                         vec[n/2] = *it;<a name="l00265"></a>00265 <a name="l00266"></a>00266                 ParallelInvert(ring, vec.begin(), vec.end());<a name="l00267"></a>00267 <a name="l00268"></a>00268                 <span class="keywordflow">for</span> (i=0, it=begin; i&lt;n/2; i++, it+=2)<a name="l00269"></a>00269                 {<a name="l00270"></a>00270                         <span class="keywordflow">if</span> (!vec[i])<a name="l00271"></a>00271                         {<a name="l00272"></a>00272                                 *it = ring.<a class="code" href="class_abstract_ring.html#ba109f4e7932d77a8251c1ec20627571">MultiplicativeInverse</a>(*it);<a name="l00273"></a>00273                                 *(it+1) = ring.<a class="code" href="class_abstract_ring.html#ba109f4e7932d77a8251c1ec20627571">MultiplicativeInverse</a>(*(it+1));<a name="l00274"></a>00274                         }<a name="l00275"></a>00275                         <span class="keywordflow">else</span><a name="l00276"></a>00276                         {<a name="l00277"></a>00277                                 <a class="code" href="gf2n_8h.html#cd9c045f0b5c2a7595a8a0872dc80f59">std::swap</a>(*it, *(it+1));<a name="l00278"></a>00278                                 *it = ring.<a class="code" href="class_abstract_ring.html#7402d5b81b64771adb9cbed50fbbd158">Multiply</a>(*it, vec[i]);<a name="l00279"></a>00279                                 *(it+1) = ring.<a class="code" href="class_abstract_ring.html#7402d5b81b64771adb9cbed50fbbd158">Multiply</a>(*(it+1), vec[i]);<a name="l00280"></a>00280                         }<a name="l00281"></a>00281                 }<a name="l00282"></a>00282                 <span class="keywordflow">if</span> (n%2 == 1)<a name="l00283"></a>00283                         *it = vec[n/2];<a name="l00284"></a>00284         }<a name="l00285"></a>00285 }<a name="l00286"></a>00286 <a name="l00287"></a>00287 <span class="keyword">struct </span>ProjectivePoint<a name="l00288"></a>00288 {<a name="l00289"></a>00289         ProjectivePoint() {}<a name="l00290"></a>00290         ProjectivePoint(<span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &amp;x, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &amp;y, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &amp;z)<a name="l00291"></a>00291                 : x(x), y(y), z(z)      {}<a name="l00292"></a>00292 <a name="l00293"></a>00293         <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> x,y,z;<a name="l00294"></a>00294 };<a name="l00295"></a>00295 <a name="l00296"></a>00296 <span class="keyword">class </span>ProjectiveDoubling<a name="l00297"></a>00297 {<a name="l00298"></a>00298 <span class="keyword">public</span>:<a name="l00299"></a>00299         ProjectiveDoubling(<span class="keyword">const</span> <a class="code" href="class_modular_arithmetic.html" title="ring of congruence classes modulo n">ModularArithmetic</a> &amp;mr, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &amp;m_a, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &amp;m_b, <span class="keyword">const</span> <a class="code" href="struct_e_c_p_point.html" title="Elliptical Curve Point.">ECPPoint</a> &amp;Q)<a name="l00300"></a>00300                 : mr(mr), firstDoubling(true), negated(false)<a name="l00301"></a>00301         {<a name="l00302"></a>00302                 <span class="keywordflow">if</span> (Q.<a class="code" href="struct_e_c_p_point.html#806dedcf077e22de7904ccfd7987bcf9">identity</a>)<a name="l00303"></a>00303                 {<a name="l00304"></a>00304                         sixteenY4 = P.x = P.y = mr.<a class="code" href="class_modular_arithmetic.html#f942a7ca057b363cb7a4c74f15a9530e">MultiplicativeIdentity</a>();<a name="l00305"></a>00305                         aZ4 = P.z = mr.<a class="code" href="class_modular_arithmetic.html#bd2425e1caf5af1a290b424cadb1517c">Identity</a>();<a name="l00306"></a>00306                 }<a name="l00307"></a>00307                 <span class="keywordflow">else</span><a name="l00308"></a>00308                 {<a name="l00309"></a>00309                         P.x = Q.<a class="code" href="struct_e_c_p_point.html#24bbcc7eb375ebe2b46d2ac9ee6bb0a8">x</a>;<a name="l00310"></a>00310                         P.y = Q.<a class="code" href="struct_e_c_p_point.html#b47b82046f543e809bd90d78bf2c85bf">y</a>;<a name="l00311"></a>00311                         sixteenY4 = P.z = mr.<a class="code" href="class_modular_arithmetic.html#f942a7ca057b363cb7a4c74f15a9530e">MultiplicativeIdentity</a>();<a name="l00312"></a>00312                         aZ4 = m_a;<a name="l00313"></a>00313                 }<a name="l00314"></a>00314         }<a name="l00315"></a>00315 <a name="l00316"></a>00316         <span class="keywordtype">void</span> Double()<a name="l00317"></a>00317         {<a name="l00318"></a>00318                 twoY = mr.<a class="code" href="class_modular_arithmetic.html#2704ef13b1d9109569613196de4a4915">Double</a>(P.y);<a name="l00319"></a>00319                 P.z = mr.<a class="code" href="class_modular_arithmetic.html#3b88a85b11eb1a826d26d01bdaafbf0a">Multiply</a>(P.z, twoY);<a name="l00320"></a>00320                 fourY2 = mr.<a class="code" href="class_modular_arithmetic.html#c378a2527fe2107d3379bc35d7cd0487">Square</a>(twoY);<a name="l00321"></a>00321                 S = mr.<a class="code" href="class_modular_arithmetic.html#3b88a85b11eb1a826d26d01bdaafbf0a">Multiply</a>(fourY2, P.x);<a name="l00322"></a>00322                 aZ4 = mr.<a class="code" href="class_modular_arithmetic.html#3b88a85b11eb1a826d26d01bdaafbf0a">Multiply</a>(aZ4, sixteenY4);<a name="l00323"></a>00323                 M = mr.<a class="code" href="class_modular_arithmetic.html#c378a2527fe2107d3379bc35d7cd0487">Square</a>(P.x);<a name="l00324"></a>00324                 M = mr.<a class="code" href="class_modular_arithmetic.html#f840f9421d210579fb9b526a90e857fe">Add</a>(mr.<a class="code" href="class_modular_arithmetic.html#f840f9421d210579fb9b526a90e857fe">Add</a>(mr.<a class="code" href="class_modular_arithmetic.html#2704ef13b1d9109569613196de4a4915">Double</a>(M), M), aZ4);<a name="l00325"></a>00325                 P.x = mr.<a class="code" href="class_modular_arithmetic.html#c378a2527fe2107d3379bc35d7cd0487">Square</a>(M);<a name="l00326"></a>00326                 mr.<a class="code" href="class_modular_arithmetic.html#2810eeb09d7dd4bf862365875f7b4237">Reduce</a>(P.x, S);<a name="l00327"></a>00327                 mr.<a class="code" href="class_modular_arithmetic.html#2810eeb09d7dd4bf862365875f7b4237">Reduce</a>(P.x, S);<a name="l00328"></a>00328                 mr.<a class="code" href="class_modular_arithmetic.html#2810eeb09d7dd4bf862365875f7b4237">Reduce</a>(S, P.x);<a name="l00329"></a>00329                 P.y = mr.<a class="code" href="class_modular_arithmetic.html#3b88a85b11eb1a826d26d01bdaafbf0a">Multiply</a>(M, S);<a name="l00330"></a>00330                 sixteenY4 = mr.<a class="code" href="class_modular_arithmetic.html#c378a2527fe2107d3379bc35d7cd0487">Square</a>(fourY2);<a name="l00331"></a>00331                 mr.<a class="code" href="class_modular_arithmetic.html#2810eeb09d7dd4bf862365875f7b4237">Reduce</a>(P.y, mr.<a class="code" href="class_modular_arithmetic.html#ae65516727b454f9f6d48bed9ebbc167">Half</a>(sixteenY4));<a name="l00332"></a>00332         }<a name="l00333"></a>00333 <a name="l00334"></a>00334         <span class="keyword">const</span> <a class="code" href="class_modular_arithmetic.html" title="ring of congruence classes modulo n">ModularArithmetic</a> &amp;mr;<a name="l00335"></a>00335         ProjectivePoint P;<a name="l00336"></a>00336         <span class="keywordtype">bool</span> firstDoubling, negated;<a name="l00337"></a>00337         <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> sixteenY4, aZ4, twoY, fourY2, S, M;<a name="l00338"></a>00338 };<a name="l00339"></a>00339 <a name="l00340"></a>00340 <span class="keyword">struct </span>ZIterator<a name="l00341"></a>00341 {<a name="l00342"></a>00342         ZIterator() {}<a name="l00343"></a>00343         ZIterator(std::vector&lt;ProjectivePoint&gt;::iterator it) : it(it) {}<a name="l00344"></a>00344         <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a>&amp; <a class="code" href="gf2n_8h.html#44ec4ea79676be4a167d23284f48e563">operator*</a>() {<span class="keywordflow">return</span> it-&gt;z;}<a name="l00345"></a>00345         <span class="keywordtype">int</span> <a class="code" href="gf2n_8h.html#af85a1c53439d93124cf51fdefb0a717">operator-</a>(ZIterator it2) {<span class="keywordflow">return</span> int(it-it2.it);}<a name="l00346"></a>00346         ZIterator <a class="code" href="gf2n_8h.html#f90f6d4d1dec04baadfc546843f8da4c">operator+</a>(<span class="keywordtype">int</span> i) {<span class="keywordflow">return</span> ZIterator(it+i);}<a name="l00347"></a>00347         ZIterator&amp; operator+=(<span class="keywordtype">int</span> i) {it+=i; <span class="keywordflow">return</span> *<span class="keyword">this</span>;}<a name="l00348"></a>00348         std::vector&lt;ProjectivePoint&gt;::iterator it;<a name="l00349"></a>00349 };<a name="l00350"></a>00350 <a name="l00351"></a>00351 <a class="code" href="struct_e_c_p_point.html" title="Elliptical Curve Point.">ECP::Point</a> ECP::ScalarMultiply(<span class="keyword">const</span> <a class="code" href="class_e_c_p.html#99c34a437007f32af4e6c4ae275358ea">Point</a> &amp;P, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &amp;k)<span class="keyword"> const</span><a name="l00352"></a>00352 <span class="keyword"></span>{<a name="l00353"></a>00353         <a class="code" href="class_abstract_group.html#4a2b3308fb5c13f70fcc5746be52ba7b">Element</a> result;<a name="l00354"></a>00354         <span class="keywordflow">if</span> (k.<a class="code" href="class_integer.html#867356d88074424328d0ebb9bea63254" title="number of significant bits = floor(log2(abs(*this))) + 1">BitCount</a>() &lt;= 5)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -