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

📄 xtr_8cpp-source.html

📁 Crypto++是一个非常强大的密码学库,主要是功能全
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>Crypto++: xtr.cpp Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.3.2 --><div class="qindex"><a class="qindex" href="index.html">Main&nbsp;Page</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Compound&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="namespacemembers.html">Namespace&nbsp;Members</a> | <a class="qindex" href="functions.html">Compound&nbsp;Members</a> | <a class="qindex" href="globals.html">File&nbsp;Members</a></div><h1>xtr.cpp</h1><div class="fragment"><pre>00001 <span class="comment">// cryptlib.cpp - written and placed in the public domain by Wei Dai</span>00002 00003 <span class="preprocessor">#include "pch.h"</span>00004 <span class="preprocessor">#include "<a class="code" href="xtr_8h.html">xtr.h</a>"</span>00005 <span class="preprocessor">#include "nbtheory.h"</span>00006 00007 <span class="preprocessor">#include "algebra.cpp"</span>00008 00009 NAMESPACE_BEGIN(CryptoPP)00010 00011 <a class="code" href="class_g_f_p2_element.html">GFP2Element</a> &amp; GFP2Element::Zero()00012 {00013         <span class="keyword">static</span> <a class="code" href="class_g_f_p2_element.html">GFP2Element</a> zero;00014         <span class="keywordflow">return</span> zero;00015 }00016 00017 <span class="keywordtype">void</span> XTR_FindPrimesAndGenerator(<a class="code" href="class_random_number_generator.html">RandomNumberGenerator</a> &amp;rng, <a class="code" href="class_integer.html">Integer</a> &amp;p, <a class="code" href="class_integer.html">Integer</a> &amp;q, <a class="code" href="class_g_f_p2_element.html">GFP2Element</a> &amp;g, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> pbits, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> qbits)00018 {00019         assert(qbits &gt; 9);      <span class="comment">// no primes exist for pbits = 10, qbits = 9</span>00020         assert(pbits &gt; qbits);00021 00022         <span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> minQ = <a class="code" href="class_integer.html#_integerz37_13">Integer::Power2</a>(qbits - 1);00023         <span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> maxQ = <a class="code" href="class_integer.html#_integerz37_13">Integer::Power2</a>(qbits) - 1;00024         <span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> minP = <a class="code" href="class_integer.html#_integerz37_13">Integer::Power2</a>(pbits - 1);00025         <span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> maxP = <a class="code" href="class_integer.html#_integerz37_13">Integer::Power2</a>(pbits) - 1;00026 00027         <a class="code" href="class_integer.html">Integer</a> r1, r2;00028         <span class="keywordflow">do</span>00029         {00030                 <span class="keywordtype">bool</span> qFound = q.<a class="code" href="class_integer.html#_integerz43_10">Randomize</a>(rng, minQ, maxQ, Integer::PRIME, 7, 12);00031                 assert(qFound);00032                 <span class="keywordtype">bool</span> solutionsExist = SolveModularQuadraticEquation(r1, r2, 1, -1, 1, q);00033                 assert(solutionsExist);00034         } <span class="keywordflow">while</span> (!p.<a class="code" href="class_integer.html#_integerz43_10">Randomize</a>(rng, minP, maxP, Integer::PRIME, CRT(rng.<a class="code" href="class_random_number_generator.html#_x917_r_n_ga2">GenerateBit</a>()?r1:r2, q, 2, 3), 3*q));00035         assert(((p.<a class="code" href="class_integer.html#_integerz49_2">Squared</a>() - p + 1) % q).IsZero());00036 00037         <a class="code" href="class_g_f_p2___o_n_b.html">GFP2_ONB&lt;ModularArithmetic&gt;</a> gfp2(p);00038         <a class="code" href="class_g_f_p2_element.html">GFP2Element</a> three = gfp2.<a class="code" href="class_g_f_p2___o_n_b.html#_g_f_p2___o_n_ba2">ConvertIn</a>(3), t;00039 00040         <span class="keywordflow">while</span> (<span class="keyword">true</span>)00041         {00042                 g.c1.Randomize(rng, Integer::Zero(), p-1);00043                 g.c2.Randomize(rng, Integer::Zero(), p-1);00044                 t = XTR_Exponentiate(g, p+1, p);00045                 <span class="keywordflow">if</span> (t.c1 == t.c2)00046                         <span class="keywordflow">continue</span>;00047                 g = XTR_Exponentiate(g, (p.<a class="code" href="class_integer.html#_integerz49_2">Squared</a>()-p+1)/q, p);00048                 <span class="keywordflow">if</span> (g != three)00049                         <span class="keywordflow">break</span>;00050         }00051         assert(XTR_Exponentiate(g, q, p) == three);00052 }00053 00054 <a class="code" href="class_g_f_p2_element.html">GFP2Element</a> XTR_Exponentiate(<span class="keyword">const</span> <a class="code" href="class_g_f_p2_element.html">GFP2Element</a> &amp;b, <span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> &amp;e, <span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> &amp;p)00055 {00056         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bitCount = e.BitCount();00057         <span class="keywordflow">if</span> (bitCount == 0)00058                 <span class="keywordflow">return</span> <a class="code" href="class_g_f_p2_element.html">GFP2Element</a>(-3, -3);00059 00060         <span class="comment">// find the lowest bit of e that is 1</span>00061         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> lowest1bit;00062         <span class="keywordflow">for</span> (lowest1bit=0; e.GetBit(lowest1bit) == 0; lowest1bit++) {}00063 00064         <a class="code" href="class_g_f_p2___o_n_b.html">GFP2_ONB&lt;MontgomeryRepresentation&gt;</a> gfp2(p);00065         <a class="code" href="class_g_f_p2_element.html">GFP2Element</a> c = gfp2.<a class="code" href="class_g_f_p2___o_n_b.html#_g_f_p2___o_n_ba2">ConvertIn</a>(b);00066         <a class="code" href="class_g_f_p2_element.html">GFP2Element</a> cp = gfp2.<a class="code" href="class_g_f_p2___o_n_b.html#_g_f_p2___o_n_ba19">PthPower</a>(c);00067         <a class="code" href="class_g_f_p2_element.html">GFP2Element</a> S[5] = {gfp2.<a class="code" href="class_g_f_p2___o_n_b.html#_g_f_p2___o_n_ba2">ConvertIn</a>(3), c, gfp2.<a class="code" href="class_g_f_p2___o_n_b.html#_g_f_p2___o_n_ba21">SpecialOperation1</a>(c)};00068 00069         <span class="comment">// do all exponents bits except the lowest zeros starting from the top</span>00070         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i;00071         <span class="keywordflow">for</span> (i = e.BitCount() - 1; i&gt;lowest1bit; i--)00072         {00073                 <span class="keywordflow">if</span> (e.GetBit(i))00074                 {00075                         gfp2.<a class="code" href="class_g_f_p2___o_n_b.html#_g_f_p2___o_n_ba20">RaiseToPthPower</a>(S[0]);00076                         gfp2.<a class="code" href="class_g_f_p2___o_n_b.html#_g_f_p2___o_n_ba11">Accumulate</a>(S[0], gfp2.<a class="code" href="class_g_f_p2___o_n_b.html#_g_f_p2___o_n_ba22">SpecialOperation2</a>(S[2], c, S[1]));00077                         S[1] = gfp2.<a class="code" href="class_g_f_p2___o_n_b.html#_g_f_p2___o_n_ba21">SpecialOperation1</a>(S[1]);00078                         S[2] = gfp2.<a class="code" href="class_g_f_p2___o_n_b.html#_g_f_p2___o_n_ba21">SpecialOperation1</a>(S[2]);00079                         S[0].swap(S[1]);00080                 }00081                 <span class="keywordflow">else</span>00082                 {00083                         gfp2.<a class="code" href="class_g_f_p2___o_n_b.html#_g_f_p2___o_n_ba20">RaiseToPthPower</a>(S[2]);00084                         gfp2.<a class="code" href="class_g_f_p2___o_n_b.html#_g_f_p2___o_n_ba11">Accumulate</a>(S[2], gfp2.<a class="code" href="class_g_f_p2___o_n_b.html#_g_f_p2___o_n_ba22">SpecialOperation2</a>(S[0], cp, S[1]));00085                         S[1] = gfp2.<a class="code" href="class_g_f_p2___o_n_b.html#_g_f_p2___o_n_ba21">SpecialOperation1</a>(S[1]);00086                         S[0] = gfp2.<a class="code" href="class_g_f_p2___o_n_b.html#_g_f_p2___o_n_ba21">SpecialOperation1</a>(S[0]);00087                         S[2].swap(S[1]);00088                 }00089         }00090 00091         <span class="comment">// now do the lowest zeros</span>00092         <span class="keywordflow">while</span> (i--)00093                 S[1] = gfp2.<a class="code" href="class_g_f_p2___o_n_b.html#_g_f_p2___o_n_ba21">SpecialOperation1</a>(S[1]);00094 00095         <span class="keywordflow">return</span> gfp2.<a class="code" href="class_g_f_p2___o_n_b.html#_g_f_p2___o_n_ba4">ConvertOut</a>(S[1]);00096 }00097 00098 <span class="keyword">template</span> <span class="keyword">class </span><a class="code" href="class_abstract_ring.html">AbstractRing&lt;GFP2Element&gt;</a>;00099 <span class="keyword">template</span> <span class="keyword">class </span><a class="code" href="class_abstract_group.html">AbstractGroup&lt;GFP2Element&gt;</a>;00100 00101 NAMESPACE_END</pre></div><hr size="1"><address style="align: right;"><small>Generated on Tue Jul 8 23:34:28 2003 for Crypto++ by<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border=0 > </a>1.3.2 </small></address></body></html>

⌨️ 快捷键说明

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