28#include <xrpl/protocol/tokens.h>
30#include <xrpl/basics/safe_cast.h>
31#include <xrpl/beast/utility/instrumentation.h>
32#include <xrpl/protocol/detail/b58_utils.h>
33#include <xrpl/protocol/digest.h>
35#include <boost/container/small_vector.hpp>
36#include <boost/endian.hpp>
37#include <boost/endian/conversion.hpp>
139 "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz";
150template <
class Hasher>
151static typename Hasher::result_type
156 return static_cast<typename Hasher::result_type
>(h);
164static typename Hasher::result_type
167 return digest<Hasher>(v.
data(), v.
size());
171template <
class Hasher,
class... Args>
172static typename Hasher::result_type
175 return digest<Hasher>(digest<Hasher>(args...));
190 auto const h = digest2<sha256_hasher>(message, size);
198 return b58_fast::encodeBase58Token(type, token, size);
208 return b58_fast::decodeBase58Token(s, type);
225 auto pbegin =
reinterpret_cast<unsigned char const*
>(message);
226 auto const pend = pbegin + size;
230 while (pbegin != pend && *pbegin == 0)
236 auto const b58begin =
reinterpret_cast<unsigned char*
>(temp);
237 auto const b58end = b58begin + temp_size;
241 while (pbegin != pend)
245 for (
auto iter = b58end; iter != b58begin; --iter)
247 carry += 256 * (iter[-1]);
248 iter[-1] = carry % 58;
252 carry == 0,
"ripple::b58_ref::detail::encodeBase58 : zero carry");
257 auto iter = b58begin;
258 while (iter != b58end && *iter == 0)
263 str.
reserve(zeroes + (b58end - iter));
265 while (iter != b58end)
273 auto psz =
reinterpret_cast<unsigned char const*
>(s.
c_str());
274 auto remain = s.
size();
296 for (
auto iter = b256.
rbegin(); iter != b256.
rend(); ++iter)
303 carry == 0,
"ripple::b58_ref::detail::decodeBase58 : zero carry");
309 b256.
begin(), b256.
end(), [](
unsigned char c) { return c != 0; });
312 result.
assign(zeroes, 0x00);
313 while (iter != b256.
end())
324 auto const expanded = 1 + size + 4;
329 auto const bufsize = expanded * 3;
331 boost::container::small_vector<std::uint8_t, 1024> buf(bufsize);
335 buf[0] = safe_cast<std::underlying_type_t<TokenType>>(type);
338 checksum(buf.data() + 1 + size, buf.data(), 1 + size);
341 buf.data(), expanded, buf.data() + expanded, bufsize - expanded);
354 if (type != safe_cast<TokenType>(
static_cast<std::uint8_t>(ret[0])))
374B58Result<std::span<std::uint8_t>>
379 if (input.
size() > 38)
381 return Unexpected(TokenCodecErrc::inputTooLarge);
384 auto count_leading_zeros =
387 for (
auto const& c : col)
398 auto const input_zeros = count_leading_zeros(input);
399 input = input.
subspan(input_zeros);
408 for (
int i = 0; i < base_2_64_coeff_buf.size(); ++i)
410 if (i * 8 >= input.
size())
414 auto const src_i_end = input.
size() - i * 8;
418 &base_2_64_coeff_buf[num_coeff], &input[src_i_end - 8], 8);
419 boost::endian::big_to_native_inplace(
420 base_2_64_coeff_buf[num_coeff]);
425 for (
int bi = 0; bi < src_i_end; ++bi)
430 base_2_64_coeff_buf[num_coeff] = be;
434 return std::span(base_2_64_coeff_buf.data(), num_coeff);
444 while (cur_2_64_end > 0)
446 base_58_10_coeff[num_58_10_coeffs] =
447 ripple::b58_fast::detail::inplace_bigint_div_rem(
448 base_2_64_coeff.
subspan(0, cur_2_64_end), B_58_10);
449 num_58_10_coeffs += 1;
450 if (base_2_64_coeff[cur_2_64_end - 1] == 0)
464 bool skip_zeros =
true;
465 auto out_index = input_zeros;
466 for (
int i = num_58_10_coeffs - 1; i >= 0; --i)
468 if (skip_zeros && base_58_10_coeff[i] == 0)
473 if (base_58_10_coeff[i] >= B_58_10)
475 return Unexpected(TokenCodecErrc::inputTooLarge);
478 ripple::b58_fast::detail::b58_10_to_b58_be(base_58_10_coeff[i]);
483 to_skip = count_leading_zeros(b58_be_s);
485 if (
out.size() < (i + 1) * 10 - to_skip)
487 return Unexpected(TokenCodecErrc::outputTooSmall);
490 for (
auto b58_coeff : b58_be_s.subspan(to_skip))
497 return out.subspan(0, out_index);
501B58Result<std::span<std::uint8_t>>
508 if (input.
size() > 52)
510 return Unexpected(TokenCodecErrc::inputTooLarge);
514 return Unexpected(TokenCodecErrc::outputTooSmall);
517 auto count_leading_zeros = [&](
auto const& col) ->
std::size_t {
519 for (
auto const& c : col)
530 auto const input_zeros = count_leading_zeros(input);
536 auto [num_full_coeffs, partial_coeff_len] =
537 ripple::b58_fast::detail::div_rem(input.
size(), 10);
538 auto const num_partial_coeffs = partial_coeff_len ? 1 : 0;
539 auto const num_b_58_10_coeffs = num_full_coeffs + num_partial_coeffs;
541 num_b_58_10_coeffs <= b_58_10_coeff.size(),
542 "ripple::b58_fast::detail::b58_to_b256_be : maximum coeff");
543 for (
auto c : input.
substr(0, partial_coeff_len))
548 return Unexpected(TokenCodecErrc::invalidEncodingChar);
550 b_58_10_coeff[0] *= 58;
551 b_58_10_coeff[0] += cur_val;
553 for (
int i = 0; i < 10; ++i)
555 for (
int j = 0; j < num_full_coeffs; ++j)
557 auto c = input[partial_coeff_len + j * 10 + i];
561 return Unexpected(TokenCodecErrc::invalidEncodingChar);
563 b_58_10_coeff[num_partial_coeffs + j] *= 58;
564 b_58_10_coeff[num_partial_coeffs + j] += cur_val;
572 result[0] = b_58_10_coeff[0];
574 for (
int i = 1; i < num_b_58_10_coeffs; ++i)
579 auto code = ripple::b58_fast::detail::inplace_bigint_mul(
580 std::span(&result[0], cur_result_size + 1), B_58_10);
581 if (code != TokenCodecErrc::success)
587 auto code = ripple::b58_fast::detail::inplace_bigint_add(
588 std::span(&result[0], cur_result_size + 1), c);
589 if (code != TokenCodecErrc::success)
594 if (result[cur_result_size] != 0)
596 cur_result_size += 1;
600 auto cur_out_i = input_zeros;
605 auto skip_zero =
true;
607 for (
int i = 0; i < 8; ++i)
622 if ((cur_out_i + 8 * (cur_result_size - 1)) >
out.size())
624 return Unexpected(TokenCodecErrc::outputTooSmall);
627 for (
int i = cur_result_size - 2; i >= 0; --i)
630 boost::endian::native_to_big_inplace(c);
631 memcpy(&out[cur_out_i], &c, 8);
635 return out.subspan(0, cur_out_i);
639B58Result<std::span<std::uint8_t>>
641 TokenType token_type,
647 if (input.
size() > tmpBufSize - 5)
649 return Unexpected(TokenCodecErrc::inputTooLarge);
651 if (input.
size() == 0)
653 return Unexpected(TokenCodecErrc::inputTooSmall);
659 size_t const checksum_i = input.
size() + 1;
663 return detail::b256_to_b58_be(b58Span, out);
670B58Result<std::span<std::uint8_t>>
677 auto const decodeResult =
683 auto const ret = decodeResult.value();
687 return Unexpected(TokenCodecErrc::inputTooSmall);
691 return Unexpected(TokenCodecErrc::mismatchedTokenType);
698 return Unexpected(TokenCodecErrc::mismatchedChecksum);
702 if (outBuf.
size() < outSize)
703 return Unexpected(TokenCodecErrc::outputTooSmall);
705 std::copy(ret.begin() + 1, ret.begin() + outSize + 1, outBuf.
begin());
706 return outBuf.
subspan(0, outSize);
724 auto r = b58_fast::encodeBase58Token(type, inSp, outSp);
727 sr.
resize(r.value().size());
740 auto r = b58_fast::decodeBase58Token(type, s, outSp);
743 sr.
resize(r.value().size());
std::string decodeBase58(std::string const &s)
std::string encodeBase58(void const *message, std::size_t size, void *temp, std::size_t temp_size)
std::string encodeBase58Token(TokenType type, void const *token, std::size_t size)
std::string decodeBase58Token(std::string const &s, TokenType type)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
static constexpr std::array< int, 256 > const alphabetReverse
std::string encodeBase58Token(TokenType type, void const *token, std::size_t size)
Encode data in Base58Check format using XRPL alphabet.
std::string decodeBase58Token(std::string const &s, TokenType type)
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
static Hasher::result_type digest2(Args const &... args)
Unexpected(E(&)[N]) -> Unexpected< E const * >
static void checksum(void *out, void const *message, std::size_t size)
Calculate a 4-byte checksum of the data.
static constexpr char const * alphabetForward