comparison libtommath/bn_mp_toradix.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
comparison
equal deleted inserted replaced
1654:cc0fc5131c5c 1655:f52919ffd3b1
1 #include <tommath_private.h> 1 #include "tommath_private.h"
2 #ifdef BN_MP_TORADIX_C 2 #ifdef BN_MP_TORADIX_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 /* stores a bignum as a ASCII string in a given radix (2..64) */ 15 /* stores a bignum as a ASCII string in a given radix (2..64) */
19 int mp_toradix (mp_int * a, char *str, int radix) 16 int mp_toradix(const mp_int *a, char *str, int radix)
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 char *_s = str; 21 char *_s = str;
25 22
26 /* check range of the radix */ 23 /* check range of the radix */
27 if ((radix < 2) || (radix > 64)) { 24 if ((radix < 2) || (radix > 64)) {
28 return MP_VAL; 25 return MP_VAL;
29 } 26 }
30 27
31 /* quick out if its zero */ 28 /* quick out if its zero */
32 if (mp_iszero(a) == MP_YES) { 29 if (mp_iszero(a) == MP_YES) {
33 *str++ = '0'; 30 *str++ = '0';
34 *str = '\0'; 31 *str = '\0';
35 return MP_OKAY; 32 return MP_OKAY;
36 } 33 }
37 34
38 if ((res = mp_init_copy (&t, a)) != MP_OKAY) { 35 if ((res = mp_init_copy(&t, a)) != MP_OKAY) {
39 return res; 36 return res;
40 } 37 }
41 38
42 /* if it is negative output a - */ 39 /* if it is negative output a - */
43 if (t.sign == MP_NEG) { 40 if (t.sign == MP_NEG) {
44 ++_s; 41 ++_s;
45 *str++ = '-'; 42 *str++ = '-';
46 t.sign = MP_ZPOS; 43 t.sign = MP_ZPOS;
47 } 44 }
48 45
49 digs = 0; 46 digs = 0;
50 while (mp_iszero (&t) == MP_NO) { 47 while (mp_iszero(&t) == MP_NO) {
51 if ((res = mp_div_d (&t, (mp_digit) radix, &t, &d)) != MP_OKAY) { 48 if ((res = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
52 mp_clear (&t); 49 mp_clear(&t);
53 return res; 50 return res;
54 } 51 }
55 *str++ = mp_s_rmap[d]; 52 *str++ = mp_s_rmap[d];
56 ++digs; 53 ++digs;
57 } 54 }
58 55
59 /* reverse the digits of the string. In this case _s points 56 /* reverse the digits of the string. In this case _s points
60 * to the first digit [exluding the sign] of the number] 57 * to the first digit [exluding the sign] of the number]
61 */ 58 */
62 bn_reverse ((unsigned char *)_s, digs); 59 bn_reverse((unsigned char *)_s, digs);
63 60
64 /* append a NULL so the string is properly terminated */ 61 /* append a NULL so the string is properly terminated */
65 *str = '\0'; 62 *str = '\0';
66 63
67 mp_clear (&t); 64 mp_clear(&t);
68 return MP_OKAY; 65 return MP_OKAY;
69 } 66 }
70 67
71 #endif 68 #endif
72 69
73 /* ref: $Format:%D$ */ 70 /* ref: HEAD -> master, tag: v1.1.0 */
74 /* git commit: $Format:%H$ */ 71 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
75 /* commit time: $Format:%ai$ */ 72 /* commit time: 2019-01-28 20:32:32 +0100 */