comparison libtommath/bn_mp_reduce_2k_l.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_L_C 2 #ifdef BN_MP_REDUCE_2K_L_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 This differs from reduce_2k since "d" can be larger 7 This differs from reduce_2k since "d" can be larger
17 than a single digit. 8 than a single digit.
18 */ 9 */
19 int mp_reduce_2k_l(mp_int *a, const mp_int *n, const mp_int *d) 10 mp_err mp_reduce_2k_l(mp_int *a, const mp_int *n, const mp_int *d)
20 { 11 {
21 mp_int q; 12 mp_int q;
22 int p, res; 13 mp_err err;
14 int p;
23 15
24 if ((res = mp_init(&q)) != MP_OKAY) { 16 if ((err = mp_init(&q)) != MP_OKAY) {
25 return res; 17 return err;
26 } 18 }
27 19
28 p = mp_count_bits(n); 20 p = mp_count_bits(n);
29 top: 21 top:
30 /* q = a/2**p, a = a mod 2**p */ 22 /* q = a/2**p, a = a mod 2**p */
31 if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) { 23 if ((err = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
32 goto LBL_ERR; 24 goto LBL_ERR;
33 } 25 }
34 26
35 /* q = q * d */ 27 /* q = q * d */
36 if ((res = mp_mul(&q, d, &q)) != MP_OKAY) { 28 if ((err = mp_mul(&q, d, &q)) != MP_OKAY) {
37 goto LBL_ERR; 29 goto LBL_ERR;
38 } 30 }
39 31
40 /* a = a + q */ 32 /* a = a + q */
41 if ((res = s_mp_add(a, &q, a)) != MP_OKAY) { 33 if ((err = s_mp_add(a, &q, a)) != MP_OKAY) {
42 goto LBL_ERR; 34 goto LBL_ERR;
43 } 35 }
44 36
45 if (mp_cmp_mag(a, n) != MP_LT) { 37 if (mp_cmp_mag(a, n) != MP_LT) {
46 if ((res = s_mp_sub(a, n, a)) != MP_OKAY) { 38 if ((err = s_mp_sub(a, n, a)) != MP_OKAY) {
47 goto LBL_ERR; 39 goto LBL_ERR;
48 } 40 }
49 goto top; 41 goto top;
50 } 42 }
51 43
52 LBL_ERR: 44 LBL_ERR:
53 mp_clear(&q); 45 mp_clear(&q);
54 return res; 46 return err;
55 } 47 }
56 48
57 #endif 49 #endif
58
59 /* ref: HEAD -> master, tag: v1.1.0 */
60 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
61 /* commit time: 2019-01-28 20:32:32 +0100 */