8#include <xrpl/basics/Slice.h>
9#include <xrpl/basics/contract.h>
10#include <xrpl/basics/hardened_hash.h>
11#include <xrpl/basics/partitioned_unordered_map.h>
12#include <xrpl/basics/strHex.h>
13#include <xrpl/beast/hash/hash_append.h>
14#include <xrpl/beast/utility/Zero.h>
15#include <xrpl/beast/utility/instrumentation.h>
17#include <boost/endian/conversion.hpp>
18#include <boost/functional/hash.hpp>
39template <
class Container,
class = std::
void_t<>>
44template <
class Container>
48 decltype(std::declval<Container const>().size()),
49 decltype(std::declval<Container const>().data()),
80template <std::
size_t Bits,
class Tag =
void>
83 static_assert((Bits % 32) == 0,
"The length of a base_uint in bits must be a multiple of 32.");
85 static_assert(Bits >= 64,
"The length of a base_uint in bits must be at least 64.");
121 [[nodiscard]] const_pointer
193 constexpr std::expected<
decltype(data_), ParseResult>
200 if (c <
'0' || c >
'f')
201 return ParseResult::BadChar;
217 return ParseResult::BadChar;
219 accum |= (nibble << shift);
221 return ParseResult::Okay;
224 decltype(
data_) ret{};
235 auto in = sv.
begin();
236 while (in != sv.
end())
239 for (
std::uint32_t const shift : {4u, 0u, 12u, 8u, 20u, 16u, 28u, 24u})
241 if (
auto const result = hexCharToUInt(*in++, shift, accum);
242 result != ParseResult::Okay)
250 constexpr decltype(data_)
256 if (result.error() == ParseResult::BadLength)
286 template <
class Container>
296 "This constructor is not intended to be used and will be soon removed. "
297 "Use base_uint::fromRaw instead.");
300 template <
class Container>
309 c.size() *
sizeof(
typename Container::value_type) ==
size(),
310 "xrpl::BaseUInt::fromRaw(Container auto) : input size match");
312 std::min(
size(), c.size() *
sizeof(
typename Container::value_type));
317 template <
class Container>
325 c.size() *
sizeof(
typename Container::value_type) ==
size(),
326 "xrpl::BaseUInt::operator=(Container auto) : input size match");
328 std::min(
size(), c.size() *
sizeof(
typename Container::value_type));
329 if (canCopy <
size())
348 if (from.size() !=
size())
353 [[nodiscard]]
constexpr int
356 for (
int i = 0; i <
kWidth; i++)
376 for (
int i = 0; i <
kWidth; i++)
394 ul = boost::endian::native_to_big(uHost);
403 for (
int i = 0; i <
kWidth; i++)
412 for (
int i = 0; i <
kWidth; i++)
421 for (
int i = 0; i <
kWidth; i++)
431 for (
int i =
kWidth - 1; i >= 0; --i)
433 data_[i] = boost::endian::native_to_big(boost::endian::big_to_native(
data_[i]) + 1);
454 for (
int i =
kWidth - 1; i >= 0; --i)
457 data_[i] = boost::endian::native_to_big(boost::endian::big_to_native(
data_[i]) - 1);
495 for (
int i =
kWidth - 1; i >= 0; i--)
498 boost::endian::big_to_native(b.
data_[i]);
507 template <
class Hasher>
512 h(a.data_.data(),
sizeof(a.data_));
524 [[nodiscard]]
constexpr bool
535 [[nodiscard]]
constexpr bool
583template <std::
size_t Bits,
class Tag>
598 if (ret.first == lhs.
cend())
599 return std::strong_ordering::equivalent;
601 return (*ret.first > *ret.second) ? std::strong_ordering::greater : std::strong_ordering::less;
604template <std::
size_t Bits,
typename Tag>
605[[nodiscard]]
constexpr bool
608 return (lhs <=> rhs) == 0;
612template <std::
size_t Bits,
class Tag>
620template <std::
size_t Bits,
class Tag>
621constexpr BaseUInt<Bits, Tag>
627template <std::
size_t Bits,
class Tag>
628constexpr BaseUInt<Bits, Tag>
634template <std::
size_t Bits,
class Tag>
635constexpr BaseUInt<Bits, Tag>
641template <std::
size_t Bits,
class Tag>
642constexpr BaseUInt<Bits, Tag>
649template <std::
size_t Bits,
class Tag>
656template <std::
size_t Bits,
class Tag>
664template <std::
size_t Bits,
class Tag>
682#ifndef __INTELLISENSE__
683static_assert(
sizeof(
uint128) == 128 / 8,
"There should be no padding bytes");
684static_assert(
sizeof(
uint160) == 160 / 8,
"There should be no padding bytes");
685static_assert(
sizeof(
uint192) == 192 / 8,
"There should be no padding bytes");
686static_assert(
sizeof(
uint256) == 256 / 8,
"There should be no padding bytes");
693template <std::
size_t Bits,
class Tag>
Integers of any length that is a multiple of 32-bits.
std::reverse_iterator< iterator > reverse_iterator
BaseUInt(std::uint64_t b)
static BaseUInt fromRaw(Container const &c)
std::ptrdiff_t difference_type
static BaseUInt fromVoid(void const *data)
const_iterator cend() const
constexpr BaseUInt(std::string_view sv) noexcept(false)
BaseUInt & operator&=(BaseUInt const &b)
bool parseHex(std::string const &str)
constexpr int signum() const
BaseUInt & operator+=(BaseUInt const &b)
constexpr bool parseHex(char const *str)
value_type const * const_pointer
const_iterator begin() const
constexpr decltype(data_) parseFromStringViewThrows(std::string_view sv) noexcept(false)
std::reverse_iterator< const_iterator > const_reverse_iterator
constexpr std::expected< decltype(data_), ParseResult > parseFromStringView(std::string_view sv) noexcept
BaseUInt & operator|=(BaseUInt const &b)
BaseUInt(Container const &c)
const_pointer data() const
static constexpr std::size_t kWidth
BaseUInt< Bits, Tag > & operator=(beast::Zero)
std::array< std::uint32_t, kWidth > data_
BaseUInt & operator=(Container const &c)
constexpr BaseUInt(beast::Zero)
HardenedHash<> hasher
Value hashing function.
BaseUInt(void const *data, VoidHelper)
friend void hash_append(Hasher &h, BaseUInt const &a) noexcept
const_pointer const_iterator
BaseUInt & operator^=(BaseUInt const &b)
constexpr BaseUInt operator~() const
static constexpr std::size_t kBytes
static std::optional< BaseUInt > fromVoidChecked(T const &from)
const_iterator end() const
BaseUInt & operator=(std::uint64_t uHost)
static constexpr std::size_t size()
value_type const & const_reference
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
const_iterator cbegin() const
Seed functor once per construction.
An immutable linear range of bytes.
T is_trivially_copyable_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
constexpr BaseUInt< Bits, Tag > operator+(BaseUInt< Bits, Tag > const &a, BaseUInt< Bits, Tag > const &b)
constexpr BaseUInt< Bits, Tag > operator^(BaseUInt< Bits, Tag > const &a, BaseUInt< Bits, Tag > const &b)
std::size_t extract(uint256 const &key)
constexpr BaseUInt< Bits, Tag > operator&(BaseUInt< Bits, Tag > const &a, BaseUInt< Bits, Tag > const &b)
constexpr bool operator==(BaseUInt< Bits, Tag > const &lhs, BaseUInt< Bits, Tag > const &rhs)
std::string strHex(FwdIt begin, FwdIt end)
constexpr BaseUInt< Bits, Tag > operator|(BaseUInt< Bits, Tag > const &a, BaseUInt< Bits, Tag > const &b)
std::ostream & operator<<(std::ostream &out, BaseUInt< Bits, Tag > const &u)
std::string to_string(BaseUInt< Bits, Tag > const &a)
Dir::ConstIterator const_iterator
std::string toShortString(BaseUInt< Bits, Tag > const &a)
constexpr std::strong_ordering operator<=>(BaseUInt< Bits, Tag > const &lhs, BaseUInt< Bits, Tag > const &rhs)
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
IsUniquelyRepresented()=default
Zero allows classes to offer efficient comparisons to zero.