2#include "../bool/bool.hpp"
3#include "../circuit_builders/circuit_builders.hpp"
4#include "../circuit_builders/circuit_builders_fwd.hpp"
5#include "../field/field.hpp"
6#include "../witness/witness.hpp"
7#include "barretenberg/common/assert.hpp"
25 , current_max(current_max)
27 ASSERT(safety == IS_UNSAFE);
28 if (current_max > MAX_VALUE)
30 throw_or_abort(
"exceeded modulus in safe_uint class");
36 static constexpr size_t MAX_BIT_NUM = barretenberg::fr::modulus.get_msb();
37 static constexpr uint256_t MAX_VALUE = barretenberg::fr::modulus - 1;
38 static constexpr size_t IS_UNSAFE = 143;
40 static_assert((uint512_t)MAX_VALUE * 3 < (uint512_t)1 << 256);
49 safe_uint_t(
field_ct const& value,
size_t bit_num, std::string
const& description =
"unknown")
52 ASSERT(bit_num <= MAX_BIT_NUM);
53 this->value.create_range_constraint(bit_num, format(
"safe_uint_t range constraint failure: ", description));
54 current_max = ((
uint256_t)1 << bit_num) - 1;
61 , current_max(const_value)
77 , current_max(other.current_max)
84 parent_context->assert_equal_constant(out.witness_index, value);
96 static safe_uint_t from_witness_index(
Builder* parent_context,
const uint32_t witness_index);
100 const size_t difference_bit_size,
101 std::string
const& description =
"")
const;
108 const size_t quotient_bit_size,
109 const size_t remainder_bit_size,
110 std::string
const& description =
"",
111 const std::function<std::pair<uint256_t, uint256_t>(
uint256_t,
uint256_t)>& get_quotient =
121 ASSERT(current_max + add_a.current_max + add_b.current_max <= MAX_VALUE &&
"Exceeded modulus in add_two");
122 auto new_val = value.add_two(add_a.value, add_b.value);
123 auto new_max = current_max + add_a.current_max + add_b.current_max;
129 ASSERT((uint512_t)current_max * (uint512_t)to_mul.current_max + (uint512_t)to_add.current_max <= MAX_VALUE &&
130 "Exceeded modulus in madd");
131 auto new_val = value.
madd(to_mul.value, to_add.value);
132 auto new_max = current_max * to_mul.current_max + to_add.current_max;
139 current_max = other.current_max;
146 current_max = other.current_max;
152 *
this = *
this + other;
158 *
this = *
this * other;
162 std::array<safe_uint_t<Builder>, 3> slice(
const uint8_t msb,
const uint8_t lsb)
const;
163 void set_public()
const { value.set_public(); }
164 operator field_ct() {
return value; }
165 operator field_ct()
const {
return value; }
182 Builder* get_context()
const {
return value.context; }
190 void assert_equal(
const safe_uint_t& rhs, std::string
const& msg =
"safe_uint_t::assert_equal")
const
194 void assert_is_not_zero(std::string
const& msg =
"safe_uint_t::assert_is_not_zero")
const;
195 void assert_is_zero(std::string
const& msg =
"safe_uint_t::assert_is_zero")
const;
196 bool is_constant()
const {
return value.is_constant(); }
200 auto new_val = (lhs.value - rhs.value).madd(predicate, rhs.value);
201 auto new_max = lhs.current_max > rhs.current_max ? lhs.current_max : rhs.current_max;
205 uint32_t get_witness_index()
const {
return value.get_witness_index(); }
208template <
typename Builder>
inline std::ostream& operator<<(std::ostream& os,
safe_uint_t<Builder> const& v)
210 return os << v.value;
213EXTERN_STDLIB_TYPE(safe_uint_t);
Definition: uint256.hpp:25
Definition: standard_circuit_builder.hpp:12
void assert_equal(const field_t &rhs, std::string const &msg="field_t::assert_equal") const
Constrain that this field is equal to the given field.
Definition: field.cpp:749
field_t madd(const field_t &to_mul, const field_t &to_add) const
Definition: field.cpp:384
Definition: safe_uint.hpp:17
safe_uint_t operator/(const safe_uint_t &other) const
Potentially less efficient than divide function - bounds remainder and quotient by max of this.
Definition: safe_uint.cpp:143
safe_uint_t subtract(const safe_uint_t &other, const size_t difference_bit_size, std::string const &description="") const
Subtraction when you have a pre-determined bound on the difference size.
Definition: safe_uint.cpp:35
safe_uint_t operator-(const safe_uint_t &other) const
Subtraction on two safe_uint_t objects.
Definition: safe_uint.cpp:67
safe_uint_t divide(const safe_uint_t &other, const size_t quotient_bit_size, const size_t remainder_bit_size, std::string const &description="", const std::function< std::pair< uint256_t, uint256_t >(uint256_t, uint256_t)> &get_quotient=[](uint256_t val, uint256_t divisor) { return std::make_pair((uint256_t)(val/(uint256_t) divisor),(uint256_t)(val %(uint256_t) divisor));}) const
division when you have a pre-determined bound on the sizes of the quotient and remainder
Definition: safe_uint.cpp:104
safe_uint_t normalize() const
Definition: safe_uint.cpp:168
bool_ct is_zero() const
Definition: safe_uint.cpp:184
Definition: witness.hpp:10
Definition: widget.bench.cpp:13