1#ifndef XRPL_PROTOCOL_B58_UTILS_H_INCLUDED
2#define XRPL_PROTOCOL_B58_UTILS_H_INCLUDED
4#include <xrpl/basics/contract.h>
5#include <xrpl/beast/utility/instrumentation.h>
6#include <xrpl/protocol/detail/token_errors.h>
8#include <boost/outcome.hpp>
9#include <boost/outcome/result.hpp>
18using Result = boost::outcome_v2::result<T, std::error_code>;
28 return {a / b, a % b};
35 unsigned __int128
const x = a;
36 unsigned __int128
const y = b;
37 unsigned __int128
const c = x * y + carry;
38 return {c & 0xffff'ffff'ffff'ffff, c >> 64};
44 unsigned __int128
const x = a;
45 unsigned __int128
const y = b;
46 unsigned __int128
const c = x + y;
47 return {c & 0xffff'ffff'ffff'ffff, c >> 64};
60 return TokenCodecErrc::inputTooSmall;
64 std::tie(a[0], carry) = carrying_add(a[0], b);
66 for (
auto& v : a.subspan(1))
70 return TokenCodecErrc::success;
72 std::tie(v, carry) = carrying_add(v, 1);
76 return TokenCodecErrc::overflowAdd;
78 return TokenCodecErrc::success;
86 return TokenCodecErrc::inputTooSmall;
89 auto const last_index = a.size() - 1;
90 if (a[last_index] != 0)
92 return TokenCodecErrc::inputTooLarge;
96 for (
auto& coeff : a.subspan(0, last_index))
98 std::tie(coeff, carry) = carrying_mul(coeff, b, carry);
100 a[last_index] = carry;
101 return TokenCodecErrc::success;
109 if (numerator.
size() == 0)
115 "ripple::b58_fast::detail::inplace_bigint_div_rem : empty "
123 unsigned __int128
const high128 = high;
124 unsigned __int128
const low128 = low;
125 return ((high128 << 64) | low128);
128 [](
unsigned __int128 num,
130 unsigned __int128
const denom128 = denom;
131 unsigned __int128
const d = num / denom128;
132 unsigned __int128
const r = num - (denom128 * d);
135 "ripple::b58_fast::detail::inplace_bigint_div_rem::div_rem_64 : "
136 "valid division result");
139 "ripple::b58_fast::detail::inplace_bigint_div_rem::div_rem_64 : "
145 int const last_index = numerator.
size() - 1;
146 std::tie(numerator[last_index], prev_rem) =
147 div_rem(numerator[last_index], divisor);
148 for (
int i = last_index - 1; i >= 0; --i)
150 unsigned __int128
const cur_num = to_u128(prev_rem, numerator[i]);
151 std::tie(numerator[i], prev_rem) = div_rem_64(cur_num, divisor);
166 "ripple::b58_fast::detail::b58_10_to_b58_be : valid input");
173 std::tie(input, rem) = div_rem(input, 58);
174 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