Mercurial > dropbear
comparison libtommath/bn_mp_div_2.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_DIV_2_C | 2 #ifdef BN_MP_DIV_2_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 /* b = a/2 */ | 6 /* b = a/2 */ |
16 int mp_div_2(const mp_int *a, mp_int *b) | 7 mp_err mp_div_2(const mp_int *a, mp_int *b) |
17 { | 8 { |
18 int x, res, oldused; | 9 int x, oldused; |
10 mp_digit r, rr, *tmpa, *tmpb; | |
11 mp_err err; | |
19 | 12 |
20 /* copy */ | 13 /* copy */ |
21 if (b->alloc < a->used) { | 14 if (b->alloc < a->used) { |
22 if ((res = mp_grow(b, a->used)) != MP_OKAY) { | 15 if ((err = mp_grow(b, a->used)) != MP_OKAY) { |
23 return res; | 16 return err; |
24 } | 17 } |
25 } | 18 } |
26 | 19 |
27 oldused = b->used; | 20 oldused = b->used; |
28 b->used = a->used; | 21 b->used = a->used; |
29 { | |
30 mp_digit r, rr, *tmpa, *tmpb; | |
31 | 22 |
32 /* source alias */ | 23 /* source alias */ |
33 tmpa = a->dp + b->used - 1; | 24 tmpa = a->dp + b->used - 1; |
34 | 25 |
35 /* dest alias */ | 26 /* dest alias */ |
36 tmpb = b->dp + b->used - 1; | 27 tmpb = b->dp + b->used - 1; |
37 | 28 |
38 /* carry */ | 29 /* carry */ |
39 r = 0; | 30 r = 0; |
40 for (x = b->used - 1; x >= 0; x--) { | 31 for (x = b->used - 1; x >= 0; x--) { |
41 /* get the carry for the next iteration */ | 32 /* get the carry for the next iteration */ |
42 rr = *tmpa & 1u; | 33 rr = *tmpa & 1u; |
43 | 34 |
44 /* shift the current digit, add in carry and store */ | 35 /* shift the current digit, add in carry and store */ |
45 *tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1)); | 36 *tmpb-- = (*tmpa-- >> 1) | (r << (MP_DIGIT_BIT - 1)); |
46 | 37 |
47 /* forward carry to next iteration */ | 38 /* forward carry to next iteration */ |
48 r = rr; | 39 r = rr; |
49 } | 40 } |
50 | 41 |
51 /* zero excess digits */ | 42 /* zero excess digits */ |
52 tmpb = b->dp + b->used; | 43 MP_ZERO_DIGITS(b->dp + b->used, oldused - b->used); |
53 for (x = b->used; x < oldused; x++) { | 44 |
54 *tmpb++ = 0; | |
55 } | |
56 } | |
57 b->sign = a->sign; | 45 b->sign = a->sign; |
58 mp_clamp(b); | 46 mp_clamp(b); |
59 return MP_OKAY; | 47 return MP_OKAY; |
60 } | 48 } |
61 #endif | 49 #endif |
62 | |
63 /* ref: HEAD -> master, tag: v1.1.0 */ | |
64 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */ | |
65 /* commit time: 2019-01-28 20:32:32 +0100 */ |