# BRCM_VERSION=3
[bcm963xx.git] / userapps / opensource / sshd / libtommath / bn_fast_mp_montgomery_reduce.c
1 /* LibTomMath, multiple-precision integer library -- Tom St Denis
2  *
3  * LibTomMath is library that provides for multiple-precision
4  * integer arithmetic as well as number theoretic functionality.
5  *
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.
9  *
10  * The library is free for all purposes without any express
11  * guarantee it works.
12  *
13  * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
14  */
15 #include <tommath.h>
16
17 /* computes xR**-1 == x (mod N) via Montgomery Reduction 
18  * 
19  * This is an optimized implementation of mp_montgomery_reduce 
20  * which uses the comba method to quickly calculate the columns of the
21  * reduction.  
22  *
23  * Based on Algorithm 14.32 on pp.601 of HAC.
24 */
25 int
26 fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
27 {
28   int     ix, res, olduse;
29   mp_word W[MP_WARRAY];
30
31   /* get old used count */
32   olduse = x->used;
33
34   /* grow a as required */
35   if (x->alloc < n->used + 1) {
36     if ((res = mp_grow (x, n->used + 1)) != MP_OKAY) {
37       return res;
38     }
39   }
40
41   {
42     register mp_word *_W;
43     register mp_digit *tmpx;
44
45     _W = W;
46     tmpx = x->dp;
47
48     /* copy the digits of a into W[0..a->used-1] */
49     for (ix = 0; ix < x->used; ix++) {
50       *_W++ = *tmpx++;
51     }
52
53     /* zero the high words of W[a->used..m->used*2] */
54     for (; ix < n->used * 2 + 1; ix++) {
55       *_W++ = 0;
56     }
57   }
58
59   for (ix = 0; ix < n->used; ix++) {
60     /* mu = ai * m' mod b
61      *
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)
65      */
66     register mp_digit mu;
67     mu = (((mp_digit) (W[ix] & MP_MASK)) * rho) & MP_MASK;
68
69     /* a = a + mu * m * b**i
70      *
71      * This is computed in place and on the fly.  The multiplication
72      * by b**i is handled by offseting which columns the results
73      * are added to.
74      *
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
82      */
83     {
84       register int iy;
85       register mp_digit *tmpn;
86       register mp_word *_W;
87
88       /* alias for the digits of the modulus */
89       tmpn = n->dp;
90
91       /* Alias for the columns set by an offset of ix */
92       _W = W + ix;
93
94       /* inner loop */
95       for (iy = 0; iy < n->used; iy++) {
96           *_W++ += ((mp_word) mu) * ((mp_word) * tmpn++);
97       }
98     }
99
100     /* now fix carry for next digit, W[ix+1] */
101     W[ix + 1] += W[ix] >> ((mp_word) DIGIT_BIT);
102   }
103
104
105   {
106     register mp_digit *tmpx;
107     register mp_word *_W, *_W1;
108
109     /* nox fix rest of carries */
110     _W1 = W + ix;
111     _W = W + ++ix;
112
113     for (; ix <= n->used * 2 + 1; ix++) {
114       *_W++ += *_W1++ >> ((mp_word) DIGIT_BIT);
115     }
116
117     /* copy out, A = A/b**n
118      *
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
122      */
123     tmpx = x->dp;
124     _W = W + n->used;
125
126     for (ix = 0; ix < n->used + 1; ix++) {
127       *tmpx++ = (mp_digit)(*_W++ & ((mp_word) MP_MASK));
128     }
129
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++) {
133       *tmpx++ = 0;
134     }
135   }
136
137   /* set the max used and clamp */
138   x->used = n->used + 1;
139   mp_clamp (x);
140
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);
144   }
145   return MP_OKAY;
146 }