Mercurial > dropbear
comparison libtommath/bn_mp_reduce_2k_l.c @ 1739:13d834efc376 fuzz
merge from main
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 15 Oct 2020 19:55:15 +0800 |
parents | 1051e4eea25a |
children |
comparison
equal
deleted
inserted
replaced
1562:768ebf737aa0 | 1739:13d834efc376 |
---|---|
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 * The library is free for all purposes without any express | |
13 * guarantee it works. | |
14 * | |
15 * Tom St Denis, [email protected], http://libtom.org | |
16 */ | |
17 | 5 |
18 /* 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 |
19 This differs from reduce_2k since "d" can be larger | 7 This differs from reduce_2k since "d" can be larger |
20 than a single digit. | 8 than a single digit. |
21 */ | 9 */ |
22 int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d) | 10 mp_err mp_reduce_2k_l(mp_int *a, const mp_int *n, const mp_int *d) |
23 { | 11 { |
24 mp_int q; | 12 mp_int q; |
25 int p, res; | 13 mp_err err; |
14 int p; | |
26 | 15 |
27 if ((res = mp_init(&q)) != MP_OKAY) { | 16 if ((err = mp_init(&q)) != MP_OKAY) { |
28 return res; | 17 return err; |
29 } | 18 } |
30 | 19 |
31 p = mp_count_bits(n); | 20 p = mp_count_bits(n); |
32 top: | 21 top: |
33 /* q = a/2**p, a = a mod 2**p */ | 22 /* q = a/2**p, a = a mod 2**p */ |
34 if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) { | 23 if ((err = mp_div_2d(a, p, &q, a)) != MP_OKAY) { |
35 goto ERR; | 24 goto LBL_ERR; |
36 } | 25 } |
37 | 26 |
38 /* q = q * d */ | 27 /* q = q * d */ |
39 if ((res = mp_mul(&q, d, &q)) != MP_OKAY) { | 28 if ((err = mp_mul(&q, d, &q)) != MP_OKAY) { |
40 goto ERR; | 29 goto LBL_ERR; |
41 } | 30 } |
42 | 31 |
43 /* a = a + q */ | 32 /* a = a + q */ |
44 if ((res = s_mp_add(a, &q, a)) != MP_OKAY) { | 33 if ((err = s_mp_add(a, &q, a)) != MP_OKAY) { |
45 goto ERR; | 34 goto LBL_ERR; |
46 } | 35 } |
47 | 36 |
48 if (mp_cmp_mag(a, n) != MP_LT) { | 37 if (mp_cmp_mag(a, n) != MP_LT) { |
49 if ((res = s_mp_sub(a, n, a)) != MP_OKAY) { | 38 if ((err = s_mp_sub(a, n, a)) != MP_OKAY) { |
50 goto ERR; | 39 goto LBL_ERR; |
51 } | 40 } |
52 goto top; | 41 goto top; |
53 } | 42 } |
54 | 43 |
55 ERR: | 44 LBL_ERR: |
56 mp_clear(&q); | 45 mp_clear(&q); |
57 return res; | 46 return err; |
58 } | 47 } |
59 | 48 |
60 #endif | 49 #endif |
61 | |
62 /* ref: $Format:%D$ */ | |
63 /* git commit: $Format:%H$ */ | |
64 /* commit time: $Format:%ai$ */ |