20 #ifndef RIPPLE_PROTOCOL_B58_UTILS_H_INCLUDED
21 #define RIPPLE_PROTOCOL_B58_UTILS_H_INCLUDED
23 #include <ripple/basics/contract.h>
25 #include <boost/outcome.hpp>
26 #include <boost/outcome/result.hpp>
37 using Result = boost::outcome_v2::result<T, std::error_code>;
47 return {a / b, a % b};
54 unsigned __int128
const x = a;
55 unsigned __int128
const y = b;
56 unsigned __int128
const c = x * y + carry;
57 return {c & 0xffff
'ffff'ffff
'ffff, c >> 64};
60 [[nodiscard]] inline std::tuple<std::uint64_t, std::uint64_t>
61 carrying_add(std::uint64_t a, std::uint64_t b)
63 unsigned __int128 const x = a;
64 unsigned __int128 const y = b;
65 unsigned __int128 const c = x + y;
66 return {c & 0xffff'ffff
'ffff'ffff, c >> 64};
83 std::tie(a[0], carry) = carrying_add(a[0], b);
91 std::tie(v, carry) = carrying_add(v, 1);
104 LogicError(
"Empty span passed to inplace_bigint_mul");
107 auto const last_index = a.
size() - 1;
108 if (a[last_index] != 0)
110 LogicError(
"Non-zero element in inplace_bigint_mul last index");
114 for (
auto& coeff : a.
subspan(0, last_index))
116 std::tie(coeff, carry) = carrying_mul(coeff, b, carry);
118 a[last_index] = carry;
125 if (numerator.
size() == 0)
135 unsigned __int128
const high128 = high;
136 unsigned __int128
const low128 = low;
137 return ((high128 << 64) | low128);
140 [](
unsigned __int128 num,
142 unsigned __int128
const denom128 = denom;
143 unsigned __int128
const d = num / denom128;
144 unsigned __int128
const r = num - (denom128 * d);
145 assert(d >> 64 == 0);
146 assert(r >> 64 == 0);
151 int const last_index = numerator.
size() - 1;
152 std::tie(numerator[last_index], prev_rem) =
153 div_rem(numerator[last_index], divisor);
154 for (
int i = last_index - 1; i >= 0; --i)
156 unsigned __int128
const cur_num = to_u128(prev_rem, numerator[i]);
157 std::tie(numerator[i], prev_rem) = div_rem_64(cur_num, divisor);
169 if (input >= B_58_10)
171 LogicError(
"Input to b58_10_to_b58_be equals or exceeds 58^10.");
180 std::tie(input, rem) = div_rem(input, 58);
181 result[resultSize - 1 - i] = rem;
192 #endif // RIPPLE_PROTOCOL_B58_UTILS_H_INCLUDED