20#include "../uint256/uint256.hpp"
29template <
class base_u
int>
class uintx {
42 constexpr uintx(
const base_uint input_lo)
47 constexpr uintx(
const base_uint input_lo,
const base_uint input_hi)
55 static constexpr size_t length() {
return 2 * base_uint::length(); }
60 constexpr explicit operator bool()
const {
return static_cast<bool>(
lo); };
61 constexpr explicit operator uint8_t()
const {
return static_cast<uint8_t
>(
lo); };
62 constexpr explicit operator uint16_t()
const {
return static_cast<uint16_t
>(
lo); };
63 constexpr explicit operator uint32_t()
const {
return static_cast<uint32_t
>(
lo); };
64 constexpr explicit operator uint64_t()
const {
return static_cast<uint64_t
>(
lo); };
66 constexpr explicit operator base_uint()
const {
return lo; }
68 [[nodiscard]]
bool get_bit(uint64_t bit_index)
const;
69 [[nodiscard]]
constexpr uint64_t
get_msb()
const
71 uint64_t hi_idx =
hi.get_msb();
72 uint64_t lo_idx =
lo.get_msb();
73 return (hi_idx || (
hi > base_uint(0))) ? (hi_idx + base_uint::length()) : lo_idx;
82 constexpr uintx slice(
const uint64_t start,
const uint64_t end)
const
84 const uint64_t range = end - start;
85 const uintx mask = range == base_uint::length() ? -
uintx(1) : (
uintx(1) << range) - 1;
86 return ((*
this) >> start) & mask;
92 base_uint res_lo =
lo - other.
lo;
93 bool borrow = res_lo >
lo;
94 base_uint res_hi =
hi - other.
hi - ((borrow) ? base_uint(1) : base_uint(0));
95 return { res_lo, res_hi };
101 const uint64_t total_shift = other;
102 if (total_shift >=
length()) {
105 if (total_shift == 0) {
108 const uint64_t num_shifted_limbs = total_shift >> (base_uint(base_uint::length()).get_msb());
109 const uint64_t limb_shift = total_shift &
static_cast<uint64_t
>(base_uint::length() - 1);
112 if (limb_shift == 0) {
113 shifted_limbs[0] =
lo;
114 shifted_limbs[1] =
hi;
116 const uint64_t remainder_shift =
static_cast<uint64_t
>(base_uint::length()) - limb_shift;
118 shifted_limbs[0] =
lo << limb_shift;
120 base_uint remainder =
lo >> remainder_shift;
122 shifted_limbs[1] = (
hi << limb_shift) + remainder;
125 if (num_shifted_limbs == 0) {
126 result.
hi = shifted_limbs[1];
127 result.
lo = shifted_limbs[0];
129 result.
hi = shifted_limbs[0];
137 const uint64_t total_shift = other;
138 if (total_shift >=
length()) {
141 if (total_shift == 0) {
144 const uint64_t num_shifted_limbs = total_shift >> (base_uint(base_uint::length()).get_msb());
146 const uint64_t limb_shift = total_shift &
static_cast<uint64_t
>(base_uint::length() - 1);
149 if (limb_shift == 0) {
150 shifted_limbs[0] =
lo;
151 shifted_limbs[1] =
hi;
153 const uint64_t remainder_shift =
static_cast<uint64_t
>(base_uint::length()) - limb_shift;
155 shifted_limbs[1] =
hi >> limb_shift;
157 base_uint remainder = (
hi) << remainder_shift;
159 shifted_limbs[0] = (
lo >> limb_shift) + remainder;
162 if (num_shifted_limbs == 0) {
163 result.
hi = shifted_limbs[1];
164 result.
lo = shifted_limbs[0];
166 result.
lo = shifted_limbs[1];
174 base_uint res_lo =
lo + other.
lo;
175 bool carry = res_lo <
lo;
176 base_uint res_hi =
hi + other.
hi + ((carry) ? base_uint(1) : base_uint(0));
177 return { res_lo, res_hi };
205 *
this = *
this + other;
210 *
this = *
this - other;
215 *
this = *
this * other;
221 *
this = *
this / other;
227 *
this = *
this % other;
244 *
this = *
this & other;
249 *
this = *
this ^ other;
254 *
this = *
this | other;
260 *
this = *
this >> other;
265 *
this = *
this << other;
constexpr uintx(uintx &&other) noexcept=default
constexpr uintx(const uint256_t &data)
constexpr uintx operator+(const uintx &other) const
uintx operator%(const uintx &other) const
uintx & operator|=(const uintx &other)
uintx & operator*=(const uintx &other)
std::pair< uintx, uintx > divmod(const uintx &b) const
constexpr uintx(const base_uint input_lo, const base_uint input_hi)
constexpr uintx slice(const uint64_t start, const uint64_t end) const
uintx & operator>>=(const uint64_t other)
bool operator!=(const uintx &other) const
bool operator<(const uintx &other) const
uintx operator*(const uintx &other) const
uintx & operator&=(const uintx &other)
static constexpr size_t length()
uintx unsafe_invmod(const uintx &modulus) const
constexpr uintx operator-(const uintx &other) const
uintx & operator+=(const uintx &other)
uintx & operator=(uintx &&other) noexcept=default
std::pair< uintx, uintx > divmod_base(const uintx &b) const
bool operator==(const uintx &other) const
uintx & operator^=(const uintx &other)
bool operator>(const uintx &other) const
bool operator<=(const uintx &other) const
bool get_bit(uint64_t bit_index) const
constexpr uint64_t get_msb() const
uintx & operator-=(const uintx &other)
std::pair< uintx, uintx > barrett_reduction() const
uintx & operator<<=(const uint64_t other)
constexpr uintx operator&(const uintx &other) const
uintx operator|(const uintx &other) const
uintx & operator=(const uintx &other)=default
bool operator>=(const uintx &other) const
uintx operator/(const uintx &other) const
uintx & operator/=(const uintx &other)
std::pair< uintx, uintx > mul_extended(const uintx &other) const
constexpr uintx(const uint64_t &data=0)
constexpr uintx operator>>(const uint64_t other) const
uintx invmod(const uintx &modulus) const
uintx operator^(const uintx &other) const
constexpr uintx operator<<(const uint64_t other) const
uintx & operator%=(const uintx &other)
constexpr uintx(const uintx &other)=default
constexpr uintx(const base_uint input_lo)
const std::vector< FF > data
crypto::Poseidon2Bn254ScalarFieldParams Params
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)
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