Mercurial > dropbear
comparison bn_mp_radix_size.c @ 2:86e0b50a9b58 libtommath-orig ltm-0.30-orig
ltm 0.30 orig import
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 31 May 2004 18:25:22 +0000 |
parents | |
children | d29b64170cf0 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 2:86e0b50a9b58 |
---|---|
1 /* LibTomMath, multiple-precision integer library -- Tom St Denis | |
2 * | |
3 * LibTomMath is a library that provides multiple-precision | |
4 * integer arithmetic as well as number theoretic functionality. | |
5 * | |
6 * The library was designed directly after the MPI library by | |
7 * Michael Fromberger but has been written from scratch with | |
8 * additional optimizations in place. | |
9 * | |
10 * The library is free for all purposes without any express | |
11 * guarantee it works. | |
12 * | |
13 * Tom St Denis, [email protected], http://math.libtomcrypt.org | |
14 */ | |
15 #include <tommath.h> | |
16 | |
17 /* returns size of ASCII reprensentation */ | |
18 int mp_radix_size (mp_int * a, int radix, int *size) | |
19 { | |
20 int res, digs; | |
21 mp_int t; | |
22 mp_digit d; | |
23 | |
24 *size = 0; | |
25 | |
26 /* special case for binary */ | |
27 if (radix == 2) { | |
28 *size = mp_count_bits (a) + (a->sign == MP_NEG ? 1 : 0) + 1; | |
29 return MP_OKAY; | |
30 } | |
31 | |
32 /* make sure the radix is in range */ | |
33 if (radix < 2 || radix > 64) { | |
34 return MP_VAL; | |
35 } | |
36 | |
37 /* init a copy of the input */ | |
38 if ((res = mp_init_copy (&t, a)) != MP_OKAY) { | |
39 return res; | |
40 } | |
41 | |
42 /* digs is the digit count */ | |
43 digs = 0; | |
44 | |
45 /* if it's negative add one for the sign */ | |
46 if (t.sign == MP_NEG) { | |
47 ++digs; | |
48 t.sign = MP_ZPOS; | |
49 } | |
50 | |
51 /* fetch out all of the digits */ | |
52 while (mp_iszero (&t) == 0) { | |
53 if ((res = mp_div_d (&t, (mp_digit) radix, &t, &d)) != MP_OKAY) { | |
54 mp_clear (&t); | |
55 return res; | |
56 } | |
57 ++digs; | |
58 } | |
59 mp_clear (&t); | |
60 | |
61 /* return digs + 1, the 1 is for the NULL byte that would be required. */ | |
62 *size = digs + 1; | |
63 return MP_OKAY; | |
64 } | |
65 |