3#include <xrpl/basics/contract.h>
4#include <xrpl/beast/utility/instrumentation.h>
5#include <xrpl/protocol/detail/token_errors.h>
7#include <boost/outcome.hpp>
8#include <boost/outcome/result.hpp>
17using Result = boost::outcome_v2::result<T, std::error_code>;
27 return {a / b, a % b};
34 unsigned __int128
const x = a;
35 unsigned __int128
const y = b;
36 unsigned __int128
const c = x * y + carry;
37 return {c & 0xffff'ffff'ffff'ffff, c >> 64};
43 unsigned __int128
const x = a;
44 unsigned __int128
const y = b;
45 unsigned __int128
const c = x + y;
46 return {c & 0xffff'ffff'ffff'ffff, c >> 64};
59 return TokenCodecErrc::inputTooSmall;
63 std::tie(a[0], carry) = carrying_add(a[0], b);
65 for (
auto& v : a.subspan(1))
69 return TokenCodecErrc::success;
71 std::tie(v, carry) = carrying_add(v, 1);
75 return TokenCodecErrc::overflowAdd;
77 return TokenCodecErrc::success;
85 return TokenCodecErrc::inputTooSmall;
88 auto const last_index = a.size() - 1;
89 if (a[last_index] != 0)
91 return TokenCodecErrc::inputTooLarge;
95 for (
auto& coeff : a.subspan(0, last_index))
97 std::tie(coeff, carry) = carrying_mul(coeff, b, carry);
99 a[last_index] = carry;
100 return TokenCodecErrc::success;
108 if (numerator.
size() == 0)
114 "xrpl::b58_fast::detail::inplace_bigint_div_rem : empty "
121 unsigned __int128
const high128 = high;
122 unsigned __int128
const low128 = low;
123 return ((high128 << 64) | low128);
126 unsigned __int128
const denom128 = denom;
127 unsigned __int128
const d = num / denom128;
128 unsigned __int128
const r = num - (denom128 * d);
131 "xrpl::b58_fast::detail::inplace_bigint_div_rem::div_rem_64 : "
132 "valid division result");
135 "xrpl::b58_fast::detail::inplace_bigint_div_rem::div_rem_64 : "
141 int const last_index = numerator.
size() - 1;
142 std::tie(numerator[last_index], prev_rem) = div_rem(numerator[last_index], divisor);
143 for (
int i = last_index - 1; i >= 0; --i)
145 unsigned __int128
const cur_num = to_u128(prev_rem, numerator[i]);
146 std::tie(numerator[i], prev_rem) = div_rem_64(cur_num, divisor);
157 [[maybe_unused]]
static constexpr std::uint64_t B_58_10 = 430804206899405824;
158 XRPL_ASSERT(input < B_58_10,
"xrpl::b58_fast::detail::b58_10_to_b58_be : valid input");
165 std::tie(input, rem) = div_rem(input, 58);
166 result[resultSize - 1 - i] = rem;
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
boost::outcome_v2::result< T, std::error_code > Result