Mercurial > dropbear
comparison libtommath/bn_mp_reduce_2k.c @ 1692:1051e4eea25a
Update LibTomMath to 1.2.0 (#84)
* update C files
* update other files
* update headers
* update makefiles
* remove mp_set/get_double()
* use ltm 1.2.0 API
* update ltm_desc
* use bundled tommath if system-tommath is too old
* XMALLOC etc. were changed to MP_MALLOC etc.
author | Steffen Jaeckel <s@jaeckel.eu> |
---|---|
date | Tue, 26 May 2020 17:36:47 +0200 |
parents | f52919ffd3b1 |
children |
comparison
equal
deleted
inserted
replaced
1691:2d3745d58843 | 1692:1051e4eea25a |
---|---|
1 #include "tommath_private.h" | 1 #include "tommath_private.h" |
2 #ifdef BN_MP_REDUCE_2K_C | 2 #ifdef BN_MP_REDUCE_2K_C |
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis | 3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */ |
4 * | 4 /* SPDX-License-Identifier: Unlicense */ |
5 * LibTomMath is a library that provides multiple-precision | |
6 * integer arithmetic as well as number theoretic functionality. | |
7 * | |
8 * The library was designed directly after the MPI library by | |
9 * Michael Fromberger but has been written from scratch with | |
10 * additional optimizations in place. | |
11 * | |
12 * SPDX-License-Identifier: Unlicense | |
13 */ | |
14 | 5 |
15 /* reduces a modulo n where n is of the form 2**p - d */ | 6 /* reduces a modulo n where n is of the form 2**p - d */ |
16 int mp_reduce_2k(mp_int *a, const mp_int *n, mp_digit d) | 7 mp_err mp_reduce_2k(mp_int *a, const mp_int *n, mp_digit d) |
17 { | 8 { |
18 mp_int q; | 9 mp_int q; |
19 int p, res; | 10 mp_err err; |
11 int p; | |
20 | 12 |
21 if ((res = mp_init(&q)) != MP_OKAY) { | 13 if ((err = mp_init(&q)) != MP_OKAY) { |
22 return res; | 14 return err; |
23 } | 15 } |
24 | 16 |
25 p = mp_count_bits(n); | 17 p = mp_count_bits(n); |
26 top: | 18 top: |
27 /* q = a/2**p, a = a mod 2**p */ | 19 /* q = a/2**p, a = a mod 2**p */ |
28 if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) { | 20 if ((err = mp_div_2d(a, p, &q, a)) != MP_OKAY) { |
29 goto LBL_ERR; | 21 goto LBL_ERR; |
30 } | 22 } |
31 | 23 |
32 if (d != 1u) { | 24 if (d != 1u) { |
33 /* q = q * d */ | 25 /* q = q * d */ |
34 if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) { | 26 if ((err = mp_mul_d(&q, d, &q)) != MP_OKAY) { |
35 goto LBL_ERR; | 27 goto LBL_ERR; |
36 } | 28 } |
37 } | 29 } |
38 | 30 |
39 /* a = a + q */ | 31 /* a = a + q */ |
40 if ((res = s_mp_add(a, &q, a)) != MP_OKAY) { | 32 if ((err = s_mp_add(a, &q, a)) != MP_OKAY) { |
41 goto LBL_ERR; | 33 goto LBL_ERR; |
42 } | 34 } |
43 | 35 |
44 if (mp_cmp_mag(a, n) != MP_LT) { | 36 if (mp_cmp_mag(a, n) != MP_LT) { |
45 if ((res = s_mp_sub(a, n, a)) != MP_OKAY) { | 37 if ((err = s_mp_sub(a, n, a)) != MP_OKAY) { |
46 goto LBL_ERR; | 38 goto LBL_ERR; |
47 } | 39 } |
48 goto top; | 40 goto top; |
49 } | 41 } |
50 | 42 |
51 LBL_ERR: | 43 LBL_ERR: |
52 mp_clear(&q); | 44 mp_clear(&q); |
53 return res; | 45 return err; |
54 } | 46 } |
55 | 47 |
56 #endif | 48 #endif |
57 | |
58 /* ref: HEAD -> master, tag: v1.1.0 */ | |
59 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */ | |
60 /* commit time: 2019-01-28 20:32:32 +0100 */ |