2003-02-28 11:08:34 -05:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
|
|
|
*
|
2003-08-04 21:24:44 -04:00
|
|
|
* LibTomMath is a library that provides multiple-precision
|
2003-02-28 11:08:34 -05:00
|
|
|
* integer arithmetic as well as number theoretic functionality.
|
|
|
|
*
|
2003-08-04 21:24:44 -04:00
|
|
|
* The library was designed directly after the MPI library by
|
2003-02-28 11:08:34 -05:00
|
|
|
* 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;
|
2003-12-24 13:59:22 -05:00
|
|
|
if ((digs < MP_WARRAY) &&
|
|
|
|
n->used <
|
2003-05-29 09:35:26 -04:00
|
|
|
(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++) {
|
2003-08-29 10:06:56 -04:00
|
|
|
/* mu = ai * rho mod b
|
|
|
|
*
|
|
|
|
* The value of rho must be precalculated via
|
|
|
|
* bn_mp_montgomery_setup() such that
|
|
|
|
* it equals -1/n0 mod b this allows the
|
|
|
|
* following inner loop to reduce the
|
|
|
|
* input one digit at a time
|
|
|
|
*/
|
2003-12-24 13:59:22 -05:00
|
|
|
mu = (mp_digit) (((mp_word)x->dp[ix]) * ((mp_word)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;
|
|
|
|
|
2003-08-29 10:06:56 -04:00
|
|
|
/* alias for digits of the modulus */
|
2003-05-29 09:35:26 -04:00
|
|
|
tmpn = n->dp;
|
2003-08-29 10:06:56 -04:00
|
|
|
|
|
|
|
/* alias for the digits of x [the input] */
|
2003-05-29 09:35:26 -04:00
|
|
|
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;
|
2003-12-24 13:59:22 -05:00
|
|
|
|
2003-05-29 09:35:26 -04:00
|
|
|
/* Multiply and add in place */
|
|
|
|
for (iy = 0; iy < n->used; iy++) {
|
2003-08-29 10:06:56 -04:00
|
|
|
/* compute product and sum */
|
2003-07-12 10:31:43 -04:00
|
|
|
r = ((mp_word)mu) * ((mp_word)*tmpn++) +
|
2003-06-19 06:04:50 -04:00
|
|
|
((mp_word) u) + ((mp_word) * tmpx);
|
2003-08-29 10:06:56 -04:00
|
|
|
|
|
|
|
/* get carry */
|
2003-06-19 06:04:50 -04:00
|
|
|
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
|
2003-08-29 10:06:56 -04:00
|
|
|
|
|
|
|
/* fix digit */
|
2003-06-19 06:04:50 -04:00
|
|
|
*tmpx++ = (mp_digit)(r & ((mp_word) MP_MASK));
|
2003-02-28 11:08:34 -05:00
|
|
|
}
|
2003-08-29 10:06:56 -04:00
|
|
|
/* At this point the ix'th digit of x should be zero */
|
|
|
|
|
|
|
|
|
|
|
|
/* propagate carries upwards as required*/
|
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-08-29 10:06:56 -04:00
|
|
|
/* at this point the n.used'th least
|
|
|
|
* significant digits of x are all zero
|
|
|
|
* which means we can shift x to the
|
|
|
|
* right by n.used digits and the
|
|
|
|
* residue is unchanged.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
2003-08-29 10:06:56 -04:00
|
|
|
/* if x >= n then x = x - n */
|
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;
|
|
|
|
}
|