25#ifndef RIPPLE_BASICS_BASE_UINT_H_INCLUDED
26#define RIPPLE_BASICS_BASE_UINT_H_INCLUDED
28#include <xrpl/basics/Expected.h>
29#include <xrpl/basics/Slice.h>
30#include <xrpl/basics/contract.h>
31#include <xrpl/basics/hardened_hash.h>
32#include <xrpl/basics/partitioned_unordered_map.h>
33#include <xrpl/basics/strHex.h>
34#include <xrpl/beast/utility/Zero.h>
35#include <xrpl/beast/utility/instrumentation.h>
37#include <boost/endian/conversion.hpp>
38#include <boost/functional/hash.hpp>
49template <
class Container,
class = std::
void_t<>>
54template <
class Container>
58 decltype(std::declval<Container const>().size()),
59 decltype(std::declval<Container const>().data()),
84template <std::
size_t Bits,
class Tag =
void>
89 "The length of a base_uint in bits must be a multiple of 32.");
93 "The length of a base_uint in bits must be at least 64.");
204 auto hexCharToUInt = [](
char c,
208 if (c < '0' || c >
'f')
221 accum |= (nibble << shift);
226 decltype(
data_) ret{};
238 while (
in != sv.
end())
241 for (
std::uint32_t shift : {4u, 0u, 12u, 8u, 20u, 16u, 28u, 24u})
243 if (
auto const result = hexCharToUInt(*
in++, shift, accum);
252 constexpr decltype(
data_)
259 Throw<std::invalid_argument>(
"invalid length for hex string");
261 Throw<std::range_error>(
"invalid hex character");
296 c.size() *
sizeof(
typename Container::value_type) ==
size(),
297 "ripple::base_uint::base_uint(Container auto) : input size match");
301 template <
class Container>
309 c.size() *
sizeof(
typename Container::value_type) ==
size(),
310 "ripple::base_uint::operator=(Container auto) : input size match");
328 if (from.size() !=
size())
336 for (
int i = 0; i <
WIDTH; i++)
346 return *
this == beast::zero;
354 for (
int i = 0; i <
WIDTH; i++)
370 ul = boost::endian::native_to_big(uHost);
379 for (
int i = 0; i <
WIDTH; i++)
388 for (
int i = 0; i <
WIDTH; i++)
397 for (
int i = 0; i <
WIDTH; i++)
407 for (
int i =
WIDTH - 1; i >= 0; --i)
409 data_[i] = boost::endian::native_to_big(
410 boost::endian::big_to_native(
data_[i]) + 1);
431 for (
int i =
WIDTH - 1; i >= 0; --i)
434 data_[i] = boost::endian::native_to_big(
435 boost::endian::big_to_native(
data_[i]) - 1);
473 for (
int i =
WIDTH; i--;)
476 boost::endian::big_to_native(b.
data_[i]);
486 template <
class Hasher>
491 h(a.data_.data(),
sizeof(a.data_));
502 [[nodiscard]]
constexpr bool
513 [[nodiscard]]
constexpr bool
542 return *
this == beast::zero;
547 return *
this != beast::zero;
561template <std::
size_t Bits,
class Tag>
576 if (ret.first == lhs.
cend())
577 return std::strong_ordering::equivalent;
579 return (*ret.first > *ret.second) ? std::strong_ordering::greater
580 : std::strong_ordering::less;
583template <std::
size_t Bits,
typename Tag>
584[[nodiscard]]
inline constexpr bool
587 return (lhs <=> rhs) == 0;
591template <std::
size_t Bits,
class Tag>
599template <std::
size_t Bits,
class Tag>
600inline constexpr base_uint<Bits, Tag>
606template <std::
size_t Bits,
class Tag>
607inline constexpr base_uint<Bits, Tag>
613template <std::
size_t Bits,
class Tag>
614inline constexpr base_uint<Bits, Tag>
620template <std::
size_t Bits,
class Tag>
621inline constexpr base_uint<Bits, Tag>
628template <std::
size_t Bits,
class Tag>
635template <std::
size_t Bits,
class Tag>
653#ifndef __INTELLISENSE__
654static_assert(
sizeof(
uint128) == 128 / 8,
"There should be no padding bytes");
655static_assert(
sizeof(
uint160) == 160 / 8,
"There should be no padding bytes");
656static_assert(
sizeof(
uint192) == 192 / 8,
"There should be no padding bytes");
657static_assert(
sizeof(
uint256) == 256 / 8,
"There should be no padding bytes");
664template <std::
size_t Bits,
class Tag>
An immutable linear range of bytes.
Integers of any length that is a multiple of 32-bits.
static std::size_t constexpr bytes
base_uint & operator+=(const base_uint &b)
const_pointer data() const
static std::optional< base_uint > fromVoidChecked(T const &from)
const base_uint operator--(int)
constexpr decltype(data_) parseFromStringViewThrows(std::string_view sv) noexcept(false)
static base_uint fromVoid(void const *data)
const_pointer const_iterator
const base_uint operator++(int)
const_iterator cbegin() const
base_uint & operator|=(const base_uint &b)
bool parseHex(std::string const &str)
constexpr base_uint(std::string_view sv) noexcept(false)
static constexpr std::size_t size()
constexpr base_uint operator~() const
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
const_iterator end() const
const_iterator cend() const
base_uint(Container const &c)
constexpr bool parseHex(const char *str)
constexpr int signum() const
base_uint & operator^=(const base_uint &b)
base_uint & operator&=(const base_uint &b)
static constexpr std::size_t WIDTH
value_type const & const_reference
value_type const * const_pointer
base_uint & operator=(std::uint64_t uHost)
constexpr Expected< decltype(data_), ParseResult > parseFromStringView(std::string_view sv) noexcept
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)
base_uint(std::uint64_t b)
const_iterator begin() const
base_uint(void const *data, VoidHelper)
constexpr base_uint(beast::Zero)
std::array< std::uint32_t, WIDTH > data_
friend void hash_append(Hasher &h, base_uint const &a) noexcept
base_uint< Bits, Tag > & operator=(beast::Zero)
Seed functor once per construction.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
constexpr base_uint< Bits, Tag > operator|(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
constexpr std::strong_ordering operator<=>(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
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)
std::string strHex(FwdIt begin, FwdIt end)
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)
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)
std::size_t extract(uint256 const &key)
Zero allows classes to offer efficient comparisons to zero.
is_uniquely_represented()=default
Construct from a raw pointer.