comparison libtommath/bn_mp_div_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_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 * SPDX-License-Identifier: Unlicense
13 */
14 5
15 /* 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) */
16 int mp_div_2d(const 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)
17 { 8 {
18 mp_digit D, r, rr; 9 mp_digit D, r, rr;
19 int x, res; 10 int x;
11 mp_err err;
20 12
21 /* if the shift count is <= 0 then we do no work */ 13 /* if the shift count is <= 0 then we do no work */
22 if (b <= 0) { 14 if (b <= 0) {
23 res = mp_copy(a, c); 15 err = mp_copy(a, c);
24 if (d != NULL) { 16 if (d != NULL) {
25 mp_zero(d); 17 mp_zero(d);
26 } 18 }
27 return res; 19 return err;
28 } 20 }
29 21
30 /* copy */ 22 /* copy */
31 if ((res = mp_copy(a, c)) != MP_OKAY) { 23 if ((err = mp_copy(a, c)) != MP_OKAY) {
32 return res; 24 return err;
33 } 25 }
34 /* '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 */
35 27
36 /* get the remainder */ 28 /* get the remainder */
37 if (d != NULL) { 29 if (d != NULL) {
38 if ((res = mp_mod_2d(a, b, d)) != MP_OKAY) { 30 if ((err = mp_mod_2d(a, b, d)) != MP_OKAY) {
39 return res; 31 return err;
40 } 32 }
41 } 33 }
42 34
43 /* shift by as many digits in the bit count */ 35 /* shift by as many digits in the bit count */
44 if (b >= DIGIT_BIT) { 36 if (b >= MP_DIGIT_BIT) {
45 mp_rshd(c, b / DIGIT_BIT); 37 mp_rshd(c, b / MP_DIGIT_BIT);
46 } 38 }
47 39
48 /* shift any bit count < DIGIT_BIT */ 40 /* shift any bit count < MP_DIGIT_BIT */
49 D = (mp_digit)(b % DIGIT_BIT); 41 D = (mp_digit)(b % MP_DIGIT_BIT);
50 if (D != 0u) { 42 if (D != 0u) {
51 mp_digit *tmpc, mask, shift; 43 mp_digit *tmpc, mask, shift;
52 44
53 /* mask */ 45 /* mask */
54 mask = ((mp_digit)1 << D) - 1uL; 46 mask = ((mp_digit)1 << D) - 1uL;
55 47
56 /* shift for lsb */ 48 /* shift for lsb */
57 shift = (mp_digit)DIGIT_BIT - D; 49 shift = (mp_digit)MP_DIGIT_BIT - D;
58 50
59 /* alias */ 51 /* alias */
60 tmpc = c->dp + (c->used - 1); 52 tmpc = c->dp + (c->used - 1);
61 53
62 /* carry */ 54 /* carry */
75 } 67 }
76 mp_clamp(c); 68 mp_clamp(c);
77 return MP_OKAY; 69 return MP_OKAY;
78 } 70 }
79 #endif 71 #endif
80
81 /* ref: HEAD -> master, tag: v1.1.0 */
82 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
83 /* commit time: 2019-01-28 20:32:32 +0100 */