Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed bug where range constraining connected witnesses threw an error #369

Merged
merged 4 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
36 changes: 34 additions & 2 deletions cpp/src/barretenberg/plonk/composer/ultra_composer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1217,8 +1217,40 @@ void UltraComposer::create_new_range_constraint(const uint32_t variable_index,
}

auto& list = range_lists[target_range];
assign_tag(variable_index, list.range_tag);
list.variable_indices.emplace_back(variable_index);
if (real_variable_tags[real_variable_index[variable_index]] != list.range_tag) {

if (real_variable_tags[real_variable_index[variable_index]] != DUMMY_TAG) {
const auto existing_tag = real_variable_tags[real_variable_index[variable_index]];
bool found_tag = false;
// existing range?
for (const auto& r : range_lists) {
if (r.second.range_tag == existing_tag) {
found_tag = true;
if (r.first < target_range) {
// already has a more restrictive range check, skip
return;
} else {
// difficult to remove an existing range check.
// Instead deep-copy the variable and apply a range check to new variable
const uint32_t copied_witness = add_variable(get_variable(variable_index));
create_add_gate({ .a = variable_index,
.b = copied_witness,
.c = zero_idx,
.a_scaling = 1,
.b_scaling = -1,
.c_scaling = 0,
.const_scaling = 0 });
// recursve w. new witness that has no tag attached
create_new_range_constraint(copied_witness, target_range, msg);
return;
}
}
}
ASSERT(found_tag == true);
}
assign_tag(variable_index, list.range_tag);
list.variable_indices.emplace_back(variable_index);
}
}

void UltraComposer::process_range_list(const RangeList& list)
Expand Down
4 changes: 4 additions & 0 deletions cpp/src/barretenberg/plonk/composer/ultra_composer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,10 @@ class UltraComposer : public ComposerBase {
void assign_tag(const uint32_t variable_index, const uint32_t tag)
{
ASSERT(tag <= current_tag);
// If we've already assigned this tag to this variable, return (can happen due to copy constraints)
if (real_variable_tags[real_variable_index[variable_index]] == tag) {
return;
}
ASSERT(real_variable_tags[real_variable_index[variable_index]] == DUMMY_TAG);
real_variable_tags[real_variable_index[variable_index]] = tag;
}
Expand Down
35 changes: 35 additions & 0 deletions cpp/src/barretenberg/plonk/composer/ultra_composer.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -758,4 +758,39 @@ TYPED_TEST(ultra_composer, ram)
TestFixture::prove_and_verify(composer, /*expected_result=*/true);
}

TYPED_TEST(ultra_composer, range_checks_on_duplicates)
{
UltraComposer composer = UltraComposer();

uint32_t a = composer.add_variable(100);
uint32_t b = composer.add_variable(100);
uint32_t c = composer.add_variable(100);
uint32_t d = composer.add_variable(100);

composer.assert_equal(a, b);
composer.assert_equal(a, c);
composer.assert_equal(a, d);

composer.create_new_range_constraint(a, 1000);
composer.create_new_range_constraint(b, 1001);
composer.create_new_range_constraint(c, 999);
composer.create_new_range_constraint(d, 1000);

composer.create_big_add_gate(
{
a,
b,
c,
d,
0,
0,
0,
0,
0,
},
false);

TestFixture::prove_and_verify(composer, /*expected_result=*/true);
}

} // namespace proof_system::plonk::test_ultra_composer