comparison libtommath/bn_mp_reduce_2k_l.c @ 1655:f52919ffd3b1

update ltm to 1.1.0 and enable FIPS 186.4 compliant key-generation (#79) * make key-generation compliant to FIPS 186.4 * fix includes in tommath_class.h * update fuzzcorpus instead of error-out * fixup fuzzing make-targets * update Makefile.in * apply necessary patches to ltm sources * clean-up not required ltm files * update to vanilla ltm 1.1.0 this already only contains the required files * remove set/get double
author Steffen Jaeckel <s_jaeckel@gmx.de>
date Mon, 16 Sep 2019 15:50:38 +0200
parents 8bba51a55704
children 1051e4eea25a
comparison
equal deleted inserted replaced
1654:cc0fc5131c5c 1655:f52919ffd3b1
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 *
5 * LibTomMath is a library that provides multiple-precision 5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality. 6 * integer arithmetic as well as number theoretic functionality.
7 * 7 *
8 * The library was designed directly after the MPI library by 8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with 9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place. 10 * additional optimizations in place.
11 * 11 *
12 * The library is free for all purposes without any express 12 * SPDX-License-Identifier: Unlicense
13 * guarantee it works.
14 *
15 * Tom St Denis, [email protected], http://libtom.org
16 */ 13 */
17 14
18 /* reduces a modulo n where n is of the form 2**p - d 15 /* reduces a modulo n where n is of the form 2**p - d
19 This differs from reduce_2k since "d" can be larger 16 This differs from reduce_2k since "d" can be larger
20 than a single digit. 17 than a single digit.
21 */ 18 */
22 int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d) 19 int mp_reduce_2k_l(mp_int *a, const mp_int *n, const mp_int *d)
23 { 20 {
24 mp_int q; 21 mp_int q;
25 int p, res; 22 int p, res;
26 23
27 if ((res = mp_init(&q)) != MP_OKAY) { 24 if ((res = mp_init(&q)) != MP_OKAY) {
30 27
31 p = mp_count_bits(n); 28 p = mp_count_bits(n);
32 top: 29 top:
33 /* q = a/2**p, a = a mod 2**p */ 30 /* q = a/2**p, a = a mod 2**p */
34 if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) { 31 if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
35 goto ERR; 32 goto LBL_ERR;
36 } 33 }
37 34
38 /* q = q * d */ 35 /* q = q * d */
39 if ((res = mp_mul(&q, d, &q)) != MP_OKAY) { 36 if ((res = mp_mul(&q, d, &q)) != MP_OKAY) {
40 goto ERR; 37 goto LBL_ERR;
41 } 38 }
42 39
43 /* a = a + q */ 40 /* a = a + q */
44 if ((res = s_mp_add(a, &q, a)) != MP_OKAY) { 41 if ((res = s_mp_add(a, &q, a)) != MP_OKAY) {
45 goto ERR; 42 goto LBL_ERR;
46 } 43 }
47 44
48 if (mp_cmp_mag(a, n) != MP_LT) { 45 if (mp_cmp_mag(a, n) != MP_LT) {
49 if ((res = s_mp_sub(a, n, a)) != MP_OKAY) { 46 if ((res = s_mp_sub(a, n, a)) != MP_OKAY) {
50 goto ERR; 47 goto LBL_ERR;
51 } 48 }
52 goto top; 49 goto top;
53 } 50 }
54 51
55 ERR: 52 LBL_ERR:
56 mp_clear(&q); 53 mp_clear(&q);
57 return res; 54 return res;
58 } 55 }
59 56
60 #endif 57 #endif
61 58
62 /* ref: $Format:%D$ */ 59 /* ref: HEAD -> master, tag: v1.1.0 */
63 /* git commit: $Format:%H$ */ 60 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
64 /* commit time: $Format:%ai$ */ 61 /* commit time: 2019-01-28 20:32:32 +0100 */