comparison libtommath/bn_mp_reduce_2k.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_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 *
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 int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d) 16 int mp_reduce_2k(mp_int *a, const mp_int *n, mp_digit d)
20 { 17 {
21 mp_int q; 18 mp_int q;
22 int p, res; 19 int p, res;
23 20
24 if ((res = mp_init(&q)) != MP_OKAY) { 21 if ((res = mp_init(&q)) != MP_OKAY) {
27 24
28 p = mp_count_bits(n); 25 p = mp_count_bits(n);
29 top: 26 top:
30 /* q = a/2**p, a = a mod 2**p */ 27 /* q = a/2**p, a = a mod 2**p */
31 if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) { 28 if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
32 goto ERR; 29 goto LBL_ERR;
33 } 30 }
34 31
35 if (d != 1) { 32 if (d != 1u) {
36 /* q = q * d */ 33 /* q = q * d */
37 if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) { 34 if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) {
38 goto ERR; 35 goto LBL_ERR;
39 } 36 }
40 } 37 }
41 38
42 /* a = a + q */ 39 /* a = a + q */
43 if ((res = s_mp_add(a, &q, a)) != MP_OKAY) { 40 if ((res = s_mp_add(a, &q, a)) != MP_OKAY) {
44 goto ERR; 41 goto LBL_ERR;
45 } 42 }
46 43
47 if (mp_cmp_mag(a, n) != MP_LT) { 44 if (mp_cmp_mag(a, n) != MP_LT) {
48 if ((res = s_mp_sub(a, n, a)) != MP_OKAY) { 45 if ((res = s_mp_sub(a, n, a)) != MP_OKAY) {
49 goto ERR; 46 goto LBL_ERR;
50 } 47 }
51 goto top; 48 goto top;
52 } 49 }
53 50
54 ERR: 51 LBL_ERR:
55 mp_clear(&q); 52 mp_clear(&q);
56 return res; 53 return res;
57 } 54 }
58 55
59 #endif 56 #endif
60 57
61 /* ref: $Format:%D$ */ 58 /* ref: HEAD -> master, tag: v1.1.0 */
62 /* git commit: $Format:%H$ */ 59 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
63 /* commit time: $Format:%ai$ */ 60 /* commit time: 2019-01-28 20:32:32 +0100 */