Mercurial > dropbear
comparison libtommath/bn_mp_div_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_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 * |
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 /* shift right by a certain bit count (store quotient in c, optional remainder in d) */ | 15 /* 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) | 16 int mp_div_2d(const mp_int *a, int b, mp_int *c, mp_int *d) |
20 { | 17 { |
21 mp_digit D, r, rr; | 18 mp_digit D, r, rr; |
22 int x, res; | 19 int x, res; |
23 | 20 |
24 /* if the shift count is <= 0 then we do no work */ | 21 /* if the shift count is <= 0 then we do no work */ |
25 if (b <= 0) { | 22 if (b <= 0) { |
26 res = mp_copy (a, c); | 23 res = mp_copy(a, c); |
27 if (d != NULL) { | 24 if (d != NULL) { |
28 mp_zero (d); | 25 mp_zero(d); |
29 } | 26 } |
30 return res; | 27 return res; |
31 } | 28 } |
32 | 29 |
33 /* copy */ | 30 /* copy */ |
34 if ((res = mp_copy (a, c)) != MP_OKAY) { | 31 if ((res = mp_copy(a, c)) != MP_OKAY) { |
35 return res; | 32 return res; |
36 } | 33 } |
37 /* 'a' should not be used after here - it might be the same as d */ | 34 /* 'a' should not be used after here - it might be the same as d */ |
38 | 35 |
39 /* get the remainder */ | 36 /* get the remainder */ |
40 if (d != NULL) { | 37 if (d != NULL) { |
41 if ((res = mp_mod_2d (a, b, d)) != MP_OKAY) { | 38 if ((res = mp_mod_2d(a, b, d)) != MP_OKAY) { |
42 return res; | 39 return res; |
43 } | 40 } |
44 } | 41 } |
45 | 42 |
46 /* shift by as many digits in the bit count */ | 43 /* shift by as many digits in the bit count */ |
47 if (b >= (int)DIGIT_BIT) { | 44 if (b >= DIGIT_BIT) { |
48 mp_rshd (c, b / DIGIT_BIT); | 45 mp_rshd(c, b / DIGIT_BIT); |
49 } | 46 } |
50 | 47 |
51 /* shift any bit count < DIGIT_BIT */ | 48 /* shift any bit count < DIGIT_BIT */ |
52 D = (mp_digit) (b % DIGIT_BIT); | 49 D = (mp_digit)(b % DIGIT_BIT); |
53 if (D != 0) { | 50 if (D != 0u) { |
54 mp_digit *tmpc, mask, shift; | 51 mp_digit *tmpc, mask, shift; |
55 | 52 |
56 /* mask */ | 53 /* mask */ |
57 mask = (((mp_digit)1) << D) - 1; | 54 mask = ((mp_digit)1 << D) - 1uL; |
58 | 55 |
59 /* shift for lsb */ | 56 /* shift for lsb */ |
60 shift = DIGIT_BIT - D; | 57 shift = (mp_digit)DIGIT_BIT - D; |
61 | 58 |
62 /* alias */ | 59 /* alias */ |
63 tmpc = c->dp + (c->used - 1); | 60 tmpc = c->dp + (c->used - 1); |
64 | 61 |
65 /* carry */ | 62 /* carry */ |
66 r = 0; | 63 r = 0; |
67 for (x = c->used - 1; x >= 0; x--) { | 64 for (x = c->used - 1; x >= 0; x--) { |
68 /* get the lower bits of this word in a temp */ | 65 /* get the lower bits of this word in a temp */ |
69 rr = *tmpc & mask; | 66 rr = *tmpc & mask; |
70 | 67 |
71 /* shift the current word and mix in the carry bits from the previous word */ | 68 /* shift the current word and mix in the carry bits from the previous word */ |
72 *tmpc = (*tmpc >> D) | (r << shift); | 69 *tmpc = (*tmpc >> D) | (r << shift); |
73 --tmpc; | 70 --tmpc; |
74 | 71 |
75 /* set the carry to the carry bits of the current word found above */ | 72 /* set the carry to the carry bits of the current word found above */ |
76 r = rr; | 73 r = rr; |
77 } | 74 } |
78 } | 75 } |
79 mp_clamp (c); | 76 mp_clamp(c); |
80 return MP_OKAY; | 77 return MP_OKAY; |
81 } | 78 } |
82 #endif | 79 #endif |
83 | 80 |
84 /* ref: $Format:%D$ */ | 81 /* ref: HEAD -> master, tag: v1.1.0 */ |
85 /* git commit: $Format:%H$ */ | 82 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */ |
86 /* commit time: $Format:%ai$ */ | 83 /* commit time: 2019-01-28 20:32:32 +0100 */ |