2018-05-02 19:43:17 +00:00
|
|
|
#include "tommath_private.h"
|
2019-10-19 14:24:39 +00:00
|
|
|
#ifdef 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
|
|
|
*/
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err 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
|
|
|
/* m = digits in modulus */
|
2019-10-29 19:08:42 +00:00
|
|
|
int 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)) {
|
2019-10-29 19:08:42 +00:00
|
|
|
mp_err err;
|
2017-08-29 20:23:48 +00:00
|
|
|
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.
|
|
|
|
*/
|
2019-10-29 19:08:42 +00:00
|
|
|
for (;;) {
|
|
|
|
mp_err err;
|
|
|
|
int i;
|
|
|
|
mp_digit mu = 0;
|
2003-09-19 22:43:07 +00:00
|
|
|
|
2019-10-29 19:08:42 +00:00
|
|
|
/* compute (x mod B**m) + k * [x/B**m] inline and inplace */
|
|
|
|
for (i = 0; i < m; i++) {
|
|
|
|
mp_word r = ((mp_word)x->dp[i + m] * (mp_word)k) + x->dp[i] + mu;
|
|
|
|
x->dp[i] = (mp_digit)(r & MP_MASK);
|
|
|
|
mu = (mp_digit)(r >> ((mp_word)MP_DIGIT_BIT));
|
|
|
|
}
|
2003-09-19 22:43:07 +00:00
|
|
|
|
2019-10-29 19:08:42 +00:00
|
|
|
/* set final carry */
|
|
|
|
x->dp[i] = mu;
|
2003-08-29 14:06:56 +00:00
|
|
|
|
2019-10-29 19:08:42 +00:00
|
|
|
/* zero words above m */
|
2019-10-29 20:48:50 +00:00
|
|
|
s_mp_zero_digs(x->dp + m + 1, (x->used - m) - 1);
|
2003-08-29 14:06:56 +00:00
|
|
|
|
2019-10-29 19:08:42 +00:00
|
|
|
/* clamp, sub and return */
|
|
|
|
mp_clamp(x);
|
2003-03-22 15:10:20 +00:00
|
|
|
|
2019-10-29 19:08:42 +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) {
|
|
|
|
break;
|
|
|
|
}
|
2003-05-17 12:33:54 +00:00
|
|
|
|
2017-08-29 20:23:48 +00:00
|
|
|
if ((err = s_mp_sub(x, n, x)) != MP_OKAY) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MP_OKAY;
|
2003-03-22 15:10:20 +00:00
|
|
|
}
|
2004-10-29 22:07:18 +00:00
|
|
|
#endif
|