1 /* TomsFastMath, a fast ISO C bignum library.
3 * This project is meant to fill in where LibTomMath
4 * falls short. That is speed ;-)
6 * This project is public domain and free for all purposes.
8 * Tom St Denis, tomstdenis@gmail.com
10 #include <tfm_private.h>
12 /* computes a = B**n mod b without division or multiplication useful for
13 * normalizing numbers in a Montgomery system.
15 void fp_montgomery_calc_normalization(fp_int *a, fp_int *b)
19 /* how many bits of last digit does b use */
20 bits = fp_count_bits (b) % DIGIT_BIT;
21 if (!bits) bits = DIGIT_BIT;
23 /* compute A = B^(n-1) * 2^(bits-1) */
25 fp_2expt (a, (b->used - 1) * DIGIT_BIT + bits - 1);
31 /* now compute C = A * B mod b */
32 for (x = bits - 1; x < (int)DIGIT_BIT; x++) {
34 if (fp_cmp_mag (a, b) != FP_LT) {