LibCrypto: Add a method to count the number of digits in a big int

This commit is contained in:
Timothy Flynn
2026-02-21 08:46:06 -05:00
committed by Tim Flynn
parent 79d5fdc871
commit ecdaa7911f
Notes: github-actions[bot] 2026-02-22 14:40:33 +00:00
3 changed files with 41 additions and 0 deletions

View File

@@ -147,6 +147,20 @@ ErrorOr<String> UnsignedBigInteger::to_base(u16 N) const
return StringView(buffer.bytes().slice(0, written - 1)).to_ascii_lowercase_string();
}
size_t UnsignedBigInteger::count_digits_in_base(u16 base) const
{
VERIFY(base <= 36);
if (is_zero())
return 1;
int size = 0;
MP_MUST(mp_radix_size(&m_mp, base, &size));
// mp_radix_size includes a null byte.
return static_cast<size_t>(size) - 1;
}
u64 UnsignedBigInteger::to_u64() const
{
return mp_get_u64(&m_mp);