1 /* mpz_powm_ui(res,base,exp,mod) -- Set RES to (base**exp) mod MOD.
3 Copyright (C) 1991, 1993, 1994, 1996 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Library General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
15 License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 MA 02111-1307, USA. */
28 mpz_powm_ui (mpz_ptr res, mpz_srcptr base, unsigned long int exp, mpz_srcptr mod)
30 mpz_powm_ui (res, base, exp, mod)
33 unsigned long int exp;
38 mp_size_t msize, bsize, rsize;
42 mp_limb_t *free_me = NULL;
46 msize = ABS (mod->_mp_size);
52 msize = 1 / msize; /* provoke a signal */
57 res->_mp_size = (msize == 1 && (mod->_mp_d)[0] == 1) ? 0 : 1;
63 /* Normalize MOD (i.e. make its most significant bit set) as required by
64 mpn_divmod. This will make the intermediate values in the calculation
65 slightly larger, but the correct result is obtained after a final
66 reduction using the original MOD value. */
68 mp = (mp_ptr) TMP_ALLOC (msize * BYTES_PER_MP_LIMB);
69 count_leading_zeros (mod_shift_cnt, mod->_mp_d[msize - 1]);
70 if (mod_shift_cnt != 0)
71 mpn_lshift (mp, mod->_mp_d, msize, mod_shift_cnt);
73 MPN_COPY (mp, mod->_mp_d, msize);
75 bsize = ABS (base->_mp_size);
78 /* The base is larger than the module. Reduce it. */
80 /* Allocate (BSIZE + 1) with space for remainder and quotient.
81 (The quotient is (bsize - msize + 1) limbs.) */
82 bp = (mp_ptr) TMP_ALLOC ((bsize + 1) * BYTES_PER_MP_LIMB);
83 MPN_COPY (bp, base->_mp_d, bsize);
84 /* We don't care about the quotient, store it above the remainder,
86 mpn_divmod (bp + msize, bp, bsize, mp, msize);
88 /* Canonicalize the base, since we are going to multiply with it
90 MPN_NORMALIZE (bp, bsize);
102 if (res->_mp_alloc < size)
104 /* We have to allocate more space for RES. If any of the input
105 parameters are identical to RES, defer deallocation of the old
108 if (rp == mp || rp == bp)
111 free_me_size = res->_mp_alloc;
114 (*_mp_free_func) (rp, res->_mp_alloc * BYTES_PER_MP_LIMB);
116 rp = (mp_ptr) (*_mp_allocate_func) (size * BYTES_PER_MP_LIMB);
117 res->_mp_alloc = size;
122 /* Make BASE, EXP and MOD not overlap with RES. */
125 /* RES and BASE are identical. Allocate temp. space for BASE. */
126 bp = (mp_ptr) TMP_ALLOC (bsize * BYTES_PER_MP_LIMB);
127 MPN_COPY (bp, rp, bsize);
131 /* RES and MOD are identical. Allocate temporary space for MOD. */
132 mp = (mp_ptr) TMP_ALLOC (msize * BYTES_PER_MP_LIMB);
133 MPN_COPY (mp, rp, msize);
137 MPN_COPY (rp, bp, bsize);
141 mp_ptr xp = (mp_ptr) TMP_ALLOC (2 * (msize + 1) * BYTES_PER_MP_LIMB);
144 mp_limb_t carry_limb;
146 negative_result = (exp & 1) && base->_mp_size < 0;
149 count_leading_zeros (c, e);
150 e = (e << c) << 1; /* shift the exp bits to the left, lose msb */
151 c = BITS_PER_MP_LIMB - 1 - c;
155 Make the result be pointed to alternately by XP and RP. This
156 helps us avoid block copying, which would otherwise be necessary
157 with the overlap restrictions of mpn_divmod. With 50% probability
158 the result after this loop will be in the area originally pointed
159 by RP (==RES->_mp_d), and with 50% probability in the area originally
167 mpn_mul_n (xp, rp, rp, rsize);
171 mpn_divmod (xp + msize, xp, xsize, mp, msize);
175 tp = rp; rp = xp; xp = tp;
178 if ((mp_limb_signed_t) e < 0)
180 mpn_mul (xp, rp, rsize, bp, bsize);
181 xsize = rsize + bsize;
184 mpn_divmod (xp + msize, xp, xsize, mp, msize);
188 tp = rp; rp = xp; xp = tp;
195 /* We shifted MOD, the modulo reduction argument, left MOD_SHIFT_CNT
196 steps. Adjust the result by reducing it with the original MOD.
198 Also make sure the result is put in RES->_mp_d (where it already
199 might be, see above). */
201 if (mod_shift_cnt != 0)
203 carry_limb = mpn_lshift (res->_mp_d, rp, rsize, mod_shift_cnt);
207 rp[rsize] = carry_limb;
213 MPN_COPY (res->_mp_d, rp, rsize);
219 mpn_divmod (rp + msize, rp, rsize, mp, msize);
223 /* Remove any leading zero words from the result. */
224 if (mod_shift_cnt != 0)
225 mpn_rshift (rp, rp, rsize, mod_shift_cnt);
226 MPN_NORMALIZE (rp, rsize);
229 res->_mp_size = negative_result == 0 ? rsize : -rsize;
232 (*_mp_free_func) (free_me, free_me_size * BYTES_PER_MP_LIMB);