comparison libtommath/bn_mp_radix_size.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_RADIX_SIZE_C 2 #ifdef BN_MP_RADIX_SIZE_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 /* returns size of ASCII reprensentation */ 15 /* returns size of ASCII reprensentation */
19 int mp_radix_size (mp_int * a, int radix, int *size) 16 int mp_radix_size(const mp_int *a, int radix, int *size)
20 { 17 {
21 int res, digs; 18 int res, digs;
22 mp_int t; 19 mp_int t;
23 mp_digit d; 20 mp_digit d;
24 21
25 *size = 0; 22 *size = 0;
26 23
27 /* make sure the radix is in range */ 24 /* make sure the radix is in range */
28 if ((radix < 2) || (radix > 64)) { 25 if ((radix < 2) || (radix > 64)) {
29 return MP_VAL; 26 return MP_VAL;
30 } 27 }
31 28
32 if (mp_iszero(a) == MP_YES) { 29 if (mp_iszero(a) == MP_YES) {
33 *size = 2; 30 *size = 2;
34 return MP_OKAY; 31 return MP_OKAY;
35 } 32 }
36 33
37 /* special case for binary */ 34 /* special case for binary */
38 if (radix == 2) { 35 if (radix == 2) {
39 *size = mp_count_bits (a) + ((a->sign == MP_NEG) ? 1 : 0) + 1; 36 *size = mp_count_bits(a) + ((a->sign == MP_NEG) ? 1 : 0) + 1;
40 return MP_OKAY; 37 return MP_OKAY;
41 } 38 }
42 39
43 /* digs is the digit count */ 40 /* digs is the digit count */
44 digs = 0; 41 digs = 0;
45 42
46 /* if it's negative add one for the sign */ 43 /* if it's negative add one for the sign */
47 if (a->sign == MP_NEG) { 44 if (a->sign == MP_NEG) {
48 ++digs; 45 ++digs;
49 } 46 }
50 47
51 /* init a copy of the input */ 48 /* init a copy of the input */
52 if ((res = mp_init_copy (&t, a)) != MP_OKAY) { 49 if ((res = mp_init_copy(&t, a)) != MP_OKAY) {
53 return res; 50 return res;
54 } 51 }
55 52
56 /* force temp to positive */ 53 /* force temp to positive */
57 t.sign = MP_ZPOS; 54 t.sign = MP_ZPOS;
58 55
59 /* fetch out all of the digits */ 56 /* fetch out all of the digits */
60 while (mp_iszero (&t) == MP_NO) { 57 while (mp_iszero(&t) == MP_NO) {
61 if ((res = mp_div_d (&t, (mp_digit) radix, &t, &d)) != MP_OKAY) { 58 if ((res = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
62 mp_clear (&t); 59 mp_clear(&t);
63 return res; 60 return res;
64 } 61 }
65 ++digs; 62 ++digs;
66 } 63 }
67 mp_clear (&t); 64 mp_clear(&t);
68 65
69 /* return digs + 1, the 1 is for the NULL byte that would be required. */ 66 /* return digs + 1, the 1 is for the NULL byte that would be required. */
70 *size = digs + 1; 67 *size = digs + 1;
71 return MP_OKAY; 68 return MP_OKAY;
72 } 69 }
73 70
74 #endif 71 #endif
75 72
76 /* ref: $Format:%D$ */ 73 /* ref: HEAD -> master, tag: v1.1.0 */
77 /* git commit: $Format:%H$ */ 74 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
78 /* commit time: $Format:%ai$ */ 75 /* commit time: 2019-01-28 20:32:32 +0100 */