Skip to content

Commit

Permalink
[fold] Address John's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
seelabs committed Mar 5, 2024
1 parent dae1fa7 commit 8c964ca
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
23 changes: 13 additions & 10 deletions src/ripple/protocol/impl/tokens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ while (i>=0)
```
For example, in base 10, the number 437 represents the integer 4*10^2 + 3*10^1 +
7*10^0. In base 16, 437 is the same as 4*16^2 + 3*16^1 7*16^0.
7*10^0. In base 16, 437 is the same as 4*16^2 + 3*16^1 + 7*16^0.
To find the coefficients that represent the integer N in base B, we start by
computing the lowest order coefficients and work up to the highest order
Expand Down Expand Up @@ -368,8 +368,9 @@ decodeBase58Token(std::string const& s, TokenType type)
// meantime MS falls back to the slower reference implementation)
namespace b58_fast {
namespace detail {
// Note: both the input and output will be BIG ENDIAN
B58Result<std::span<std::uint8_t>>
b256_to_b58(std::span<std::uint8_t const> input, std::span<std::uint8_t> out)
b256_to_b58_be(std::span<std::uint8_t const> input, std::span<std::uint8_t> out)
{
// Max valid input is 38 bytes:
// (33 bytes for nodepublic + 1 byte token + 4 bytes checksum)
Expand Down Expand Up @@ -456,7 +457,7 @@ b256_to_b58(std::span<std::uint8_t const> input, std::span<std::uint8_t> out)
out.begin(), out.begin() + input_zeros, ::ripple::alphabetForward[0]);

// iterate through the base 58^10 coeff
// convert to base 58 little endian then
// convert to base 58 big endian then
// convert to alphabet big endian
bool skip_zeros = true;
auto out_index = input_zeros;
Expand Down Expand Up @@ -489,10 +490,9 @@ b256_to_b58(std::span<std::uint8_t const> input, std::span<std::uint8_t> out)
return out.subspan(0, out_index);
}

// Note the input is in BIG ENDIAN form (some fn in this module use little
// endian)
// Note the input is BIG ENDIAN (some fn in this module use little endian)
B58Result<std::span<std::uint8_t>>
b58_to_b256(std::string_view input, std::span<std::uint8_t> out)
b58_to_b256_be(std::string_view input, std::span<std::uint8_t> out)
{
// Convert from b58 to b 58^10

Expand Down Expand Up @@ -638,7 +638,7 @@ encodeBase58Token(
// buf[checksum_i..checksum_i + 4] = checksum
checksum(buf.data() + checksum_i, buf.data(), checksum_i);
std::span<std::uint8_t const> b58Span(buf.data(), input.size() + 5);
return detail::b256_to_b58(b58Span, out);
return detail::b256_to_b58_be(b58Span, out);
}
// Convert from base 58 to base 256, largest coefficients first
// The input is encoded in XPRL format, with the token in the first
Expand All @@ -653,7 +653,7 @@ decodeBase58Token(
{
std::array<std::uint8_t, 64> tmpBuf;
auto const decodeResult =
detail::b58_to_b256(s, std::span(tmpBuf.data(), tmpBuf.size()));
detail::b58_to_b256_be(s, std::span(tmpBuf.data(), tmpBuf.size()));

if (!decodeResult)
return decodeResult;
Expand Down Expand Up @@ -689,8 +689,11 @@ encodeBase58Token(TokenType type, void const* token, std::size_t size)
{
std::string sr;
// The largest object encoded as base58 is 33 bytes; This will be encoded in
// at most ceil(log(2^256,58)) bytes, or 46 bytes. 64 is plenty (and there's
// not real benefit making it smaller)
// at most ceil(log(2^256,58)) bytes, or 46 bytes. 128 is plenty (and
// there's not real benefit making it smaller). Note that 46 bytes may be
// encoded in more than 46 base58 chars. Since decode uses 64 as the
// over-allocation, this function uses 128 (again, over-allocation assuming
// 2 base 58 char per byte)
sr.resize(128);
std::span<std::uint8_t> outSp(
reinterpret_cast<std::uint8_t*>(sr.data()), sr.size());
Expand Down
6 changes: 4 additions & 2 deletions src/ripple/protocol/tokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,12 @@ decodeBase58Token(std::string const& s, TokenType type);
namespace detail {
// Expose detail functions for unit tests only
B58Result<std::span<std::uint8_t>>
b256_to_b58(std::span<std::uint8_t const> input, std::span<std::uint8_t> out);
b256_to_b58_be(
std::span<std::uint8_t const> input,
std::span<std::uint8_t> out);

B58Result<std::span<std::uint8_t>>
b58_to_b256(std::string_view input, std::span<std::uint8_t> out);
b58_to_b256_be(std::string_view input, std::span<std::uint8_t> out);
} // namespace detail

} // namespace b58_fast
Expand Down
6 changes: 3 additions & 3 deletions src/test/basics/base58_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ class base58_test : public beast::unit_test::suite
std::span const outBuf{b58ResultBuf[i]};
if (i == 0)
{
auto const r =
ripple::b58_fast::detail::b256_to_b58(b256Data, outBuf);
auto const r = ripple::b58_fast::detail::b256_to_b58_be(
b256Data, outBuf);
BEAST_EXPECT(r);
b58Result[i] = r.value();
}
Expand Down Expand Up @@ -288,7 +288,7 @@ class base58_test : public beast::unit_test::suite
b58Result[i].data(),
b58Result[i].data() + b58Result[i].size());
auto const r =
ripple::b58_fast::detail::b58_to_b256(in, outBuf);
ripple::b58_fast::detail::b58_to_b256_be(in, outBuf);
BEAST_EXPECT(r);
b256Result[i] = r.value();
}
Expand Down

0 comments on commit 8c964ca

Please sign in to comment.