comparison libtommath/bn_mp_cnt_lsb.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_CNT_LSB_C 2 #ifdef BN_MP_CNT_LSB_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 static const int lnz[16] = { 15 static const int lnz[16] = {
19 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 16 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
20 }; 17 };
21 18
22 /* Counts the number of lsbs which are zero before the first zero bit */ 19 /* Counts the number of lsbs which are zero before the first zero bit */
23 int mp_cnt_lsb(mp_int *a) 20 int mp_cnt_lsb(const mp_int *a)
24 { 21 {
25 int x; 22 int x;
26 mp_digit q, qq; 23 mp_digit q, qq;
27 24
28 /* easy out */ 25 /* easy out */
29 if (mp_iszero(a) == MP_YES) { 26 if (mp_iszero(a) == MP_YES) {
30 return 0; 27 return 0;
31 } 28 }
32 29
33 /* scan lower digits until non-zero */ 30 /* scan lower digits until non-zero */
34 for (x = 0; (x < a->used) && (a->dp[x] == 0); x++) {} 31 for (x = 0; (x < a->used) && (a->dp[x] == 0u); x++) {}
35 q = a->dp[x]; 32 q = a->dp[x];
36 x *= DIGIT_BIT; 33 x *= DIGIT_BIT;
37 34
38 /* now scan this digit until a 1 is found */ 35 /* now scan this digit until a 1 is found */
39 if ((q & 1) == 0) { 36 if ((q & 1u) == 0u) {
40 do { 37 do {
41 qq = q & 15; 38 qq = q & 15u;
42 x += lnz[qq]; 39 x += lnz[qq];
43 q >>= 4; 40 q >>= 4;
44 } while (qq == 0); 41 } while (qq == 0u);
45 } 42 }
46 return x; 43 return x;
47 } 44 }
48 45
49 #endif 46 #endif
50 47
51 /* ref: $Format:%D$ */ 48 /* ref: HEAD -> master, tag: v1.1.0 */
52 /* git commit: $Format:%H$ */ 49 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
53 /* commit time: $Format:%ai$ */ 50 /* commit time: 2019-01-28 20:32:32 +0100 */