Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ uint<Builder, Native> uint<Builder, Native>::operator+(const uint& other) const

uint<Builder, Native> result(ctx);
result.witness_index = gate.c;
result.witness_status = WitnessStatus::WEAK_NORMALIZED;
result.normalize();

return result;
}
Expand Down Expand Up @@ -94,7 +94,7 @@ uint<Builder, Native> uint<Builder, Native>::operator-(const uint& other) const

uint<Builder, Native> result(ctx);
result.witness_index = gate.c;
result.witness_status = WitnessStatus::WEAK_NORMALIZED;
result.normalize();

return result;
}
Expand Down Expand Up @@ -139,9 +139,8 @@ uint<Builder, Native> uint<Builder, Native>::operator*(const uint& other) const
ctx->decompose_into_default_range(gate.d, width);

uint<Builder, Native> result(ctx);
result.accumulators = constrain_accumulators(ctx, gate.c);
result.witness_index = gate.c;
result.witness_status = WitnessStatus::OK;
result.normalize();

return result;
}
Expand Down Expand Up @@ -248,13 +247,11 @@ std::pair<uint<Builder, Native>, uint<Builder, Native>> uint<Builder, Native>::d
ctx->decompose_into_default_range(delta_idx, width);
uint<Builder, Native> quotient(ctx);
quotient.witness_index = quotient_idx;
quotient.accumulators = constrain_accumulators(ctx, quotient.witness_index);
quotient.witness_status = WitnessStatus::OK;
quotient.normalize();

uint<Builder, Native> remainder(ctx);
remainder.witness_index = remainder_idx;
remainder.accumulators = constrain_accumulators(ctx, remainder.witness_index);
remainder.witness_status = WitnessStatus::OK;
remainder.normalize();

return std::make_pair(quotient, remainder);
}
Expand Down
37 changes: 12 additions & 25 deletions barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/uint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ std::vector<uint32_t> uint<Builder, Native>::constrain_accumulators(Builder* con
template <typename Builder, typename Native>
uint<Builder, Native>::uint(const witness_t<Builder>& witness)
: context(witness.context)
, witness_status(WitnessStatus::OK)
{
if (witness.witness_index == IS_CONSTANT) {
additive_constant = witness.witness;
Expand All @@ -35,7 +34,6 @@ template <typename Builder, typename Native>
uint<Builder, Native>::uint(const field_t<Builder>& value)
: context(value.context)
, additive_constant(0)
, witness_status(WitnessStatus::OK)
{
if (value.witness_index == IS_CONSTANT) {
additive_constant = value.additive_constant;
Expand All @@ -51,7 +49,6 @@ template <typename Builder, typename Native>
uint<Builder, Native>::uint(Builder* builder, const uint256_t& value)
: context(builder)
, additive_constant(value)
, witness_status(WitnessStatus::OK)
, accumulators()
, witness_index(IS_CONSTANT)
{}
Expand All @@ -60,7 +57,6 @@ template <typename Builder, typename Native>
uint<Builder, Native>::uint(const uint256_t& value)
: context(nullptr)
, additive_constant(value)
, witness_status(WitnessStatus::OK)
, accumulators()
, witness_index(IS_CONSTANT)
{}
Expand All @@ -69,7 +65,6 @@ template <typename Builder, typename Native>
uint<Builder, Native>::uint(const byte_array<Builder>& other)
: context(other.get_context())
, additive_constant(0)
, witness_status(WitnessStatus::WEAK_NORMALIZED)
, accumulators()
, witness_index(IS_CONSTANT)
{
Expand All @@ -88,6 +83,9 @@ uint<Builder, Native>::uint(const byte_array<Builder>& other)
} else {
witness_index = accumulator.witness_index;
}

// We need to constrain the accumulators, so we normalize here.
normalize();
}

template <typename Builder, typename Native>
Expand All @@ -99,7 +97,6 @@ template <typename Builder, typename Native>
uint<Builder, Native>::uint(Builder* parent_context, const std::vector<bool_t<Builder>>& wires)
: context(parent_context)
, additive_constant(0)
, witness_status(WitnessStatus::WEAK_NORMALIZED)
, accumulators()
, witness_index(IS_CONSTANT)
{
Expand All @@ -117,13 +114,15 @@ uint<Builder, Native>::uint(Builder* parent_context, const std::vector<bool_t<Bu
} else {
witness_index = accumulator.witness_index;
}

// We need to constrain the accumulators, so we normalize here.
normalize();
}

template <typename Builder, typename Native>
uint<Builder, Native>::uint(const uint& other)
: context(other.context)
, additive_constant(other.additive_constant)
, witness_status(other.witness_status)
, accumulators(other.accumulators)
, witness_index(other.witness_index)
{}
Expand All @@ -132,7 +131,6 @@ template <typename Builder, typename Native>
uint<Builder, Native>::uint(uint&& other)
: context(other.context)
, additive_constant(other.additive_constant)
, witness_status(other.witness_status)
, accumulators(other.accumulators)
, witness_index(other.witness_index)
{}
Expand All @@ -141,7 +139,6 @@ template <typename Builder, typename Native> uint<Builder, Native>& uint<Builder
{
context = other.context;
additive_constant = other.additive_constant;
witness_status = other.witness_status;
accumulators = other.accumulators;
witness_index = other.witness_index;
return *this;
Expand All @@ -151,7 +148,6 @@ template <typename Builder, typename Native> uint<Builder, Native>& uint<Builder
{
context = other.context;
additive_constant = other.additive_constant;
witness_status = other.witness_status;
accumulators = other.accumulators;
witness_index = other.witness_index;
return *this;
Expand All @@ -177,10 +173,8 @@ template <typename Builder, typename Native> uint<Builder, Native> uint<Builder,
return *this;
}

if (witness_status == WitnessStatus::WEAK_NORMALIZED) {
accumulators = constrain_accumulators(context, witness_index);
witness_status = WitnessStatus::OK;
}
// Constrain the accumulators
accumulators = constrain_accumulators(context, witness_index);
return *this;
}

Expand All @@ -189,25 +183,18 @@ template <typename Builder, typename Native> uint256_t uint<Builder, Native>::ge
if (!context || is_constant()) {
return additive_constant;
}
return (uint256_t(context->get_variable(witness_index))) & MASK;
}

template <typename Builder, typename Native> uint256_t uint<Builder, Native>::get_unbounded_value() const
{
if (!context || is_constant()) {
return additive_constant;
}
return (uint256_t(context->get_variable(witness_index)));
const uint256_t witness_value = context->get_variable(witness_index);
ASSERT(witness_value.get_msb() < width, "uint::get_value(): witness value exceeds type width");

return witness_value & MASK;
}

template <typename Builder, typename Native> bool_t<Builder> uint<Builder, Native>::at(const size_t bit_index) const
{
if (is_constant()) {
return bool_t<Builder>(context, get_value().get_bit(bit_index));
}
if (witness_status != WitnessStatus::OK) {
normalize();
}

const uint64_t slice_bit_position = bit_index % bits_per_limb;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,11 @@ template <typename Builder, typename Native> class uint {
uint256_t get_additive_constant() const { return additive_constant; }

std::vector<uint32_t> get_accumulators() const { return accumulators; }
uint256_t get_unbounded_value() const;

protected:
Builder* context;

enum WitnessStatus { OK, NOT_NORMALIZED, WEAK_NORMALIZED };

mutable uint256_t additive_constant;
mutable WitnessStatus witness_status;

// N.B. Not an accumulator! Contains 6-bit slices of input
mutable std::vector<uint32_t> accumulators;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,18 +510,11 @@ void UltraCircuitBuilder_<ExecutionTrace>::create_balanced_add_gate(const add_qu
}
check_selector_length_consistency();
++this->num_gates;
// Why 3? TODO: return to this
// The purpose of this gate is to do enable lazy 32-bit addition.
// Consider a + b = c mod 2^32
// We want the 4th wire to represent the quotient:
// w1 + w2 = w4 * 2^32 + w3
// If we allow this overflow 'flag' to range from 0 to 3, instead of 0 to 1,
// we can get away with chaining a few addition operations together with basic add gates,
// before having to use this gate.
// (N.B. a larger value would be better, the value '3' is for Turbo backwards compatibility.
// In Turbo this method uses a custom gate,
// where we were limited to a 2-bit range check by the degree of the custom gate identity.
create_new_range_constraint(in.d, 3);

// Range constrain the 4-th wire to {0, 1}. Since the inputs being added never exceed (2^x - 1)
// during uintx arithmetic, we can safely use a 1-bit range check here. In other words, we do not
// allow lazy uintx addition.
create_new_range_constraint(in.d, 1);
}
/**
* @brief Create a multiplication gate with q_m * a * b + q_3 * c + q_const = 0
Expand Down
Loading