Sophie

Sophie

distrib > Mandriva > 10.2 > i586 > media > contrib > by-pkgid > 7457b841ac8136d3a1a9d3d960c5252e > files > 1319

libcryptopp-doc-5.2.1-2mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Crypto++: modarith.h Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.3.7 -->
<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">Class&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">Class&nbsp;Members</a> | <a class="qindex" href="globals.html">File&nbsp;Members</a></div>
<h1>modarith.h</h1><pre class="fragment"><div>00001 <span class="preprocessor">#ifndef CRYPTOPP_MODARITH_H</span>
00002 <span class="preprocessor"></span><span class="preprocessor">#define CRYPTOPP_MODARITH_H</span>
00003 <span class="preprocessor"></span>
00004 <span class="comment">// implementations are in integer.cpp</span>
00005 
00006 <span class="preprocessor">#include "<a class="code" href="cryptlib_8h.html">cryptlib.h</a>"</span>
00007 <span class="preprocessor">#include "misc.h"</span>
00008 <span class="preprocessor">#include "<a class="code" href="integer_8h.html">integer.h</a>"</span>
00009 <span class="preprocessor">#include "algebra.h"</span>
00010 
00011 NAMESPACE_BEGIN(CryptoPP)
00012 
00013 CRYPTOPP_DLL_TEMPLATE_CLASS <a class="code" href="class_abstract_group.html">AbstractGroup</a>&lt;<a class="code" href="class_integer.html">Integer</a>&gt;;
00014 CRYPTOPP_DLL_TEMPLATE_CLASS <a class="code" href="class_abstract_ring.html">AbstractRing</a>&lt;Integer&gt;;
00015 CRYPTOPP_DLL_TEMPLATE_CLASS <a class="code" href="class_abstract_euclidean_domain.html">AbstractEuclideanDomain</a>&lt;Integer&gt;;
00016 <span class="comment"></span>
00017 <span class="comment">//! ring of congruence classes modulo n</span>
00018 <span class="comment"></span><span class="comment">/*! \note this implementation represents each congruence class as the smallest non-negative integer in that class */</span>
<a name="l00019"></a><a class="code" href="class_modular_arithmetic.html">00019</a> class CRYPTOPP_DLL <a class="code" href="class_modular_arithmetic.html">ModularArithmetic</a> : public <a class="code" href="class_abstract_ring.html">AbstractRing</a>&lt;Integer&gt;
00020 {
00021 <span class="keyword">public</span>:
00022 
00023         <span class="keyword">typedef</span> <span class="keywordtype">int</span> RandomizationParameter;
00024         <span class="keyword">typedef</span> Integer <a class="code" href="class_integer.html">Element</a>;
00025 
00026         <a class="code" href="class_modular_arithmetic.html">ModularArithmetic</a>(<span class="keyword">const</span> Integer &amp;modulus = Integer::One())
00027                 : modulus(modulus), result((word)0, modulus.reg.size()) {}
00028 
00029         <a class="code" href="class_modular_arithmetic.html">ModularArithmetic</a>(<span class="keyword">const</span> <a class="code" href="class_modular_arithmetic.html">ModularArithmetic</a> &amp;ma)
00030                 : modulus(ma.<a class="code" href="class_modular_arithmetic.html#_montgomery_representationp0">modulus</a>), result((word)0, modulus.reg.size()) {}
00031 
00032         <a class="code" href="class_modular_arithmetic.html">ModularArithmetic</a>(<a class="code" href="class_buffered_transformation.html">BufferedTransformation</a> &amp;bt);  <span class="comment">// construct from BER encoded parameters</span>
00033 
00034         <span class="keyword">virtual</span> <a class="code" href="class_modular_arithmetic.html">ModularArithmetic</a> * Clone()<span class="keyword"> const </span>{<span class="keywordflow">return</span> <span class="keyword">new</span> <a class="code" href="class_modular_arithmetic.html">ModularArithmetic</a>(*<span class="keyword">this</span>);}
00035 
00036         <span class="keywordtype">void</span> DEREncode(<a class="code" href="class_buffered_transformation.html">BufferedTransformation</a> &amp;bt) <span class="keyword">const</span>;
00037 
00038         <span class="keywordtype">void</span> DEREncodeElement(<a class="code" href="class_buffered_transformation.html">BufferedTransformation</a> &amp;out, <span class="keyword">const</span> Element &amp;a) <span class="keyword">const</span>;
00039         <span class="keywordtype">void</span> BERDecodeElement(<a class="code" href="class_buffered_transformation.html">BufferedTransformation</a> &amp;in, Element &amp;a) <span class="keyword">const</span>;
00040 
00041         <span class="keyword">const</span> Integer&amp; GetModulus()<span class="keyword"> const </span>{<span class="keywordflow">return</span> modulus;}
00042         <span class="keywordtype">void</span> SetModulus(<span class="keyword">const</span> Integer &amp;newModulus) {modulus = newModulus; result.<a class="code" href="class_integer.html#_integerr0">reg</a>.<a class="code" href="class_sec_block.html#_sec_block_with_hinta25">resize</a>(modulus.reg.size());}
00043 
00044         <span class="keyword">virtual</span> <span class="keywordtype">bool</span> IsMontgomeryRepresentation()<span class="keyword"> const </span>{<span class="keywordflow">return</span> <span class="keyword">false</span>;}
00045 
00046         <span class="keyword">virtual</span> Integer ConvertIn(<span class="keyword">const</span> Integer &amp;a)<span class="keyword"> const</span>
00047 <span class="keyword">                </span>{<span class="keywordflow">return</span> a%modulus;}
00048 
00049         <span class="keyword">virtual</span> Integer ConvertOut(<span class="keyword">const</span> Integer &amp;a)<span class="keyword"> const</span>
00050 <span class="keyword">                </span>{<span class="keywordflow">return</span> a;}
00051 
00052         <span class="keyword">const</span> Integer&amp; Half(<span class="keyword">const</span> Integer &amp;a) <span class="keyword">const</span>;
00053 
00054         <span class="keywordtype">bool</span> Equal(<span class="keyword">const</span> Integer &amp;a, <span class="keyword">const</span> Integer &amp;b)<span class="keyword"> const</span>
00055 <span class="keyword">                </span>{<span class="keywordflow">return</span> a==b;}
00056 
00057         <span class="keyword">const</span> Integer&amp; Identity()<span class="keyword"> const</span>
00058 <span class="keyword">                </span>{<span class="keywordflow">return</span> <a class="code" href="class_integer.html#_integerz37_12">Integer::Zero</a>();}
00059 
00060         <span class="keyword">const</span> Integer&amp; Add(<span class="keyword">const</span> Integer &amp;a, <span class="keyword">const</span> Integer &amp;b) <span class="keyword">const</span>;
00061 
00062         Integer&amp; Accumulate(Integer &amp;a, <span class="keyword">const</span> Integer &amp;b) <span class="keyword">const</span>;
00063 
00064         <span class="keyword">const</span> Integer&amp; Inverse(<span class="keyword">const</span> Integer &amp;a) <span class="keyword">const</span>;
00065 
00066         <span class="keyword">const</span> Integer&amp; Subtract(<span class="keyword">const</span> Integer &amp;a, <span class="keyword">const</span> Integer &amp;b) <span class="keyword">const</span>;
00067 
00068         Integer&amp; Reduce(Integer &amp;a, <span class="keyword">const</span> Integer &amp;b) <span class="keyword">const</span>;
00069 
00070         <span class="keyword">const</span> Integer&amp; Double(<span class="keyword">const</span> Integer &amp;a)<span class="keyword"> const</span>
00071 <span class="keyword">                </span>{<span class="keywordflow">return</span> Add(a, a);}
00072 
00073         <span class="keyword">const</span> Integer&amp; MultiplicativeIdentity()<span class="keyword"> const</span>
00074 <span class="keyword">                </span>{<span class="keywordflow">return</span> <a class="code" href="class_integer.html#_integerz37_13">Integer::One</a>();}
00075 
00076         <span class="keyword">const</span> Integer&amp; Multiply(<span class="keyword">const</span> Integer &amp;a, <span class="keyword">const</span> Integer &amp;b)<span class="keyword"> const</span>
00077 <span class="keyword">                </span>{<span class="keywordflow">return</span> result1 = a*b%modulus;}
00078 
00079         <span class="keyword">const</span> Integer&amp; <a class="code" href="class_square.html">Square</a>(<span class="keyword">const</span> Integer &amp;a)<span class="keyword"> const</span>
00080 <span class="keyword">                </span>{<span class="keywordflow">return</span> result1 = a.Squared()%modulus;}
00081 
00082         <span class="keywordtype">bool</span> IsUnit(<span class="keyword">const</span> Integer &amp;a)<span class="keyword"> const</span>
00083 <span class="keyword">                </span>{<span class="keywordflow">return</span> Integer::Gcd(a, modulus).<a class="code" href="class_integer.html#_integerz49_5">IsUnit</a>();}
00084 
00085         <span class="keyword">const</span> Integer&amp; MultiplicativeInverse(<span class="keyword">const</span> Integer &amp;a)<span class="keyword"> const</span>
00086 <span class="keyword">                </span>{<span class="keywordflow">return</span> result1 = a.InverseMod(modulus);}
00087 
00088         <span class="keyword">const</span> Integer&amp; Divide(<span class="keyword">const</span> Integer &amp;a, <span class="keyword">const</span> Integer &amp;b)<span class="keyword"> const</span>
00089 <span class="keyword">                </span>{<span class="keywordflow">return</span> Multiply(a, MultiplicativeInverse(b));}
00090 
00091         Integer CascadeExponentiate(<span class="keyword">const</span> Integer &amp;x, <span class="keyword">const</span> Integer &amp;e1, <span class="keyword">const</span> Integer &amp;y, <span class="keyword">const</span> Integer &amp;e2) <span class="keyword">const</span>;
00092 
00093         <span class="keywordtype">void</span> SimultaneousExponentiate(Element *results, <span class="keyword">const</span> Element &amp;base, <span class="keyword">const</span> Integer *exponents, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> exponentsCount) <span class="keyword">const</span>;
00094 
00095         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> MaxElementBitLength()<span class="keyword"> const</span>
00096 <span class="keyword">                </span>{<span class="keywordflow">return</span> (modulus-1).BitCount();}
00097 
00098         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> MaxElementByteLength()<span class="keyword"> const</span>
00099 <span class="keyword">                </span>{<span class="keywordflow">return</span> (modulus-1).ByteCount();}
00100 
00101         Element RandomElement( <a class="code" href="class_random_number_generator.html">RandomNumberGenerator</a> &amp;rng , <span class="keyword">const</span> RandomizationParameter &amp;ignore_for_now = 0 ) <span class="keyword">const</span>
00102                 <span class="comment">// left RandomizationParameter arg as ref in case RandomizationParameter becomes a more complicated struct</span>
00103         { 
00104                 <span class="keywordflow">return</span> Element( rng , Integer( (<span class="keywordtype">long</span>) 0) , modulus - Integer( (<span class="keywordtype">long</span>) 1 )   ) ; 
00105         }   
00106 
00107         <span class="keywordtype">bool</span> operator==(<span class="keyword">const</span> <a class="code" href="class_modular_arithmetic.html">ModularArithmetic</a> &amp;rhs)<span class="keyword"> const</span>
00108 <span class="keyword">                </span>{<span class="keywordflow">return</span> modulus == rhs.<a class="code" href="class_modular_arithmetic.html#_montgomery_representationp0">modulus</a>;}
00109 
00110         <span class="keyword">static</span> <span class="keyword">const</span> RandomizationParameter DefaultRandomizationParameter ;
00111 
00112 <span class="keyword">protected</span>:
00113         Integer modulus;
00114         <span class="keyword">mutable</span> Integer result, result1;
00115 
00116 };
00117 
00118 <span class="comment">// const ModularArithmetic::RandomizationParameter ModularArithmetic::DefaultRandomizationParameter = 0 ;</span>
00119 <span class="comment"></span>
00120 <span class="comment">//! do modular arithmetics in Montgomery representation for increased speed</span>
00121 <span class="comment"></span><span class="comment">/*! \note the Montgomery representation represents each congruence class [a] as a*r%n, where r is a convenient power of 2 */</span>
<a name="l00122"></a><a class="code" href="class_montgomery_representation.html">00122</a> <span class="keyword">class </span>CRYPTOPP_DLL MontgomeryRepresentation : <span class="keyword">public</span> <a class="code" href="class_modular_arithmetic.html">ModularArithmetic</a>
00123 {
00124 <span class="keyword">public</span>:
00125         MontgomeryRepresentation(<span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> &amp;modulus);       <span class="comment">// modulus must be odd</span>
00126 
00127         <span class="keyword">virtual</span> <a class="code" href="class_modular_arithmetic.html">ModularArithmetic</a> * Clone()<span class="keyword"> const </span>{<span class="keywordflow">return</span> <span class="keyword">new</span> MontgomeryRepresentation(*<span class="keyword">this</span>);}
00128 
00129         <span class="keywordtype">bool</span> IsMontgomeryRepresentation()<span class="keyword"> const </span>{<span class="keywordflow">return</span> <span class="keyword">true</span>;}
00130 
00131         <a class="code" href="class_integer.html">Integer</a> ConvertIn(<span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> &amp;a)<span class="keyword"> const</span>
00132 <span class="keyword">                </span>{<span class="keywordflow">return</span> (a&lt;&lt;(WORD_BITS*modulus.<a class="code" href="class_integer.html#_integerr0">reg</a>.<a class="code" href="class_sec_block.html#_sec_block_with_hinta15">size</a>()))%modulus;}
00133 
00134         <a class="code" href="class_integer.html">Integer</a> ConvertOut(<span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> &amp;a) <span class="keyword">const</span>;
00135 
00136         <span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a>&amp; MultiplicativeIdentity()<span class="keyword"> const</span>
00137 <span class="keyword">                </span>{<span class="keywordflow">return</span> result1 = Integer::Power2(WORD_BITS*modulus.<a class="code" href="class_integer.html#_integerr0">reg</a>.<a class="code" href="class_sec_block.html#_sec_block_with_hinta15">size</a>())%modulus;}
00138 
00139         <span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a>&amp; Multiply(<span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> &amp;a, <span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> &amp;b) <span class="keyword">const</span>;
00140 
00141         <span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a>&amp; <a class="code" href="class_square.html">Square</a>(<span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> &amp;a) <span class="keyword">const</span>;
00142 
00143         <span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a>&amp; MultiplicativeInverse(<span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> &amp;a) <span class="keyword">const</span>;
00144 
00145         <a class="code" href="class_integer.html">Integer</a> CascadeExponentiate(<span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> &amp;x, <span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> &amp;e1, <span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> &amp;y, <span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> &amp;e2)<span class="keyword"> const</span>
00146 <span class="keyword">                </span>{<span class="keywordflow">return</span> <a class="code" href="class_abstract_ring.html">AbstractRing&lt;Integer&gt;::CascadeExponentiate</a>(x, e1, y, e2);}
00147 
00148         <span class="keywordtype">void</span> SimultaneousExponentiate(Element *results, <span class="keyword">const</span> Element &amp;base, <span class="keyword">const</span> <a class="code" href="class_integer.html">Integer</a> *exponents, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> exponentsCount)<span class="keyword"> const</span>
00149 <span class="keyword">                </span>{<a class="code" href="class_abstract_ring.html">AbstractRing&lt;Integer&gt;::SimultaneousExponentiate</a>(results, base, exponents, exponentsCount);}
00150 
00151 <span class="keyword">private</span>:
00152         <a class="code" href="class_integer.html">Integer</a> u;
00153         <span class="keyword">mutable</span> <a class="code" href="class_sec_block.html">SecAlignedWordBlock</a> workspace;
00154 };
00155 
00156 NAMESPACE_END
00157 
00158 <span class="preprocessor">#endif</span>
</div></pre><hr size="1"><address style="align: right;"><small>Generated on Sun Nov 7 08:23:58 2004 for Crypto++ by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0 ></a> 1.3.7 </small></address>
</body>
</html>