25 #ifndef RIPPLE_BASICS_BASE_UINT_H_INCLUDED
26 #define RIPPLE_BASICS_BASE_UINT_H_INCLUDED
28 #include <ripple/basics/hardened_hash.h>
29 #include <ripple/basics/strHex.h>
30 #include <ripple/beast/cxx17/type_traits.h>
31 #include <ripple/beast/utility/Zero.h>
32 #include <boost/endian/conversion.hpp>
33 #include <boost/functional/hash.hpp>
43 template <
class Container,
class = std::
void_t<>>
48 template <
class Container>
52 decltype(std::declval<Container const>().size()),
53 decltype(std::declval<Container const>().data()),
62 template <std::
size_t Bits,
class Tag =
void>
67 "The length of a base_uint in bits must be a multiple of 32.");
71 "The length of a base_uint in bits must be at least 64.");
193 assert(c.size() *
sizeof(
typename Container::value_type) ==
size());
197 template <
class Container>
204 assert(c.size() *
sizeof(
typename Container::value_type) ==
size());
221 for (
int i = 0; i <
WIDTH; i++)
231 return *
this == beast::zero;
239 for (
int i = 0; i <
WIDTH; i++)
255 ul = boost::endian::native_to_big(uHost);
264 for (
int i = 0; i <
WIDTH; i++)
273 for (
int i = 0; i <
WIDTH; i++)
282 for (
int i = 0; i <
WIDTH; i++)
292 for (
int i =
WIDTH - 1; i >= 0; --i)
294 data_[i] = boost::endian::native_to_big(
295 boost::endian::big_to_native(
data_[i]) + 1);
316 for (
int i =
WIDTH - 1; i >= 0; --i)
318 auto prev =
data_[i];
319 data_[i] = boost::endian::native_to_big(
320 boost::endian::big_to_native(
data_[i]) - 1);
344 for (
int i =
WIDTH; i--;)
347 boost::endian::big_to_native(b.
data_[i]);
357 template <
class Hasher>
362 h(a.data_.data(),
sizeof(a.data_));
372 unsigned char* pOut =
begin();
374 for (
int i = 0; i <
sizeof(
data_); ++i)
384 *pOut++ = (hi << 4) | lo;
406 SetHex(
const char* psz,
bool bStrict =
false)
409 auto pBegin =
reinterpret_cast<const unsigned char*
>(psz);
412 while (isspace(*pBegin))
416 if (!bStrict && pBegin[0] ==
'0' && tolower(pBegin[1]) ==
'x')
425 if ((
unsigned int)(pEnd - pBegin) > 2 *
size())
426 pBegin = pEnd - 2 *
size();
428 unsigned char* pOut =
end() - ((pEnd - pBegin + 1) / 2);
432 if ((pEnd - pBegin) & 1)
435 while (pBegin != pEnd)
438 auto cLow = pBegin == pEnd ? 0 :
charUnHex(*pBegin++);
440 if (cHigh == -1 || cLow == -1)
443 *pOut++ = (cHigh << 4) | cLow;
477 return *
this == beast::zero;
482 return *
this != beast::zero;
495 template <std::
size_t Bits,
class Tag>
501 if (ret.first == a.
cend())
505 if (*ret.first > *ret.second)
512 template <std::
size_t Bits,
class Tag>
519 template <std::
size_t Bits,
class Tag>
526 template <std::
size_t Bits,
class Tag>
533 template <std::
size_t Bits,
class Tag>
540 template <std::
size_t Bits,
class Tag>
547 template <std::
size_t Bits,
class Tag>
555 template <std::
size_t Bits,
class Tag>
562 template <std::
size_t Bits,
class Tag>
570 template <std::
size_t Bits,
class Tag>
571 inline const base_uint<Bits, Tag>
577 template <std::
size_t Bits,
class Tag>
578 inline const base_uint<Bits, Tag>
584 template <std::
size_t Bits,
class Tag>
585 inline const base_uint<Bits, Tag>
591 template <std::
size_t Bits,
class Tag>
592 inline const base_uint<Bits, Tag>
599 template <std::
size_t Bits,
class Tag>
609 template <
typename T>
620 template <
typename T>
631 template <std::
size_t Bits,
class Tag>
638 #ifndef __INTELLISENSE__
639 static_assert(
sizeof(
uint128) == 128 / 8,
"There should be no padding bytes");
640 static_assert(
sizeof(
uint160) == 160 / 8,
"There should be no padding bytes");
641 static_assert(
sizeof(
uint256) == 256 / 8,
"There should be no padding bytes");
648 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
int charUnHex(unsigned char c)
Converts a hex digit to the corresponding integer.
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
bool SetHexExact(std::string const &str)
bool SetHex(std::string const &str, bool bStrict=false)
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)
auto from_hex_text(char const *text) -> std::enable_if_t< std::is_same< T, base_uint< T::bytes *8, typename T::tag_type >>::value, T >
const_iterator cbegin() const
static constexpr std::size_t WIDTH
std::string to_string(ListDisposition disposition)
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
const base_uint operator~() const
is_uniquely_represented()=default
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_
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.
bool SetHex(const char *psz, bool bStrict=false)
Parse a hex string into a base_uint The input can be:
Seed functor once per construction.
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)
bool SetHexExact(const char *psz)
Parse a hex string into a base_uint The string must contain exactly bytes * 2 hex characters and must...
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
base_uint(void const *data, VoidHelper)
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)
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)
bool operator<(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
value_type const * const_pointer