1#include <xrpl/protocol/detail/token_errors.h>
3#include <boost/multiprecision/cpp_int.hpp>
5#include <gtest/gtest.h>
21#include <xrpl/protocol/detail/b58_utils.h>
22#include <xrpl/protocol/tokens.h>
33[[nodiscard]]
inline auto
34randEngine() -> std::mt19937&
36 static std::mt19937 kR = [] {
37 std::random_device rd;
38 return std::mt19937{rd()};
43constexpr int kNumTokenTypeIndexes = 9;
45[[nodiscard]]
inline auto
46tokenTypeAndSize(
int i) -> std::tuple<xrpl::TokenType, std::size_t>
48 assert(i < kNumTokenTypeIndexes);
72 throw std::invalid_argument(
73 "Invalid token selection passed to tokenTypeAndSize() "
78[[nodiscard]]
inline auto
79randomTokenTypeAndSize() -> std::tuple<xrpl::TokenType, std::size_t>
82 auto& rng = randEngine();
83 std::uniform_int_distribution<> d(0, 8);
84 return tokenTypeAndSize(d(rng));
88[[nodiscard]]
inline auto
89randomB256TestData(std::span<std::uint8_t> d)
90 -> std::tuple<xrpl::TokenType, std::span<std::uint8_t>>
92 auto& rng = randEngine();
93 std::uniform_int_distribution<std::uint8_t> dist(0, 255);
94 auto [tokType, tokSize] = randomTokenTypeAndSize();
95 std::generate(d.begin(), d.begin() + tokSize, [&] { return dist(rng); });
96 return {tokType, d.subspan(0, tokSize)};
100printAsChar(std::span<std::uint8_t> a, std::span<std::uint8_t> b)
102 auto const asString = [](std::span<std::uint8_t> s) {
108 auto const sa = asString(a);
109 auto const sb = asString(b);
110 std::cerr <<
"\n\n" << sa <<
"\n" << sb <<
"\n";
114printAsInt(std::span<std::uint8_t> a, std::span<std::uint8_t> b)
116 auto const asString = [](std::span<std::uint8_t> s) -> std::string {
117 std::stringstream sstr;
118 for (
auto const i : s)
124 auto const sa = asString(a);
125 auto const sb = asString(b);
126 std::cerr <<
"\n\n" << sa <<
"\n" << sb <<
"\n";
131namespace multiprecision_utils {
133[[nodiscard]] boost::multiprecision::checked_uint512_t
134toBoostMP(std::span<std::uint64_t> in)
136 boost::multiprecision::checked_uint512_t mbp = 0;
137 for (
auto const& i : std::views::reverse(in))
145[[nodiscard]] std::vector<std::uint64_t>
146randomBigInt(std::uint8_t minSize = 1, std::uint8_t maxSize = 5)
148 auto& eng = randEngine();
149 std::uniform_int_distribution<std::uint8_t> numCoeffDist(minSize, maxSize);
150 std::uniform_int_distribution<std::uint64_t> dist;
151 auto const numCoeff = numCoeffDist(eng);
152 std::vector<std::uint64_t> coeffs;
154 for (
auto i = 0uz; i < numCoeff; ++i)
162struct Base58MultiprecisionTest :
public ::testing::Test
164 static constexpr std::size_t kIters = 100000;
166 std::mt19937& eng = randEngine();
167 std::uniform_int_distribution<std::uint64_t> dist;
168 std::uniform_int_distribution<std::uint64_t> dist1{1};
171TEST_F(Base58MultiprecisionTest, div_rem_matches_boost)
173 using namespace boost::multiprecision;
175 for (
auto i = 0uz; i < kIters; ++i)
177 std::uint64_t
const d = dist(eng);
180 auto bigInt = multiprecision_utils::randomBigInt();
181 auto const boostBigInt =
182 multiprecision_utils::toBoostMP(std::span<std::uint64_t>(bigInt.data(), bigInt.size()));
184 auto const refDiv = boostBigInt / d;
185 auto const refMod = boostBigInt % d;
187 auto const mod = b58_fast::detail::inplaceBigintDivRem(
188 std::span<uint64_t>(bigInt.data(), bigInt.size()), d);
189 auto const foundDiv = multiprecision_utils::toBoostMP(bigInt);
190 EXPECT_EQ(refMod.convert_to<std::uint64_t>(), mod);
191 EXPECT_EQ(foundDiv, refDiv);
195TEST_F(Base58MultiprecisionTest, add_matches_boost)
197 using namespace boost::multiprecision;
199 for (
auto i = 0uz; i < kIters; ++i)
201 std::uint64_t
const d = dist(eng);
202 auto bigInt = multiprecision_utils::randomBigInt( 2);
205 bigInt[bigInt.size() - 1] -= 1;
207 auto const boostBigInt =
208 multiprecision_utils::toBoostMP(std::span<std::uint64_t>(bigInt.data(), bigInt.size()));
210 auto const refAdd = boostBigInt + d;
212 auto const result = b58_fast::detail::inplaceBigintAdd(
213 std::span<uint64_t>(bigInt.data(), bigInt.size()), d);
215 auto const foundAdd = multiprecision_utils::toBoostMP(bigInt);
216 EXPECT_EQ(refAdd, foundAdd);
220TEST_F(Base58MultiprecisionTest, add_reports_overflow)
222 using namespace boost::multiprecision;
224 for (
auto i = 0uz; i < kIters; ++i)
226 std::uint64_t
const d = dist1(eng);
230 auto const boostBigInt =
231 multiprecision_utils::toBoostMP(std::span<std::uint64_t>(bigInt.data(), bigInt.size()));
233 auto const refAdd = boostBigInt + d;
235 auto const result = b58_fast::detail::inplaceBigintAdd(
236 std::span<uint64_t>(bigInt.data(), bigInt.size()), d);
238 auto const foundAdd = multiprecision_utils::toBoostMP(bigInt);
239 EXPECT_NE(refAdd, foundAdd);
243TEST_F(Base58MultiprecisionTest, mul_matches_boost)
245 using namespace boost::multiprecision;
247 for (
auto i = 0uz; i < kIters; ++i)
249 std::uint64_t
const d = dist(eng);
250 auto bigInt = multiprecision_utils::randomBigInt( 2);
253 bigInt[bigInt.size() - 1] = 0;
254 auto const boostBigInt =
255 multiprecision_utils::toBoostMP(std::span<std::uint64_t>(bigInt.data(), bigInt.size()));
257 auto const refMul = boostBigInt * d;
259 auto const result = b58_fast::detail::inplaceBigintMul(
260 std::span<uint64_t>(bigInt.data(), bigInt.size()), d);
262 auto const foundMul = multiprecision_utils::toBoostMP(bigInt);
263 EXPECT_EQ(refMul, foundMul);
267TEST_F(Base58MultiprecisionTest, mul_reports_input_too_large)
269 using namespace boost::multiprecision;
271 for (
auto i = 0uz; i < kIters; ++i)
273 std::uint64_t
const d = dist1(eng);
276 auto const boostBigInt =
277 multiprecision_utils::toBoostMP(std::span<std::uint64_t>(bigInt.data(), bigInt.size()));
279 auto const refMul = boostBigInt * d;
281 auto const result = b58_fast::detail::inplaceBigintMul(
282 std::span<uint64_t>(bigInt.data(), bigInt.size()), d);
284 auto const foundMul = multiprecision_utils::toBoostMP(bigInt);
285 EXPECT_NE(refMul, foundMul);
291struct Base58FastMatchesRefTest :
public ::testing::Test
294 testRawEncode(std::span<std::uint8_t> b256Data)
296 std::array<std::uint8_t, 64> b58ResultBuf[2];
297 std::array<std::span<std::uint8_t>, 2> b58Result;
299 std::array<std::uint8_t, 64> b256ResultBuf[2];
300 std::array<std::span<std::uint8_t>, 2> b256Result;
301 for (
auto i = 0uz; i < 2; ++i)
303 std::span
const outBuf{b58ResultBuf[i]};
306 auto const r = xrpl::b58_fast::detail::b256ToB58Be(b256Data, outBuf);
308 b58Result[i] = r.value();
312 std::array<std::uint8_t, 128> tmpBuf{};
315 EXPECT_TRUE(s.
size());
320 auto const rawB58SameSize = b58Result[0].
size() == b58Result[1].
size();
321 EXPECT_TRUE(rawB58SameSize);
324 auto const rawB58SameData =
326 EXPECT_TRUE(rawB58SameData);
329 printAsChar(b58Result[0], b58Result[1]);
333 for (
auto i = 0uz; i < 2; ++i)
335 std::span
const outBuf{b256ResultBuf[i].
data(), b256ResultBuf[i].
size()};
338 std::string
const in(
339 b58Result[i].
data(), b58Result[i].
data() + b58Result[i].
size());
340 auto const r = xrpl::b58_fast::detail::b58ToB256Be(in, outBuf);
342 b256Result[i] = r.value();
346 std::string
const st(b58Result[i].
begin(), b58Result[i].
end());
348 EXPECT_TRUE(s.
size());
354 auto const rawB256SameSize = b256Result[0].
size() == b256Result[1].
size();
355 EXPECT_TRUE(rawB256SameSize);
358 auto const rawB256SameData =
360 EXPECT_TRUE(rawB256SameData);
361 if (!rawB256SameData)
363 printAsInt(b256Result[0], b256Result[1]);
369 testTokenEncode(
xrpl::TokenType const tokType, std::span<std::uint8_t> b256Data)
371 std::array<std::uint8_t, 64> b58ResultBuf[2];
372 std::array<std::span<std::uint8_t>, 2> b58Result;
374 std::array<std::uint8_t, 64> b256ResultBuf[2];
375 std::array<std::span<std::uint8_t>, 2> b256Result;
376 for (
auto i = 0uz; i < 2; ++i)
378 std::span
const outBuf{b58ResultBuf[i].
data(), b58ResultBuf[i].
size()};
381 auto const r = xrpl::b58_fast::encodeBase58Token(tokType, b256Data, outBuf);
383 b58Result[i] = r.value();
387 std::string
const s =
389 EXPECT_TRUE(s.
size());
394 auto const tokenB58SameSize = b58Result[0].
size() == b58Result[1].
size();
395 EXPECT_TRUE(tokenB58SameSize);
396 if (tokenB58SameSize)
398 auto const tokenB58SameData =
400 EXPECT_TRUE(tokenB58SameData);
401 if (!tokenB58SameData)
403 printAsChar(b58Result[0], b58Result[1]);
407 for (
auto i = 0uz; i < 2; ++i)
409 std::span
const outBuf{b256ResultBuf[i].
data(), b256ResultBuf[i].
size()};
412 std::string
const in(
413 b58Result[i].
data(), b58Result[i].
data() + b58Result[i].
size());
414 auto const r = xrpl::b58_fast::decodeBase58Token(tokType, in, outBuf);
416 b256Result[i] = r.value();
420 std::string
const st(b58Result[i].
begin(), b58Result[i].
end());
422 EXPECT_TRUE(s.
size());
428 auto const tokenB256SameSize = b256Result[0].
size() == b256Result[1].
size();
429 EXPECT_TRUE(tokenB256SameSize);
430 if (tokenB256SameSize)
432 auto const tokenB256SameData =
434 EXPECT_TRUE(tokenB256SameData);
435 if (!tokenB256SameData)
437 printAsInt(b256Result[0], b256Result[1]);
443 testIt(
xrpl::TokenType const tokType, std::span<std::uint8_t> b256Data)
445 testRawEncode(b256Data);
446 testTokenEncode(tokType, b256Data);
450TEST_F(Base58FastMatchesRefTest, every_token_type_over_every_byte_value)
453 for (
int i = 0; i < kNumTokenTypeIndexes; ++i)
455 std::array<std::uint8_t, 128> b256DataBuf{};
456 auto const [tokType, tokSize] = tokenTypeAndSize(i);
457 for (
int d = 0; d <= 255; ++d)
460 testIt(tokType, std::span(b256DataBuf.
data(), tokSize));
465TEST_F(Base58FastMatchesRefTest, random_data)
467 constexpr std::size_t kIters = 100000;
469 for (
auto i = 0uz; i < kIters; ++i)
471 std::array<std::uint8_t, 128> b256DataBuf{};
472 auto const [tokType, b256Data] = randomB256TestData(b256DataBuf);
473 testIt(tokType, b256Data);
std::string decodeBase58(std::string const &s)
std::string encodeBase58(void const *message, std::size_t size, void *temp, std::size_t tempSize)
std::string encodeBase58Token(TokenType type, void const *token, std::size_t size)
std::string decodeBase58Token(std::string const &s, TokenType type)
TEST_F(BaseUintDeathTest, from_raw_size_mismatch)
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.