libtommath/bn_mp_dr_reduce.c

95 lines
2.3 KiB
C
Raw Normal View History

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