comparison libtommath/bn_mp_radix_size.c @ 1739:13d834efc376 fuzz

merge from main
author Matt Johnston <matt@ucc.asn.au>
date Thu, 15 Oct 2020 19:55:15 +0800
parents 1051e4eea25a
children
comparison
equal deleted inserted replaced
1562:768ebf737aa0 1739:13d834efc376
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 /* 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 * The library is free for all purposes without any express
13 * guarantee it works.
14 *
15 * Tom St Denis, [email protected], http://libtom.org
16 */
17 5
18 /* returns size of ASCII reprensentation */ 6 /* returns size of ASCII representation */
19 int mp_radix_size (mp_int * a, int radix, int *size) 7 mp_err mp_radix_size(const mp_int *a, int radix, int *size)
20 { 8 {
21 int res, digs; 9 mp_err err;
22 mp_int t; 10 int digs;
23 mp_digit d; 11 mp_int t;
12 mp_digit d;
24 13
25 *size = 0; 14 *size = 0;
26 15
27 /* make sure the radix is in range */ 16 /* make sure the radix is in range */
28 if ((radix < 2) || (radix > 64)) { 17 if ((radix < 2) || (radix > 64)) {
29 return MP_VAL; 18 return MP_VAL;
30 } 19 }
31 20
32 if (mp_iszero(a) == MP_YES) { 21 if (MP_IS_ZERO(a)) {
33 *size = 2; 22 *size = 2;
34 return MP_OKAY; 23 return MP_OKAY;
35 } 24 }
36 25
37 /* special case for binary */ 26 /* special case for binary */
38 if (radix == 2) { 27 if (radix == 2) {
39 *size = mp_count_bits (a) + ((a->sign == MP_NEG) ? 1 : 0) + 1; 28 *size = (mp_count_bits(a) + ((a->sign == MP_NEG) ? 1 : 0) + 1);
40 return MP_OKAY; 29 return MP_OKAY;
41 } 30 }
42 31
43 /* digs is the digit count */ 32 /* digs is the digit count */
44 digs = 0; 33 digs = 0;
45 34
46 /* if it's negative add one for the sign */ 35 /* if it's negative add one for the sign */
47 if (a->sign == MP_NEG) { 36 if (a->sign == MP_NEG) {
48 ++digs; 37 ++digs;
49 } 38 }
50 39
51 /* init a copy of the input */ 40 /* init a copy of the input */
52 if ((res = mp_init_copy (&t, a)) != MP_OKAY) { 41 if ((err = mp_init_copy(&t, a)) != MP_OKAY) {
53 return res; 42 return err;
54 } 43 }
55 44
56 /* force temp to positive */ 45 /* force temp to positive */
57 t.sign = MP_ZPOS; 46 t.sign = MP_ZPOS;
58 47
59 /* fetch out all of the digits */ 48 /* fetch out all of the digits */
60 while (mp_iszero (&t) == MP_NO) { 49 while (!MP_IS_ZERO(&t)) {
61 if ((res = mp_div_d (&t, (mp_digit) radix, &t, &d)) != MP_OKAY) { 50 if ((err = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
62 mp_clear (&t); 51 goto LBL_ERR;
63 return res; 52 }
64 } 53 ++digs;
65 ++digs; 54 }
66 }
67 mp_clear (&t);
68 55
69 /* return digs + 1, the 1 is for the NULL byte that would be required. */ 56 /* return digs + 1, the 1 is for the NULL byte that would be required. */
70 *size = digs + 1; 57 *size = digs + 1;
71 return MP_OKAY; 58 err = MP_OKAY;
59
60 LBL_ERR:
61 mp_clear(&t);
62 return err;
72 } 63 }
73 64
74 #endif 65 #endif
75
76 /* ref: $Format:%D$ */
77 /* git commit: $Format:%H$ */
78 /* commit time: $Format:%ai$ */