📄 rabin_8cpp-source.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>Crypto++: rabin.cpp Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"><link href="tabs.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.5.2 --><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="classes.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>rabin.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// rabin.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 <span class="preprocessor">#include "<a class="code" href="rabin_8h.html">rabin.h</a>"</span><a name="l00005"></a>00005 <span class="preprocessor">#include "nbtheory.h"</span><a name="l00006"></a>00006 <span class="preprocessor">#include "asn.h"</span><a name="l00007"></a>00007 <span class="preprocessor">#include "sha.h"</span><a name="l00008"></a>00008 <span class="preprocessor">#include "modarith.h"</span><a name="l00009"></a>00009 <a name="l00010"></a>00010 NAMESPACE_BEGIN(CryptoPP)<a name="l00011"></a>00011 <a name="l00012"></a><a class="code" href="class_rabin_function.html#b41bb9ae4282a5b0962067769b15e5a5">00012</a> void <a class="code" href="class_rabin_function.html" title="_">RabinFunction</a>::BERDecode(<a class="code" href="class_buffered_transformation.html" title="interface for buffered transformations">BufferedTransformation</a> &bt)<a name="l00013"></a>00013 {<a name="l00014"></a>00014 <a class="code" href="class_b_e_r_sequence_decoder.html" title="BER Sequence Decoder.">BERSequenceDecoder</a> seq(bt);<a name="l00015"></a>00015 m_n.BERDecode(seq);<a name="l00016"></a>00016 m_r.BERDecode(seq);<a name="l00017"></a>00017 m_s.BERDecode(seq);<a name="l00018"></a>00018 seq.<a class="code" href="class_b_e_r_general_decoder.html#40b14625c98ac7febb8ca218d02358ad">MessageEnd</a>();<a name="l00019"></a>00019 }<a name="l00020"></a>00020 <a name="l00021"></a><a class="code" href="class_rabin_function.html#dad07259c4a50f912bf7be6ce2572d49">00021</a> <span class="keywordtype">void</span> <a class="code" href="class_rabin_function.html#dad07259c4a50f912bf7be6ce2572d49">RabinFunction::DEREncode</a>(<a class="code" href="class_buffered_transformation.html" title="interface for buffered transformations">BufferedTransformation</a> &bt)<span class="keyword"> const</span><a name="l00022"></a>00022 <span class="keyword"></span>{<a name="l00023"></a>00023 <a class="code" href="class_d_e_r_sequence_encoder.html" title="DER Sequence Encoder.">DERSequenceEncoder</a> seq(bt);<a name="l00024"></a>00024 <a class="code" href="class_rabin_function.html#f361185b5962701c78c410945f6280bc">m_n</a>.<a class="code" href="class_integer.html#6ab51a05bee88cfa690179611e8a084e" title="encode using Distinguished Encoding Rules, put result into a BufferedTransformation...">DEREncode</a>(seq);<a name="l00025"></a>00025 <a class="code" href="class_rabin_function.html#c98a931385ae02de294a7a7da66fddfd">m_r</a>.<a class="code" href="class_integer.html#6ab51a05bee88cfa690179611e8a084e" title="encode using Distinguished Encoding Rules, put result into a BufferedTransformation...">DEREncode</a>(seq);<a name="l00026"></a>00026 <a class="code" href="class_rabin_function.html#601264d1c703941468aaaa7c1918dec2">m_s</a>.<a class="code" href="class_integer.html#6ab51a05bee88cfa690179611e8a084e" title="encode using Distinguished Encoding Rules, put result into a BufferedTransformation...">DEREncode</a>(seq);<a name="l00027"></a>00027 seq.<a class="code" href="class_d_e_r_general_encoder.html#cb5f1a94ba97027c603b019bd5228510">MessageEnd</a>();<a name="l00028"></a>00028 }<a name="l00029"></a>00029 <a name="l00030"></a><a class="code" href="class_rabin_function.html#d186fb4ee71efe6dc7257b89395ba172">00030</a> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> <a class="code" href="class_rabin_function.html#d186fb4ee71efe6dc7257b89395ba172">RabinFunction::ApplyFunction</a>(<span class="keyword">const</span> <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> &in)<span class="keyword"> const</span><a name="l00031"></a>00031 <span class="keyword"></span>{<a name="l00032"></a>00032 <a class="code" href="class_crypto_material.html#a1da44802c5cf00946043922cb9e7e6e">DoQuickSanityCheck</a>();<a name="l00033"></a>00033 <a name="l00034"></a>00034 <a class="code" href="class_integer.html" title="multiple precision integer and basic arithmetics">Integer</a> out = in.<a class="code" href="class_integer.html#7b5e639045868c5ac338f4180e1c7efa">Squared</a>()%<a class="code" href="class_rabin_function.html#f361185b5962701c78c410945f6280bc">m_n</a>;<a name="l00035"></a>00035 <span class="keywordflow">if</span> (in.<a class="code" href="class_integer.html#ed4bb7208a18b986ef3e1a7d92e06d1d">IsOdd</a>())<a name="l00036"></a>00036 out = out*<a class="code" href="class_rabin_function.html#c98a931385ae02de294a7a7da66fddfd">m_r</a>%<a class="code" href="class_rabin_function.html#f361185b5962701c78c410945f6280bc">m_n</a>;<a name="l00037"></a>00037 <span class="keywordflow">if</span> (Jacobi(in, <a class="code" href="class_rabin_function.html#f361185b5962701c78c410945f6280bc">m_n</a>)==-1)<a name="l00038"></a>00038 out = out*<a class="code" href="class_rabin_function.html#601264d1c703941468aaaa7c1918dec2">m_s</a>%<a class="code" href="class_rabin_function.html#f361185b5962701c78c410945f6280bc">m_n</a>;<a name="l00039"></a>00039 <span class="keywordflow">return</span> out;<a name="l00040"></a>00040 }<a name="l00041"></a>00041 <a name="l00042"></a><a class="code" href="class_rabin_function.html#c1f4bf45382c16a486616269884d95be">00042</a> <span class="keywordtype">bool</span> <a class="code" href="class_rabin_function.html#c1f4bf45382c16a486616269884d95be" title="check this object for errors">RabinFunction::Validate</a>(<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> level)<span class="keyword"> const</span><a name="l00043"></a>00043 <span class="keyword"></span>{<a name="l00044"></a>00044 <span class="keywordtype">bool</span> pass = <span class="keyword">true</span>;<a name="l00045"></a>00045 pass = pass && <a class="code" href="class_rabin_function.html#f361185b5962701c78c410945f6280bc">m_n</a> > <a class="code" href="class_integer.html#8c070592581bf6c2f928c72bfa1c1638" title="avoid calling constructors for these frequently used integers">Integer::One</a>() && <a class="code" href="class_rabin_function.html#f361185b5962701c78c410945f6280bc">m_n</a>%4 == 1;<a name="l00046"></a>00046 pass = pass && <a class="code" href="class_rabin_function.html#c98a931385ae02de294a7a7da66fddfd">m_r</a> > <a class="code" href="class_integer.html#8c070592581bf6c2f928c72bfa1c1638" title="avoid calling constructors for these frequently used integers">Integer::One</a>() && <a class="code" href="class_rabin_function.html#c98a931385ae02de294a7a7da66fddfd">m_r</a> < <a class="code" href="class_rabin_function.html#f361185b5962701c78c410945f6280bc">m_n</a>;<a name="l00047"></a>00047 pass = pass && <a class="code" href="class_rabin_function.html#601264d1c703941468aaaa7c1918dec2">m_s</a> > <a class="code" href="class_integer.html#8c070592581bf6c2f928c72bfa1c1638" title="avoid calling constructors for these frequently used integers">Integer::One</a>() && <a class="code" href="class_rabin_function.html#601264d1c703941468aaaa7c1918dec2">m_s</a> < m_n;<a name="l00048"></a>00048 <span class="keywordflow">if</span> (level >= 1)<a name="l00049"></a>00049 pass = pass && Jacobi(<a class="code" href="class_rabin_function.html#c98a931385ae02de294a7a7da66fddfd">m_r</a>, m_n) == -1 && Jacobi(<a class="code" href="class_rabin_function.html#601264d1c703941468aaaa7c1918dec2">m_s</a>, m_n) == -1;<a name="l00050"></a>00050 <span class="keywordflow">return</span> pass;<a name="l00051"></a>00051 }<a name="l00052"></a>00052 <a name="l00053"></a><a class="code" href="class_rabin_function.html#cce821b84d2ba2aa7a63984146a95dcb">00053</a> <span class="keywordtype">bool</span> <a class="code" href="class_rabin_function.html#cce821b84d2ba2aa7a63984146a95dcb" title="to be implemented by derived classes, users should use one of the above functions...">RabinFunction::GetVoidValue</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> *name, <span class="keyword">const</span> std::type_info &valueType, <span class="keywordtype">void</span> *pValue)<span class="keyword"> const</span><a name="l00054"></a>00054 <span class="keyword"></span>{<a name="l00055"></a>00055 <span class="keywordflow">return</span> GetValueHelper(<span class="keyword">this</span>, name, valueType, pValue).Assignable()<a name="l00056"></a>00056 CRYPTOPP_GET_FUNCTION_ENTRY(Modulus)<a name="l00057"></a>00057 CRYPTOPP_GET_FUNCTION_ENTRY(QuadraticResidueModPrime1)<a name="l00058"></a>00058 CRYPTOPP_GET_FUNCTION_ENTRY(QuadraticResidueModPrime2)<a name="l00059"></a>00059 ;<a name="l00060"></a>00060 }<a name="l00061"></a>00061 <a name="l00062"></a><a class="code" href="class_rabin_function.html#e585a0d58b1b95579459c2b42eea980c">00062</a> <span class="keywordtype">void</span> <a class="code" href="class_rabin_function.html#e585a0d58b1b95579459c2b42eea980c" title="assign values from source to this object">RabinFunction::AssignFrom</a>(<span class="keyword">const</span> <a class="code" href="class_name_value_pairs.html" title="interface for retrieving values given their names">NameValuePairs</a> &source)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -