2003-02-28 11:08:34 -05:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
|
|
|
*
|
|
|
|
* LibTomMath is library that provides for multiple-precision
|
|
|
|
* integer arithmetic as well as number theoretic functionality.
|
|
|
|
*
|
|
|
|
* The library is designed directly after the MPI library by
|
|
|
|
* Michael Fromberger but has been written from scratch with
|
|
|
|
* additional optimizations in place.
|
|
|
|
*
|
|
|
|
* The library is free for all purposes without any express
|
|
|
|
* guarantee it works.
|
|
|
|
*
|
2003-03-12 21:11:11 -05:00
|
|
|
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
2003-02-28 11:08:34 -05:00
|
|
|
*/
|
|
|
|
#include <tommath.h>
|
|
|
|
|
2003-05-29 09:35:26 -04:00
|
|
|
/* computes xR**-1 == x (mod N) via Montgomery Reduction */
|
2003-02-28 11:08:34 -05:00
|
|
|
int
|
2003-05-29 09:35:26 -04:00
|
|
|
mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
|
2003-02-28 11:08:34 -05:00
|
|
|
{
|
2003-02-28 11:09:08 -05:00
|
|
|
int ix, res, digs;
|
2003-05-29 09:35:26 -04:00
|
|
|
mp_digit mu;
|
2003-02-28 11:08:34 -05:00
|
|
|
|
2003-05-17 08:33:54 -04:00
|
|
|
/* can the fast reduction [comba] method be used?
|
|
|
|
*
|
|
|
|
* Note that unlike in mp_mul you're safely allowed *less*
|
|
|
|
* than the available columns [255 per default] since carries
|
|
|
|
* are fixed up in the inner loop.
|
|
|
|
*/
|
2003-05-29 09:35:26 -04:00
|
|
|
digs = n->used * 2 + 1;
|
|
|
|
if ((digs < MP_WARRAY) &&
|
|
|
|
n->used <
|
|
|
|
(1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
|
|
|
|
return fast_mp_montgomery_reduce (x, n, rho);
|
2003-02-28 11:08:34 -05:00
|
|
|
}
|
|
|
|
|
2003-05-17 08:33:54 -04:00
|
|
|
/* grow the input as required */
|
2003-05-29 09:35:26 -04:00
|
|
|
if (x->alloc < digs) {
|
|
|
|
if ((res = mp_grow (x, digs)) != MP_OKAY) {
|
2003-02-28 11:08:34 -05:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2003-05-29 09:35:26 -04:00
|
|
|
x->used = digs;
|
2003-02-28 11:08:34 -05:00
|
|
|
|
2003-05-29 09:35:26 -04:00
|
|
|
for (ix = 0; ix < n->used; ix++) {
|
|
|
|
/* mu = ai * m' mod b */
|
|
|
|
mu = (x->dp[ix] * rho) & MP_MASK;
|
2003-02-28 11:08:34 -05:00
|
|
|
|
2003-05-29 09:35:26 -04:00
|
|
|
/* a = a + mu * m * b**i */
|
2003-02-28 11:08:34 -05:00
|
|
|
{
|
|
|
|
register int iy;
|
2003-05-29 09:35:26 -04:00
|
|
|
register mp_digit *tmpn, *tmpx, u;
|
2003-02-28 11:08:34 -05:00
|
|
|
register mp_word r;
|
|
|
|
|
|
|
|
/* aliases */
|
2003-05-29 09:35:26 -04:00
|
|
|
tmpn = n->dp;
|
|
|
|
tmpx = x->dp + ix;
|
2003-02-28 11:08:34 -05:00
|
|
|
|
2003-05-29 09:35:26 -04:00
|
|
|
/* set the carry to zero */
|
|
|
|
u = 0;
|
|
|
|
|
|
|
|
/* Multiply and add in place */
|
|
|
|
for (iy = 0; iy < n->used; iy++) {
|
|
|
|
r = ((mp_word) mu) * ((mp_word) * tmpn++) +
|
|
|
|
((mp_word) u) + ((mp_word) * tmpx);
|
|
|
|
u = (r >> ((mp_word) DIGIT_BIT));
|
|
|
|
*tmpx++ = (r & ((mp_word) MP_MASK));
|
2003-02-28 11:08:34 -05:00
|
|
|
}
|
|
|
|
/* propagate carries */
|
2003-05-29 09:35:26 -04:00
|
|
|
while (u) {
|
2003-06-06 15:35:48 -04:00
|
|
|
*tmpx += u;
|
|
|
|
u = *tmpx >> DIGIT_BIT;
|
2003-05-29 09:35:26 -04:00
|
|
|
*tmpx++ &= MP_MASK;
|
2003-02-28 11:08:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-29 09:35:26 -04:00
|
|
|
/* x = x/b**n.used */
|
2003-06-06 15:35:48 -04:00
|
|
|
mp_clamp(x);
|
2003-05-29 09:35:26 -04:00
|
|
|
mp_rshd (x, n->used);
|
2003-02-28 11:08:34 -05:00
|
|
|
|
|
|
|
/* if A >= m then A = A - m */
|
2003-05-29 09:35:26 -04:00
|
|
|
if (mp_cmp_mag (x, n) != MP_LT) {
|
|
|
|
return s_mp_sub (x, n, x);
|
2003-02-28 11:08:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return MP_OKAY;
|
|
|
|
}
|