You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

251 lines
7.2 KiB

25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
  1. <HTML>
  2. <HEAD>
  3. <!-- Created by texi2html 1.56k from cln.texi on 2 June 2000 -->
  4. <TITLE>CLN, a Class Library for Numbers - 7. Modular integers</TITLE>
  5. </HEAD>
  6. <BODY>
  7. Go to the <A HREF="cln_1.html">first</A>, <A HREF="cln_6.html">previous</A>, <A HREF="cln_8.html">next</A>, <A HREF="cln_13.html">last</A> section, <A HREF="cln_toc.html">table of contents</A>.
  8. <P><HR><P>
  9. <H1><A NAME="SEC49" HREF="cln_toc.html#TOC49">7. Modular integers</A></H1>
  10. <P>
  11. <A NAME="IDX241"></A>
  12. <H2><A NAME="SEC50" HREF="cln_toc.html#TOC50">7.1 Modular integer rings</A></H2>
  13. <P>
  14. <A NAME="IDX242"></A>
  15. <P>
  16. CLN implements modular integers, i.e. integers modulo a fixed integer N.
  17. The modulus is explicitly part of every modular integer. CLN doesn't
  18. allow you to (accidentally) mix elements of different modular rings,
  19. e.g. <CODE>(3 mod 4) + (2 mod 5)</CODE> will result in a runtime error.
  20. (Ideally one would imagine a generic data type <CODE>cl_MI(N)</CODE>, but C++
  21. doesn't have generic types. So one has to live with runtime checks.)
  22. <P>
  23. The class of modular integer rings is
  24. <PRE>
  25. Ring
  26. cl_ring
  27. &#60;cl_ring.h&#62;
  28. |
  29. |
  30. Modular integer ring
  31. cl_modint_ring
  32. &#60;cl_modinteger.h&#62;
  33. </PRE>
  34. <P>
  35. <A NAME="IDX243"></A>
  36. <P>
  37. and the class of all modular integers (elements of modular integer rings) is
  38. <PRE>
  39. Modular integer
  40. cl_MI
  41. &#60;cl_modinteger.h&#62;
  42. </PRE>
  43. <P>
  44. Modular integer rings are constructed using the function
  45. <DL COMPACT>
  46. <DT><CODE>cl_modint_ring cl_find_modint_ring (const cl_I&#38; N)</CODE>
  47. <DD>
  48. <A NAME="IDX244"></A>
  49. This function returns the modular ring <SAMP>`Z/NZ'</SAMP>. It takes care
  50. of finding out about special cases of <CODE>N</CODE>, like powers of two
  51. and odd numbers for which Montgomery multiplication will be a win,
  52. <A NAME="IDX245"></A>
  53. and precomputes any necessary auxiliary data for computing modulo <CODE>N</CODE>.
  54. There is a cache table of rings, indexed by <CODE>N</CODE> (or, more precisely,
  55. by <CODE>abs(N)</CODE>). This ensures that the precomputation costs are reduced
  56. to a minimum.
  57. </DL>
  58. <P>
  59. Modular integer rings can be compared for equality:
  60. <DL COMPACT>
  61. <DT><CODE>bool operator== (const cl_modint_ring&#38;, const cl_modint_ring&#38;)</CODE>
  62. <DD>
  63. <A NAME="IDX246"></A>
  64. <DT><CODE>bool operator!= (const cl_modint_ring&#38;, const cl_modint_ring&#38;)</CODE>
  65. <DD>
  66. <A NAME="IDX247"></A>
  67. These compare two modular integer rings for equality. Two different calls
  68. to <CODE>cl_find_modint_ring</CODE> with the same argument necessarily return the
  69. same ring because it is memoized in the cache table.
  70. </DL>
  71. <H2><A NAME="SEC51" HREF="cln_toc.html#TOC51">7.2 Functions on modular integers</A></H2>
  72. <P>
  73. Given a modular integer ring <CODE>R</CODE>, the following members can be used.
  74. <DL COMPACT>
  75. <DT><CODE>cl_I R-&#62;modulus</CODE>
  76. <DD>
  77. <A NAME="IDX248"></A>
  78. This is the ring's modulus, normalized to be nonnegative: <CODE>abs(N)</CODE>.
  79. <DT><CODE>cl_MI R-&#62;zero()</CODE>
  80. <DD>
  81. <A NAME="IDX249"></A>
  82. This returns <CODE>0 mod N</CODE>.
  83. <DT><CODE>cl_MI R-&#62;one()</CODE>
  84. <DD>
  85. <A NAME="IDX250"></A>
  86. This returns <CODE>1 mod N</CODE>.
  87. <DT><CODE>cl_MI R-&#62;canonhom (const cl_I&#38; x)</CODE>
  88. <DD>
  89. <A NAME="IDX251"></A>
  90. This returns <CODE>x mod N</CODE>.
  91. <DT><CODE>cl_I R-&#62;retract (const cl_MI&#38; x)</CODE>
  92. <DD>
  93. <A NAME="IDX252"></A>
  94. This is a partial inverse function to <CODE>R-&#62;canonhom</CODE>. It returns the
  95. standard representative (<CODE>&#62;=0</CODE>, <CODE>&#60;N</CODE>) of <CODE>x</CODE>.
  96. <DT><CODE>cl_MI R-&#62;random(cl_random_state&#38; randomstate)</CODE>
  97. <DD>
  98. <DT><CODE>cl_MI R-&#62;random()</CODE>
  99. <DD>
  100. <A NAME="IDX253"></A>
  101. This returns a random integer modulo <CODE>N</CODE>.
  102. </DL>
  103. <P>
  104. The following operations are defined on modular integers.
  105. <DL COMPACT>
  106. <DT><CODE>cl_modint_ring x.ring ()</CODE>
  107. <DD>
  108. <A NAME="IDX254"></A>
  109. Returns the ring to which the modular integer <CODE>x</CODE> belongs.
  110. <DT><CODE>cl_MI operator+ (const cl_MI&#38;, const cl_MI&#38;)</CODE>
  111. <DD>
  112. <A NAME="IDX255"></A>
  113. Returns the sum of two modular integers. One of the arguments may also
  114. be a plain integer.
  115. <DT><CODE>cl_MI operator- (const cl_MI&#38;, const cl_MI&#38;)</CODE>
  116. <DD>
  117. <A NAME="IDX256"></A>
  118. Returns the difference of two modular integers. One of the arguments may also
  119. be a plain integer.
  120. <DT><CODE>cl_MI operator- (const cl_MI&#38;)</CODE>
  121. <DD>
  122. Returns the negative of a modular integer.
  123. <DT><CODE>cl_MI operator* (const cl_MI&#38;, const cl_MI&#38;)</CODE>
  124. <DD>
  125. <A NAME="IDX257"></A>
  126. Returns the product of two modular integers. One of the arguments may also
  127. be a plain integer.
  128. <DT><CODE>cl_MI square (const cl_MI&#38;)</CODE>
  129. <DD>
  130. <A NAME="IDX258"></A>
  131. Returns the square of a modular integer.
  132. <DT><CODE>cl_MI recip (const cl_MI&#38; x)</CODE>
  133. <DD>
  134. <A NAME="IDX259"></A>
  135. Returns the reciprocal <CODE>x^-1</CODE> of a modular integer <CODE>x</CODE>. <CODE>x</CODE>
  136. must be coprime to the modulus, otherwise an error message is issued.
  137. <DT><CODE>cl_MI div (const cl_MI&#38; x, const cl_MI&#38; y)</CODE>
  138. <DD>
  139. <A NAME="IDX260"></A>
  140. Returns the quotient <CODE>x*y^-1</CODE> of two modular integers <CODE>x</CODE>, <CODE>y</CODE>.
  141. <CODE>y</CODE> must be coprime to the modulus, otherwise an error message is issued.
  142. <DT><CODE>cl_MI expt_pos (const cl_MI&#38; x, const cl_I&#38; y)</CODE>
  143. <DD>
  144. <A NAME="IDX261"></A>
  145. <CODE>y</CODE> must be &#62; 0. Returns <CODE>x^y</CODE>.
  146. <DT><CODE>cl_MI expt (const cl_MI&#38; x, const cl_I&#38; y)</CODE>
  147. <DD>
  148. <A NAME="IDX262"></A>
  149. Returns <CODE>x^y</CODE>. If <CODE>y</CODE> is negative, <CODE>x</CODE> must be coprime to the
  150. modulus, else an error message is issued.
  151. <DT><CODE>cl_MI operator&#60;&#60; (const cl_MI&#38; x, const cl_I&#38; y)</CODE>
  152. <DD>
  153. <A NAME="IDX263"></A>
  154. Returns <CODE>x*2^y</CODE>.
  155. <DT><CODE>cl_MI operator&#62;&#62; (const cl_MI&#38; x, const cl_I&#38; y)</CODE>
  156. <DD>
  157. <A NAME="IDX264"></A>
  158. Returns <CODE>x*2^-y</CODE>. When <CODE>y</CODE> is positive, the modulus must be odd,
  159. or an error message is issued.
  160. <DT><CODE>bool operator== (const cl_MI&#38;, const cl_MI&#38;)</CODE>
  161. <DD>
  162. <A NAME="IDX265"></A>
  163. <DT><CODE>bool operator!= (const cl_MI&#38;, const cl_MI&#38;)</CODE>
  164. <DD>
  165. <A NAME="IDX266"></A>
  166. Compares two modular integers, belonging to the same modular integer ring,
  167. for equality.
  168. <DT><CODE>cl_boolean zerop (const cl_MI&#38; x)</CODE>
  169. <DD>
  170. <A NAME="IDX267"></A>
  171. Returns true if <CODE>x</CODE> is <CODE>0 mod N</CODE>.
  172. </DL>
  173. <P>
  174. The following output functions are defined (see also the chapter on
  175. input/output).
  176. <DL COMPACT>
  177. <DT><CODE>void fprint (cl_ostream stream, const cl_MI&#38; x)</CODE>
  178. <DD>
  179. <A NAME="IDX268"></A>
  180. <DT><CODE>cl_ostream operator&#60;&#60; (cl_ostream stream, const cl_MI&#38; x)</CODE>
  181. <DD>
  182. <A NAME="IDX269"></A>
  183. Prints the modular integer <CODE>x</CODE> on the <CODE>stream</CODE>. The output may depend
  184. on the global printer settings in the variable <CODE>cl_default_print_flags</CODE>.
  185. </DL>
  186. <P><HR><P>
  187. Go to the <A HREF="cln_1.html">first</A>, <A HREF="cln_6.html">previous</A>, <A HREF="cln_8.html">next</A>, <A HREF="cln_13.html">last</A> section, <A HREF="cln_toc.html">table of contents</A>.
  188. </BODY>
  189. </HTML>