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.

215 lines
6.5 KiB

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