comparison libtommath/bn_mp_div_2d.c @ 1733:d529a52b2f7c coverity coverity

merge coverity from main
author Matt Johnston <matt@ucc.asn.au>
date Fri, 26 Jun 2020 21:07:34 +0800
parents 1051e4eea25a
children
comparison
equal deleted inserted replaced
1643:b59623a64678 1733:d529a52b2f7c
1 #include <tommath_private.h> 1 #include "tommath_private.h"
2 #ifdef BN_MP_DIV_2D_C 2 #ifdef BN_MP_DIV_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 * The library is free for all purposes without any express
13 * guarantee it works.
14 *
15 * Tom St Denis, [email protected], http://libtom.org
16 */
17 5
18 /* shift right by a certain bit count (store quotient in c, optional remainder in d) */ 6 /* shift right by a certain bit count (store quotient in c, optional remainder in d) */
19 int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d) 7 mp_err mp_div_2d(const mp_int *a, int b, mp_int *c, mp_int *d)
20 { 8 {
21 mp_digit D, r, rr; 9 mp_digit D, r, rr;
22 int x, res; 10 int x;
11 mp_err err;
23 12
24 /* if the shift count is <= 0 then we do no work */ 13 /* if the shift count is <= 0 then we do no work */
25 if (b <= 0) { 14 if (b <= 0) {
26 res = mp_copy (a, c); 15 err = mp_copy(a, c);
27 if (d != NULL) { 16 if (d != NULL) {
28 mp_zero (d); 17 mp_zero(d);
29 } 18 }
30 return res; 19 return err;
31 } 20 }
32 21
33 /* copy */ 22 /* copy */
34 if ((res = mp_copy (a, c)) != MP_OKAY) { 23 if ((err = mp_copy(a, c)) != MP_OKAY) {
35 return res; 24 return err;
36 } 25 }
37 /* 'a' should not be used after here - it might be the same as d */ 26 /* 'a' should not be used after here - it might be the same as d */
38 27
39 /* get the remainder */ 28 /* get the remainder */
40 if (d != NULL) { 29 if (d != NULL) {
41 if ((res = mp_mod_2d (a, b, d)) != MP_OKAY) { 30 if ((err = mp_mod_2d(a, b, d)) != MP_OKAY) {
42 return res; 31 return err;
43 } 32 }
44 } 33 }
45 34
46 /* shift by as many digits in the bit count */ 35 /* shift by as many digits in the bit count */
47 if (b >= (int)DIGIT_BIT) { 36 if (b >= MP_DIGIT_BIT) {
48 mp_rshd (c, b / DIGIT_BIT); 37 mp_rshd(c, b / MP_DIGIT_BIT);
49 } 38 }
50 39
51 /* shift any bit count < DIGIT_BIT */ 40 /* shift any bit count < MP_DIGIT_BIT */
52 D = (mp_digit) (b % DIGIT_BIT); 41 D = (mp_digit)(b % MP_DIGIT_BIT);
53 if (D != 0) { 42 if (D != 0u) {
54 mp_digit *tmpc, mask, shift; 43 mp_digit *tmpc, mask, shift;
55 44
56 /* mask */ 45 /* mask */
57 mask = (((mp_digit)1) << D) - 1; 46 mask = ((mp_digit)1 << D) - 1uL;
58 47
59 /* shift for lsb */ 48 /* shift for lsb */
60 shift = DIGIT_BIT - D; 49 shift = (mp_digit)MP_DIGIT_BIT - D;
61 50
62 /* alias */ 51 /* alias */
63 tmpc = c->dp + (c->used - 1); 52 tmpc = c->dp + (c->used - 1);
64 53
65 /* carry */ 54 /* carry */
66 r = 0; 55 r = 0;
67 for (x = c->used - 1; x >= 0; x--) { 56 for (x = c->used - 1; x >= 0; x--) {
68 /* get the lower bits of this word in a temp */ 57 /* get the lower bits of this word in a temp */
69 rr = *tmpc & mask; 58 rr = *tmpc & mask;
70 59
71 /* shift the current word and mix in the carry bits from the previous word */ 60 /* shift the current word and mix in the carry bits from the previous word */
72 *tmpc = (*tmpc >> D) | (r << shift); 61 *tmpc = (*tmpc >> D) | (r << shift);
73 --tmpc; 62 --tmpc;
74 63
75 /* set the carry to the carry bits of the current word found above */ 64 /* set the carry to the carry bits of the current word found above */
76 r = rr; 65 r = rr;
77 } 66 }
78 } 67 }
79 mp_clamp (c); 68 mp_clamp(c);
80 return MP_OKAY; 69 return MP_OKAY;
81 } 70 }
82 #endif 71 #endif
83
84 /* ref: $Format:%D$ */
85 /* git commit: $Format:%H$ */
86 /* commit time: $Format:%ai$ */