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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

99.05
/src/lib/codec/base58/base58.cpp
1
/*
2
* (C) 2018,2020 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include <botan/base58.h>
8

9
#include <botan/bigint.h>
10
#include <botan/exceptn.h>
11
#include <botan/hash.h>
12
#include <botan/internal/ct_utils.h>
13
#include <botan/internal/divide.h>
14
#include <botan/internal/loadstor.h>
15

16
namespace Botan {
17

18
namespace {
19

20
uint32_t sha256_d_checksum(const uint8_t input[], size_t input_length) {
16✔
21
   auto sha256 = HashFunction::create_or_throw("SHA-256");
16✔
22

23
   std::vector<uint8_t> checksum(32);
16✔
24

25
   sha256->update(input, input_length);
16✔
26
   sha256->final(checksum);
16✔
27

28
   sha256->update(checksum);
16✔
29
   sha256->final(checksum);
16✔
30

31
   return load_be<uint32_t>(checksum.data(), 0);
16✔
32
}
32✔
33

34
char lookup_base58_char(uint8_t x) {
333✔
35
   // "123456789 ABCDEFGH JKLMN PQRSTUVWXYZ abcdefghijk mnopqrstuvwxyz"
36
   BOTAN_DEBUG_ASSERT(x < 58);
333✔
37

38
   const auto is_dec_19 = CT::Mask<uint8_t>::is_lte(x, 8);
333✔
39
   const auto is_alpha_AH = CT::Mask<uint8_t>::is_within_range(x, 9, 16);
333✔
40
   const auto is_alpha_JN = CT::Mask<uint8_t>::is_within_range(x, 17, 21);
333✔
41
   const auto is_alpha_PZ = CT::Mask<uint8_t>::is_within_range(x, 22, 32);
333✔
42
   const auto is_alpha_ak = CT::Mask<uint8_t>::is_within_range(x, 33, 43);
333✔
43
   // otherwise in 'm'-'z'
44

45
   const char c_19 = '1' + x;
333✔
46
   const char c_AH = 'A' + (x - 9);
333✔
47
   const char c_JN = 'J' + (x - 17);
333✔
48
   const char c_PZ = 'P' + (x - 22);
333✔
49
   const char c_ak = 'a' + (x - 33);
333✔
50
   const char c_mz = 'm' + (x - 44);
333✔
51

52
   char ret = c_mz;
333✔
53
   ret = is_dec_19.select(c_19, ret);
333✔
54
   ret = is_alpha_AH.select(c_AH, ret);
333✔
55
   ret = is_alpha_JN.select(c_JN, ret);
333✔
56
   ret = is_alpha_PZ.select(c_PZ, ret);
333✔
57
   ret = is_alpha_ak.select(c_ak, ret);
333✔
58

59
   return ret;
333✔
60
}
61

62
std::string base58_encode(BigInt v, size_t leading_zeros) {
26✔
63
   const word radix = 58;
26✔
64

65
   std::string result;
26✔
66
   BigInt q;
26✔
67

68
   while(v.is_nonzero()) {
718✔
69
      word r;
333✔
70
      ct_divide_word(v, radix, q, r);
333✔
71
      result.push_back(lookup_base58_char(static_cast<uint8_t>(r)));
333✔
72
      v.swap(q);
333✔
73
   }
74

75
   for(size_t i = 0; i != leading_zeros; ++i)
38✔
76
      result.push_back('1');  // 'zero' byte
12✔
77

78
   return std::string(result.rbegin(), result.rend());
26✔
79
}
33✔
80

81
template <typename T, typename Z>
82
size_t count_leading_zeros(const T input[], size_t input_length, Z zero) {
45✔
83
   size_t leading_zeros = 0;
45✔
84

85
   while(leading_zeros < input_length && input[leading_zeros] == zero)
88✔
86
      leading_zeros += 1;
24✔
87

88
   return leading_zeros;
89
}
90

91
uint8_t base58_value_of(char input) {
336✔
92
   // "123456789 ABCDEFGH JKLMN PQRSTUVWXYZ abcdefghijk mnopqrstuvwxyz"
93

94
   const uint8_t c = static_cast<uint8_t>(input);
336✔
95

96
   const auto is_dec_19 = CT::Mask<uint8_t>::is_within_range(c, uint8_t('1'), uint8_t('9'));
336✔
97
   const auto is_alpha_AH = CT::Mask<uint8_t>::is_within_range(c, uint8_t('A'), uint8_t('H'));
336✔
98
   const auto is_alpha_JN = CT::Mask<uint8_t>::is_within_range(c, uint8_t('J'), uint8_t('N'));
336✔
99
   const auto is_alpha_PZ = CT::Mask<uint8_t>::is_within_range(c, uint8_t('P'), uint8_t('Z'));
336✔
100

101
   const auto is_alpha_ak = CT::Mask<uint8_t>::is_within_range(c, uint8_t('a'), uint8_t('k'));
336✔
102
   const auto is_alpha_mz = CT::Mask<uint8_t>::is_within_range(c, uint8_t('m'), uint8_t('z'));
336✔
103

104
   const uint8_t c_dec_19 = c - uint8_t('1');
336✔
105
   const uint8_t c_AH = c - uint8_t('A') + 9;
336✔
106
   const uint8_t c_JN = c - uint8_t('J') + 17;
336✔
107
   const uint8_t c_PZ = c - uint8_t('P') + 22;
336✔
108

109
   const uint8_t c_ak = c - uint8_t('a') + 33;
336✔
110
   const uint8_t c_mz = c - uint8_t('m') + 44;
336✔
111

112
   uint8_t ret = 0xFF;  // default value
336✔
113

114
   ret = is_dec_19.select(c_dec_19, ret);
336✔
115
   ret = is_alpha_AH.select(c_AH, ret);
336✔
116
   ret = is_alpha_JN.select(c_JN, ret);
336✔
117
   ret = is_alpha_PZ.select(c_PZ, ret);
336✔
118
   ret = is_alpha_ak.select(c_ak, ret);
336✔
119
   ret = is_alpha_mz.select(c_mz, ret);
336✔
120
   return ret;
336✔
121
}
122

123
}
124

125
std::string base58_encode(const uint8_t input[], size_t input_length) {
19✔
126
   BigInt v(input, input_length);
19✔
127
   return base58_encode(v, count_leading_zeros(input, input_length, 0));
38✔
128
}
19✔
129

130
std::string base58_check_encode(const uint8_t input[], size_t input_length) {
7✔
131
   BigInt v(input, input_length);
7✔
132
   v <<= 32;
7✔
133
   v += sha256_d_checksum(input, input_length);
7✔
134
   return base58_encode(v, count_leading_zeros(input, input_length, 0));
21✔
135
}
7✔
136

137
std::vector<uint8_t> base58_decode(const char input[], size_t input_length) {
38✔
138
   const size_t leading_zeros = count_leading_zeros(input, input_length, '1');
38✔
139

140
   BigInt v;
38✔
141

142
   for(size_t i = leading_zeros; i != input_length; ++i) {
363✔
143
      const char c = input[i];
336✔
144

145
      if(c == ' ' || c == '\n')
336✔
146
         continue;
×
147

148
      const uint8_t idx = base58_value_of(c);
336✔
149

150
      if(idx == 0xFF)
336✔
151
         throw Decoding_Error("Invalid base58");
11✔
152

153
      v *= 58;
325✔
154
      v += idx;
325✔
155
   }
156

157
   std::vector<uint8_t> output(v.bytes() + leading_zeros);
38✔
158
   v.binary_encode(output.data() + leading_zeros);
27✔
159
   return output;
27✔
160
}
27✔
161

162
std::vector<uint8_t> base58_check_decode(const char input[], size_t input_length) {
10✔
163
   std::vector<uint8_t> dec = base58_decode(input, input_length);
10✔
164

165
   if(dec.size() < 4)
10✔
166
      throw Decoding_Error("Invalid base58 too short for checksum");
1✔
167

168
   const uint32_t computed_checksum = sha256_d_checksum(dec.data(), dec.size() - 4);
9✔
169
   const uint32_t checksum = load_be<uint32_t>(&dec[dec.size() - 4], 0);
9✔
170

171
   if(checksum != computed_checksum)
9✔
172
      throw Decoding_Error("Invalid base58 checksum");
4✔
173

174
   dec.resize(dec.size() - 4);
5✔
175

176
   return dec;
5✔
177
}
5✔
178

179
}
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

© 2025 Coveralls, Inc