- freebn(temp);
- freebn(tmp2);
-
- debug(result);
- leave(("<modmult\n"));
-}
-
-static void modpow(Bignum r1, Bignum r2, Bignum modulus, Bignum result) {
- Bignum temp = newbn(modulus[0]+1);
- Bignum tmp2 = newbn(modulus[0]+1);
- int i;
- int bit, bits, digit, smallbit;
-
- enter((">modpow\n"));
- debug(r1);
- debug(r2);
- debug(modulus);
-
- for (i=1; i<=result[0]; i++)
- result[i] = (i==1); /* result := 1 */
- for (i=1; i<=temp[0]; i++)
- temp[i] = (i > r1[0] ? 0 : r1[i]); /* temp := r1 */
-
- bits = 1+msb(r2);
-
- for (bit = 0; bit < bits; bit++) {
- digit = 1 + bit / 16;
- smallbit = bit % 16;
-
- debug(temp);
- if (digit <= r2[0] && (r2[digit] & (1<<smallbit))) {
- dmsg(("bit %d\n", bit));
- modmult(temp, result, modulus, tmp2);
- add(tmp2, Zero, result);
- debug(result);
- }
-
- modmult(temp, temp, modulus, tmp2);
- add(tmp2, Zero, temp);
+ /* Fixup result in case the modulus was shifted */
+ if (mshift) {
+ for (i = mlen - 1; i < 2*mlen - 1; i++)
+ a[i] = (a[i] << mshift) | (a[i+1] >> (16-mshift));
+ a[2*mlen-1] = a[2*mlen-1] << mshift;
+ bigmod(a, m, mlen);
+ for (i = 2*mlen - 1; i >= mlen; i--)
+ a[i] = (a[i] >> mshift) | (a[i-1] << (16-mshift));