1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-03 12:00:59 +00:00

bignum: Add bn_digitcount

This commit is contained in:
Saleem Rashid 2017-07-26 11:32:48 +01:00 committed by Pavol Rusnak
parent dfdb4d2d76
commit 558bc429ec
2 changed files with 42 additions and 0 deletions

View File

@ -81,6 +81,17 @@ static inline void bn_copy(const bignum256 *a, bignum256 *b) {
int bn_bitcount(const bignum256 *a);
static inline int bn_digitcount(const bignum256 *a)
{
int bitcount = bn_bitcount(a);
if (bitcount == 256) {
return 78;
} else {
return bitcount * 78 / 256 + 1;
}
}
void bn_zero(bignum256 *a);
int bn_is_zero(const bignum256 *a);

View File

@ -350,6 +350,36 @@ START_TEST(test_bignum_bitcount)
}
END_TEST
START_TEST(test_bignum_digitcount)
{
bignum256 a;
bn_zero(&a);
ck_assert_int_eq(bn_digitcount(&a), 1);
bn_one(&a);
ck_assert_int_eq(bn_digitcount(&a), 1);
bn_read_uint32(10, &a);
ck_assert_int_eq(bn_digitcount(&a), 2);
bn_read_uint32(11, &a);
ck_assert_int_eq(bn_digitcount(&a), 2);
bn_read_uint32(100, &a);
ck_assert_int_eq(bn_digitcount(&a), 3);
bn_read_uint32(0x3fffffff, &a);
ck_assert_int_eq(bn_digitcount(&a), 10);
bn_read_uint32(0xffffffff, &a);
ck_assert_int_eq(bn_digitcount(&a), 10);
bn_read_be(fromhex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), &a);
ck_assert_int_eq(bn_digitcount(&a), 78);
}
END_TEST
START_TEST(test_bignum_is_less)
{
bignum256 a;
@ -3193,6 +3223,7 @@ Suite *test_suite(void)
tcase_add_test(tc, test_bignum_is_even);
tcase_add_test(tc, test_bignum_is_odd);
tcase_add_test(tc, test_bignum_bitcount);
tcase_add_test(tc, test_bignum_digitcount);
tcase_add_test(tc, test_bignum_is_less);
tcase_add_test(tc, test_bignum_format);
suite_add_tcase(s, tc);