📄 nbtheory_8h-source.html
字号:
<a name="l00062"></a>00062 <span class="comment">// ********** other number theoretic functions ************</span><a name="l00063"></a>00063 <a name="l00064"></a>00064 <span class="keyword">inline</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> GCD(<span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &a, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &b)<a name="l00065"></a>00065 {<span class="keywordflow">return</span> <a class="code" href="class_integer.html#2d4d29937f8ef666717530b30f137c37" title="greatest common divisor">Integer::Gcd</a>(a,b);}<a name="l00066"></a>00066 <span class="keyword">inline</span> <span class="keywordtype">bool</span> RelativelyPrime(<span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &a, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &b)<a name="l00067"></a>00067 {<span class="keywordflow">return</span> <a class="code" href="class_integer.html#2d4d29937f8ef666717530b30f137c37" title="greatest common divisor">Integer::Gcd</a>(a,b) == <a class="code" href="class_integer.html#8c070592581bf6c2f928c72bfa1c1638" title="avoid calling constructors for these frequently used integers">Integer::One</a>();}<a name="l00068"></a>00068 <span class="keyword">inline</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> LCM(<span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &a, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &b)<a name="l00069"></a>00069 {<span class="keywordflow">return</span> a/<a class="code" href="class_integer.html#2d4d29937f8ef666717530b30f137c37" title="greatest common divisor">Integer::Gcd</a>(a,b)*b;}<a name="l00070"></a>00070 <span class="keyword">inline</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> EuclideanMultiplicativeInverse(<span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &a, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &b)<a name="l00071"></a>00071 {<span class="keywordflow">return</span> a.<a class="code" href="class_integer.html#881f9c714ee42f35718725a43d4d7db3" title="calculate multiplicative inverse of *this mod n">InverseMod</a>(b);}<a name="l00072"></a>00072 <a name="l00073"></a>00073 <span class="comment">// use Chinese Remainder Theorem to calculate x given x mod p and x mod q</span><a name="l00074"></a>00074 CRYPTOPP_DLL <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> CRYPTOPP_API CRT(<span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &xp, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &p, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &xq, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &q);<a name="l00075"></a>00075 <span class="comment">// use this one if u = inverse of p mod q has been precalculated</span><a name="l00076"></a>00076 CRYPTOPP_DLL <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> CRYPTOPP_API CRT(<span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &xp, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &p, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &xq, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &q, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &u);<a name="l00077"></a>00077 <a name="l00078"></a>00078 <span class="comment">// if b is prime, then Jacobi(a, b) returns 0 if a%b==0, 1 if a is quadratic residue mod b, -1 otherwise</span><a name="l00079"></a>00079 <span class="comment">// check a number theory book for what Jacobi symbol means when b is not prime</span><a name="l00080"></a>00080 CRYPTOPP_DLL <span class="keywordtype">int</span> CRYPTOPP_API Jacobi(<span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &a, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &b);<a name="l00081"></a>00081 <a name="l00082"></a>00082 <span class="comment">// calculates the Lucas function V_e(p, 1) mod n</span><a name="l00083"></a>00083 CRYPTOPP_DLL <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> CRYPTOPP_API Lucas(<span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &e, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &p, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &n);<a name="l00084"></a>00084 <span class="comment">// calculates x such that m==Lucas(e, x, p*q), p q primes</span><a name="l00085"></a>00085 CRYPTOPP_DLL <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> CRYPTOPP_API InverseLucas(<span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &e, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &m, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &p, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &q);<a name="l00086"></a>00086 <span class="comment">// use this one if u=inverse of p mod q has been precalculated</span><a name="l00087"></a>00087 CRYPTOPP_DLL <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> CRYPTOPP_API InverseLucas(<span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &e, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &m, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &p, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &q, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &u);<a name="l00088"></a>00088 <a name="l00089"></a>00089 <span class="keyword">inline</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> ModularExponentiation(<span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &a, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &e, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &m)<a name="l00090"></a>00090 {<span class="keywordflow">return</span> a_exp_b_mod_c(a, e, m);}<a name="l00091"></a>00091 <span class="comment">// returns x such that x*x%p == a, p prime</span><a name="l00092"></a>00092 CRYPTOPP_DLL <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> CRYPTOPP_API ModularSquareRoot(<span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &a, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &p);<a name="l00093"></a>00093 <span class="comment">// returns x such that a==ModularExponentiation(x, e, p*q), p q primes,</span><a name="l00094"></a>00094 <span class="comment">// and e relatively prime to (p-1)*(q-1)</span><a name="l00095"></a>00095 CRYPTOPP_DLL <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> CRYPTOPP_API ModularRoot(<span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &a, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &e, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &p, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &q);<a name="l00096"></a>00096 <span class="comment">// use this one if dp=d%(p-1), dq=d%(q-1), (d is inverse of e mod (p-1)*(q-1))</span><a name="l00097"></a>00097 <span class="comment">// and u=inverse of p mod q have been precalculated</span><a name="l00098"></a>00098 CRYPTOPP_DLL <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> CRYPTOPP_API ModularRoot(<span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &a, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &dp, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &dq, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &p, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &q, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &u);<a name="l00099"></a>00099 <a name="l00100"></a>00100 <span class="comment">// find r1 and r2 such that ax^2 + bx + c == 0 (mod p) for x in {r1, r2}, p prime</span><a name="l00101"></a>00101 <span class="comment">// returns true if solutions exist</span><a name="l00102"></a>00102 CRYPTOPP_DLL <span class="keywordtype">bool</span> CRYPTOPP_API SolveModularQuadraticEquation(<a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &r1, <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &r2, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &a, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &b, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &c, <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &p);<a name="l00103"></a>00103 <a name="l00104"></a>00104 <span class="comment">// returns log base 2 of estimated number of operations to calculate discrete log or factor a number</span><a name="l00105"></a>00105 CRYPTOPP_DLL <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> CRYPTOPP_API DiscreteLogWorkFactor(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bitlength);<a name="l00106"></a>00106 CRYPTOPP_DLL <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> CRYPTOPP_API FactoringWorkFactor(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bitlength);<a name="l00107"></a>00107 <a name="l00108"></a>00108 <span class="comment">// ********************************************************</span><a name="l00109"></a>00109 <span class="comment"></span><a name="l00110"></a>00110 <span class="comment">//! generator of prime numbers of special forms</span><a name="l00111"></a><a class="code" href="class_prime_and_generator.html">00111</a> <span class="comment"></span><span class="keyword">class </span>CRYPTOPP_DLL PrimeAndGenerator<a name="l00112"></a>00112 {<a name="l00113"></a>00113 <span class="keyword">public</span>:<a name="l00114"></a><a class="code" href="class_prime_and_generator.html#45b2743c9edd5e67bb4f5241d3fdd890">00114</a> PrimeAndGenerator() {}<a name="l00115"></a>00115 <span class="comment">// generate a random prime p of the form 2*q+delta, where delta is 1 or -1 and q is also prime</span><a name="l00116"></a>00116 <span class="comment">// Precondition: pbits > 5</span><a name="l00117"></a>00117 <span class="comment">// warning: this is slow, because primes of this form are harder to find</span><a name="l00118"></a><a class="code" href="class_prime_and_generator.html#35fcc8d77d72793bb4afc386b454dfc1">00118</a> PrimeAndGenerator(<span class="keywordtype">signed</span> <span class="keywordtype">int</span> delta, <a class="code" href="class_random_number_generator.html" title="interface for random number generators">RandomNumberGenerator</a> &rng, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> pbits)<a name="l00119"></a>00119 {Generate(delta, rng, pbits, pbits-1);}<a name="l00120"></a>00120 <span class="comment">// generate a random prime p of the form 2*r*q+delta, where q is also prime</span><a name="l00121"></a>00121 <span class="comment">// Precondition: qbits > 4 && pbits > qbits</span><a name="l00122"></a><a class="code" href="class_prime_and_generator.html#f014ff08d285a45c5deafc4f6a9c2abe">00122</a> PrimeAndGenerator(<span class="keywordtype">signed</span> <span class="keywordtype">int</span> delta, <a class="code" href="class_random_number_generator.html" title="interface for random number generators">RandomNumberGenerator</a> &rng, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> pbits, <span class="keywordtype">unsigned</span> qbits)<a name="l00123"></a>00123 {Generate(delta, rng, pbits, qbits);}<a name="l00124"></a>00124 <a name="l00125"></a>00125 <span class="keywordtype">void</span> Generate(<span class="keywordtype">signed</span> <span class="keywordtype">int</span> delta, <a class="code" href="class_random_number_generator.html" title="interface for random number generators">RandomNumberGenerator</a> &rng, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> pbits, <span class="keywordtype">unsigned</span> qbits);<a name="l00126"></a>00126 <a name="l00127"></a><a class="code" href="class_prime_and_generator.html#cb878f8b71f1260b6458c50ad87d592a">00127</a> <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a>& Prime()<span class="keyword"> const </span>{<span class="keywordflow">return</span> p;}<a name="l00128"></a><a class="code" href="class_prime_and_generator.html#e2d1a2b6be1c325680c7cc6d1da399a3">00128</a> <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a>& SubPrime()<span class="keyword"> const </span>{<span class="keywordflow">return</span> q;}<a name="l00129"></a><a class="code" href="class_prime_and_generator.html#00e94acffa91d09a711616c60ce2327e">00129</a> <span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a>& Generator()<span class="keyword"> const </span>{<span class="keywordflow">return</span> g;}<a name="l00130"></a>00130 <a name="l00131"></a>00131 <span class="keyword">private</span>:<a name="l00132"></a>00132 <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> p, q, g;<a name="l00133"></a>00133 };<a name="l00134"></a>00134 <a name="l00135"></a>00135 NAMESPACE_END<a name="l00136"></a>00136 <a name="l00137"></a>00137 <span class="preprocessor">#endif</span></pre></div><hr size="1"><address style="text-align: right;"><small>Generated on Fri Jun 1 11:11:22 2007 for Crypto++ by <a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.2 </small></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -