25 #ifndef RIPPLE_BASICS_BASE_UINT_H_INCLUDED
26 #define RIPPLE_BASICS_BASE_UINT_H_INCLUDED
28 #include <ripple/basics/Expected.h>
29 #include <ripple/basics/Slice.h>
30 #include <ripple/basics/contract.h>
31 #include <ripple/basics/hardened_hash.h>
32 #include <ripple/basics/strHex.h>
33 #include <ripple/beast/utility/Zero.h>
34 #include <boost/endian/conversion.hpp>
35 #include <boost/functional/hash.hpp>
46 template <
class Container,
class = std::
void_t<>>
51 template <
class Container>
55 decltype(std::declval<Container const>().size()),
56 decltype(std::declval<Container const>().data()),
81 template <std::
size_t Bits,
class Tag =
void>
86 "The length of a base_uint in bits must be a multiple of 32.");
90 "The length of a base_uint in bits must be at least 64.");
201 auto hexCharToUInt = [](
char c,
205 if (c < '0' || c >
'f')
218 accum |= (nibble << shift);
223 decltype(
data_) ret{};
230 if (sv.size() !=
size() * 2)
234 auto in = sv.begin();
235 while (
in != sv.end())
238 for (
std::uint32_t shift : {4u, 0u, 12u, 8u, 20u, 16u, 28u, 24u})
240 if (
auto const result = hexCharToUInt(*
in++, shift, accum);
249 constexpr decltype(
data_)
256 Throw<std::invalid_argument>(
"invalid length for hex string");
258 Throw<std::range_error>(
"invalid hex character");
292 assert(c.size() *
sizeof(
typename Container::value_type) ==
size());
296 template <
class Container>
303 assert(c.size() *
sizeof(
typename Container::value_type) ==
size());
321 if (from.size() !=
size())
329 for (
int i = 0; i <
WIDTH; i++)
339 return *
this == beast::zero;
347 for (
int i = 0; i <
WIDTH; i++)
363 ul = boost::endian::native_to_big(uHost);
372 for (
int i = 0; i <
WIDTH; i++)
381 for (
int i = 0; i <
WIDTH; i++)
390 for (
int i = 0; i <
WIDTH; i++)
400 for (
int i =
WIDTH - 1; i >= 0; --i)
402 data_[i] = boost::endian::native_to_big(
403 boost::endian::big_to_native(
data_[i]) + 1);
424 for (
int i =
WIDTH - 1; i >= 0; --i)
427 data_[i] = boost::endian::native_to_big(
428 boost::endian::big_to_native(
data_[i]) - 1);
466 for (
int i =
WIDTH; i--;)
469 boost::endian::big_to_native(b.
data_[i]);
479 template <
class Hasher>
484 h(a.data_.data(),
sizeof(a.data_));
495 [[nodiscard]] constexpr
bool
506 [[nodiscard]] constexpr
bool
534 return *
this == beast::zero;
539 return *
this != beast::zero;
552 template <std::
size_t Bits,
class Tag>
567 if (ret.first == lhs.
cend())
568 return std::strong_ordering::equivalent;
570 return (*ret.first > *ret.second) ? std::strong_ordering::greater
571 : std::strong_ordering::less;
574 template <std::
size_t Bits,
typename Tag>
575 [[nodiscard]]
inline constexpr
bool
578 return (lhs <=> rhs) == 0;
582 template <std::
size_t Bits,
class Tag>
583 inline constexpr
bool
590 template <std::
size_t Bits,
class Tag>
591 inline constexpr base_uint<Bits, Tag>
597 template <std::
size_t Bits,
class Tag>
598 inline constexpr base_uint<Bits, Tag>
604 template <std::
size_t Bits,
class Tag>
605 inline constexpr base_uint<Bits, Tag>
611 template <std::
size_t Bits,
class Tag>
612 inline constexpr base_uint<Bits, Tag>
619 template <std::
size_t Bits,
class Tag>
626 template <std::
size_t Bits,
class Tag>
633 #ifndef __INTELLISENSE__
634 static_assert(
sizeof(
uint128) == 128 / 8,
"There should be no padding bytes");
635 static_assert(
sizeof(
uint160) == 160 / 8,
"There should be no padding bytes");
636 static_assert(
sizeof(
uint256) == 256 / 8,
"There should be no padding bytes");
643 template <std::
size_t Bits,
class Tag>
constexpr base_uint< Bits, Tag > operator|(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
const base_uint operator--(int)
friend void hash_append(Hasher &h, base_uint const &a) noexcept
Construct from a raw pointer.
An immutable linear range of bytes.
base_uint(Container const &c)
const_pointer data() const
Unexpected(E(&)[N]) -> Unexpected< E const * >
constexpr std::strong_ordering operator<=>(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
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)
const_iterator cbegin() const
static constexpr std::size_t WIDTH
static std::optional< base_uint > fromVoidChecked(T const &from)
std::ostream & operator<<(std::ostream &os, TOffer< TIn, TOut > const &offer)
bool operator==(Manifest const &lhs, Manifest const &rhs)
constexpr static std::size_t size()
const_pointer const_iterator
constexpr Expected< decltype(data_), ParseResult > parseFromStringView(std::string_view sv) noexcept
constexpr base_uint operator~() const
bool parseHex(std::string const &str)
is_uniquely_represented()=default
Integers of any length that is a multiple of 32-bits.
value_type const & const_reference
base_uint & operator|=(const base_uint &b)
static constexpr std::size_t bytes
std::array< std::uint32_t, WIDTH > data_
constexpr base_uint< Bits, Tag > operator+(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
base_uint & operator&=(const base_uint &b)
const_iterator begin() const
Zero allows classes to offer efficient comparisons to zero.
Seed functor once per construction.
constexpr base_uint< Bits, Tag > operator^(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
constexpr int signum() const
constexpr base_uint(beast::Zero)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
constexpr bool parseHex(const char *str)
constexpr base_uint< Bits, Tag > operator&(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
base_uint(void const *data, VoidHelper)
constexpr decltype(data_) parseFromStringViewThrows(std::string_view sv) noexcept(false)
base_uint< Bits, Tag > & operator=(beast::Zero)
base_uint(std::uint64_t b)
static base_uint fromVoid(void const *data)
const_iterator end() const
base_uint & operator^=(const base_uint &b)
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
const base_uint operator++(int)
const_iterator cend() const
std::string strHex(FwdIt begin, FwdIt end)
base_uint & operator=(std::uint64_t uHost)
base_uint & operator+=(const base_uint &b)
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
value_type const * const_pointer
constexpr base_uint(std::string_view sv) noexcept(false)