10#include <xrpl/basics/Expected.h>
11#include <xrpl/basics/safe_cast.h>
12#include <xrpl/beast/utility/instrumentation.h>
13#include <xrpl/protocol/detail/b58_utils.h>
14#include <xrpl/protocol/detail/token_errors.h>
15#include <xrpl/protocol/digest.h>
16#include <xrpl/protocol/tokens.h>
18#include <boost/container/small_vector.hpp>
19#include <boost/endian/conversion.hpp>
124static constexpr char const*
alphabetForward =
"rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz";
135template <
class Hasher>
136static typename Hasher::result_type
141 return static_cast<typename Hasher::result_type
>(h);
144template <
class Hasher,
class T, std::
size_t N,
class = std::enable_if_t<sizeof(T) == 1>>
145static typename Hasher::result_type
148 return digest<Hasher>(v.
data(), v.
size());
152template <
class Hasher,
class... Args>
153static typename Hasher::result_type
156 return digest<Hasher>(digest<Hasher>(args...));
171 auto const h = digest2<sha256_hasher>(message, size);
179 return b58_fast::encodeBase58Token(type, token, size);
189 return b58_fast::decodeBase58Token(s, type);
202 auto pbegin =
reinterpret_cast<unsigned char const*
>(message);
203 auto const pend = pbegin + size;
207 while (pbegin != pend && *pbegin == 0)
213 auto const b58begin =
reinterpret_cast<unsigned char*
>(temp);
214 auto const b58end = b58begin + temp_size;
218 while (pbegin != pend)
222 for (
auto iter = b58end; iter != b58begin; --iter)
224 carry += 256 * (iter[-1]);
225 iter[-1] = carry % 58;
228 XRPL_ASSERT(carry == 0,
"xrpl::b58_ref::detail::encodeBase58 : zero carry");
233 auto iter = b58begin;
234 while (iter != b58end && *iter == 0)
239 str.
reserve(zeroes + (b58end - iter));
241 while (iter != b58end)
249 auto psz =
reinterpret_cast<unsigned char const*
>(s.
c_str());
250 auto remain = s.
size();
272 for (
auto iter = b256.
rbegin(); iter != b256.
rend(); ++iter)
278 XRPL_ASSERT(carry == 0,
"xrpl::b58_ref::detail::decodeBase58 : zero carry");
286 result.
assign(zeroes, 0x00);
287 while (iter != b256.
end())
298 auto const expanded = 1 + size + 4;
303 auto const bufsize = expanded * 3;
305 boost::container::small_vector<std::uint8_t, 1024> buf(bufsize);
309 buf[0] = safe_cast<std::underlying_type_t<TokenType>>(type);
312 checksum(buf.data() + 1 + size, buf.data(), 1 + size);
327 if (type != safe_cast<TokenType>(
static_cast<std::uint8_t>(ret[0])))
347B58Result<std::span<std::uint8_t>>
352 if (input.
size() > 38)
354 return Unexpected(TokenCodecErrc::inputTooLarge);
359 for (
auto const& c : col)
370 auto const input_zeros = count_leading_zeros(input);
371 input = input.
subspan(input_zeros);
379 for (
int i = 0; i < base_2_64_coeff_buf.size(); ++i)
381 if (i * 8 >= input.
size())
385 auto const src_i_end = input.
size() - i * 8;
388 std::memcpy(&base_2_64_coeff_buf[num_coeff], &input[src_i_end - 8], 8);
389 boost::endian::big_to_native_inplace(base_2_64_coeff_buf[num_coeff]);
394 for (
int bi = 0; bi < src_i_end; ++bi)
399 base_2_64_coeff_buf[num_coeff] = be;
403 return std::span(base_2_64_coeff_buf.data(), num_coeff);
413 while (cur_2_64_end > 0)
415 base_58_10_coeff[num_58_10_coeffs] =
416 xrpl::b58_fast::detail::inplace_bigint_div_rem(base_2_64_coeff.
subspan(0, cur_2_64_end), B_58_10);
417 num_58_10_coeffs += 1;
418 if (base_2_64_coeff[cur_2_64_end - 1] == 0)
431 bool skip_zeros =
true;
432 auto out_index = input_zeros;
433 for (
int i = num_58_10_coeffs - 1; i >= 0; --i)
435 if (skip_zeros && base_58_10_coeff[i] == 0)
440 if (base_58_10_coeff[i] >= B_58_10)
442 return Unexpected(TokenCodecErrc::inputTooLarge);
449 to_skip = count_leading_zeros(b58_be_s);
451 if (
out.size() < (i + 1) * 10 - to_skip)
453 return Unexpected(TokenCodecErrc::outputTooSmall);
456 for (
auto b58_coeff : b58_be_s.subspan(to_skip))
463 return out.subspan(0, out_index);
467B58Result<std::span<std::uint8_t>>
474 if (input.
size() > 52)
476 return Unexpected(TokenCodecErrc::inputTooLarge);
480 return Unexpected(TokenCodecErrc::outputTooSmall);
483 auto count_leading_zeros = [&](
auto const& col) ->
std::size_t {
485 for (
auto const& c : col)
496 auto const input_zeros = count_leading_zeros(input);
502 auto [num_full_coeffs, partial_coeff_len] = xrpl::b58_fast::detail::div_rem(input.
size(), 10);
503 auto const num_partial_coeffs = partial_coeff_len ? 1 : 0;
504 auto const num_b_58_10_coeffs = num_full_coeffs + num_partial_coeffs;
505 XRPL_ASSERT(num_b_58_10_coeffs <= b_58_10_coeff.size(),
"xrpl::b58_fast::detail::b58_to_b256_be : maximum coeff");
506 for (
unsigned char c : input.substr(0, partial_coeff_len))
511 return Unexpected(TokenCodecErrc::invalidEncodingChar);
513 b_58_10_coeff[0] *= 58;
514 b_58_10_coeff[0] += cur_val;
516 for (
int i = 0; i < 10; ++i)
518 for (
int j = 0; j < num_full_coeffs; ++j)
520 unsigned char c = input[partial_coeff_len + j * 10 + i];
524 return Unexpected(TokenCodecErrc::invalidEncodingChar);
526 b_58_10_coeff[num_partial_coeffs + j] *= 58;
527 b_58_10_coeff[num_partial_coeffs + j] += cur_val;
535 result[0] = b_58_10_coeff[0];
537 for (
int i = 1; i < num_b_58_10_coeffs; ++i)
542 auto code = xrpl::b58_fast::detail::inplace_bigint_mul(
std::span(&result[0], cur_result_size + 1), B_58_10);
543 if (code != TokenCodecErrc::success)
549 auto code = xrpl::b58_fast::detail::inplace_bigint_add(
std::span(&result[0], cur_result_size + 1), c);
550 if (code != TokenCodecErrc::success)
555 if (result[cur_result_size] != 0)
557 cur_result_size += 1;
561 auto cur_out_i = input_zeros;
566 auto skip_zero =
true;
568 for (
int i = 0; i < 8; ++i)
583 if ((cur_out_i + 8 * (cur_result_size - 1)) >
out.size())
585 return Unexpected(TokenCodecErrc::outputTooSmall);
588 for (
int i = cur_result_size - 2; i >= 0; --i)
591 boost::endian::native_to_big_inplace(c);
592 memcpy(&out[cur_out_i], &c, 8);
596 return out.subspan(0, cur_out_i);
600B58Result<std::span<std::uint8_t>>
605 if (input.
size() > tmpBufSize - 5)
607 return Unexpected(TokenCodecErrc::inputTooLarge);
609 if (input.
size() == 0)
611 return Unexpected(TokenCodecErrc::inputTooSmall);
617 size_t const checksum_i = input.
size() + 1;
621 return detail::b256_to_b58_be(b58Span, out);
628B58Result<std::span<std::uint8_t>>
632 auto const decodeResult = detail::b58_to_b256_be(s,
std::span(tmpBuf.
data(), tmpBuf.
size()));
637 auto const ret = decodeResult.value();
641 return Unexpected(TokenCodecErrc::inputTooSmall);
645 return Unexpected(TokenCodecErrc::mismatchedTokenType);
652 return Unexpected(TokenCodecErrc::mismatchedChecksum);
656 if (outBuf.
size() < outSize)
657 return Unexpected(TokenCodecErrc::outputTooSmall);
659 std::copy(ret.begin() + 1, ret.begin() + outSize + 1, outBuf.
begin());
660 return outBuf.
subspan(0, outSize);
676 auto r = b58_fast::encodeBase58Token(type, inSp, outSp);
679 sr.
resize(r.value().size());
691 auto r = b58_fast::decodeBase58Token(type, s, outSp);
694 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 void checksum(void *out, void const *message, std::size_t size)
Calculate a 4-byte checksum of the data.
Unexpected(E(&)[N]) -> Unexpected< E const * >
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
static constexpr char const * alphabetForward
static Hasher::result_type digest2(Args const &... args)
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 constexpr std::array< int, 256 > const alphabetReverse