1 /* LibTomMath, multiple-precision integer library -- Tom St Denis
3 * LibTomMath is library that provides for multiple-precision
4 * integer arithmetic as well as number theoretic functionality.
6 * The library is designed directly after the MPI library by
7 * Michael Fromberger but has been written from scratch with
8 * additional optimizations in place.
10 * The library is free for all purposes without any express
13 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
17 /* computes xR**-1 == x (mod N) via Montgomery Reduction
19 * This is an optimized implementation of mp_montgomery_reduce
20 * which uses the comba method to quickly calculate the columns of the
23 * Based on Algorithm 14.32 on pp.601 of HAC.
26 fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
31 /* get old used count */
34 /* grow a as required */
35 if (x->alloc < n->used + 1) {
36 if ((res = mp_grow (x, n->used + 1)) != MP_OKAY) {
43 register mp_digit *tmpx;
48 /* copy the digits of a into W[0..a->used-1] */
49 for (ix = 0; ix < x->used; ix++) {
53 /* zero the high words of W[a->used..m->used*2] */
54 for (; ix < n->used * 2 + 1; ix++) {
59 for (ix = 0; ix < n->used; ix++) {
62 * We avoid a double precision multiplication (which isn't required)
63 * by casting the value down to a mp_digit. Note this requires
64 * that W[ix-1] have the carry cleared (see after the inner loop)
67 mu = (((mp_digit) (W[ix] & MP_MASK)) * rho) & MP_MASK;
69 /* a = a + mu * m * b**i
71 * This is computed in place and on the fly. The multiplication
72 * by b**i is handled by offseting which columns the results
75 * Note the comba method normally doesn't handle carries in the
76 * inner loop In this case we fix the carry from the previous
77 * column since the Montgomery reduction requires digits of the
78 * result (so far) [see above] to work. This is
79 * handled by fixing up one carry after the inner loop. The
80 * carry fixups are done in order so after these loops the
81 * first m->used words of W[] have the carries fixed
85 register mp_digit *tmpn;
88 /* alias for the digits of the modulus */
91 /* Alias for the columns set by an offset of ix */
95 for (iy = 0; iy < n->used; iy++) {
96 *_W++ += ((mp_word) mu) * ((mp_word) * tmpn++);
100 /* now fix carry for next digit, W[ix+1] */
101 W[ix + 1] += W[ix] >> ((mp_word) DIGIT_BIT);
106 register mp_digit *tmpx;
107 register mp_word *_W, *_W1;
109 /* nox fix rest of carries */
113 for (; ix <= n->used * 2 + 1; ix++) {
114 *_W++ += *_W1++ >> ((mp_word) DIGIT_BIT);
117 /* copy out, A = A/b**n
119 * The result is A/b**n but instead of converting from an
120 * array of mp_word to mp_digit than calling mp_rshd
121 * we just copy them in the right order
126 for (ix = 0; ix < n->used + 1; ix++) {
127 *tmpx++ = (mp_digit)(*_W++ & ((mp_word) MP_MASK));
130 /* zero oldused digits, if the input a was larger than
131 * m->used+1 we'll have to clear the digits */
132 for (; ix < olduse; ix++) {
137 /* set the max used and clamp */
138 x->used = n->used + 1;
141 /* if A >= m then A = A - m */
142 if (mp_cmp_mag (x, n) != MP_LT) {
143 return s_mp_sub (x, n, x);