14#include "../uint128/uint128.hpp"
15#include "barretenberg/common/serialize.hpp"
16#include "barretenberg/common/throw_or_abort.hpp"
27 constexpr uint256_t(
const uint64_t a = 0) noexcept
31 constexpr uint256_t(
const uint64_t a,
const uint64_t b,
const uint64_t c,
const uint64_t d) noexcept
36 : data{ other.data[0], other.data[1], other.data[2], other.data[3] }
40 explicit constexpr uint256_t(std::string input)
noexcept
43 constexpr auto HexCharToInt = [](uint8_t Input) {
45 (Input >=
'a' && Input <=
'f') || (Input >=
'A' && Input <=
'F') || (Input >=
'0' && Input <=
'9');
47 throw_or_abort(
"Error, uint256 constructed from string_view with invalid hex parameter");
50 ((Input >=
'a') && (Input <=
'f')) ? (Input - (
static_cast<uint8_t
>(
'a') -
static_cast<uint8_t
>(10)))
51 : ((Input >=
'A') && (Input <=
'F')) ? (Input - (
static_cast<uint8_t
>(
'A') -
static_cast<uint8_t
>(10)))
52 : ((Input >=
'0') && (Input <=
'9')) ? (Input -
static_cast<uint8_t
>(
'0'))
57 std::array<uint64_t, 4> limbs{ 0, 0, 0, 0 };
58 size_t start_index = 0;
59 if (input.size() == 66 && input[0] ==
'0' && input[1] ==
'x') {
61 }
else if (input.size() != 64) {
62 throw_or_abort(
"Error, uint256 constructed from string_view with invalid length");
64 for (
size_t j = 0; j < 4; ++j) {
66 const size_t limb_index = start_index + j * 16;
67 for (
size_t i = 0; i < 8; ++i) {
68 const size_t byte_index = limb_index + (i * 2);
69 uint8_t nibble_hi = HexCharToInt(
static_cast<uint8_t
>(input[byte_index]));
70 uint8_t nibble_lo = HexCharToInt(
static_cast<uint8_t
>(input[byte_index + 1]));
71 uint8_t
byte =
static_cast<uint8_t
>((nibble_hi * 16) + nibble_lo);
82 static constexpr uint256_t from_uint128(
const uint128_t a)
noexcept
84 return {
static_cast<uint64_t
>(a),
static_cast<uint64_t
>(a >> 64), 0, 0 };
87 constexpr explicit operator uint128_t() {
return (
static_cast<uint128_t
>(data[1]) << 64) + data[0]; }
93 explicit constexpr operator bool()
const {
return static_cast<bool>(data[0]); };
95 template <std::
integral T>
explicit constexpr operator T()
const {
return static_cast<T
>(data[0]); };
97 [[nodiscard]]
constexpr bool get_bit(uint64_t bit_index)
const;
98 [[nodiscard]]
constexpr uint64_t get_msb()
const;
100 [[nodiscard]]
constexpr uint256_t slice(uint64_t start, uint64_t end)
const;
119 constexpr bool operator==(
const uint256_t& other)
const;
120 constexpr bool operator!=(
const uint256_t& other)
const;
121 constexpr bool operator!()
const;
123 constexpr bool operator>(
const uint256_t& other)
const;
124 constexpr bool operator<(
const uint256_t& other)
const;
125 constexpr bool operator>=(
const uint256_t& other)
const;
126 constexpr bool operator<=(
const uint256_t& other)
const;
128 static constexpr size_t length() {
return 256; }
132 *
this = *
this + other;
137 *
this = *
this - other;
142 *
this = *
this * other;
147 *
this = *
this / other;
152 *
this = *
this % other;
169 *
this = *
this & other;
174 *
this = *
this ^ other;
179 *
this = *
this | other;
185 *
this = *
this >> other;
190 *
this = *
this << other;
194 [[nodiscard]]
constexpr std::pair<uint256_t, uint256_t> mul_extended(
const uint256_t& other)
const;
198 [[nodiscard]]
constexpr std::pair<uint256_t, uint256_t> divmod(
const uint256_t& b)
const;
201 [[nodiscard]]
static constexpr std::pair<uint64_t, uint64_t> mul_wide(uint64_t a, uint64_t b);
202 [[nodiscard]]
static constexpr std::pair<uint64_t, uint64_t> addc(uint64_t a, uint64_t b, uint64_t carry_in);
203 [[nodiscard]]
static constexpr uint64_t addc_discard_hi(uint64_t a, uint64_t b, uint64_t carry_in);
204 [[nodiscard]]
static constexpr uint64_t sbb_discard_hi(uint64_t a, uint64_t b, uint64_t borrow_in);
205 [[nodiscard]]
static constexpr std::pair<uint64_t, uint64_t> sbb(uint64_t a, uint64_t b, uint64_t borrow_in);
206 [[nodiscard]]
static constexpr uint64_t mac_discard_hi(uint64_t a, uint64_t b, uint64_t c, uint64_t carry_in);
207 [[nodiscard]]
static constexpr std::pair<uint64_t, uint64_t> mac(uint64_t a,
213inline std::ostream& operator<<(std::ostream& os,
uint256_t const& a)
215 std::ios_base::fmtflags f(os.flags());
216 os << std::hex <<
"0x" << std::setfill(
'0') << std::setw(16) << a.data[3] << std::setw(16) << a.data[2]
217 << std::setw(16) << a.data[1] << std::setw(16) << a.data[0];
222template <
typename B>
inline void read(B& it,
uint256_t& value)
224 using serialize::read;
236template <
typename B>
inline void write(B& it,
uint256_t const& value)
238 using serialize::write;
239 write(it, value.data[3]);
240 write(it, value.data[2]);
241 write(it, value.data[1]);
242 write(it, value.data[0]);
247#include "./uint256_impl.hpp"
Definition: uint256.hpp:25
constexpr uint256_t slice(uint64_t start, uint64_t end) const
Definition: uint256_impl.hpp:157
Definition: field2_declarations.hpp:6