2004-10-29 22:07:18 +00:00
|
|
|
#include <tommath.h>
|
|
|
|
#ifdef BN_MP_MONTGOMERY_REDUCE_C
|
2003-02-28 16:08:34 +00:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
|
|
|
*
|
2003-08-05 01:24:44 +00:00
|
|
|
* LibTomMath is a library that provides multiple-precision
|
2003-02-28 16:08:34 +00:00
|
|
|
* integer arithmetic as well as number theoretic functionality.
|
|
|
|
*
|
2003-08-05 01:24:44 +00:00
|
|
|
* The library was designed directly after the MPI library by
|
2003-02-28 16:08:34 +00: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.
|
|
|
|
*
|
2007-04-18 09:58:18 +00:00
|
|
|
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
2003-02-28 16:08:34 +00:00
|
|
|
*/
|
|
|
|
|
2003-05-29 13:35:26 +00:00
|
|
|
/* computes xR**-1 == x (mod N) via Montgomery Reduction */
|
2003-02-28 16:08:34 +00:00
|
|
|
int
|
2003-05-29 13:35:26 +00:00
|
|
|
mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
|
2003-02-28 16:08:34 +00:00
|
|
|
{
|
2003-02-28 16:09:08 +00:00
|
|
|
int ix, res, digs;
|
2003-05-29 13:35:26 +00:00
|
|
|
mp_digit mu;
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2003-05-17 12:33:54 +00:00
|
|
|
/* can the fast reduction [comba] method be used?
|
|
|
|
*
|
2004-10-29 22:07:18 +00:00
|
|
|
* Note that unlike in mul you're safely allowed *less*
|
2003-05-17 12:33:54 +00:00
|
|
|
* than the available columns [255 per default] since carries
|
|
|
|
* are fixed up in the inner loop.
|
|
|
|
*/
|
2003-05-29 13:35:26 +00:00
|
|
|
digs = n->used * 2 + 1;
|
2003-12-24 18:59:22 +00:00
|
|
|
if ((digs < MP_WARRAY) &&
|
|
|
|
n->used <
|
2003-05-29 13:35:26 +00:00
|
|
|
(1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
|
|
|
|
return fast_mp_montgomery_reduce (x, n, rho);
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
|
|
|
|
2003-05-17 12:33:54 +00:00
|
|
|
/* grow the input as required */
|
2003-05-29 13:35:26 +00:00
|
|
|
if (x->alloc < digs) {
|
|
|
|
if ((res = mp_grow (x, digs)) != MP_OKAY) {
|
2003-02-28 16:08:34 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2003-05-29 13:35:26 +00:00
|
|
|
x->used = digs;
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2003-05-29 13:35:26 +00:00
|
|
|
for (ix = 0; ix < n->used; ix++) {
|
2003-08-29 14:06:56 +00:00
|
|
|
/* mu = ai * rho mod b
|
|
|
|
*
|
|
|
|
* The value of rho must be precalculated via
|
2004-10-29 22:07:18 +00:00
|
|
|
* montgomery_setup() such that
|
2003-08-29 14:06:56 +00:00
|
|
|
* it equals -1/n0 mod b this allows the
|
|
|
|
* following inner loop to reduce the
|
|
|
|
* input one digit at a time
|
|
|
|
*/
|
2003-12-24 18:59:22 +00:00
|
|
|
mu = (mp_digit) (((mp_word)x->dp[ix]) * ((mp_word)rho) & MP_MASK);
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2003-05-29 13:35:26 +00:00
|
|
|
/* a = a + mu * m * b**i */
|
2003-02-28 16:08:34 +00:00
|
|
|
{
|
|
|
|
register int iy;
|
2003-05-29 13:35:26 +00:00
|
|
|
register mp_digit *tmpn, *tmpx, u;
|
2003-02-28 16:08:34 +00:00
|
|
|
register mp_word r;
|
|
|
|
|
2003-08-29 14:06:56 +00:00
|
|
|
/* alias for digits of the modulus */
|
2003-05-29 13:35:26 +00:00
|
|
|
tmpn = n->dp;
|
2003-08-29 14:06:56 +00:00
|
|
|
|
|
|
|
/* alias for the digits of x [the input] */
|
2003-05-29 13:35:26 +00:00
|
|
|
tmpx = x->dp + ix;
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2003-05-29 13:35:26 +00:00
|
|
|
/* set the carry to zero */
|
|
|
|
u = 0;
|
2003-12-24 18:59:22 +00:00
|
|
|
|
2003-05-29 13:35:26 +00:00
|
|
|
/* Multiply and add in place */
|
|
|
|
for (iy = 0; iy < n->used; iy++) {
|
2003-08-29 14:06:56 +00:00
|
|
|
/* compute product and sum */
|
2003-07-12 14:31:43 +00:00
|
|
|
r = ((mp_word)mu) * ((mp_word)*tmpn++) +
|
2003-06-19 10:04:50 +00:00
|
|
|
((mp_word) u) + ((mp_word) * tmpx);
|
2003-08-29 14:06:56 +00:00
|
|
|
|
|
|
|
/* get carry */
|
2003-06-19 10:04:50 +00:00
|
|
|
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
|
2003-08-29 14:06:56 +00:00
|
|
|
|
|
|
|
/* fix digit */
|
2003-06-19 10:04:50 +00:00
|
|
|
*tmpx++ = (mp_digit)(r & ((mp_word) MP_MASK));
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
2003-08-29 14:06:56 +00:00
|
|
|
/* At this point the ix'th digit of x should be zero */
|
|
|
|
|
|
|
|
|
|
|
|
/* propagate carries upwards as required*/
|
2003-05-29 13:35:26 +00:00
|
|
|
while (u) {
|
2003-06-06 19:35:48 +00:00
|
|
|
*tmpx += u;
|
|
|
|
u = *tmpx >> DIGIT_BIT;
|
2003-05-29 13:35:26 +00:00
|
|
|
*tmpx++ &= MP_MASK;
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-29 14:06:56 +00: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 13:35:26 +00:00
|
|
|
/* x = x/b**n.used */
|
2003-06-06 19:35:48 +00:00
|
|
|
mp_clamp(x);
|
2003-05-29 13:35:26 +00:00
|
|
|
mp_rshd (x, n->used);
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2003-08-29 14:06:56 +00:00
|
|
|
/* if x >= n then x = x - n */
|
2003-05-29 13:35:26 +00:00
|
|
|
if (mp_cmp_mag (x, n) != MP_LT) {
|
|
|
|
return s_mp_sub (x, n, x);
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return MP_OKAY;
|
|
|
|
}
|
2004-10-29 22:07:18 +00:00
|
|
|
#endif
|
2005-08-01 16:37:28 +00:00
|
|
|
|
|
|
|
/* $Source$ */
|
|
|
|
/* $Revision$ */
|
|
|
|
/* $Date$ */
|