1 /* mpz_powm(res,base,exp,mod) -- Set RES to (base**exp) mod MOD.
3 Copyright (C) 1991, 1992 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 General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 The GNU MP Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with the GNU MP Library; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
28 mpz_powm (MP_INT *res, const MP_INT *base, const MP_INT *exp,
31 mpz_powm (res, base, exp, mod)
37 #else /* BERKELEY_MP */
40 pow (const MP_INT *base, const MP_INT *exp, const MP_INT *mod, MP_INT *res)
42 pow (base, exp, mod, res)
48 #endif /* BERKELEY_MP */
50 mp_ptr rp, ep, mp, bp;
51 mp_size esize, msize, bsize, rsize;
55 mp_limb *free_me = NULL;
58 esize = ABS (exp->size);
59 msize = ABS (mod->size);
65 /* Normalize MOD (i.e. make its most significant bit set) as required by
66 mpn_div. This will make the intermediate values in the calculation
67 slightly larger, but the correct result is obtained after a final
68 reduction using the original MOD value. */
70 mp = (mp_ptr) alloca (msize * BYTES_PER_MP_LIMB);
71 count_leading_zeros (mod_shift_cnt, mod->d[msize - 1]);
72 if (mod_shift_cnt != 0)
73 (void) mpn_lshift (mp, mod->d, msize, mod_shift_cnt);
75 MPN_COPY (mp, mod->d, msize);
77 bsize = ABS (base->size);
80 /* The base is larger than the module. Reduce it. */
82 /* Allocate (BSIZE + 1) with space for remainder and quotient.
83 (The quotient is (bsize - msize + 1) limbs.) */
84 bp = (mp_ptr) alloca ((bsize + 1) * BYTES_PER_MP_LIMB);
85 MPN_COPY (bp, base->d, bsize);
86 /* We don't care about the quotient, store it above the remainder,
88 mpn_div (bp + msize, bp, bsize, mp, msize);
90 while (bsize > 0 && bp[bsize - 1] == 0)
96 bsize = ABS (base->size);
99 if (res->alloc < size)
101 /* We have to allocate more space for RES. If any of the input
102 parameters are identical to RES, defer deallocation of the old
105 if (rp == ep || rp == mp || rp == bp)
108 free_me_size = res->alloc;
111 (*_mp_free_func) (rp, res->alloc * BYTES_PER_MP_LIMB);
113 rp = (mp_ptr) (*_mp_allocate_func) (size * BYTES_PER_MP_LIMB);
119 /* Make BASE, EXP and MOD not overlap with RES. */
122 /* RES and BASE are identical. Allocate temp. space for BASE. */
123 bp = (mp_ptr) alloca (bsize * BYTES_PER_MP_LIMB);
124 MPN_COPY (bp, rp, bsize);
128 /* RES and EXP are identical. Allocate temp. space for EXP. */
129 ep = (mp_ptr) alloca (esize * BYTES_PER_MP_LIMB);
130 MPN_COPY (ep, rp, esize);
134 /* RES and MOD are identical. Allocate temporary space for MOD. */
135 mp = (mp_ptr) alloca (msize * BYTES_PER_MP_LIMB);
136 MPN_COPY (mp, rp, msize);
147 MPN_COPY (rp, bp, bsize);
153 mp_ptr dummyp = (mp_ptr) alloca ((msize + 1) * BYTES_PER_MP_LIMB);
154 mp_ptr xp = (mp_ptr) alloca (2 * (msize + 1) * BYTES_PER_MP_LIMB);
159 negative_result = (ep[0] & 1) && base->size < 0;
163 count_leading_zeros (c, e);
164 e <<= (c + 1); /* shift the exp bits to the left, loose msb */
165 c = BITS_PER_MP_LIMB - 1 - c;
169 Make the result be pointed to alternatingly by XP and RP. This
170 helps us avoid block copying, which would otherwise be necessary
171 with the overlap restrictions of mpn_div. With 50% probability
172 the result after this loop will be in the area originally pointed
173 by RP (==RES->D), and with 50% probability in the area originally
183 xsize = mpn_mul (xp, rp, rsize, rp, rsize);
184 mpn_div (dummyp, xp, xsize, mp, msize);
186 /* Remove any leading zero words from the result. */
189 while (xsize > 0 && xp[xsize - 1] == 0)
192 tp = rp; rp = xp; xp = tp;
193 tsize = rsize; rsize = xsize; xsize = tsize;
195 if ((mp_limb_signed) e < 0)
198 xsize = mpn_mul (xp, rp, rsize, bp, bsize);
200 xsize = mpn_mul (xp, bp, bsize, rp, rsize);
201 mpn_div (dummyp, xp, xsize, mp, msize);
203 /* Remove any leading zero words from the result. */
206 while (xsize > 0 && xp[xsize - 1] == 0)
209 tp = rp; rp = xp; xp = tp;
210 tsize = rsize; rsize = xsize; xsize = tsize;
220 c = BITS_PER_MP_LIMB;
223 /* We shifted MOD, the modulo reduction argument, left MOD_SHIFT_CNT
224 steps. Adjust the result by reducing it with the original MOD.
226 Also make sure the result is put in RES->D (where it already
227 might be, see above). */
229 carry_limb = mpn_lshift (res->d, rp, rsize, mod_shift_cnt);
233 rp[rsize] = carry_limb;
236 mpn_div (dummyp, rp, rsize, mp, msize);
237 /* Remove any leading zero words from the result. */
240 while (rsize > 0 && rp[rsize - 1] == 0)
242 rsize = mpn_rshift (rp, rp, rsize, mod_shift_cnt);
245 res->size = negative_result >= 0 ? rsize : -rsize;
248 (*_mp_free_func) (free_me, free_me_size * BYTES_PER_MP_LIMB);