3 <!-- Created by texi2html 1.56k from cln.texi on 2 June 2000 -->
5 <TITLE>CLN, a Class Library for Numbers - 7. Modular integers</TITLE>
8 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>.
12 <H1><A NAME="SEC49" HREF="cln_toc.html#TOC49">7. Modular integers</A></H1>
19 <H2><A NAME="SEC50" HREF="cln_toc.html#TOC50">7.1 Modular integer rings</A></H2>
25 CLN implements modular integers, i.e. integers modulo a fixed integer N.
26 The modulus is explicitly part of every modular integer. CLN doesn't
27 allow you to (accidentally) mix elements of different modular rings,
28 e.g. <CODE>(3 mod 4) + (2 mod 5)</CODE> will result in a runtime error.
29 (Ideally one would imagine a generic data type <CODE>cl_MI(N)</CODE>, but C++
30 doesn't have generic types. So one has to live with runtime checks.)
34 The class of modular integer rings is
46 <cl_modinteger.h>
54 and the class of all modular integers (elements of modular integer rings) is
61 <cl_modinteger.h>
65 Modular integer rings are constructed using the function
70 <DT><CODE>cl_modint_ring cl_find_modint_ring (const cl_I& N)</CODE>
73 This function returns the modular ring <SAMP>`Z/NZ'</SAMP>. It takes care
74 of finding out about special cases of <CODE>N</CODE>, like powers of two
75 and odd numbers for which Montgomery multiplication will be a win,
77 and precomputes any necessary auxiliary data for computing modulo <CODE>N</CODE>.
78 There is a cache table of rings, indexed by <CODE>N</CODE> (or, more precisely,
79 by <CODE>abs(N)</CODE>). This ensures that the precomputation costs are reduced
84 Modular integer rings can be compared for equality:
89 <DT><CODE>bool operator== (const cl_modint_ring&, const cl_modint_ring&)</CODE>
92 <DT><CODE>bool operator!= (const cl_modint_ring&, const cl_modint_ring&)</CODE>
95 These compare two modular integer rings for equality. Two different calls
96 to <CODE>cl_find_modint_ring</CODE> with the same argument necessarily return the
97 same ring because it is memoized in the cache table.
102 <H2><A NAME="SEC51" HREF="cln_toc.html#TOC51">7.2 Functions on modular integers</A></H2>
105 Given a modular integer ring <CODE>R</CODE>, the following members can be used.
110 <DT><CODE>cl_I R->modulus</CODE>
112 <A NAME="IDX248"></A>
113 This is the ring's modulus, normalized to be nonnegative: <CODE>abs(N)</CODE>.
115 <DT><CODE>cl_MI R->zero()</CODE>
117 <A NAME="IDX249"></A>
118 This returns <CODE>0 mod N</CODE>.
120 <DT><CODE>cl_MI R->one()</CODE>
122 <A NAME="IDX250"></A>
123 This returns <CODE>1 mod N</CODE>.
125 <DT><CODE>cl_MI R->canonhom (const cl_I& x)</CODE>
127 <A NAME="IDX251"></A>
128 This returns <CODE>x mod N</CODE>.
130 <DT><CODE>cl_I R->retract (const cl_MI& x)</CODE>
132 <A NAME="IDX252"></A>
133 This is a partial inverse function to <CODE>R->canonhom</CODE>. It returns the
134 standard representative (<CODE>>=0</CODE>, <CODE><N</CODE>) of <CODE>x</CODE>.
136 <DT><CODE>cl_MI R->random(cl_random_state& randomstate)</CODE>
138 <DT><CODE>cl_MI R->random()</CODE>
140 <A NAME="IDX253"></A>
141 This returns a random integer modulo <CODE>N</CODE>.
145 The following operations are defined on modular integers.
150 <DT><CODE>cl_modint_ring x.ring ()</CODE>
152 <A NAME="IDX254"></A>
153 Returns the ring to which the modular integer <CODE>x</CODE> belongs.
155 <DT><CODE>cl_MI operator+ (const cl_MI&, const cl_MI&)</CODE>
157 <A NAME="IDX255"></A>
158 Returns the sum of two modular integers. One of the arguments may also
161 <DT><CODE>cl_MI operator- (const cl_MI&, const cl_MI&)</CODE>
163 <A NAME="IDX256"></A>
164 Returns the difference of two modular integers. One of the arguments may also
167 <DT><CODE>cl_MI operator- (const cl_MI&)</CODE>
169 Returns the negative of a modular integer.
171 <DT><CODE>cl_MI operator* (const cl_MI&, const cl_MI&)</CODE>
173 <A NAME="IDX257"></A>
174 Returns the product of two modular integers. One of the arguments may also
177 <DT><CODE>cl_MI square (const cl_MI&)</CODE>
179 <A NAME="IDX258"></A>
180 Returns the square of a modular integer.
182 <DT><CODE>cl_MI recip (const cl_MI& x)</CODE>
184 <A NAME="IDX259"></A>
185 Returns the reciprocal <CODE>x^-1</CODE> of a modular integer <CODE>x</CODE>. <CODE>x</CODE>
186 must be coprime to the modulus, otherwise an error message is issued.
188 <DT><CODE>cl_MI div (const cl_MI& x, const cl_MI& y)</CODE>
190 <A NAME="IDX260"></A>
191 Returns the quotient <CODE>x*y^-1</CODE> of two modular integers <CODE>x</CODE>, <CODE>y</CODE>.
192 <CODE>y</CODE> must be coprime to the modulus, otherwise an error message is issued.
194 <DT><CODE>cl_MI expt_pos (const cl_MI& x, const cl_I& y)</CODE>
196 <A NAME="IDX261"></A>
197 <CODE>y</CODE> must be > 0. Returns <CODE>x^y</CODE>.
199 <DT><CODE>cl_MI expt (const cl_MI& x, const cl_I& y)</CODE>
201 <A NAME="IDX262"></A>
202 Returns <CODE>x^y</CODE>. If <CODE>y</CODE> is negative, <CODE>x</CODE> must be coprime to the
203 modulus, else an error message is issued.
205 <DT><CODE>cl_MI operator<< (const cl_MI& x, const cl_I& y)</CODE>
207 <A NAME="IDX263"></A>
208 Returns <CODE>x*2^y</CODE>.
210 <DT><CODE>cl_MI operator>> (const cl_MI& x, const cl_I& y)</CODE>
212 <A NAME="IDX264"></A>
213 Returns <CODE>x*2^-y</CODE>. When <CODE>y</CODE> is positive, the modulus must be odd,
214 or an error message is issued.
216 <DT><CODE>bool operator== (const cl_MI&, const cl_MI&)</CODE>
218 <A NAME="IDX265"></A>
219 <DT><CODE>bool operator!= (const cl_MI&, const cl_MI&)</CODE>
221 <A NAME="IDX266"></A>
222 Compares two modular integers, belonging to the same modular integer ring,
225 <DT><CODE>cl_boolean zerop (const cl_MI& x)</CODE>
227 <A NAME="IDX267"></A>
228 Returns true if <CODE>x</CODE> is <CODE>0 mod N</CODE>.
232 The following output functions are defined (see also the chapter on
238 <DT><CODE>void fprint (cl_ostream stream, const cl_MI& x)</CODE>
240 <A NAME="IDX268"></A>
241 <DT><CODE>cl_ostream operator<< (cl_ostream stream, const cl_MI& x)</CODE>
243 <A NAME="IDX269"></A>
244 Prints the modular integer <CODE>x</CODE> on the <CODE>stream</CODE>. The output may depend
245 on the global printer settings in the variable <CODE>cl_default_print_flags</CODE>.
249 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>.