Bump actions/upload-artifacts version
[libtommath.git] / mp_reduce_2k_l.c
blob31d9a188203487c1be6668d0d98167ffd1c48f9f
1 #include "tommath_private.h"
2 #ifdef MP_REDUCE_2K_L_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
6 /* reduces a modulo n where n is of the form 2**p - d
7 This differs from reduce_2k since "d" can be larger
8 than a single digit.
9 */
10 mp_err mp_reduce_2k_l(mp_int *a, const mp_int *n, const mp_int *d)
12 mp_int q;
13 mp_err err;
14 int p;
16 if ((err = mp_init(&q)) != MP_OKAY) {
17 return err;
20 p = mp_count_bits(n);
22 for (;;) {
23 /* q = a/2**p, a = a mod 2**p */
24 if ((err = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
25 goto LBL_ERR;
28 /* q = q * d */
29 if ((err = mp_mul(&q, d, &q)) != MP_OKAY) {
30 goto LBL_ERR;
33 /* a = a + q */
34 if ((err = s_mp_add(a, &q, a)) != MP_OKAY) {
35 goto LBL_ERR;
38 if (mp_cmp_mag(a, n) == MP_LT) {
39 break;
41 if ((err = s_mp_sub(a, n, a)) != MP_OKAY) {
42 goto LBL_ERR;
47 LBL_ERR:
48 mp_clear(&q);
49 return err;
52 #endif