/src/botan/src/lib/math/bigint/big_code.cpp
Line | Count | Source |
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/stl_util.h> |
14 | | |
15 | | namespace Botan { |
16 | | |
17 | | namespace { |
18 | | |
19 | | consteval word decimal_conversion_radix() { |
20 | | if constexpr(sizeof(word) == 8) { |
21 | | return 10000000000000000000U; |
22 | | } else { |
23 | | return 1000000000U; |
24 | | } |
25 | | } |
26 | | |
27 | | consteval size_t decimal_conversion_radix_digits() { |
28 | | if constexpr(sizeof(word) == 8) { |
29 | | return 19; |
30 | | } else { |
31 | | return 9; |
32 | | } |
33 | | } |
34 | | |
35 | | } // namespace |
36 | | |
37 | 3.63k | std::string BigInt::to_dec_string() const { |
38 | 3.63k | constexpr word conversion_radix = decimal_conversion_radix(); |
39 | 3.63k | constexpr size_t radix_digits = decimal_conversion_radix_digits(); |
40 | | // Use the largest power of 10 that fits in a word |
41 | | |
42 | | // (over-)estimate of the number of digits needed; log2(10) ~ 3.3219 |
43 | 3.63k | const size_t digit_estimate = static_cast<size_t>(1 + (this->bits() / 3.32)); |
44 | | |
45 | | // (over-)estimate of db such that conversion_radix^db > *this |
46 | 3.63k | const size_t digit_blocks = (digit_estimate + radix_digits - 1) / radix_digits; |
47 | | |
48 | 3.63k | BigInt value = *this; |
49 | 3.63k | value.set_sign(Positive); |
50 | | |
51 | | // Extract groups of digits into words |
52 | 3.63k | std::vector<word> digit_groups(digit_blocks); |
53 | | |
54 | 21.4k | for(size_t i = 0; i != digit_blocks; ++i) { |
55 | 17.8k | word remainder = 0; |
56 | 17.8k | ct_divide_word(value, conversion_radix, value, remainder); |
57 | 17.8k | digit_groups[i] = remainder; |
58 | 17.8k | } |
59 | | |
60 | 3.63k | BOTAN_ASSERT_NOMSG(value.is_zero()); |
61 | | |
62 | | // Extract digits from the groups |
63 | 3.63k | std::vector<uint8_t> digits(digit_blocks * radix_digits); |
64 | | |
65 | 21.4k | for(size_t i = 0; i != digit_blocks; ++i) { |
66 | 17.8k | word remainder = digit_groups[i]; |
67 | 357k | for(size_t j = 0; j != radix_digits; ++j) { |
68 | | // Compiler should convert div/mod by 10 into mul by magic constant |
69 | 339k | const word digit = remainder % 10; |
70 | 339k | remainder /= 10; |
71 | 339k | digits[radix_digits * i + j] = static_cast<uint8_t>(digit); |
72 | 339k | } |
73 | 17.8k | } |
74 | | |
75 | | // remove leading zeros |
76 | 63.4k | while(!digits.empty() && digits.back() == 0) { |
77 | 59.8k | digits.pop_back(); |
78 | 59.8k | } |
79 | | |
80 | 3.63k | BOTAN_ASSERT_NOMSG(digit_estimate >= digits.size()); |
81 | | |
82 | | // Reverse the digits to big-endian and format to text |
83 | 3.63k | std::string s; |
84 | 3.63k | s.reserve(1 + digits.size()); |
85 | | |
86 | 3.63k | if(is_negative()) { |
87 | 0 | s += "-"; |
88 | 0 | } |
89 | | |
90 | | // Reverse and convert to textual digits |
91 | 283k | for(auto i = digits.rbegin(); i != digits.rend(); ++i) { |
92 | 279k | s.push_back(*i + '0'); // assumes ASCII |
93 | 279k | } |
94 | | |
95 | 3.63k | if(s.empty()) { |
96 | 0 | s += "0"; |
97 | 0 | } |
98 | | |
99 | 3.63k | return s; |
100 | 3.63k | } |
101 | | |
102 | 4.52k | std::string BigInt::to_hex_string() const { |
103 | 4.52k | const size_t this_bytes = this->bytes(); |
104 | 4.52k | std::vector<uint8_t> bits(std::max<size_t>(1, this_bytes)); |
105 | | |
106 | 4.52k | if(this_bytes > 0) { |
107 | 3.91k | this->serialize_to(bits); |
108 | 3.91k | } |
109 | | |
110 | 4.52k | std::string hrep; |
111 | 4.52k | if(is_negative()) { |
112 | 214 | hrep += "-"; |
113 | 214 | } |
114 | 4.52k | hrep += "0x"; |
115 | 4.52k | hrep += hex_encode(bits); |
116 | 4.52k | return hrep; |
117 | 4.52k | } |
118 | | |
119 | | /* |
120 | | * Encode two BigInt, with leading 0s if needed, and concatenate |
121 | | */ |
122 | 881 | secure_vector<uint8_t> BigInt::encode_fixed_length_int_pair(const BigInt& n1, const BigInt& n2, size_t bytes) { |
123 | 881 | if(n1.is_negative() || n2.is_negative()) { |
124 | 0 | throw Encoding_Error("encode_fixed_length_int_pair: values must be positive"); |
125 | 0 | } |
126 | 881 | if(n1.bytes() > bytes || n2.bytes() > bytes) { |
127 | 22 | throw Encoding_Error("encode_fixed_length_int_pair: values too large to encode properly"); |
128 | 22 | } |
129 | 859 | secure_vector<uint8_t> output(2 * bytes); |
130 | 859 | BufferStuffer stuffer(output); |
131 | 859 | n1.serialize_to(stuffer.next(bytes)); |
132 | 859 | n2.serialize_to(stuffer.next(bytes)); |
133 | 859 | return output; |
134 | 881 | } |
135 | | |
136 | 0 | BigInt BigInt::decode(std::span<const uint8_t> buf, Base base) { |
137 | 0 | if(base == Binary) { |
138 | 0 | return BigInt::from_bytes(buf); |
139 | 0 | } |
140 | 0 | return BigInt::decode(buf.data(), buf.size(), base); |
141 | 0 | } |
142 | | |
143 | | /* |
144 | | * Decode a BigInt |
145 | | */ |
146 | 29.3k | BigInt BigInt::decode(const uint8_t buf[], size_t length, Base base) { |
147 | 29.3k | if(base == Binary) { |
148 | 0 | return BigInt::from_bytes(std::span{buf, length}); |
149 | 29.3k | } else if(base == Hexadecimal) { |
150 | 12 | BigInt r; |
151 | 12 | secure_vector<uint8_t> binary; |
152 | | |
153 | 12 | if(length % 2) { |
154 | | // Handle lack of leading 0 |
155 | 2 | const char buf0_with_leading_0[2] = {'0', static_cast<char>(buf[0])}; |
156 | | |
157 | 2 | binary = hex_decode_locked(buf0_with_leading_0, 2); |
158 | | |
159 | 2 | if(length > 1) { |
160 | 0 | binary += hex_decode_locked(cast_uint8_ptr_to_char(&buf[1]), length - 1, false); |
161 | 0 | } |
162 | 10 | } else { |
163 | 10 | binary = hex_decode_locked(cast_uint8_ptr_to_char(buf), length, false); |
164 | 10 | } |
165 | | |
166 | 12 | r.assign_from_bytes(binary); |
167 | 12 | return r; |
168 | 29.3k | } else if(base == Decimal) { |
169 | 29.3k | BigInt r; |
170 | | // This could be made faster using the same trick as to_dec_string |
171 | 2.36M | for(size_t i = 0; i != length; ++i) { |
172 | 2.33M | const char c = buf[i]; |
173 | | |
174 | 2.33M | if(c < '0' || c > '9') { |
175 | 0 | throw Invalid_Argument("BigInt::decode: invalid decimal char"); |
176 | 0 | } |
177 | | |
178 | 2.33M | const uint8_t x = c - '0'; |
179 | 2.33M | BOTAN_ASSERT_NOMSG(x < 10); |
180 | | |
181 | 2.33M | r *= 10; |
182 | 2.33M | r += x; |
183 | 2.33M | } |
184 | 29.3k | return r; |
185 | 29.3k | } else { |
186 | 0 | throw Invalid_Argument("Unknown BigInt decoding method"); |
187 | 0 | } |
188 | 29.3k | } |
189 | | |
190 | | } // namespace Botan |