comparison libtommath/bn_mp_mod_2d.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_MOD_2D_C 2 #ifdef BN_MP_MOD_2D_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 /* calc a value mod 2**b */ 15 /* calc a value mod 2**b */
19 int 16 int mp_mod_2d(const mp_int *a, int b, mp_int *c)
20 mp_mod_2d (mp_int * a, int b, mp_int * c)
21 { 17 {
22 int x, res; 18 int x, res;
23 19
24 /* if b is <= 0 then zero the int */ 20 /* if b is <= 0 then zero the int */
25 if (b <= 0) { 21 if (b <= 0) {
26 mp_zero (c); 22 mp_zero(c);
27 return MP_OKAY; 23 return MP_OKAY;
28 } 24 }
29 25
30 /* if the modulus is larger than the value than return */ 26 /* if the modulus is larger than the value than return */
31 if (b >= (int) (a->used * DIGIT_BIT)) { 27 if (b >= (a->used * DIGIT_BIT)) {
32 res = mp_copy (a, c); 28 res = mp_copy(a, c);
33 return res; 29 return res;
34 } 30 }
35 31
36 /* copy */ 32 /* copy */
37 if ((res = mp_copy (a, c)) != MP_OKAY) { 33 if ((res = mp_copy(a, c)) != MP_OKAY) {
38 return res; 34 return res;
39 } 35 }
40 36
41 /* zero digits above the last digit of the modulus */ 37 /* zero digits above the last digit of the modulus */
42 for (x = (b / DIGIT_BIT) + (((b % DIGIT_BIT) == 0) ? 0 : 1); x < c->used; x++) { 38 for (x = (b / DIGIT_BIT) + (((b % DIGIT_BIT) == 0) ? 0 : 1); x < c->used; x++) {
43 c->dp[x] = 0; 39 c->dp[x] = 0;
44 } 40 }
45 /* clear the digit that is not completely outside/inside the modulus */ 41 /* clear the digit that is not completely outside/inside the modulus */
46 c->dp[b / DIGIT_BIT] &= 42 c->dp[b / DIGIT_BIT] &=
47 (mp_digit) ((((mp_digit) 1) << (((mp_digit) b) % DIGIT_BIT)) - ((mp_digit) 1)); 43 ((mp_digit)1 << (mp_digit)(b % DIGIT_BIT)) - (mp_digit)1;
48 mp_clamp (c); 44 mp_clamp(c);
49 return MP_OKAY; 45 return MP_OKAY;
50 } 46 }
51 #endif 47 #endif
52 48
53 /* ref: $Format:%D$ */ 49 /* ref: HEAD -> master, tag: v1.1.0 */
54 /* git commit: $Format:%H$ */ 50 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
55 /* commit time: $Format:%ai$ */ 51 /* commit time: 2019-01-28 20:32:32 +0100 */