2018-05-02 19:43:17 +00:00
|
|
|
#include "tommath_private.h"
|
2004-10-29 22:07:18 +00:00
|
|
|
#ifdef BN_MP_DR_REDUCE_C
|
2019-04-07 13:29:11 +00:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
|
|
|
|
/* SPDX-License-Identifier: Unlicense */
|
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
|
|
|
*/
|
2017-09-20 14:59:43 +00:00
|
|
|
int mp_dr_reduce(mp_int *x, const mp_int *n, mp_digit k)
|
2003-03-22 15:10:20 +00:00
|
|
|
{
|
2017-08-29 20:23:48 +00:00
|
|
|
int err, i, m;
|
|
|
|
mp_word r;
|
|
|
|
mp_digit mu, *tmpx1, *tmpx2;
|
2003-09-19 22:43:07 +00:00
|
|
|
|
2017-08-29 20:23:48 +00:00
|
|
|
/* m = digits in modulus */
|
|
|
|
m = n->used;
|
2003-09-19 22:43:07 +00:00
|
|
|
|
2017-08-29 20:23:48 +00: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 12:33:54 +00:00
|
|
|
|
2017-08-29 20:23:48 +00:00
|
|
|
/* top of loop, this is where the code resumes if
|
|
|
|
* another reduction pass is required.
|
|
|
|
*/
|
2003-05-29 13:35:26 +00:00
|
|
|
top:
|
2017-08-29 20:23:48 +00:00
|
|
|
/* aliases for digits */
|
|
|
|
/* alias for lower half of x */
|
|
|
|
tmpx1 = x->dp;
|
2003-09-19 22:43:07 +00:00
|
|
|
|
2017-08-29 20:23:48 +00:00
|
|
|
/* alias for upper half of x, or x/B**m */
|
|
|
|
tmpx2 = x->dp + m;
|
2003-09-19 22:43:07 +00:00
|
|
|
|
2017-08-29 20:23:48 +00:00
|
|
|
/* set carry to zero */
|
|
|
|
mu = 0;
|
2003-09-19 22:43:07 +00:00
|
|
|
|
2017-08-29 20:23:48 +00:00
|
|
|
/* compute (x mod B**m) + k * [x/B**m] inline and inplace */
|
|
|
|
for (i = 0; i < m; i++) {
|
2017-10-15 17:58:35 +00:00
|
|
|
r = ((mp_word)*tmpx2++ * (mp_word)k) + *tmpx1 + mu;
|
2003-06-19 10:04:50 +00:00
|
|
|
*tmpx1++ = (mp_digit)(r & MP_MASK);
|
2019-04-13 06:46:57 +00:00
|
|
|
mu = (mp_digit)(r >> ((mp_word)MP_DIGIT_BIT));
|
2017-08-29 20:23:48 +00:00
|
|
|
}
|
2003-08-29 14:06:56 +00:00
|
|
|
|
2017-08-29 20:23:48 +00:00
|
|
|
/* set final carry */
|
|
|
|
*tmpx1++ = mu;
|
2003-08-29 14:06:56 +00:00
|
|
|
|
2017-08-29 20:23:48 +00:00
|
|
|
/* zero words above m */
|
|
|
|
for (i = m + 1; i < x->used; i++) {
|
2003-05-29 13:35:26 +00:00
|
|
|
*tmpx1++ = 0;
|
2017-08-29 20:23:48 +00:00
|
|
|
}
|
2003-03-22 15:10:20 +00:00
|
|
|
|
2017-08-29 20:23:48 +00:00
|
|
|
/* clamp, sub and return */
|
|
|
|
mp_clamp(x);
|
2003-05-17 12:33:54 +00:00
|
|
|
|
2017-08-29 20:23:48 +00: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 15:10:20 +00:00
|
|
|
}
|
2004-10-29 22:07:18 +00:00
|
|
|
#endif
|