8#include <xrpl/basics/Expected.h>
9#include <xrpl/basics/Slice.h>
10#include <xrpl/basics/contract.h>
11#include <xrpl/basics/hardened_hash.h>
12#include <xrpl/basics/partitioned_unordered_map.h>
13#include <xrpl/basics/strHex.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>
29template <
class Container,
class = std::
void_t<>>
34template <
class Container>
38 decltype(std::declval<Container const>().size()),
39 decltype(std::declval<Container const>().data()),
64template <std::
size_t Bits,
class Tag =
void>
67 static_assert((Bits % 32) == 0,
"The length of a base_uint in bits must be a multiple of 32.");
69 static_assert(Bits >= 64,
"The length of a base_uint in bits must be at least 64.");
182 if (c <
'0' || c >
'f')
195 accum |= (nibble << shift);
200 decltype(
data_) ret{};
212 while (
in != sv.
end())
215 for (
std::uint32_t const shift : {4u, 0u, 12u, 8u, 20u, 16u, 28u, 24u})
217 if (
auto const result = hexCharToUInt(*
in++, shift, accum);
226 constexpr decltype(
data_)
233 Throw<std::invalid_argument>(
"invalid length for hex string");
235 Throw<std::range_error>(
"invalid hex character");
270 c.size() *
sizeof(
typename Container::value_type) ==
size(),
271 "xrpl::base_uint::base_uint(Container auto) : input size match");
275 template <
class Container>
283 c.size() *
sizeof(
typename Container::value_type) ==
size(),
284 "xrpl::base_uint::operator=(Container auto) : input size match");
302 if (from.size() !=
size())
310 for (
int i = 0; i <
WIDTH; i++)
320 return *
this == beast::zero;
328 for (
int i = 0; i <
WIDTH; i++)
346 ul = boost::endian::native_to_big(uHost);
355 for (
int i = 0; i <
WIDTH; i++)
364 for (
int i = 0; i <
WIDTH; i++)
373 for (
int i = 0; i <
WIDTH; i++)
383 for (
int i =
WIDTH - 1; i >= 0; --i)
385 data_[i] = boost::endian::native_to_big(boost::endian::big_to_native(
data_[i]) + 1);
406 for (
int i =
WIDTH - 1; i >= 0; --i)
409 data_[i] = boost::endian::native_to_big(boost::endian::big_to_native(
data_[i]) - 1);
447 for (
int i =
WIDTH; i--;)
450 boost::endian::big_to_native(b.
data_[i]);
459 template <
class Hasher>
464 h(a.data_.data(),
sizeof(a.data_));
475 [[nodiscard]]
constexpr bool
486 [[nodiscard]]
constexpr bool
515 return *
this == beast::zero;
520 return *
this != beast::zero;
534template <std::
size_t Bits,
class Tag>
549 if (ret.first == lhs.
cend())
550 return std::strong_ordering::equivalent;
552 return (*ret.first > *ret.second) ? std::strong_ordering::greater : std::strong_ordering::less;
555template <std::
size_t Bits,
typename Tag>
556[[nodiscard]]
inline constexpr bool
559 return (lhs <=> rhs) == 0;
563template <std::
size_t Bits,
class Tag>
571template <std::
size_t Bits,
class Tag>
572inline constexpr base_uint<Bits, Tag>
578template <std::
size_t Bits,
class Tag>
579inline constexpr base_uint<Bits, Tag>
585template <std::
size_t Bits,
class Tag>
586inline constexpr base_uint<Bits, Tag>
592template <std::
size_t Bits,
class Tag>
593inline constexpr base_uint<Bits, Tag>
600template <std::
size_t Bits,
class Tag>
607template <std::
size_t Bits,
class Tag>
615template <std::
size_t Bits,
class Tag>
633#ifndef __INTELLISENSE__
634static_assert(
sizeof(
uint128) == 128 / 8,
"There should be no padding bytes");
635static_assert(
sizeof(
uint160) == 160 / 8,
"There should be no padding bytes");
636static_assert(
sizeof(
uint192) == 192 / 8,
"There should be no padding bytes");
637static_assert(
sizeof(
uint256) == 256 / 8,
"There should be no padding bytes");
644template <std::
size_t Bits,
class Tag>
An immutable linear range of bytes.
Integers of any length that is a multiple of 32-bits.
const_iterator cend() const
base_uint(std::uint64_t b)
static base_uint fromVoid(void const *data)
base_uint(void const *data, VoidHelper)
const_iterator cbegin() const
base_uint & operator&=(base_uint const &b)
base_uint & operator^=(base_uint const &b)
bool parseHex(std::string const &str)
constexpr decltype(data_) parseFromStringViewThrows(std::string_view sv) noexcept(false)
base_uint & operator=(std::uint64_t uHost)
base_uint & operator|=(base_uint const &b)
std::enable_if_t< detail::is_contiguous_container< Container >::value &&std::is_trivially_copyable< typename Container::value_type >::value, base_uint & > operator=(Container const &c)
constexpr base_uint(std::string_view sv) noexcept(false)
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
static std::optional< base_uint > fromVoidChecked(T const &from)
const_pointer data() const
const_pointer const_iterator
static constexpr std::size_t WIDTH
base_uint(Container const &c)
value_type const & const_reference
constexpr int signum() const
base_uint< Bits, Tag > & operator=(beast::Zero)
base_uint & operator+=(base_uint const &b)
constexpr bool parseHex(char const *str)
value_type const * const_pointer
constexpr base_uint operator~() const
constexpr Expected< decltype(data_), ParseResult > parseFromStringView(std::string_view sv) noexcept
const_iterator begin() const
std::array< std::uint32_t, WIDTH > data_
constexpr base_uint(beast::Zero)
base_uint const operator++(int)
base_uint const operator--(int)
static std::size_t constexpr bytes
const_iterator end() const
static constexpr std::size_t size()
friend void hash_append(Hasher &h, base_uint const &a) noexcept
Seed functor once per construction.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::size_t extract(uint256 const &key)
constexpr base_uint< Bits, Tag > operator+(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
std::string to_string(base_uint< Bits, Tag > const &a)
constexpr base_uint< Bits, Tag > operator|(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
std::string strHex(FwdIt begin, FwdIt end)
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
constexpr base_uint< Bits, Tag > operator^(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
constexpr base_uint< Bits, Tag > operator&(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
constexpr std::strong_ordering operator<=>(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
std::string to_short_string(base_uint< Bits, Tag > const &a)
Zero allows classes to offer efficient comparisons to zero.
is_uniquely_represented()=default
Construct from a raw pointer.