8#include <botan/bigint.h>
11#include <botan/mem_ops.h>
12#include <botan/internal/divide.h>
13#include <botan/internal/stl_util.h>
19consteval word decimal_conversion_radix() {
20 if constexpr(
sizeof(
word) == 8) {
21 return 10000000000000000000U;
27consteval size_t decimal_conversion_radix_digits() {
28 if constexpr(
sizeof(
word) == 8) {
39 constexpr word conversion_radix = decimal_conversion_radix();
40 constexpr size_t radix_digits = decimal_conversion_radix_digits();
43 const size_t digit_estimate =
static_cast<size_t>(1 + (
static_cast<double>(this->
bits()) / 3.32));
46 const size_t digit_blocks = (digit_estimate + radix_digits - 1) / radix_digits;
52 std::vector<word> digit_groups(digit_blocks);
54 for(
size_t i = 0; i != digit_blocks; ++i) {
57 digit_groups[i] = remainder;
63 std::vector<uint8_t> digits(digit_blocks * radix_digits);
65 for(
size_t i = 0; i != digit_blocks; ++i) {
66 word remainder = digit_groups[i];
67 for(
size_t j = 0; j != radix_digits; ++j) {
69 const word digit = remainder % 10;
71 digits[radix_digits * i + j] =
static_cast<uint8_t
>(digit);
76 while(!digits.empty() && digits.back() == 0) {
84 s.reserve(1 + digits.size());
93 for(
auto i = digits.rbegin(); i != digits.rend(); ++i) {
94 s.push_back(*i +
'0');
105 const size_t this_bytes = this->
bytes();
106 std::vector<uint8_t>
bits(std::max<size_t>(1, this_bytes));
126 throw Encoding_Error(
"encode_fixed_length_int_pair: values must be positive");
129 throw Encoding_Error(
"encode_fixed_length_int_pair: values too large to encode properly");
155 if(length % 2 == 1) {
157 const char buf0_with_leading_0[2] = {
'0',
static_cast<char>(buf[0])};
168 r.assign_from_bytes(binary);
173 for(
size_t i = 0; i != length; ++i) {
174 const char c = buf[i];
176 if(c <
'0' || c >
'9') {
180 const uint8_t x = c -
'0';
#define BOTAN_ASSERT_NOMSG(expr)
static BigInt decode(const uint8_t buf[], size_t length)
std::string to_dec_string() const
std::string to_hex_string() const
static secure_vector< uint8_t > encode_fixed_length_int_pair(const BigInt &n1, const BigInt &n2, size_t bytes)
void serialize_to(std::span< uint8_t > out) const
static BigInt from_bytes(std::span< const uint8_t > bytes)
Helper class to ease in-place marshalling of concatenated fixed-length values.
constexpr std::span< uint8_t > next(size_t bytes)
secure_vector< uint8_t > hex_decode_locked(const char input[], size_t input_length, bool ignore_ws)
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
void ct_divide_word(const BigInt &x, word y, BigInt &q_out, word &r_out)
const char * cast_uint8_ptr_to_char(const uint8_t *b)
std::vector< T, secure_allocator< T > > secure_vector
std::conditional_t< HasNative64BitRegisters, std::uint64_t, uint32_t > word