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/contract.h>
30 #include <ripple/basics/hardened_hash.h>
31 #include <ripple/basics/strHex.h>
32 #include <ripple/beast/utility/Zero.h>
33 #include <boost/endian/conversion.hpp>
34 #include <boost/functional/hash.hpp>
44 template <
class Container,
class = std::
void_t<>>
49 template <
class Container>
53 decltype(std::declval<Container const>().size()),
54 decltype(std::declval<Container const>().data()),
74 template <std::
size_t Bits,
class Tag =
void>
79 "The length of a base_uint in bits must be a multiple of 32.");
83 "The length of a base_uint in bits must be at least 64.");
194 auto hexCharToUInt = [](
char c,
198 if (c < '0' || c >
'f')
211 accum |= (nibble << shift);
216 decltype(
data_) ret{};
223 if (sv.size() !=
size() * 2)
227 auto in = sv.begin();
228 while (
in != sv.end())
231 for (
std::uint32_t shift : {4u, 0u, 12u, 8u, 20u, 16u, 28u, 24u})
233 if (
auto const result = hexCharToUInt(*
in++, shift, accum);
242 constexpr decltype(
data_)
249 Throw<std::invalid_argument>(
"invalid length for hex string");
251 Throw<std::range_error>(
"invalid hex character");
285 assert(c.size() *
sizeof(
typename Container::value_type) ==
size());
289 template <
class Container>
296 assert(c.size() *
sizeof(
typename Container::value_type) ==
size());
314 if (from.size() !=
size())
322 for (
int i = 0; i <
WIDTH; i++)
332 return *
this == beast::zero;
340 for (
int i = 0; i <
WIDTH; i++)
356 ul = boost::endian::native_to_big(uHost);
365 for (
int i = 0; i <
WIDTH; i++)
374 for (
int i = 0; i <
WIDTH; i++)
383 for (
int i = 0; i <
WIDTH; i++)
393 for (
int i =
WIDTH - 1; i >= 0; --i)
395 data_[i] = boost::endian::native_to_big(
396 boost::endian::big_to_native(
data_[i]) + 1);
417 for (
int i =
WIDTH - 1; i >= 0; --i)
419 auto prev =
data_[i];
420 data_[i] = boost::endian::native_to_big(
421 boost::endian::big_to_native(
data_[i]) - 1);
445 for (
int i =
WIDTH; i--;)
448 boost::endian::big_to_native(b.
data_[i]);
458 template <
class Hasher>
463 h(a.data_.data(),
sizeof(a.data_));
474 [[nodiscard]] constexpr
bool
485 [[nodiscard]] constexpr
bool
513 return *
this == beast::zero;
518 return *
this != beast::zero;
531 template <std::
size_t Bits,
class Tag>
537 if (ret.first == a.
cend())
541 if (*ret.first > *ret.second)
548 template <std::
size_t Bits,
class Tag>
555 template <std::
size_t Bits,
class Tag>
562 template <std::
size_t Bits,
class Tag>
569 template <std::
size_t Bits,
class Tag>
576 template <std::
size_t Bits,
class Tag>
583 template <std::
size_t Bits,
class Tag>
591 template <std::
size_t Bits,
class Tag>
598 template <std::
size_t Bits,
class Tag>
606 template <std::
size_t Bits,
class Tag>
607 inline const base_uint<Bits, Tag>
613 template <std::
size_t Bits,
class Tag>
614 inline const base_uint<Bits, Tag>
620 template <std::
size_t Bits,
class Tag>
621 inline const base_uint<Bits, Tag>
627 template <std::
size_t Bits,
class Tag>
628 inline const base_uint<Bits, Tag>
635 template <std::
size_t Bits,
class Tag>
642 template <std::
size_t Bits,
class Tag>
649 #ifndef __INTELLISENSE__
650 static_assert(
sizeof(
uint128) == 128 / 8,
"There should be no padding bytes");
651 static_assert(
sizeof(
uint160) == 160 / 8,
"There should be no padding bytes");
652 static_assert(
sizeof(
uint256) == 256 / 8,
"There should be no padding bytes");
659 template <std::
size_t Bits,
class Tag>
bool operator>(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
const 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.
bool operator>=(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
base_uint(Container const &c)
const_pointer data() const
Unexpected(E(&)[N]) -> Unexpected< E const * >
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
const 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
bool operator<=(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
base_uint & operator|=(const base_uint &b)
const base_uint< Bits, Tag > operator+(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
static constexpr std::size_t bytes
std::array< std::uint32_t, WIDTH > data_
bool operator<(CanonicalTXSet::Key const &lhs, CanonicalTXSet::Key const &rhs)
int compare(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
base_uint & operator&=(const base_uint &b)
const_iterator begin() const
bool operator!=(Manifest const &lhs, Manifest const &rhs)
Zero allows classes to offer efficient comparisons to zero.
Seed functor once per construction.
constexpr int signum() const
constexpr base_uint(beast::Zero)
const base_uint< Bits, Tag > operator^(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
const base_uint< Bits, Tag > operator&(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
constexpr bool parseHex(const char *str)
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)