1 /* mpn_powlo -- Compute R = U^E mod B^n, where B is the limb base.
3 Copyright 2007, 2008, 2009 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 Lesser General Public License as published by
9 the Free Software Foundation; either version 3 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 Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
26 #define getbit(p,bi) \
27 ((p[(bi - 1) / GMP_LIMB_BITS] >> (bi - 1) % GMP_LIMB_BITS) & 1)
29 static inline mp_limb_t
30 getbits (const mp_limb_t *p, mp_bitcnt_t bi, int nbits)
38 return p[0] & (((mp_limb_t) 1 << bi) - 1);
42 bi -= nbits; /* bit index of low bit to extract */
43 i = bi / GMP_NUMB_BITS; /* word index of low bit to extract */
44 bi %= GMP_NUMB_BITS; /* bit index in low word */
45 r = p[i] >> bi; /* extract (low) bits */
46 nbits_in_r = GMP_NUMB_BITS - bi; /* number of bits now in r */
47 if (nbits_in_r < nbits) /* did we get enough bits? */
48 r += p[i + 1] << nbits_in_r; /* prepend bits from higher word */
49 return r & (((mp_limb_t ) 1 << nbits) - 1);
54 win_size (mp_bitcnt_t eb)
57 static mp_bitcnt_t x[] = {1,7,25,81,241,673,1793,4609,11521,28161,~(mp_bitcnt_t)0};
58 for (k = 0; eb > x[k]; k++)
63 /* rp[n-1..0] = bp[n-1..0] ^ ep[en-1..0] mod B^n, B is the limb base.
64 Requires that ep[en-1] is non-zero.
65 Uses scratch space tp[3n-1..0], i.e., 3n words. */
67 mpn_powlo (mp_ptr rp, mp_srcptr bp,
68 mp_srcptr ep, mp_size_t en,
69 mp_size_t n, mp_ptr tp)
73 int windowsize, this_windowsize;
75 mp_limb_t *pp, *this_pp, *last_pp;
80 ASSERT (en > 1 || (en == 1 && ep[0] > 1));
84 count_leading_zeros (cnt, ep[en - 1]);
85 ebi = (mp_bitcnt_t) en * GMP_LIMB_BITS - cnt;
87 windowsize = win_size (ebi);
89 pp = TMP_ALLOC_LIMBS ((n << (windowsize - 1)) + n); /* + n is for mullo ign part */
93 MPN_COPY (this_pp, bp, n);
97 /* Store b^2 in b2. */
98 mpn_sqr (tp, bp, n); /* FIXME: Use "mpn_sqrlo" */
99 MPN_COPY (b2p, tp, n);
101 /* Precompute odd powers of b and put them in the temporary area at pp. */
102 for (i = (1 << (windowsize - 1)) - 1; i > 0; i--)
106 mpn_mullo_n (this_pp, last_pp, b2p, n);
109 expbits = getbits (ep, ebi, windowsize);
110 if (ebi < windowsize)
115 count_trailing_zeros (cnt, expbits);
119 MPN_COPY (rp, pp + n * (expbits >> 1), n);
123 while (getbit (ep, ebi) == 0)
125 mpn_sqr (tp, rp, n); /* FIXME: Use "mpn_sqrlo" */
126 MPN_COPY (rp, tp, n);
132 /* The next bit of the exponent is 1. Now extract the largest block of
133 bits <= windowsize, and such that the least significant bit is 1. */
135 expbits = getbits (ep, ebi, windowsize);
136 this_windowsize = windowsize;
137 if (ebi < windowsize)
139 this_windowsize -= windowsize - ebi;
145 count_trailing_zeros (cnt, expbits);
146 this_windowsize -= cnt;
153 MPN_COPY (rp, tp, n);
156 while (this_windowsize != 0);
158 mpn_mullo_n (tp, rp, pp + n * (expbits >> 1), n);
159 MPN_COPY (rp, tp, n);