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
137 [[nodiscard]] const_iterator
142 [[nodiscard]] const_iterator
147 [[nodiscard]] const_iterator
152 [[nodiscard]] const_iterator
193 constexpr std::expected<
decltype(data_), ParseResult>
198 auto const hexCharToUInt =
201 if (c <
'0' || c >
'f')
202 return ParseResult::BadChar;
218 return ParseResult::BadChar;
220 accum |= (nibble << shift);
222 return ParseResult::Okay;
225 decltype(
data_) ret{};
236 auto in = sv.
begin();
237 while (in != sv.
end())
240 for (
std::uint32_t const shift : {4u, 0u, 12u, 8u, 20u, 16u, 28u, 24u})
242 if (
auto const result = hexCharToUInt(*in++, shift, accum);
243 result != ParseResult::Okay)
251 constexpr decltype(data_)
257 if (result.error() == ParseResult::BadLength)
287 template <
class Container>
297 "This constructor is not intended to be used and will be soon removed. "
298 "Use base_uint::fromRaw instead.");
301 template <
class Container>
310 c.size() *
sizeof(
typename Container::value_type) ==
size(),
311 "xrpl::BaseUInt::fromRaw(Container auto) : input size match");
313 std::min(
size(), c.size() *
sizeof(
typename Container::value_type));
318 template <
class Container>
326 c.size() *
sizeof(
typename Container::value_type) ==
size(),
327 "xrpl::BaseUInt::operator=(Container auto) : input size match");
329 std::min(
size(), c.size() *
sizeof(
typename Container::value_type));
330 if (canCopy <
size())
349 if (from.size() !=
size())
354 [[nodiscard]]
constexpr int
357 for (
int i = 0; i <
kWidth; i++)
377 for (
int i = 0; i <
kWidth; i++)
395 ul = boost::endian::native_to_big(uHost);
404 for (
int i = 0; i <
kWidth; i++)
413 for (
int i = 0; i <
kWidth; i++)
422 for (
int i = 0; i <
kWidth; i++)
432 for (
int i =
kWidth - 1; i >= 0; --i)
434 data_[i] = boost::endian::native_to_big(boost::endian::big_to_native(
data_[i]) + 1);
455 for (
int i =
kWidth - 1; i >= 0; --i)
458 data_[i] = boost::endian::native_to_big(boost::endian::big_to_native(
data_[i]) - 1);
496 for (
int i =
kWidth - 1; i >= 0; i--)
499 boost::endian::big_to_native(b.
data_[i]);
508 template <
class Hasher>
513 h(a.data_.data(),
sizeof(a.data_));
525 [[nodiscard]]
constexpr bool
536 [[nodiscard]]
constexpr bool
590template <std::
size_t Bits,
class Tag>
605 if (ret.first == lhs.
cend())
606 return std::strong_ordering::equivalent;
608 return (*ret.first > *ret.second) ? std::strong_ordering::greater : std::strong_ordering::less;
611template <std::
size_t Bits,
typename Tag>
612[[nodiscard]]
constexpr bool
615 return (lhs <=> rhs) == 0;
619template <std::
size_t Bits,
class 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>
648template <std::
size_t Bits,
class Tag>
649constexpr BaseUInt<Bits, Tag>
656template <std::
size_t Bits,
class Tag>
663template <std::
size_t Bits,
class Tag>
671template <std::
size_t Bits,
class Tag>
689#ifndef __INTELLISENSE__
690static_assert(
sizeof(
UInt128) == 128 / 8,
"There should be no padding bytes");
691static_assert(
sizeof(
UInt160) == 160 / 8,
"There should be no padding bytes");
692static_assert(
sizeof(
UInt192) == 192 / 8,
"There should be no padding bytes");
693static_assert(
sizeof(
UInt256) == 256 / 8,
"There should be no padding bytes");
700template <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)
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)
std::string toShortString(BaseUInt< Bits, Tag > const &a)
constexpr std::strong_ordering operator<=>(BaseUInt< Bits, Tag > const &lhs, BaseUInt< Bits, Tag > const &rhs)
std::size_t extract(UInt256 const &key)
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
IsUniquelyRepresented()=default
Zero allows classes to offer efficient comparisons to zero.