tommath/bn_mp_dr_reduce.c

93 lines
2.3 KiB
C
Raw Normal View History

#include "tommath_private.h"
2004-10-29 18:07:18 -04:00
#ifdef BN_MP_DR_REDUCE_C
2003-03-22 10:10:20 -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-03-22 10:10:20 -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-03-22 10:10:20 -05:00
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
2018-12-29 11:56:20 -05:00
* SPDX-License-Identifier: Unlicense
2003-03-22 10:10:20 -05:00
*/
2003-05-29 09:35:26 -04:00
/* reduce "x" in place modulo "n" using the Diminished Radix algorithm.
2003-03-22 10:10:20 -05:00
*
2003-05-17 08:33:54 -04:00
* Based on algorithm from the paper
2003-03-22 10:10:20 -05:00
*
* "Generating Efficient Primes for Discrete Log Cryptosystems"
2004-12-22 21:40:37 -05:00
* Chae Hoon Lim, Pil Joong Lee,
2003-03-22 10:10:20 -05:00
* POSTECH Information Research Laboratories
*
* The modulus must be of a special format [see manual]
2003-05-29 09:35:26 -04:00
*
* Has been modified to use algorithm 7.10 from the LTM book instead
2003-08-29 10:06:56 -04:00
*
2003-09-19 18:43:07 -04:00
* Input x must be in the range 0 <= x <= (n-1)**2
2003-03-22 10:10:20 -05:00
*/
2017-09-20 10:59:43 -04:00
int mp_dr_reduce(mp_int *x, const mp_int *n, mp_digit k)
2003-03-22 10:10:20 -05:00
{
2017-08-29 16:23:48 -04:00
int err, i, m;
mp_word r;
mp_digit mu, *tmpx1, *tmpx2;
2003-09-19 18:43:07 -04:00
2017-08-29 16:23:48 -04:00
/* m = digits in modulus */
m = n->used;
2003-09-19 18:43:07 -04:00
2017-08-29 16:23:48 -04:00
/* ensure that "x" has at least 2m digits */
if (x->alloc < (m + m)) {
if ((err = mp_grow(x, m + m)) != MP_OKAY) {
return err;
}
}
2003-05-17 08:33:54 -04:00
2017-08-29 16:23:48 -04:00
/* top of loop, this is where the code resumes if
* another reduction pass is required.
*/
2003-05-29 09:35:26 -04:00
top:
2017-08-29 16:23:48 -04:00
/* aliases for digits */
/* alias for lower half of x */
tmpx1 = x->dp;
2003-09-19 18:43:07 -04:00
2017-08-29 16:23:48 -04:00
/* alias for upper half of x, or x/B**m */
tmpx2 = x->dp + m;
2003-09-19 18:43:07 -04:00
2017-08-29 16:23:48 -04:00
/* set carry to zero */
mu = 0;
2003-09-19 18:43:07 -04:00
2017-08-29 16:23:48 -04:00
/* compute (x mod B**m) + k * [x/B**m] inline and inplace */
for (i = 0; i < m; i++) {
2017-10-15 13:58:35 -04:00
r = ((mp_word)*tmpx2++ * (mp_word)k) + *tmpx1 + mu;
2003-06-19 06:04:50 -04:00
*tmpx1++ = (mp_digit)(r & MP_MASK);
mu = (mp_digit)(r >> ((mp_word)DIGIT_BIT));
2017-08-29 16:23:48 -04:00
}
2003-08-29 10:06:56 -04:00
2017-08-29 16:23:48 -04:00
/* set final carry */
*tmpx1++ = mu;
2003-08-29 10:06:56 -04:00
2017-08-29 16:23:48 -04:00
/* zero words above m */
for (i = m + 1; i < x->used; i++) {
2003-05-29 09:35:26 -04:00
*tmpx1++ = 0;
2017-08-29 16:23:48 -04:00
}
2003-03-22 10:10:20 -05:00
2017-08-29 16:23:48 -04:00
/* clamp, sub and return */
mp_clamp(x);
2003-05-17 08:33:54 -04:00
2017-08-29 16:23:48 -04:00
/* if x >= n then subtract and reduce again
* Each successive "recursion" makes the input smaller and smaller.
*/
if (mp_cmp_mag(x, n) != MP_LT) {
if ((err = s_mp_sub(x, n, x)) != MP_OKAY) {
return err;
}
goto top;
}
return MP_OKAY;
2003-03-22 10:10:20 -05:00
}
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$ */