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