1 /* mpn_powlo -- Compute R = U^E mod R^n, where R is the limb base. 2 3 Copyright 2007, 2008, 2009 Free Software Foundation, Inc. 4 5 This file is part of the GNU MP Library. 6 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. 11 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. 16 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/. */ 19 20 21 #include "gmp.h" 22 #include "gmp-impl.h" 23 #include "longlong.h" 24 25 26 #define getbit(p,bi) \ 27 ((p[(bi - 1) / GMP_LIMB_BITS] >> (bi - 1) % GMP_LIMB_BITS) & 1) 28 29 static inline mp_limb_t 30 getbits (const mp_limb_t *p, unsigned long bi, int nbits) 31 { 32 int nbits_in_r; 33 mp_limb_t r; 34 mp_size_t i; 35 36 if (bi < nbits) 37 { 38 return p[0] & (((mp_limb_t) 1 << bi) - 1); 39 } 40 else 41 { 42 bi -= nbits; /* bit index of low bit to extract */ 43 i = bi / GMP_LIMB_BITS; /* word index of low bit to extract */ 44 bi %= GMP_LIMB_BITS; /* bit index in low word */ 45 r = p[i] >> bi; /* extract (low) bits */ 46 nbits_in_r = GMP_LIMB_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); 50 } 51 } 52 53 static inline int 54 win_size (unsigned long eb) 55 { 56 int k; 57 static unsigned long x[] = {1,7,25,81,241,673,1793,4609,11521,28161,~0ul}; 58 for (k = 0; eb > x[k]; k++) 59 ; 60 return k; 61 } 62 63 /* rp[n-1..0] = bp[n-1..0] ^ ep[en-1..0] mod R^n, R 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. */ 66 void 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) 70 { 71 int cnt; 72 long ebi; 73 int windowsize, this_windowsize; 74 mp_limb_t expbits; 75 mp_limb_t *pp, *this_pp, *last_pp; 76 mp_limb_t *b2p; 77 long i; 78 TMP_DECL; 79 80 ASSERT (en > 1 || (en == 1 && ep[0] > 1)); 81 82 TMP_MARK; 83 84 count_leading_zeros (cnt, ep[en - 1]); 85 ebi = en * GMP_LIMB_BITS - cnt; 86 87 windowsize = win_size (ebi); 88 89 pp = TMP_ALLOC_LIMBS ((n << (windowsize - 1)) + n); /* + n is for mullow ign part */ 90 91 this_pp = pp; 92 93 MPN_COPY (this_pp, bp, n); 94 95 b2p = tp + 2*n; 96 97 /* Store b^2 in b2. */ 98 mpn_sqr_n (tp, bp, n); /* FIXME: Use "mpn_sqrlo" */ 99 MPN_COPY (b2p, tp, n); 100 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--) 103 { 104 last_pp = this_pp; 105 this_pp += n; 106 mpn_mullow_n (this_pp, last_pp, b2p, n); 107 } 108 109 expbits = getbits (ep, ebi, windowsize); 110 ebi -= windowsize; 111 if (ebi < 0) 112 ebi = 0; 113 114 count_trailing_zeros (cnt, expbits); 115 ebi += cnt; 116 expbits >>= cnt; 117 118 MPN_COPY (rp, pp + n * (expbits >> 1), n); 119 120 while (ebi != 0) 121 { 122 while (getbit (ep, ebi) == 0) 123 { 124 mpn_sqr_n (tp, rp, n); /* FIXME: Use "mpn_sqrlo" */ 125 MPN_COPY (rp, tp, n); 126 ebi--; 127 if (ebi == 0) 128 goto done; 129 } 130 131 /* The next bit of the exponent is 1. Now extract the largest block of 132 bits <= windowsize, and such that the least significant bit is 1. */ 133 134 expbits = getbits (ep, ebi, windowsize); 135 ebi -= windowsize; 136 this_windowsize = windowsize; 137 if (ebi < 0) 138 { 139 this_windowsize += ebi; 140 ebi = 0; 141 } 142 143 count_trailing_zeros (cnt, expbits); 144 this_windowsize -= cnt; 145 ebi += cnt; 146 expbits >>= cnt; 147 148 do 149 { 150 mpn_sqr_n (tp, rp, n); 151 MPN_COPY (rp, tp, n); 152 this_windowsize--; 153 } 154 while (this_windowsize != 0); 155 156 mpn_mullow_n (tp, rp, pp + n * (expbits >> 1), n); 157 MPN_COPY (rp, tp, n); 158 } 159 160 done: 161 TMP_FREE; 162 } 163