/tmp/bitcoin/src/base58.cpp
Line | Count | Source |
1 | | // Copyright (c) 2014-present The Bitcoin Core developers |
2 | | // Distributed under the MIT software license, see the accompanying |
3 | | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
4 | | |
5 | | #include <base58.h> |
6 | | |
7 | | #include <hash.h> |
8 | | #include <uint256.h> |
9 | | #include <util/strencodings.h> |
10 | | #include <util/string.h> |
11 | | |
12 | | #include <algorithm> |
13 | | #include <cassert> |
14 | | #include <cstdint> |
15 | | #include <cstring> |
16 | | |
17 | | #include <limits> |
18 | | |
19 | | using util::ContainsNUL; |
20 | | |
21 | | /** All alphanumeric characters except for "0", "I", "O", and "l" */ |
22 | | static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; |
23 | | static const int8_t mapBase58[256] = { |
24 | | -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, |
25 | | -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, |
26 | | -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, |
27 | | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,-1,-1,-1,-1,-1,-1, |
28 | | -1, 9,10,11,12,13,14,15, 16,-1,17,18,19,20,21,-1, |
29 | | 22,23,24,25,26,27,28,29, 30,31,32,-1,-1,-1,-1,-1, |
30 | | -1,33,34,35,36,37,38,39, 40,41,42,43,-1,44,45,46, |
31 | | 47,48,49,50,51,52,53,54, 55,56,57,-1,-1,-1,-1,-1, |
32 | | -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, |
33 | | -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, |
34 | | -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, |
35 | | -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, |
36 | | -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, |
37 | | -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, |
38 | | -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, |
39 | | -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1, |
40 | | }; |
41 | | |
42 | | [[nodiscard]] static bool DecodeBase58Raw(std::string_view str, std::vector<unsigned char>& vch, int max_ret_len) |
43 | 21.9k | { |
44 | 21.9k | const char* psz = str.data(); |
45 | 21.9k | const char* const end = str.data() + str.size(); |
46 | | // Skip leading spaces. |
47 | 22.0k | while (psz != end && IsSpace(*psz)) |
48 | 14 | psz++; |
49 | | // Skip and count leading '1's. |
50 | 21.9k | int zeroes = 0; |
51 | 21.9k | int length = 0; |
52 | 22.2k | while (psz != end && *psz == '1') { |
53 | 298 | zeroes++; |
54 | 298 | if (zeroes > max_ret_len) return false; |
55 | 298 | psz++; |
56 | 298 | } |
57 | | // Allocate enough space in big-endian base256 representation. |
58 | 21.9k | const size_t size{static_cast<size_t>(std::min<uint64_t>( |
59 | 21.9k | uint64_t(end - psz) * 733 / 1000 + 1, // log(58) / log(256), rounded up. |
60 | 21.9k | uint64_t(std::max(max_ret_len - zeroes, 0)) + 1))}; |
61 | 21.9k | std::vector<unsigned char> b256(size); |
62 | | // Process the characters. |
63 | 21.9k | static_assert(std::size(mapBase58) == 256, "mapBase58.size() should be 256"); // guarantee not out of range |
64 | 1.52M | while (psz != end && !IsSpace(*psz)) { |
65 | | // Decode base58 character |
66 | 1.50M | int carry{mapBase58[uint8_t(*psz)]}; |
67 | 1.50M | if (carry == -1) // Invalid b58 character |
68 | 448 | return false; |
69 | 1.50M | int i = 0; |
70 | 51.6M | for (std::vector<unsigned char>::reverse_iterator it = b256.rbegin(); (carry != 0 || i < length) && (it != b256.rend()); ++it, ++i) { |
71 | 50.1M | carry += 58 * (*it); |
72 | 50.1M | *it = carry % 256; |
73 | 50.1M | carry /= 256; |
74 | 50.1M | } |
75 | 1.50M | assert(carry == 0); |
76 | 1.50M | length = i; |
77 | 1.50M | if (length + zeroes > max_ret_len) return false; |
78 | 1.49M | psz++; |
79 | 1.49M | } |
80 | | // Skip trailing spaces. |
81 | 21.0k | while (psz != end && IsSpace(*psz)) |
82 | 16 | psz++; |
83 | 21.0k | if (psz != end) |
84 | 3 | return false; |
85 | | // Skip leading zeroes in b256. |
86 | 21.0k | std::vector<unsigned char>::iterator it = b256.begin() + (size - length); |
87 | | // Copy result into output vector. |
88 | 21.0k | vch.reserve(zeroes + (b256.end() - it)); |
89 | 21.0k | vch.assign(zeroes, 0x00); |
90 | 1.10M | while (it != b256.end()) |
91 | 1.08M | vch.push_back(*(it++)); |
92 | 21.0k | return true; |
93 | 21.0k | } |
94 | | |
95 | | std::string EncodeBase58(std::span<const unsigned char> input) |
96 | 148k | { |
97 | | // Skip & count leading zeroes. |
98 | 148k | int zeroes = 0; |
99 | 148k | int length = 0; |
100 | 149k | while (input.size() > 0 && input[0] == 0) { |
101 | 355 | input = input.subspan(1); |
102 | 355 | zeroes++; |
103 | 355 | } |
104 | | // Allocate enough space in big-endian base58 representation. |
105 | 148k | int size = input.size() * 138 / 100 + 1; // log(256) / log(58), rounded up. |
106 | 148k | std::vector<unsigned char> b58(size); |
107 | | // Process the bytes. |
108 | 10.8M | while (input.size() > 0) { |
109 | 10.6M | int carry = input[0]; |
110 | 10.6M | int i = 0; |
111 | | // Apply "b58 = b58 * 256 + ch". |
112 | 586M | for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); (carry != 0 || i < length) && (it != b58.rend()); it++, i++) { |
113 | 575M | carry += 256 * (*it); |
114 | 575M | *it = carry % 58; |
115 | 575M | carry /= 58; |
116 | 575M | } |
117 | | |
118 | 10.6M | assert(carry == 0); |
119 | 10.6M | length = i; |
120 | 10.6M | input = input.subspan(1); |
121 | 10.6M | } |
122 | | // Skip leading zeroes in base58 result. |
123 | 148k | std::vector<unsigned char>::iterator it = b58.begin() + (size - length); |
124 | 148k | while (it != b58.end() && *it == 0) |
125 | 0 | it++; |
126 | | // Translate the result into a string. |
127 | 148k | std::string str; |
128 | 148k | str.reserve(zeroes + (b58.end() - it)); |
129 | 148k | str.assign(zeroes, '1'); |
130 | 14.6M | while (it != b58.end()) |
131 | 14.4M | str += pszBase58[*(it++)]; |
132 | 148k | return str; |
133 | 148k | } |
134 | | |
135 | | bool DecodeBase58(std::string_view str, std::vector<unsigned char>& vchRet, int max_ret_len) |
136 | 303 | { |
137 | 303 | if (ContainsNUL(str)) { |
138 | 3 | return false; |
139 | 3 | } |
140 | 300 | return DecodeBase58Raw(str, vchRet, max_ret_len); |
141 | 303 | } |
142 | | |
143 | | std::string EncodeBase58Check(std::span<const unsigned char> input) |
144 | 148k | { |
145 | | // add 4-byte hash check to the end |
146 | 148k | std::vector<unsigned char> vch(input.begin(), input.end()); |
147 | 148k | uint256 hash = Hash(vch); |
148 | 148k | vch.insert(vch.end(), hash.data(), hash.data() + 4); |
149 | 148k | return EncodeBase58(vch); |
150 | 148k | } |
151 | | |
152 | | [[nodiscard]] static bool DecodeBase58CheckRaw(std::string_view str, std::vector<unsigned char>& vchRet, int max_ret_len) |
153 | 21.6k | { |
154 | 21.6k | if (!DecodeBase58Raw(str, vchRet, max_ret_len > std::numeric_limits<int>::max() - 4 ? std::numeric_limits<int>::max() : max_ret_len + 4) || |
155 | 21.6k | (vchRet.size() < 4)) { |
156 | 845 | vchRet.clear(); |
157 | 845 | return false; |
158 | 845 | } |
159 | | // re-calculate the checksum, ensure it matches the included 4-byte checksum |
160 | 20.8k | uint256 hash = Hash(std::span{vchRet}.first(vchRet.size() - 4)); |
161 | 20.8k | if (memcmp(&hash, &vchRet[vchRet.size() - 4], 4) != 0) { |
162 | 71 | vchRet.clear(); |
163 | 71 | return false; |
164 | 71 | } |
165 | 20.7k | vchRet.resize(vchRet.size() - 4); |
166 | 20.7k | return true; |
167 | 20.8k | } |
168 | | |
169 | | bool DecodeBase58Check(std::string_view str, std::vector<unsigned char>& vchRet, int max_ret) |
170 | 21.7k | { |
171 | 21.7k | if (ContainsNUL(str)) { |
172 | 1 | return false; |
173 | 1 | } |
174 | 21.6k | return DecodeBase58CheckRaw(str, vchRet, max_ret); |
175 | 21.7k | } |