Skip to content

Commit 7498196

Browse files
committed
added shortcuts for mp_radix_size and fixed mp_radix_sizeinbase for negative input
1 parent 6a8e45f commit 7498196

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

bn_mp_radix_size.c

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mp_err mp_radix_size(const mp_int *a, int radix, int *size)
88
{
99
mp_err err;
1010
mp_int a_, b;
11-
11+
int rem, bit_count, ln2;
1212

1313
*size = 0;
1414

@@ -22,12 +22,41 @@ mp_err mp_radix_size(const mp_int *a, int radix, int *size)
2222
return MP_OKAY;
2323
}
2424

25-
/* special case for binary */
26-
if (radix == 2) {
27-
*size = mp_count_bits(a) + ((a->sign == MP_NEG) ? 1 : 0) + 1;
25+
/* A small shortcut for powers of two. */
26+
switch (radix) {
27+
case 4:
28+
ln2 = 2;
29+
break;
30+
case 8:
31+
ln2 = 3;
32+
break;
33+
case 16:
34+
ln2 = 4;
35+
break;
36+
case 32:
37+
ln2 = 5;
38+
break;
39+
case 64:
40+
ln2 = 6;
41+
break;
42+
default:
43+
ln2 = 0;
44+
break;
45+
}
46+
if (ln2 != 0) {
47+
bit_count = mp_count_bits(a) + 1;
48+
*size = bit_count/ln2;
49+
rem = bit_count - (*size) * ln2;
50+
/* Add 1 for the remainder if any and 1 for "\0". */
51+
*size += (rem == 0) ? 1 : 2;
52+
/* And one extra character for the minus sign */
53+
if (a->sign == MP_NEG) {
54+
(*size)++;
55+
}
2856
return MP_OKAY;
2957
}
3058

59+
3160
if ((err = mp_init(&b)) != MP_OKAY) {
3261
goto LBL_ERR;
3362
}
@@ -43,7 +72,7 @@ mp_err mp_radix_size(const mp_int *a, int radix, int *size)
4372
/* mp_ilogb truncates to zero, hence we need one extra put on top. */
4473
(*size)++;
4574

46-
/* if it's negative add one for the sign */
75+
/* The sign needs to have a place to live, too*/
4776
if (a->sign == MP_NEG) {
4877
(*size)++;
4978
}

bn_mp_radix_sizeinbase.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ mp_err mp_radix_sizeinbase(const mp_int *a, const int base, int *size)
203203
#if ( (defined MP_8BIT) || (defined MP_16BIT) )
204204
mp_int bi_k_bis;
205205
#endif
206-
int ln2 = 0, rem, bit_count;
206+
int ln2, rem, bit_count;
207207
mp_err e = MP_OKAY;
208208

209209
if ((base < 2) || (base > 64)) {
@@ -240,13 +240,18 @@ mp_err mp_radix_sizeinbase(const mp_int *a, const int base, int *size)
240240
ln2 = 6;
241241
break;
242242
default:
243+
ln2 = 0;
243244
break;
244245
}
245246
if (ln2 != 0) {
246247
*size = bit_count/ln2;
247248
rem = bit_count - (*size) * ln2;
248249
/* Add 1 for the remainder if any and 1 for "\0". */
249250
*size += (rem == 0) ? 1 : 2;
251+
/* And one extra character for the minus sign */
252+
if (a->sign == MP_NEG) {
253+
(*size)++;
254+
}
250255
return MP_OKAY;
251256
}
252257

@@ -305,6 +310,11 @@ mp_err mp_radix_sizeinbase(const mp_int *a, const int base, int *size)
305310
*size += 2;
306311
#endif
307312

313+
/* And before we forget it: one extra character for the minus sign */
314+
if (a->sign == MP_NEG) {
315+
(*size)++;
316+
}
317+
308318
LTM_E1:
309319
mp_clear_multi(&bi_bit_count, &bi_k, NULL);
310320
#if ( (defined MP_8BIT) || (defined MP_16BIT) )

0 commit comments

Comments
 (0)