2018-05-02 19:43:17 +00:00
|
|
|
#include "tommath_private.h"
|
2019-10-19 14:24:39 +00:00
|
|
|
#ifdef S_MP_MONTGOMERY_REDUCE_FAST_C
|
2019-04-07 13:29:11 +00:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
|
|
|
|
/* SPDX-License-Identifier: Unlicense */
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2003-12-24 18:59:22 +00:00
|
|
|
/* computes xR**-1 == x (mod N) via Montgomery Reduction
|
|
|
|
*
|
2004-10-29 22:07:18 +00:00
|
|
|
* This is an optimized implementation of montgomery_reduce
|
2003-02-28 16:09:08 +00:00
|
|
|
* which uses the comba method to quickly calculate the columns of the
|
2003-12-24 18:59:22 +00:00
|
|
|
* reduction.
|
2003-02-28 16:09:08 +00:00
|
|
|
*
|
|
|
|
* Based on Algorithm 14.32 on pp.601 of HAC.
|
|
|
|
*/
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err s_mp_montgomery_reduce_fast(mp_int *x, const mp_int *n, mp_digit rho)
|
2003-02-28 16:08:34 +00:00
|
|
|
{
|
2019-05-12 22:22:18 +00:00
|
|
|
int ix, olduse;
|
2019-05-19 15:16:13 +00:00
|
|
|
mp_err err;
|
2017-08-30 18:23:46 +00:00
|
|
|
mp_word W[MP_WARRAY];
|
|
|
|
|
2019-05-23 16:00:21 +00:00
|
|
|
if (x->used > MP_WARRAY) {
|
2017-08-29 14:41:08 +00:00
|
|
|
return MP_VAL;
|
|
|
|
}
|
|
|
|
|
2017-08-30 18:23:46 +00:00
|
|
|
/* get old used count */
|
|
|
|
olduse = x->used;
|
|
|
|
|
|
|
|
/* grow a as required */
|
|
|
|
if (x->alloc < (n->used + 1)) {
|
2019-05-19 15:16:13 +00:00
|
|
|
if ((err = mp_grow(x, n->used + 1)) != MP_OKAY) {
|
|
|
|
return err;
|
2017-08-30 18:23:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* first we have to get the digits of the input into
|
|
|
|
* an array of double precision words W[...]
|
|
|
|
*/
|
|
|
|
{
|
2015-11-25 20:59:46 +00:00
|
|
|
mp_word *_W;
|
2017-08-30 18:23:46 +00:00
|
|
|
mp_digit *tmpx;
|
|
|
|
|
|
|
|
/* alias for the W[] array */
|
|
|
|
_W = W;
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2017-08-30 18:23:46 +00:00
|
|
|
/* alias for the digits of x*/
|
|
|
|
tmpx = x->dp;
|
2003-02-28 16:09:08 +00:00
|
|
|
|
2017-08-30 18:23:46 +00:00
|
|
|
/* copy the digits of a into W[0..a->used-1] */
|
|
|
|
for (ix = 0; ix < x->used; ix++) {
|
|
|
|
*_W++ = *tmpx++;
|
|
|
|
}
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2017-08-30 18:23:46 +00:00
|
|
|
/* zero the high words of W[a->used..m->used*2] */
|
2019-05-11 08:22:20 +00:00
|
|
|
if (ix < ((n->used * 2) + 1)) {
|
|
|
|
MP_ZERO_BUFFER(_W, sizeof(mp_word) * (size_t)(((n->used * 2) + 1) - ix));
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
2017-08-30 18:23:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* now we proceed to zero successive digits
|
|
|
|
* from the least significant upwards
|
|
|
|
*/
|
|
|
|
for (ix = 0; ix < n->used; ix++) {
|
|
|
|
/* mu = ai * m' mod b
|
|
|
|
*
|
|
|
|
* We avoid a double precision multiplication (which isn't required)
|
|
|
|
* by casting the value down to a mp_digit. Note this requires
|
|
|
|
* that W[ix-1] have the carry cleared (see after the inner loop)
|
|
|
|
*/
|
|
|
|
mp_digit mu;
|
2017-10-15 17:58:35 +00:00
|
|
|
mu = ((W[ix] & MP_MASK) * rho) & MP_MASK;
|
2017-08-30 18:23:46 +00:00
|
|
|
|
|
|
|
/* a = a + mu * m * b**i
|
|
|
|
*
|
|
|
|
* This is computed in place and on the fly. The multiplication
|
|
|
|
* by b**i is handled by offseting which columns the results
|
|
|
|
* are added to.
|
|
|
|
*
|
|
|
|
* Note the comba method normally doesn't handle carries in the
|
|
|
|
* inner loop In this case we fix the carry from the previous
|
|
|
|
* column since the Montgomery reduction requires digits of the
|
|
|
|
* result (so far) [see above] to work. This is
|
|
|
|
* handled by fixing up one carry after the inner loop. The
|
|
|
|
* carry fixups are done in order so after these loops the
|
|
|
|
* first m->used words of W[] have the carries fixed
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
int iy;
|
|
|
|
mp_digit *tmpn;
|
|
|
|
mp_word *_W;
|
|
|
|
|
|
|
|
/* alias for the digits of the modulus */
|
|
|
|
tmpn = n->dp;
|
|
|
|
|
|
|
|
/* Alias for the columns set by an offset of ix */
|
|
|
|
_W = W + ix;
|
|
|
|
|
|
|
|
/* inner loop */
|
|
|
|
for (iy = 0; iy < n->used; iy++) {
|
2017-10-15 17:58:35 +00:00
|
|
|
*_W++ += (mp_word)mu * (mp_word)*tmpn++;
|
2017-08-30 18:23:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now fix carry for next digit, W[ix+1] */
|
2019-04-13 06:46:57 +00:00
|
|
|
W[ix + 1] += W[ix] >> (mp_word)MP_DIGIT_BIT;
|
2017-08-30 18:23:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* now we have to propagate the carries and
|
|
|
|
* shift the words downward [all those least
|
|
|
|
* significant digits we zeroed].
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
mp_digit *tmpx;
|
|
|
|
mp_word *_W, *_W1;
|
|
|
|
|
|
|
|
/* nox fix rest of carries */
|
|
|
|
|
|
|
|
/* alias for current word */
|
|
|
|
_W1 = W + ix;
|
|
|
|
|
|
|
|
/* alias for next word, where the carry goes */
|
|
|
|
_W = W + ++ix;
|
|
|
|
|
2019-09-10 07:35:15 +00:00
|
|
|
for (; ix < ((n->used * 2) + 1); ix++) {
|
2019-04-13 06:46:57 +00:00
|
|
|
*_W++ += *_W1++ >> (mp_word)MP_DIGIT_BIT;
|
2017-08-30 18:23:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* copy out, A = A/b**n
|
|
|
|
*
|
|
|
|
* The result is A/b**n but instead of converting from an
|
|
|
|
* array of mp_word to mp_digit than calling mp_rshd
|
|
|
|
* we just copy them in the right order
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* alias for destination word */
|
|
|
|
tmpx = x->dp;
|
|
|
|
|
|
|
|
/* alias for shifted double precision result */
|
|
|
|
_W = W + n->used;
|
|
|
|
|
|
|
|
for (ix = 0; ix < (n->used + 1); ix++) {
|
2017-10-15 17:58:35 +00:00
|
|
|
*tmpx++ = *_W++ & (mp_word)MP_MASK;
|
2017-08-30 18:23:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* zero oldused digits, if the input a was larger than
|
|
|
|
* m->used+1 we'll have to clear the digits
|
|
|
|
*/
|
2019-05-11 08:22:20 +00:00
|
|
|
MP_ZERO_DIGITS(tmpx, olduse - ix);
|
2017-08-30 18:23:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set the max used and clamp */
|
|
|
|
x->used = n->used + 1;
|
|
|
|
mp_clamp(x);
|
|
|
|
|
|
|
|
/* if A >= m then A = A - m */
|
|
|
|
if (mp_cmp_mag(x, n) != MP_LT) {
|
|
|
|
return s_mp_sub(x, n, x);
|
|
|
|
}
|
|
|
|
return MP_OKAY;
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
2004-10-29 22:07:18 +00:00
|
|
|
#endif
|