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

chore: reads the return data #6669

Merged
merged 34 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
598eb8b
reads the return data
guipublic May 24, 2024
94005c9
Merge branch 'master' into gd/databus_2
guipublic May 24, 2024
1415da8
Merge branch 'master' into gd/databus_2
guipublic May 24, 2024
b235225
add a simple test for the databus program
ledwards2225 May 24, 2024
1d65d1e
Merge branch 'master' into gd/databus_2
guipublic May 27, 2024
b523175
do not optimise return-data
guipublic May 27, 2024
a0b0ea3
Merge branch 'master' into gd/databus_2
guipublic May 28, 2024
60e2f8e
Merge branch 'master' into gd/databus_2
guipublic May 28, 2024
a427fa8
better flavor
guipublic May 28, 2024
c1be84d
Merge branch 'master' into gd/databus_2
guipublic May 29, 2024
17a442e
Merge branch 'master' into gd/databus_2
guipublic May 29, 2024
169909d
fix merge issue
guipublic May 29, 2024
987dc67
Merge branch 'master' into gd/databus_2
guipublic May 29, 2024
3cd6c41
update path to acir artifacts
guipublic May 30, 2024
8e18baf
Merge branch 'master' into gd/databus_2
guipublic May 30, 2024
059aa37
Merge branch 'master' into gd/databus_2
guipublic May 30, 2024
b8f22c5
disable acir databus test from running in the CI (artifact is missing)
guipublic May 30, 2024
33c6955
Merge branch 'master' into gd/databus_2
guipublic May 30, 2024
9e5f917
Merge branch 'master' into gd/databus_2
guipublic May 30, 2024
576854b
Merge branch 'master' into gd/databus_2
guipublic May 31, 2024
e79886a
use mega builder for the databus, when flavor is mega-honk
guipublic May 31, 2024
f23bece
Merge branch 'master' into gd/databus_2
guipublic May 31, 2024
67ab111
Merge branch 'master' into gd/databus_2
guipublic May 31, 2024
51042c7
fix build issue
ledwards2225 May 31, 2024
c0dda12
Merge branch 'master' into gd/databus_2
guipublic Jun 5, 2024
10b31f5
merge issues
guipublic Jun 5, 2024
731eb8c
Merge branch 'master' into gd/databus_2
guipublic Jun 5, 2024
37fcdf7
test using process ROM method
ledwards2225 Jun 5, 2024
5604c18
use constraint specific methods
ledwards2225 Jun 5, 2024
ff1364a
use template specialization instead of concepts
ledwards2225 Jun 5, 2024
e8dad40
update comment on return data
ledwards2225 Jun 5, 2024
9efd5a5
comments
ledwards2225 Jun 5, 2024
fd8fc71
Merge branch 'master' into gd/databus_2
ledwards2225 Jun 5, 2024
a69a27e
Merge branch 'master' into gd/databus_2
guipublic Jun 19, 2024
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 @@ -35,7 +35,8 @@ class AcirIntegrationTest : public ::testing::Test {
acir_format::AcirProgramStack get_program_stack_data_from_test_file(const std::string& test_program_name,
bool honk_recursion)
{
std::string base_path = "../../acir_tests/acir_tests/" + test_program_name + "/target";
std::string base_path =
"../../../noir/noir-repo/test_programs/execution_success/" + test_program_name + "/target";
std::string bytecode_path = base_path + "/program.json";
std::string witness_path = base_path + "/witness.gz";

Expand Down Expand Up @@ -425,6 +426,29 @@ INSTANTIATE_TEST_SUITE_P(AcirTests,
AcirIntegrationFoldingTest,
testing::Values("fold_basic", "fold_basic_nested_call"));

/**
*@brief A basic test of a circuit generated in noir that makes use of the databus
*
*/
TEST_F(AcirIntegrationTest, DISABLED_Databus)
{
using Flavor = MegaFlavor;
using Builder = Flavor::CircuitBuilder;

std::string test_name = "databus";
info("Test: ", test_name);
acir_format::AcirProgram acir_program = get_program_data_from_test_file(test_name);

// Construct a bberg circuit from the acir representation
Builder builder = acir_format::create_circuit<Builder>(acir_program.constraints, 0, acir_program.witness);

// This prints a summary of the types of gates in the circuit
builder.blocks.summarize();

// Construct and verify Honk proof
EXPECT_TRUE(prove_and_verify_honk<Flavor>(builder));
}

/**
* @brief Ensure that adding gates post-facto to a circuit generated from acir still results in a valid circuit
* @details This is a pattern required by e.g. ClientIvc which appends recursive verifiers to acir-generated circuits
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,10 @@ BlockConstraint handle_memory_init(Program::Opcode::MemoryInit const& mem_init)
}

// Databus is only supported for Goblin, non Goblin builders will treat call_data and return_data as normal array.
if (IsMegaBuilder<Builder>) {
if (std::holds_alternative<Program::BlockType::CallData>(mem_init.block_type.value)) {
block.type = BlockType::CallData;
} else if (std::holds_alternative<Program::BlockType::ReturnData>(mem_init.block_type.value)) {
block.type = BlockType::ReturnData;
}
if (std::holds_alternative<Program::BlockType::CallData>(mem_init.block_type.value)) {
block.type = BlockType::CallData;
} else if (std::holds_alternative<Program::BlockType::ReturnData>(mem_init.block_type.value)) {
block.type = BlockType::ReturnData;
}

return block;
Expand Down Expand Up @@ -509,7 +507,7 @@ AcirFormat circuit_serde_to_acir_format(Program::Circuit const& circuit, bool ho
gate.value);
}
for (const auto& [block_id, block] : block_id_to_block_constraint) {
if (!block.trace.empty()) {
if (!block.trace.empty() || block.type == BlockType::ReturnData) {
af.block_constraints.push_back(block);
}
}
Expand Down
120 changes: 87 additions & 33 deletions barretenberg/cpp/src/barretenberg/dsl/acir_format/block_constraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,71 @@ template <typename Builder> bb::stdlib::field_t<Builder> poly_to_field_ct(const

template <typename Builder>
void create_block_constraints(Builder& builder, const BlockConstraint constraint, bool has_valid_witness_assignments)
requires IsNotMegaBuilder<Builder>
guipublic marked this conversation as resolved.
Show resolved Hide resolved
{
using field_ct = bb::stdlib::field_t<Builder>;
using rom_table_ct = bb::stdlib::rom_table<Builder>;
using ram_table_ct = bb::stdlib::ram_table<Builder>;

std::vector<field_ct> init;
for (auto i : constraint.init) {
field_ct value = poly_to_field_ct(i, builder);
init.push_back(value);
}

switch (constraint.type) {
case BlockType::CallData:
case BlockType::ReturnData:
case BlockType::ROM: {
rom_table_ct table(init);
for (auto& op : constraint.trace) {
ASSERT(op.access_type == 0);
field_ct value = poly_to_field_ct(op.value, builder);
field_ct index = poly_to_field_ct(op.index, builder);
// For a ROM table, constant read should be optimized out:
// The rom_table won't work with a constant read because the table may not be initialized
ASSERT(op.index.q_l != 0);
// We create a new witness w to avoid issues with non-valid witness assignements:
// if witness are not assigned, then w will be zero and table[w] will work
fr w_value = 0;
if (has_valid_witness_assignments) {
// If witness are assigned, we use the correct value for w
w_value = index.get_value();
}
field_ct w = field_ct::from_witness(&builder, w_value);
value.assert_equal(table[w]);
w.assert_equal(index);
}
} break;
case BlockType::RAM: {
ram_table_ct table(init);
for (auto& op : constraint.trace) {
field_ct value = poly_to_field_ct(op.value, builder);
field_ct index = poly_to_field_ct(op.index, builder);

// We create a new witness w to avoid issues with non-valid witness assignements.
// If witness are not assigned, then index will be zero and table[index] won't hit bounds check.
fr index_value = has_valid_witness_assignments ? index.get_value() : 0;
// Create new witness and ensure equal to index.
field_ct::from_witness(&builder, index_value).assert_equal(index);

if (op.access_type == 0) {
value.assert_equal(table.read(index));
} else {
ASSERT(op.access_type == 1);
table.write(index, value);
}
}
} break;
default:
ASSERT(false);
break;
}
}

template <typename Builder>
void create_block_constraints(Builder& builder, const BlockConstraint constraint, bool has_valid_witness_assignments)
requires IsMegaBuilder<Builder>
{
using field_ct = bb::stdlib::field_t<Builder>;
using rom_table_ct = bb::stdlib::rom_table<Builder>;
Expand Down Expand Up @@ -79,44 +144,33 @@ void create_block_constraints(Builder& builder, const BlockConstraint constraint
}
} break;
case BlockType::CallData: {
if constexpr (IsMegaBuilder<Builder>) {
databus_ct databus;
// Populate the calldata in the databus
databus.calldata.set_values(init);
for (const auto& op : constraint.trace) {
ASSERT(op.access_type == 0);
field_ct value = poly_to_field_ct(op.value, builder);
field_ct index = poly_to_field_ct(op.index, builder);
fr w_value = 0;
if (has_valid_witness_assignments) {
// If witness are assigned, we use the correct value for w
w_value = index.get_value();
}
field_ct w = field_ct::from_witness(&builder, w_value);
value.assert_equal(databus.calldata[w]);
w.assert_equal(index);
databus_ct databus;
// Populate the calldata in the databus
databus.calldata.set_values(init);
for (const auto& op : constraint.trace) {
ASSERT(op.access_type == 0);
field_ct value = poly_to_field_ct(op.value, builder);
field_ct index = poly_to_field_ct(op.index, builder);
fr w_value = 0;
if (has_valid_witness_assignments) {
// If witness are assigned, we use the correct value for w
w_value = index.get_value();
}
field_ct w = field_ct::from_witness(&builder, w_value);
value.assert_equal(databus.calldata[w]);
w.assert_equal(index);
}
} break;
case BlockType::ReturnData: {
if constexpr (IsMegaBuilder<Builder>) {
databus_ct databus;
// Populate the returndata in the databus
databus.return_data.set_values(init);
for (const auto& op : constraint.trace) {
ASSERT(op.access_type == 0);
field_ct value = poly_to_field_ct(op.value, builder);
field_ct index = poly_to_field_ct(op.index, builder);
fr w_value = 0;
if (has_valid_witness_assignments) {
// If witness are assigned, we use the correct value for w
w_value = index.get_value();
}
field_ct w = field_ct::from_witness(&builder, w_value);
value.assert_equal(databus.return_data[w]);
w.assert_equal(index);
}
databus_ct databus;
// Populate the returndata in the databus
databus.return_data.set_values(init);
int c = 0;
for (const auto& value : init) {
value.assert_equal(databus.return_data[c]);
c++;
}
ASSERT(constraint.trace.size() == 0);
} break;
default:
ASSERT(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ struct BlockConstraint {
template <typename Builder>
void create_block_constraints(Builder& builder,
const BlockConstraint constraint,
bool has_valid_witness_assignments = true);
bool has_valid_witness_assignments = true)
requires IsMegaBuilder<Builder>;

template <typename Builder>
void create_block_constraints(Builder& builder,
const BlockConstraint constraint,
bool has_valid_witness_assignments = true)
requires IsNotMegaBuilder<Builder>;

template <typename B> inline void read(B& buf, MemOp& mem_op)
{
Expand Down
Loading