2015-11-11 19:49:07 -05:00
|
|
|
#include <tommath_private.h>
|
2004-10-29 18:07:18 -04:00
|
|
|
#ifdef BN_MP_MONTGOMERY_REDUCE_C
|
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.
|
|
|
|
*
|
2015-10-30 17:55:29 -04:00
|
|
|
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
2003-02-28 11:08:34 -05:00
|
|
|
*/
|
|
|
|
|
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?
|
|
|
|
*
|
2004-10-29 18:07:18 -04:00
|
|
|
* Note that unlike in mul you're safely allowed *less*
|
2003-05-17 08:33:54 -04:00
|
|
|
* than the available columns [255 per default] since carries
|
|
|
|
* are fixed up in the inner loop.
|
|
|
|
*/
|
2015-11-13 04:28:23 -05:00
|
|
|
digs = (n->used * 2) + 1;
|
2003-12-24 13:59:22 -05:00
|
|
|
if ((digs < MP_WARRAY) &&
|
2015-11-13 04:28:23 -05:00
|
|
|
(n->used <
|
|
|
|
(1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
|
2017-08-30 14:08:58 -04:00
|
|
|
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) {
|
2017-08-30 14:08:58 -04:00
|
|
|
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
|
2004-10-29 18:07:18 -04:00
|
|
|
* montgomery_setup() such that
|
2003-08-29 10:06:56 -04:00
|
|
|
* it equals -1/n0 mod b this allows the
|
|
|
|
* following inner loop to reduce the
|
|
|
|
* input one digit at a time
|
|
|
|
*/
|
2017-08-30 14:08:58 -04: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
|
|
|
{
|
2015-11-25 15:59:46 -05:00
|
|
|
int iy;
|
|
|
|
mp_digit *tmpn, *tmpx, u;
|
|
|
|
mp_word r;
|
2003-02-28 11:08:34 -05:00
|
|
|
|
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 */
|
2015-11-13 04:28:23 -05:00
|
|
|
r = ((mp_word)mu * (mp_word)*tmpn++) +
|
|
|
|
(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*/
|
2015-10-25 11:49:26 -04:00
|
|
|
while (u != 0) {
|
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);
|
2017-08-30 14:08:58 -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 */
|
2017-08-30 14:08:58 -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;
|
|
|
|
}
|
2004-10-29 18:07:18 -04:00
|
|
|
#endif
|
2005-08-01 12:37:28 -04:00
|
|
|
|
2017-08-28 10:27:26 -04:00
|
|
|
/* ref: $Format:%D$ */
|
|
|
|
/* git commit: $Format:%H$ */
|
|
|
|
/* commit time: $Format:%ai$ */
|