2#include "../bitop/get_msb.hpp"
3#include "./uint256.hpp"
4#include "barretenberg/common/assert.hpp"
7constexpr std::pair<uint64_t, uint64_t> uint256_t::mul_wide(
const uint64_t a,
const uint64_t b)
9 const uint64_t a_lo = a & 0xffffffffULL;
10 const uint64_t a_hi = a >> 32ULL;
11 const uint64_t b_lo = b & 0xffffffffULL;
12 const uint64_t b_hi = b >> 32ULL;
14 const uint64_t lo_lo = a_lo * b_lo;
15 const uint64_t hi_lo = a_hi * b_lo;
16 const uint64_t lo_hi = a_lo * b_hi;
17 const uint64_t hi_hi = a_hi * b_hi;
19 const uint64_t cross = (lo_lo >> 32ULL) + (hi_lo & 0xffffffffULL) + lo_hi;
21 return { (cross << 32ULL) | (lo_lo & 0xffffffffULL), (hi_lo >> 32ULL) + (cross >> 32ULL) + hi_hi };
25constexpr std::pair<uint64_t, uint64_t> uint256_t::addc(
const uint64_t a,
const uint64_t b,
const uint64_t carry_in)
27 const uint64_t sum = a + b;
28 const auto carry_temp =
static_cast<uint64_t
>(sum < a);
29 const uint64_t r = sum + carry_in;
30 const uint64_t carry_out = carry_temp +
static_cast<uint64_t
>(r < carry_in);
31 return { r, carry_out };
34constexpr uint64_t uint256_t::addc_discard_hi(
const uint64_t a,
const uint64_t b,
const uint64_t carry_in)
36 return a + b + carry_in;
39constexpr std::pair<uint64_t, uint64_t> uint256_t::sbb(
const uint64_t a,
const uint64_t b,
const uint64_t borrow_in)
41 const uint64_t t_1 = a - (borrow_in >> 63ULL);
42 const auto borrow_temp_1 =
static_cast<uint64_t
>(t_1 > a);
43 const uint64_t t_2 = t_1 - b;
44 const auto borrow_temp_2 =
static_cast<uint64_t
>(t_2 > t_1);
46 return { t_2, 0ULL - (borrow_temp_1 | borrow_temp_2) };
49constexpr uint64_t uint256_t::sbb_discard_hi(
const uint64_t a,
const uint64_t b,
const uint64_t borrow_in)
51 return a - b - (borrow_in >> 63ULL);
55constexpr std::pair<uint64_t, uint64_t> uint256_t::mac(
const uint64_t a,
58 const uint64_t carry_in)
60 std::pair<uint64_t, uint64_t> result = mul_wide(b, c);
62 const auto overflow_c =
static_cast<uint64_t
>(result.first < a);
63 result.first += carry_in;
64 const auto overflow_carry =
static_cast<uint64_t
>(result.first < carry_in);
65 result.second += (overflow_c + overflow_carry);
69constexpr uint64_t uint256_t::mac_discard_hi(
const uint64_t a,
72 const uint64_t carry_in)
74 return (b * c + a + carry_in);
77constexpr std::pair<uint256_t, uint256_t> uint256_t::divmod(
const uint256_t& b)
const
79 if (*
this == 0 || b == 0) {
95 uint64_t bit_difference = get_msb() - b.get_msb();
101 if (divisor > remainder) {
108 while (remainder >= b) {
112 if (remainder >= divisor) {
113 remainder -= divisor;
116 quotient |= accumulator;
122 return { quotient, remainder };
125constexpr std::pair<uint256_t, uint256_t> uint256_t::mul_extended(
const uint256_t& other)
const
127 const auto [r0, t0] = mul_wide(data[0], other.data[0]);
128 const auto [q0, t1] = mac(t0, data[0], other.data[1], 0);
129 const auto [q1, t2] = mac(t1, data[0], other.data[2], 0);
130 const auto [q2, z0] = mac(t2, data[0], other.data[3], 0);
132 const auto [r1, t3] = mac(q0, data[1], other.data[0], 0);
133 const auto [q3, t4] = mac(q1, data[1], other.data[1], t3);
134 const auto [q4, t5] = mac(q2, data[1], other.data[2], t4);
135 const auto [q5, z1] = mac(z0, data[1], other.data[3], t5);
137 const auto [r2, t6] = mac(q3, data[2], other.data[0], 0);
138 const auto [q6, t7] = mac(q4, data[2], other.data[1], t6);
139 const auto [q7, t8] = mac(q5, data[2], other.data[2], t7);
140 const auto [q8, z2] = mac(z1, data[2], other.data[3], t8);
142 const auto [r3, t9] = mac(q6, data[3], other.data[0], 0);
143 const auto [r4, t10] = mac(q7, data[3], other.data[1], t9);
144 const auto [r5, t11] = mac(q8, data[3], other.data[2], t10);
145 const auto [r6, r7] = mac(z2, data[3], other.data[3], t11);
159 const uint64_t range = end - start;
161 return ((*
this) >> start) & mask;
166 uint256_t accumulator{ data[0], data[1], data[2], data[3] };
167 uint256_t to_mul{ data[0], data[1], data[2], data[3] };
168 const uint64_t maximum_set_bit = exponent.get_msb();
170 for (
int i =
static_cast<int>(maximum_set_bit) - 1; i >= 0; --i) {
171 accumulator *= accumulator;
172 if (exponent.get_bit(
static_cast<uint64_t
>(i))) {
173 accumulator *= to_mul;
184constexpr bool uint256_t::get_bit(
const uint64_t bit_index)
const
186 ASSERT(bit_index < 256);
187 if (bit_index > 255) {
188 return static_cast<bool>(0);
190 const auto idx =
static_cast<size_t>(bit_index >> 6);
191 const size_t shift = bit_index & 63;
192 return static_cast<bool>((data[idx] >> shift) & 1);
195constexpr uint64_t uint256_t::get_msb()
const
197 uint64_t idx = numeric::get_msb(data[3]);
198 idx = (idx == 0 && data[3] == 0) ? numeric::get_msb(data[2]) : idx + 64;
199 idx = (idx == 0 && data[2] == 0) ? numeric::get_msb(data[1]) : idx + 64;
200 idx = (idx == 0 && data[1] == 0) ? numeric::get_msb(data[0]) : idx + 64;
206 const auto [r0, t0] = addc(data[0], other.data[0], 0);
207 const auto [r1, t1] = addc(data[1], other.data[1], t0);
208 const auto [r2, t2] = addc(data[2], other.data[2], t1);
209 const auto r3 = addc_discard_hi(data[3], other.data[3], t2);
210 return { r0, r1, r2, r3 };
216 const auto [r0, t0] = sbb(data[0], other.data[0], 0);
217 const auto [r1, t1] = sbb(data[1], other.data[1], t0);
218 const auto [r2, t2] = sbb(data[2], other.data[2], t1);
219 const auto r3 = sbb_discard_hi(data[3], other.data[3], t2);
220 return { r0, r1, r2, r3 };
223constexpr uint256_t uint256_t::operator-()
const
230 const auto [r0, t0] = mac(0, data[0], other.data[0], 0ULL);
231 const auto [q0, t1] = mac(0, data[0], other.data[1], t0);
232 const auto [q1, t2] = mac(0, data[0], other.data[2], t1);
233 const auto q2 = mac_discard_hi(0, data[0], other.data[3], t2);
235 const auto [r1, t3] = mac(q0, data[1], other.data[0], 0ULL);
236 const auto [q3, t4] = mac(q1, data[1], other.data[1], t3);
237 const auto q4 = mac_discard_hi(q2, data[1], other.data[2], t4);
239 const auto [r2, t5] = mac(q3, data[2], other.data[0], 0ULL);
240 const auto q5 = mac_discard_hi(q4, data[2], other.data[1], t5);
242 const auto r3 = mac_discard_hi(q5, data[3], other.data[0], 0ULL);
244 return { r0, r1, r2, r3 };
249 return divmod(other).first;
254 return divmod(other).second;
259 return { data[0] & other.data[0], data[1] & other.data[1], data[2] & other.data[2], data[3] & other.data[3] };
264 return { data[0] ^ other.data[0], data[1] ^ other.data[1], data[2] ^ other.data[2], data[3] ^ other.data[3] };
269 return { data[0] | other.data[0], data[1] | other.data[1], data[2] | other.data[2], data[3] | other.data[3] };
272constexpr uint256_t uint256_t::operator~()
const
274 return { ~data[0], ~data[1], ~data[2], ~data[3] };
277constexpr bool uint256_t::operator==(
const uint256_t& other)
const
279 return data[0] == other.data[0] && data[1] == other.data[1] && data[2] == other.data[2] && data[3] == other.data[3];
282constexpr bool uint256_t::operator!=(
const uint256_t& other)
const
284 return !(*
this == other);
287constexpr bool uint256_t::operator!()
const
292constexpr bool uint256_t::operator>(
const uint256_t& other)
const
294 bool t0 = data[3] > other.data[3];
295 bool t1 = data[3] == other.data[3] && data[2] > other.data[2];
296 bool t2 = data[3] == other.data[3] && data[2] == other.data[2] && data[1] > other.data[1];
298 data[3] == other.data[3] && data[2] == other.data[2] && data[1] == other.data[1] && data[0] > other.data[0];
299 return t0 || t1 || t2 || t3;
302constexpr bool uint256_t::operator>=(
const uint256_t& other)
const
304 return (*
this > other) || (*
this == other);
307constexpr bool uint256_t::operator<(
const uint256_t& other)
const
309 return other > *
this;
312constexpr bool uint256_t::operator<=(
const uint256_t& other)
const
314 return (*
this < other) || (*
this == other);
319 uint64_t total_shift = other.data[0];
321 if (total_shift >= 256 || (other.data[1] != 0U) || (other.data[2] != 0U) || (other.data[3] != 0U)) {
325 if (total_shift == 0) {
329 uint64_t num_shifted_limbs = total_shift >> 6ULL;
330 uint64_t limb_shift = total_shift & 63ULL;
332 std::array<uint64_t, 4> shifted_limbs = { 0, 0, 0, 0 };
334 if (limb_shift == 0) {
335 shifted_limbs[0] = data[0];
336 shifted_limbs[1] = data[1];
337 shifted_limbs[2] = data[2];
338 shifted_limbs[3] = data[3];
340 uint64_t remainder_shift = 64ULL - limb_shift;
342 shifted_limbs[3] = data[3] >> limb_shift;
344 uint64_t remainder = (data[3]) << remainder_shift;
346 shifted_limbs[2] = (data[2] >> limb_shift) + remainder;
348 remainder = (data[2]) << remainder_shift;
350 shifted_limbs[1] = (data[1] >> limb_shift) + remainder;
352 remainder = (data[1]) << remainder_shift;
354 shifted_limbs[0] = (data[0] >> limb_shift) + remainder;
358 for (
size_t i = 0; i < 4 - num_shifted_limbs; ++i) {
359 result.data[i] = shifted_limbs[
static_cast<size_t>(i + num_shifted_limbs)];
367 uint64_t total_shift = other.data[0];
369 if (total_shift >= 256 || (other.data[1] != 0U) || (other.data[2] != 0U) || (other.data[3] != 0U)) {
373 if (total_shift == 0) {
376 uint64_t num_shifted_limbs = total_shift >> 6ULL;
377 uint64_t limb_shift = total_shift & 63ULL;
379 std::array<uint64_t, 4> shifted_limbs = { 0, 0, 0, 0 };
381 if (limb_shift == 0) {
382 shifted_limbs[0] = data[0];
383 shifted_limbs[1] = data[1];
384 shifted_limbs[2] = data[2];
385 shifted_limbs[3] = data[3];
387 uint64_t remainder_shift = 64ULL - limb_shift;
389 shifted_limbs[0] = data[0] << limb_shift;
391 uint64_t remainder = data[0] >> remainder_shift;
393 shifted_limbs[1] = (data[1] << limb_shift) + remainder;
395 remainder = data[1] >> remainder_shift;
397 shifted_limbs[2] = (data[2] << limb_shift) + remainder;
399 remainder = data[2] >> remainder_shift;
401 shifted_limbs[3] = (data[3] << limb_shift) + remainder;
405 for (
size_t i = 0; i < 4 - num_shifted_limbs; ++i) {
406 result.data[
static_cast<size_t>(i + num_shifted_limbs)] = shifted_limbs[i];
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