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 shift : {4u, 0u, 12u, 8u, 20u, 16u, 28u, 24u})
225 constexpr decltype(
data_)
232 Throw<std::invalid_argument>(
"invalid length for hex string");
234 Throw<std::range_error>(
"invalid hex character");
268 c.size() *
sizeof(
typename Container::value_type) ==
size(),
269 "xrpl::base_uint::base_uint(Container auto) : input size match");
273 template <
class Container>
281 c.size() *
sizeof(
typename Container::value_type) ==
size(),
282 "xrpl::base_uint::operator=(Container auto) : input size match");
300 if (from.size() !=
size())
308 for (
int i = 0; i <
WIDTH; i++)
318 return *
this == beast::zero;
326 for (
int i = 0; i <
WIDTH; i++)
342 ul = boost::endian::native_to_big(uHost);
351 for (
int i = 0; i <
WIDTH; i++)
360 for (
int i = 0; i <
WIDTH; i++)
369 for (
int i = 0; i <
WIDTH; i++)
379 for (
int i =
WIDTH - 1; i >= 0; --i)
381 data_[i] = boost::endian::native_to_big(boost::endian::big_to_native(
data_[i]) + 1);
402 for (
int i =
WIDTH - 1; i >= 0; --i)
405 data_[i] = boost::endian::native_to_big(boost::endian::big_to_native(
data_[i]) - 1);
443 for (
int i =
WIDTH; i--;)
454 template <
class Hasher>
459 h(a.data_.data(),
sizeof(a.data_));
470 [[nodiscard]]
constexpr bool
481 [[nodiscard]]
constexpr bool
510 return *
this == beast::zero;
515 return *
this != beast::zero;
529template <std::
size_t Bits,
class Tag>
544 if (ret.first == lhs.
cend())
545 return std::strong_ordering::equivalent;
547 return (*ret.first > *ret.second) ? std::strong_ordering::greater : std::strong_ordering::less;
550template <std::
size_t Bits,
typename Tag>
551[[nodiscard]]
inline constexpr bool
554 return (lhs <=> rhs) == 0;
558template <std::
size_t Bits,
class Tag>
566template <std::
size_t Bits,
class Tag>
567inline constexpr base_uint<Bits, Tag>
573template <std::
size_t Bits,
class Tag>
574inline constexpr base_uint<Bits, Tag>
580template <std::
size_t Bits,
class Tag>
581inline constexpr base_uint<Bits, Tag>
587template <std::
size_t Bits,
class Tag>
588inline constexpr base_uint<Bits, Tag>
595template <std::
size_t Bits,
class Tag>
602template <std::
size_t Bits,
class Tag>
610template <std::
size_t Bits,
class Tag>
628#ifndef __INTELLISENSE__
629static_assert(
sizeof(
uint128) == 128 / 8,
"There should be no padding bytes");
630static_assert(
sizeof(
uint160) == 160 / 8,
"There should be no padding bytes");
631static_assert(
sizeof(
uint192) == 192 / 8,
"There should be no padding bytes");
632static_assert(
sizeof(
uint256) == 256 / 8,
"There should be no padding bytes");
639template <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.