20#include "../uint128/uint128.hpp"
35#if defined(__wasm__) || !defined(__SIZEOF_INT128__)
36#define WASM_NUM_LIMBS 9
37#define WASM_LIMB_BITS 29
43 constexpr uint256_t(
const uint64_t
a,
const uint64_t
b,
const uint64_t c,
const uint64_t d) noexcept
48 :
data{ other.data[0], other.data[1], other.data[2], other.data[3] }
52 explicit constexpr uint256_t(std::string input)
noexcept
55 constexpr auto HexCharToInt = [](uint8_t Input) {
57 (Input >=
'a' && Input <=
'f') || (Input >=
'A' && Input <=
'F') || (Input >=
'0' && Input <=
'9');
59 throw_or_abort(
"Error, uint256 constructed from string_view with invalid hex parameter");
62 ((Input >=
'a') && (Input <=
'f')) ? (Input - (
static_cast<uint8_t
>(
'a') -
static_cast<uint8_t
>(10)))
63 : ((Input >=
'A') && (Input <=
'F')) ? (Input - (
static_cast<uint8_t
>(
'A') -
static_cast<uint8_t
>(10)))
64 : ((Input >=
'0') && (Input <=
'9')) ? (Input -
static_cast<uint8_t
>(
'0'))
70 size_t start_index = 0;
71 if (input.size() == 66 && input[0] ==
'0' && input[1] ==
'x') {
73 }
else if (input.size() != 64) {
74 throw_or_abort(
"Error, uint256 constructed from string_view with invalid length");
76 for (
size_t j = 0; j < 4; ++j) {
78 const size_t limb_index = start_index + j * 16;
79 for (
size_t i = 0; i < 8; ++i) {
80 const size_t byte_index = limb_index + (i * 2);
81 uint8_t nibble_hi = HexCharToInt(
static_cast<uint8_t
>(input[byte_index]));
82 uint8_t nibble_lo = HexCharToInt(
static_cast<uint8_t
>(input[byte_index + 1]));
83 uint8_t
byte =
static_cast<uint8_t
>((nibble_hi * 16) + nibble_lo);
96 return {
static_cast<uint64_t
>(
a),
static_cast<uint64_t
>(
a >> 64), 0, 0 };
105 explicit constexpr operator
bool()
const {
return static_cast<bool>(
data[0]); };
107 template <std::
integral T>
explicit constexpr operator T()
const {
return static_cast<T
>(
data[0]); };
109 [[nodiscard]]
constexpr bool get_bit(uint64_t bit_index)
const;
110 [[nodiscard]]
constexpr uint64_t
get_msb()
const;
112 [[nodiscard]]
constexpr uint256_t slice(uint64_t start, uint64_t end)
const;
140 static constexpr size_t length() {
return 256; }
144 *
this = *
this + other;
149 *
this = *
this - other;
154 *
this = *
this * other;
159 *
this = *
this / other;
164 *
this = *
this % other;
181 *
this = *
this & other;
186 *
this = *
this ^ other;
191 *
this = *
this | other;
197 *
this = *
this >> other;
202 *
this = *
this << other;
217 void msgpack_schema(
auto& packer)
const { packer.pack_alias(
"uint256_t",
"bin32"); }
222 [[nodiscard]]
static constexpr uint64_t
addc_discard_hi(uint64_t
a, uint64_t
b, uint64_t carry_in);
223 [[nodiscard]]
static constexpr uint64_t
sbb_discard_hi(uint64_t
a, uint64_t
b, uint64_t borrow_in);
225 [[nodiscard]]
static constexpr uint64_t
mac_discard_hi(uint64_t
a, uint64_t
b, uint64_t c, uint64_t carry_in);
230#if defined(__wasm__) || !defined(__SIZEOF_INT128__)
231 static constexpr void wasm_madd(
const uint64_t& left_limb,
232 const uint64_t* right_limbs,
248 std::ios_base::fmtflags f(os.flags());
constexpr std::pair< uint256_t, uint256_t > mul_extended(const uint256_t &other) const
Compute the result of multiplication modulu 2**512.
constexpr uint256_t operator^(const uint256_t &other) const
constexpr uint256_t operator~() const
constexpr uint256_t(const uint64_t a=0) noexcept
constexpr uint256_t & operator>>=(const uint256_t &other)
constexpr uint256_t operator%(const uint256_t &other) const
constexpr uint256_t(uint256_t &&other) noexcept=default
static constexpr std::pair< uint64_t, uint64_t > addc(uint64_t a, uint64_t b, uint64_t carry_in)
constexpr uint256_t operator*(const uint256_t &other) const
constexpr uint256_t & operator<<=(const uint256_t &other)
constexpr uint256_t & operator++()
constexpr uint256_t operator+(const uint256_t &other) const
constexpr uint256_t & operator*=(const uint256_t &other)
static constexpr uint64_t sbb_discard_hi(uint64_t a, uint64_t b, uint64_t borrow_in)
void msgpack_schema(auto &packer) const
static constexpr std::array< uint64_t, WASM_NUM_LIMBS > wasm_convert(const uint64_t *data)
Convert from 4 64-bit limbs to 9 29-bit limbs.
size_t hash() const noexcept
constexpr bool get_bit(uint64_t bit_index) const
void msgpack_unpack(auto o)
constexpr uint256_t & operator&=(const uint256_t &other)
constexpr uint256_t & operator%=(const uint256_t &other)
constexpr uint256_t operator<<(const uint256_t &other) const
constexpr bool operator!() const
constexpr uint256_t & operator-=(const uint256_t &other)
constexpr bool operator==(const uint256_t &other) const
constexpr uint256_t operator&(const uint256_t &other) const
constexpr uint256_t & operator=(const uint256_t &other) noexcept=default
static constexpr void wasm_madd(const uint64_t &left_limb, const uint64_t *right_limbs, uint64_t &result_0, uint64_t &result_1, uint64_t &result_2, uint64_t &result_3, uint64_t &result_4, uint64_t &result_5, uint64_t &result_6, uint64_t &result_7, uint64_t &result_8)
Multiply one limb by 9 limbs and add to resulting limbs.
constexpr bool operator>(const uint256_t &other) const
static constexpr std::pair< uint64_t, uint64_t > mac(uint64_t a, uint64_t b, uint64_t c, uint64_t carry_in)
constexpr bool operator<(const uint256_t &other) const
constexpr uint256_t & operator^=(const uint256_t &other)
static constexpr std::pair< uint64_t, uint64_t > sbb(uint64_t a, uint64_t b, uint64_t borrow_in)
constexpr bool operator>=(const uint256_t &other) const
constexpr uint256_t operator|(const uint256_t &other) const
constexpr bool operator!=(const uint256_t &other) const
constexpr uint256_t operator-() const
constexpr uint256_t & operator|=(const uint256_t &other)
static constexpr uint64_t mac_discard_hi(uint64_t a, uint64_t b, uint64_t c, uint64_t carry_in)
constexpr uint256_t operator/(const uint256_t &other) const
constexpr uint256_t pow(const uint256_t &exponent) const
constexpr uint256_t slice(uint64_t start, uint64_t end) const
static constexpr std::pair< uint64_t, uint64_t > mul_wide(uint64_t a, uint64_t b)
static constexpr uint64_t addc_discard_hi(uint64_t a, uint64_t b, uint64_t carry_in)
constexpr bool operator<=(const uint256_t &other) const
constexpr ~uint256_t() noexcept=default
constexpr std::pair< uint256_t, uint256_t > divmod(const uint256_t &b) const
constexpr uint256_t & operator/=(const uint256_t &other)
constexpr uint256_t operator>>(const uint256_t &other) const
static constexpr uint256_t from_uint128(const uint128_t a) noexcept
constexpr uint256_t & operator=(uint256_t &&other) noexcept=default
constexpr uint256_t(const uint256_t &other) noexcept
constexpr uint64_t get_msb() const
constexpr uint256_t & operator--()
constexpr uint256_t(const uint64_t a, const uint64_t b, const uint64_t c, const uint64_t d) noexcept
void msgpack_pack(auto &packer) const
constexpr uint256_t(std::string input) noexcept
constexpr uint256_t & operator+=(const uint256_t &other)
static constexpr size_t length()
void read(B &it, uint256_t &value)
std::ostream & operator<<(std::ostream &os, uint256_t const &a)
void write(B &it, uint256_t const &value)
size_t hash_as_tuple(const Ts &... ts)
void read(auto &it, msgpack_concepts::HasMsgPack auto &obj)
Automatically derived read for any object that defines .msgpack() (implicitly defined by MSGPACK_FIEL...
void write(auto &buf, const msgpack_concepts::HasMsgPack auto &obj)
Automatically derived write for any object that defines .msgpack() (implicitly defined by MSGPACK_FIE...
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
unsigned __int128 uint128_t
void throw_or_abort(std::string const &err)