1 #include "tommath_private.h"
2 #ifdef MP_MONTGOMERY_SETUP_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
6 /* setups the montgomery reduction stuff */
7 mp_err
mp_montgomery_setup(const mp_int
*n
, mp_digit
*rho
)
11 /* fast inversion mod 2**k
13 * Based on the fact that
15 * XA = 1 (mod 2**n) => (X(2-XA)) A = 1 (mod 2**2n)
16 * => 2*X*A - X*X*A*A = 1
25 x
= (((b
+ 2u) & 4u) << 1) + b
; /* here x*a==1 mod 2**4 */
26 x
*= 2u - (b
* x
); /* here x*a==1 mod 2**8 */
27 x
*= 2u - (b
* x
); /* here x*a==1 mod 2**16 */
28 #if defined(MP_64BIT) || !(defined(MP_16BIT))
29 x
*= 2u - (b
* x
); /* here x*a==1 mod 2**32 */
32 x
*= 2u - (b
* x
); /* here x*a==1 mod 2**64 */
35 /* rho = -1/m mod b */
36 *rho
= (mp_digit
)(((mp_word
)1 << (mp_word
)MP_DIGIT_BIT
) - x
) & MP_MASK
;