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>
36#include <boost/endian/conversion.hpp>
37#include <boost/functional/hash.hpp>
48template <
class Container,
class = std::
void_t<>>
53template <
class Container>
57 decltype(std::declval<Container const>().size()),
58 decltype(std::declval<Container const>().data()),
83template <std::
size_t Bits,
class Tag =
void>
88 "The length of a base_uint in bits must be a multiple of 32.");
92 "The length of a base_uint in bits must be at least 64.");
203 auto hexCharToUInt = [](
char c,
207 if (c < '0' || c >
'f')
220 accum |= (nibble << shift);
225 decltype(
data_) ret{};
237 while (
in != sv.
end())
240 for (
std::uint32_t shift : {4u, 0u, 12u, 8u, 20u, 16u, 28u, 24u})
242 if (
auto const result = hexCharToUInt(*
in++, shift, accum);
251 constexpr decltype(
data_)
258 Throw<std::invalid_argument>(
"invalid length for hex string");
260 Throw<std::range_error>(
"invalid hex character");
295 c.size() *
sizeof(
typename Container::value_type) ==
size(),
296 "ripple::base_uint::base_uint(Container auto) : input size match");
300 template <
class Container>
308 c.size() *
sizeof(
typename Container::value_type) ==
size(),
309 "ripple::base_uint::operator=(Container auto) : input size match");
327 if (from.size() !=
size())
335 for (
int i = 0; i <
WIDTH; i++)
345 return *
this == beast::zero;
353 for (
int i = 0; i <
WIDTH; i++)
369 ul = boost::endian::native_to_big(uHost);
378 for (
int i = 0; i <
WIDTH; i++)
387 for (
int i = 0; i <
WIDTH; i++)
396 for (
int i = 0; i <
WIDTH; i++)
406 for (
int i =
WIDTH - 1; i >= 0; --i)
408 data_[i] = boost::endian::native_to_big(
409 boost::endian::big_to_native(
data_[i]) + 1);
430 for (
int i =
WIDTH - 1; i >= 0; --i)
433 data_[i] = boost::endian::native_to_big(
434 boost::endian::big_to_native(
data_[i]) - 1);
472 for (
int i =
WIDTH; i--;)
475 boost::endian::big_to_native(b.
data_[i]);
485 template <
class Hasher>
490 h(a.data_.data(),
sizeof(a.data_));
501 [[nodiscard]]
constexpr bool
512 [[nodiscard]]
constexpr bool
541 return *
this == beast::zero;
546 return *
this != beast::zero;
560template <std::
size_t Bits,
class Tag>
575 if (ret.first == lhs.
cend())
576 return std::strong_ordering::equivalent;
578 return (*ret.first > *ret.second) ? std::strong_ordering::greater
579 : std::strong_ordering::less;
582template <std::
size_t Bits,
typename Tag>
583[[nodiscard]]
inline constexpr bool
586 return (lhs <=> rhs) == 0;
590template <std::
size_t Bits,
class Tag>
598template <std::
size_t Bits,
class Tag>
599inline constexpr base_uint<Bits, Tag>
605template <std::
size_t Bits,
class Tag>
606inline constexpr base_uint<Bits, Tag>
612template <std::
size_t Bits,
class Tag>
613inline constexpr base_uint<Bits, Tag>
619template <std::
size_t Bits,
class Tag>
620inline constexpr base_uint<Bits, Tag>
627template <std::
size_t Bits,
class Tag>
634template <std::
size_t Bits,
class Tag>
652#ifndef __INTELLISENSE__
653static_assert(
sizeof(
uint128) == 128 / 8,
"There should be no padding bytes");
654static_assert(
sizeof(
uint160) == 160 / 8,
"There should be no padding bytes");
655static_assert(
sizeof(
uint192) == 192 / 8,
"There should be no padding bytes");
656static_assert(
sizeof(
uint256) == 256 / 8,
"There should be no padding bytes");
663template <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.