From 8c964ca5342c4e8f77f449ed6a57f9680e135ad0 Mon Sep 17 00:00:00 2001 From: seelabs Date: Tue, 5 Mar 2024 11:15:00 -0500 Subject: [PATCH] [fold] Address John's comments --- src/ripple/protocol/impl/tokens.cpp | 23 +++++++++++++---------- src/ripple/protocol/tokens.h | 6 ++++-- src/test/basics/base58_test.cpp | 6 +++--- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/ripple/protocol/impl/tokens.cpp b/src/ripple/protocol/impl/tokens.cpp index a0396568c4c..8445eec38ca 100644 --- a/src/ripple/protocol/impl/tokens.cpp +++ b/src/ripple/protocol/impl/tokens.cpp @@ -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 @@ -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> -b256_to_b58(std::span input, std::span out) +b256_to_b58_be(std::span input, std::span out) { // Max valid input is 38 bytes: // (33 bytes for nodepublic + 1 byte token + 4 bytes checksum) @@ -456,7 +457,7 @@ b256_to_b58(std::span input, std::span 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; @@ -489,10 +490,9 @@ b256_to_b58(std::span input, std::span 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> -b58_to_b256(std::string_view input, std::span out) +b58_to_b256_be(std::string_view input, std::span out) { // Convert from b58 to b 58^10 @@ -638,7 +638,7 @@ encodeBase58Token( // buf[checksum_i..checksum_i + 4] = checksum checksum(buf.data() + checksum_i, buf.data(), checksum_i); std::span 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 @@ -653,7 +653,7 @@ decodeBase58Token( { std::array 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; @@ -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 outSp( reinterpret_cast(sr.data()), sr.size()); diff --git a/src/ripple/protocol/tokens.h b/src/ripple/protocol/tokens.h index 7aed76baab3..f51c3f96f95 100644 --- a/src/ripple/protocol/tokens.h +++ b/src/ripple/protocol/tokens.h @@ -120,10 +120,12 @@ decodeBase58Token(std::string const& s, TokenType type); namespace detail { // Expose detail functions for unit tests only B58Result> -b256_to_b58(std::span input, std::span out); +b256_to_b58_be( + std::span input, + std::span out); B58Result> -b58_to_b256(std::string_view input, std::span out); +b58_to_b256_be(std::string_view input, std::span out); } // namespace detail } // namespace b58_fast diff --git a/src/test/basics/base58_test.cpp b/src/test/basics/base58_test.cpp index 4bc78c3b418..6f3d495d7a9 100644 --- a/src/test/basics/base58_test.cpp +++ b/src/test/basics/base58_test.cpp @@ -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(); } @@ -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(); }