Skip to content
Open
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 @@ -36,6 +36,12 @@ void databus<Builder>::bus_vector::set_values(const std::vector<field_pt>& entri
context->append_to_bus_vector(bus_idx, entries.back().get_witness_index());
}
length = entries.size();

// Preserve tags to restore them in future reads (following the ROM/RAM pattern)
_tags.resize(entries_in.size());
for (size_t i = 0; i < length; ++i) {
_tags[i] = entries_in[i].get_origin_tag();
}
}

template <typename Builder>
Expand All @@ -58,7 +64,13 @@ field_t<Builder> databus<Builder>::bus_vector::operator[](const field_pt& index)

// Read from the bus vector at the specified index. Creates a single read gate
uint32_t output_idx = context->read_bus_vector(bus_idx, index_witness_idx);
return field_pt::from_witness_index(context, output_idx);
auto result = field_pt::from_witness_index(context, output_idx);

// If the index is legitimate, restore the tag (following the ROM/RAM pattern)
if (raw_index < length) {
result.set_origin_tag(_tags[raw_index]);
}
return result;
}

template class databus<bb::MegaCircuitBuilder>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ template <typename Builder> class databus {

private:
mutable std::vector<field_pt> entries; // bus vector entries
std::vector<OriginTag> _tags; // origin tags for each entry (restored on read)
size_t length = 0;
BusId bus_idx; // Idx of column in bus
mutable Builder* context = nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ template <typename Builder_> class field_t {

bool predicate_witness = uint256_t(a.get_value()) < uint256_t(b.get_value());
bool_t<Builder> predicate(witness_t<Builder>(ctx, predicate_witness));
predicate.set_origin_tag(OriginTag(a.get_origin_tag(), b.get_origin_tag()));
field_t predicate_valid = b.add_two(-(a) + range_constant - 1, -field_t(predicate) * range_constant);
predicate_valid.create_range_constraint(num_bits);
return predicate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,14 @@ template <typename Builder> class stdlib_field : public testing::Test {
#ifndef NDEBUG
EXPECT_THROW(q + q, std::runtime_error);
#endif

// ranged_less_than: check tag behavior
auto rlt_a = field_ct(witness_ct(&builder, uint256_t(50)));
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

failed before fix, passes after

auto rlt_b = field_ct(witness_ct(&builder, uint256_t(100)));
rlt_a.set_origin_tag(submitted_value_origin_tag);
rlt_b.set_origin_tag(challenge_origin_tag);
auto rlt_result = rlt_a.template ranged_less_than<8>(rlt_b);
EXPECT_EQ(rlt_result.get_origin_tag(), first_two_merged_tag);
}

void test_validate_context()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ template <typename Field> class StdlibCodec {
// All challenges must be circuit witnesses.
BB_ASSERT(builder);
BB_ASSERT(!challenge.is_constant());
return T(challenge, fr::from_witness_index(builder, builder->zero_idx()));
auto high_limb = fr::from_witness_index(builder, builder->zero_idx());
high_limb.set_origin_tag(challenge.get_origin_tag());
return T(challenge, high_limb);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ field_t<Builder> logic<Builder>::create_logic_constraint(
Builder* ctx = b.get_context();
uint256_t a_native(a.get_value());
field_pt a_witness = field_pt::from_witness_index(ctx, ctx->put_constant_variable(a_native));
a_witness.set_origin_tag(a.get_origin_tag());
return create_logic_constraint(a_witness, b, num_bits, is_xor_gate, get_chunk);
}

if (!a.is_constant() && b.is_constant()) {
Builder* ctx = a.get_context();
uint256_t b_native(b.get_value());
field_pt b_witness = field_pt::from_witness_index(ctx, ctx->put_constant_variable(b_native));
b_witness.set_origin_tag(b.get_origin_tag());
return create_logic_constraint(a, b_witness, num_bits, is_xor_gate, get_chunk);
}

Expand Down Expand Up @@ -111,6 +113,7 @@ field_t<Builder> logic<Builder>::create_logic_constraint(
a.assert_equal(a_accumulator, "stdlib logic: failed to reconstruct left operand");
b.assert_equal(b_accumulator, "stdlib logic: failed to reconstruct right operand");

res.set_origin_tag(OriginTag(a.get_origin_tag(), b.get_origin_tag()));
return res;
}
template class logic<bb::UltraCircuitBuilder>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,40 @@ TYPED_TEST(LogicTest, DifferentWitnessSameResult)
bool result = CircuitChecker::check(builder);
EXPECT_EQ(result, false);
}

TYPED_TEST(LogicTest, OriginTagConsistency)
{
STDLIB_TYPE_ALIASES
auto builder = Builder();

const size_t parent_id = 0;
const auto tag_a = OriginTag(parent_id, /*round_id=*/0, /*is_submitted=*/true);
const auto tag_b = OriginTag(parent_id, /*round_id=*/0, /*is_submitted=*/false);
const auto merged_tag = OriginTag(tag_a, tag_b);

uint256_t a_val = 0x0f;
uint256_t b_val = 0xa3;

// Witness-witness path
field_ct x = witness_ct(&builder, a_val);
field_ct y = witness_ct(&builder, b_val);
x.set_origin_tag(tag_a);
y.set_origin_tag(tag_b);
field_ct and_result = stdlib::logic<Builder>::create_logic_constraint(x, y, 8, false);
EXPECT_EQ(and_result.get_origin_tag(), merged_tag);

field_ct xor_result = stdlib::logic<Builder>::create_logic_constraint(x, y, 8, true);
EXPECT_EQ(xor_result.get_origin_tag(), merged_tag);

// Constant-witness path (left constant)
field_ct x_const(&builder, a_val);
x_const.set_origin_tag(tag_a);
field_ct and_result_lc = stdlib::logic<Builder>::create_logic_constraint(x_const, y, 8, false);
EXPECT_EQ(and_result_lc.get_origin_tag(), merged_tag);

// Constant-witness path (right constant)
field_ct y_const(&builder, b_val);
y_const.set_origin_tag(tag_b);
field_ct and_result_rc = stdlib::logic<Builder>::create_logic_constraint(x, y_const, 8, false);
EXPECT_EQ(and_result_rc.get_origin_tag(), merged_tag);
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,17 @@ plookup::ReadData<field_t<Builder>> plookup_read<Builder>::get_lookup_accumulato
const auto accumulator_witnesses =
ctx->create_gates_from_plookup_accumulators(id, lookup_data, lhs_index, key_b_witness);

const auto merged_tag = OriginTag(key_a.get_origin_tag(), key_b.get_origin_tag());
for (size_t i = 0; i < lookup_data[ColumnIdx::C1].size(); ++i) {
lookup[ColumnIdx::C1].emplace_back(
field_t<Builder>::from_witness_index(ctx, accumulator_witnesses[ColumnIdx::C1][i]));
lookup[ColumnIdx::C2].emplace_back(
field_t<Builder>::from_witness_index(ctx, accumulator_witnesses[ColumnIdx::C2][i]));
lookup[ColumnIdx::C3].emplace_back(
field_t<Builder>::from_witness_index(ctx, accumulator_witnesses[ColumnIdx::C3][i]));
lookup[ColumnIdx::C1].back().set_origin_tag(merged_tag);
lookup[ColumnIdx::C2].back().set_origin_tag(merged_tag);
lookup[ColumnIdx::C3].back().set_origin_tag(merged_tag);
}
}
return lookup;
Expand Down
Loading