7#include <botan/base58.h>
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>
20uint32_t sha256_d_checksum(
const uint8_t input[],
size_t input_length) {
23 std::vector<uint8_t> checksum(32);
25 sha256->update(input, input_length);
26 sha256->final(checksum);
28 sha256->update(checksum);
29 sha256->final(checksum);
34char lookup_base58_char(uint8_t x) {
45 const char c_19 =
'1' + x;
46 const char c_AH =
'A' + (x - 9);
47 const char c_JN =
'J' + (x - 17);
48 const char c_PZ =
'P' + (x - 22);
49 const char c_ak =
'a' + (x - 33);
50 const char c_mz =
'm' + (x - 44);
53 ret = is_dec_19.select(c_19, ret);
54 ret = is_alpha_AH.select(c_AH, ret);
55 ret = is_alpha_JN.select(c_JN, ret);
56 ret = is_alpha_PZ.select(c_PZ, ret);
57 ret = is_alpha_ak.select(c_ak, ret);
63 const word radix = 58;
68 while(v.is_nonzero()) {
71 result.push_back(lookup_base58_char(
static_cast<uint8_t
>(r)));
75 for(
size_t i = 0; i != leading_zeros; ++i) {
76 result.push_back(
'1');
79 return std::string(result.rbegin(), result.rend());
82template <
typename T,
typename Z>
83size_t count_leading_zeros(
const T input[],
size_t input_length, Z zero) {
84 size_t leading_zeros = 0;
86 while(leading_zeros < input_length && input[leading_zeros] == zero) {
93uint8_t base58_value_of(
char input) {
96 const uint8_t c =
static_cast<uint8_t
>(input);
106 const uint8_t c_dec_19 = c - uint8_t(
'1');
107 const uint8_t c_AH = c - uint8_t(
'A') + 9;
108 const uint8_t c_JN = c - uint8_t(
'J') + 17;
109 const uint8_t c_PZ = c - uint8_t(
'P') + 22;
111 const uint8_t c_ak = c - uint8_t(
'a') + 33;
112 const uint8_t c_mz = c - uint8_t(
'm') + 44;
116 ret = is_dec_19.select(c_dec_19, ret);
117 ret = is_alpha_AH.select(c_AH, ret);
118 ret = is_alpha_JN.select(c_JN, ret);
119 ret = is_alpha_PZ.select(c_PZ, ret);
120 ret = is_alpha_ak.select(c_ak, ret);
121 ret = is_alpha_mz.select(c_mz, ret);
128 BigInt v(input, input_length);
129 return base58_encode(v, count_leading_zeros(input, input_length, 0));
133 BigInt v(input, input_length);
135 v += sha256_d_checksum(input, input_length);
136 return base58_encode(v, count_leading_zeros(input, input_length, 0));
139std::vector<uint8_t>
base58_decode(
const char input[],
size_t input_length) {
140 const size_t leading_zeros = count_leading_zeros(input, input_length,
'1');
144 for(
size_t i = leading_zeros; i != input_length; ++i) {
145 const char c = input[i];
147 if(c ==
' ' || c ==
'\n') {
151 const uint8_t idx = base58_value_of(c);
165 std::vector<uint8_t> dec =
base58_decode(input, input_length);
171 const uint32_t computed_checksum = sha256_d_checksum(dec.data(), dec.size() - 4);
174 if(checksum != computed_checksum) {
178 dec.resize(dec.size() - 4);
#define BOTAN_DEBUG_ASSERT(expr)
T serialize(size_t len) const
static constexpr Mask< T > is_lte(T x, T y)
static constexpr Mask< T > is_within_range(T v, T l, T u)
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
std::vector< uint8_t > base58_check_decode(const char input[], size_t input_length)
std::string base58_encode(const uint8_t input[], size_t input_length)
std::string base58_check_encode(const uint8_t input[], size_t input_length)
void ct_divide_word(const BigInt &x, word y, BigInt &q_out, word &r_out)
std::vector< uint8_t > base58_decode(const char input[], size_t input_length)
constexpr auto load_be(ParamTs &&... params)