• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 20209445534

14 Dec 2025 02:31PM UTC coverage: 90.361% (+0.004%) from 90.357%
20209445534

push

github

web-flow
Merge pull request #5176 from randombit/jack/div10

Add a helper for integer division by 10

100959 of 111728 relevant lines covered (90.36%)

12794836.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.05
/src/lib/math/bigint/big_code.cpp
1
/*
2
* BigInt Encoding/Decoding
3
* (C) 1999-2010,2012,2019,2021 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/bigint.h>
9

10
#include <botan/hex.h>
11
#include <botan/mem_ops.h>
12
#include <botan/internal/divide.h>
13
#include <botan/internal/mp_core.h>
14
#include <botan/internal/stl_util.h>
15

16
namespace Botan {
17

18
namespace {
19

20
consteval word decimal_conversion_radix() {
21
   if constexpr(sizeof(word) == 8) {
22
      return 10000000000000000000U;
23
   } else {
24
      return 1000000000U;
25
   }
26
}
27

28
consteval size_t decimal_conversion_radix_digits() {
29
   if constexpr(sizeof(word) == 8) {
30
      return 19;
31
   } else {
32
      return 9;
33
   }
34
}
35

36
}  // namespace
37

38
std::string BigInt::to_dec_string() const {
60✔
39
   // Use the largest power of 10 that fits in a word
40
   constexpr word conversion_radix = decimal_conversion_radix();
60✔
41
   constexpr size_t radix_digits = decimal_conversion_radix_digits();
60✔
42

43
   // (over-)estimate of the number of digits needed; log2(10) ~ 3.3219
44
   const size_t digit_estimate = static_cast<size_t>(1 + (static_cast<double>(this->bits()) / 3.32));
60✔
45

46
   // (over-)estimate of db such that conversion_radix^db > *this
47
   const size_t digit_blocks = (digit_estimate + radix_digits - 1) / radix_digits;
60✔
48

49
   BigInt value = *this;
60✔
50
   value.set_sign(Positive);
60✔
51

52
   // Extract groups of digits into words
53
   std::vector<word> digit_groups(digit_blocks);
60✔
54

55
   for(size_t i = 0; i != digit_blocks; ++i) {
132✔
56
      word remainder = 0;
72✔
57
      ct_divide_word(value, conversion_radix, value, remainder);
72✔
58
      digit_groups[i] = remainder;
72✔
59
   }
60

61
   BOTAN_ASSERT_NOMSG(value.is_zero());
120✔
62

63
   // Extract digits from the groups
64
   std::vector<uint8_t> digits(digit_blocks * radix_digits);
60✔
65

66
   for(size_t i = 0; i != digit_blocks; ++i) {
132✔
67
      word remainder = digit_groups[i];
72✔
68
      for(size_t j = 0; j != radix_digits; ++j) {
1,440✔
69
         const word new_remainder = divide_10(remainder);
1,368✔
70
         const word digit = remainder - new_remainder * 10;
1,368✔
71
         digits[radix_digits * i + j] = static_cast<uint8_t>(digit);
1,368✔
72
         remainder = new_remainder;
1,368✔
73
      }
74
   }
75

76
   // remove leading zeros
77
   while(!digits.empty() && digits.back() == 0) {
960✔
78
      digits.pop_back();
900✔
79
   }
80

81
   BOTAN_ASSERT_NOMSG(digit_estimate >= digits.size());
60✔
82

83
   // Reverse the digits to big-endian and format to text
84
   std::string s;
60✔
85
   s.reserve(1 + digits.size());
60✔
86

87
   if(is_negative()) {
60✔
88
      s += "-";
5✔
89
   }
90

91
   // Reverse and convert to textual digits
92
   // TODO(Botan4) use std::ranges::reverse_view here once available (need newer Clang)
93
   // NOLINTNEXTLINE(modernize-loop-convert)
94
   for(auto i = digits.rbegin(); i != digits.rend(); ++i) {
528✔
95
      s.push_back(*i + '0');  // assumes ASCII
468✔
96
   }
97

98
   if(s.empty()) {
60✔
99
      s += "0";
6✔
100
   }
101

102
   return s;
120✔
103
}
120✔
104

105
std::string BigInt::to_hex_string() const {
85✔
106
   const size_t this_bytes = this->bytes();
85✔
107
   std::vector<uint8_t> bits(std::max<size_t>(1, this_bytes));
143✔
108

109
   if(this_bytes > 0) {
85✔
110
      this->serialize_to(bits);
83✔
111
   }
112

113
   std::string hrep;
85✔
114
   if(is_negative()) {
85✔
115
      hrep += "-";
5✔
116
   }
117
   hrep += "0x";
85✔
118
   hrep += hex_encode(bits);
170✔
119
   return hrep;
85✔
120
}
85✔
121

122
/*
123
* Encode two BigInt, with leading 0s if needed, and concatenate
124
*/
125
secure_vector<uint8_t> BigInt::encode_fixed_length_int_pair(const BigInt& n1, const BigInt& n2, size_t bytes) {
97✔
126
   if(n1.is_negative() || n2.is_negative()) {
97✔
127
      throw Encoding_Error("encode_fixed_length_int_pair: values must be positive");
×
128
   }
129
   if(n1.bytes() > bytes || n2.bytes() > bytes) {
97✔
130
      throw Encoding_Error("encode_fixed_length_int_pair: values too large to encode properly");
×
131
   }
132
   secure_vector<uint8_t> output(2 * bytes);
97✔
133
   BufferStuffer stuffer(output);
97✔
134
   n1.serialize_to(stuffer.next(bytes));
97✔
135
   n2.serialize_to(stuffer.next(bytes));
97✔
136
   return output;
97✔
137
}
×
138

139
BigInt BigInt::decode(std::span<const uint8_t> buf, Base base) {
46,674✔
140
   if(base == Binary) {
46,674✔
141
      return BigInt::from_bytes(buf);
×
142
   }
143
   return BigInt::decode(buf.data(), buf.size(), base);
46,674✔
144
}
145

146
/*
147
* Decode a BigInt
148
*/
149
BigInt BigInt::decode(const uint8_t buf[], size_t length, Base base) {
46,674✔
150
   if(base == Binary) {
46,674✔
151
      return BigInt::from_bytes(std::span{buf, length});
×
152
   } else if(base == Hexadecimal) {
46,674✔
153
      BigInt r;
42,407✔
154
      secure_vector<uint8_t> binary;
42,407✔
155

156
      if(length % 2 == 1) {
42,407✔
157
         // Handle lack of leading 0
158
         const char buf0_with_leading_0[2] = {'0', static_cast<char>(buf[0])};
4,236✔
159

160
         binary = hex_decode_locked(buf0_with_leading_0, 2);
8,472✔
161

162
         if(length > 1) {
4,236✔
163
            binary += hex_decode_locked(cast_uint8_ptr_to_char(&buf[1]), length - 1, false);
6,100✔
164
         }
165
      } else {
166
         binary = hex_decode_locked(cast_uint8_ptr_to_char(buf), length, false);
76,342✔
167
      }
168

169
      r.assign_from_bytes(binary);
42,407✔
170
      return r;
42,407✔
171
   } else if(base == Decimal) {
46,674✔
172
      BigInt r;
4,267✔
173
      // This could be made faster using the same trick as to_dec_string
174
      for(size_t i = 0; i != length; ++i) {
382,960✔
175
         const char c = buf[i];
378,693✔
176

177
         if(c < '0' || c > '9') {
378,693✔
178
            throw Invalid_Argument("BigInt::decode: invalid decimal char");
×
179
         }
180

181
         const uint8_t x = c - '0';
378,693✔
182
         BOTAN_ASSERT_NOMSG(x < 10);
378,693✔
183

184
         r *= 10;
378,693✔
185
         r += x;
378,693✔
186
      }
187
      return r;
4,267✔
188
   } else {
4,267✔
189
      throw Invalid_Argument("Unknown BigInt decoding method");
×
190
   }
191
}
192

193
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc