Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
databus.test.cpp
Go to the documentation of this file.
2#include <cstddef>
3#include <cstdint>
4#include <gtest/gtest.h>
5
12
15
16using namespace bb;
17namespace {
19
20using FlavorTypes = ::testing::Types<MegaFlavor, MegaZKFlavor>;
21
22template <typename Flavor> class DataBusTests : public ::testing::Test {
23 protected:
24 static void SetUpTestSuite() { bb::srs::init_file_crs_factory(bb::srs::bb_crs_path()); }
25
26 using Curve = curve::BN254;
27 using FF = Curve::ScalarField;
28 using Builder = typename Flavor::CircuitBuilder;
29 using Prover = UltraProver_<Flavor>;
30 using Verifier = UltraVerifier_<Flavor, DefaultIO>;
31
32 // Construct and verify a MegaHonk proof for a given circuit
33 static bool construct_and_verify_proof(MegaCircuitBuilder& builder)
34 {
36 auto verification_key = std::make_shared<typename Flavor::VerificationKey>(prover_instance->get_precomputed());
37 auto vk_and_hash = std::make_shared<typename Flavor::VKAndHash>(verification_key);
38
39 Prover prover{ prover_instance, verification_key };
40 auto proof = prover.construct_proof();
41 Verifier verifier{ vk_and_hash };
42 bool result = verifier.verify_proof(proof).result;
43 return result;
44 }
45
46 // Construct a Mega circuit with some arbitrary sample gates
47 static Builder construct_test_builder()
48 {
49 auto op_queue = std::make_shared<bb::ECCOpQueue>();
50 auto builder = MegaCircuitBuilder{ op_queue };
52 return builder;
53 }
54
65 static Builder construct_circuit_with_databus_reads(Builder& builder, const BusId& bus_idx)
66 {
67
68 const uint32_t NUM_BUS_ENTRIES = 5; // number of entries in the bus column
69 const uint32_t NUM_READS = 7; // greater than size of bus to ensure duplicates
70
71 // Add some arbitrary values to the bus column
72 for (size_t i = 0; i < NUM_BUS_ENTRIES; ++i) {
73 FF val = FF::random_element();
74 uint32_t val_witness_idx = builder.add_variable(val);
75 builder.add_public_calldata(bus_idx, val_witness_idx);
76 }
77
78 // Read from the bus at some random indices
79 for (size_t i = 0; i < NUM_READS; ++i) {
80 uint32_t read_idx = engine.get_random_uint32() % NUM_BUS_ENTRIES;
81 uint32_t read_idx_witness_idx = builder.add_variable(FF(read_idx));
82 builder.read_calldata(bus_idx, read_idx_witness_idx);
83 }
84
85 return builder;
86 }
87};
88
89TYPED_TEST_SUITE(DataBusTests, FlavorTypes);
90
95TYPED_TEST(DataBusTests, KernelCallDataRead)
96{
97 typename TypeParam::CircuitBuilder builder = this->construct_test_builder();
98 this->construct_circuit_with_databus_reads(builder, BusId::KERNEL_CALLDATA);
99 EXPECT_TRUE(CircuitChecker::check(builder));
100 EXPECT_TRUE(this->construct_and_verify_proof(builder));
101}
102
107TYPED_TEST(DataBusTests, AppCallDataRead)
108{
109 for (size_t idx = 0; idx < MAX_APPS_PER_KERNEL; ++idx) {
110 typename TypeParam::CircuitBuilder builder = this->construct_test_builder();
111 this->construct_circuit_with_databus_reads(builder, static_cast<BusId>(idx + 1));
112
113 EXPECT_TRUE(CircuitChecker::check(builder)) << "Circuit check failed for app calldata bus with index " << idx;
114 EXPECT_TRUE(this->construct_and_verify_proof(builder)) << "Failed for app calldata bus with index " << idx;
115 }
116}
117
122TYPED_TEST(DataBusTests, ReturnDataRead)
123{
124 typename TypeParam::CircuitBuilder builder = this->construct_test_builder();
125 this->construct_circuit_with_databus_reads(builder, BusId::RETURNDATA);
126
127 EXPECT_TRUE(CircuitChecker::check(builder));
128 EXPECT_TRUE(this->construct_and_verify_proof(builder));
129}
130
135TYPED_TEST(DataBusTests, ReadAll)
136{
137 typename TypeParam::CircuitBuilder builder = this->construct_test_builder();
138 this->construct_circuit_with_databus_reads(builder, BusId::KERNEL_CALLDATA);
139 for (size_t idx = 0; idx < MAX_APPS_PER_KERNEL; ++idx) {
140 this->construct_circuit_with_databus_reads(builder, static_cast<BusId>(idx + 1));
141 }
142 this->construct_circuit_with_databus_reads(builder, BusId::RETURNDATA);
143
144 EXPECT_TRUE(CircuitChecker::check(builder));
145 EXPECT_TRUE(this->construct_and_verify_proof(builder));
146}
147
153TYPED_TEST(DataBusTests, CallDataDuplicateRead)
154{
155 // Construct a circuit and add some ecc op gates and arithmetic gates
156 typename TypeParam::CircuitBuilder builder = this->construct_test_builder();
157 using FF = TypeParam::FF;
158
159 // Add some values to calldata
160
161 std::vector<FF> calldata_values = { 7, 10, 3, 12, 1 };
162 for (auto& val : calldata_values) {
163 builder.add_public_calldata(BusId::KERNEL_CALLDATA, builder.add_variable(val));
164 }
165
166 // Define some read indices with a duplicate
167 std::vector<uint32_t> read_indices = { 1, 4, 1 };
168
169 // Create some calldata read gates and store the variable indices of the result for later
170 std::vector<uint32_t> result_witness_indices;
171 for (uint32_t& read_idx : read_indices) {
172 // Create a variable corresponding to the index at which we want to read into calldata
173 uint32_t read_idx_witness_idx = builder.add_variable(FF(read_idx));
174
175 auto value_witness_idx = builder.read_calldata(BusId::KERNEL_CALLDATA, read_idx_witness_idx);
176 result_witness_indices.emplace_back(value_witness_idx);
177 }
178
179 // Check that the read result is as expected and that the duplicate reads produce the same result
180 auto expected_read_result_at_1 = calldata_values[1];
181 auto expected_read_result_at_4 = calldata_values[4];
182 auto duplicate_read_result_0 = builder.get_variable(result_witness_indices[0]);
183 auto duplicate_read_result_1 = builder.get_variable(result_witness_indices[1]);
184 auto duplicate_read_result_2 = builder.get_variable(result_witness_indices[2]);
185 EXPECT_EQ(duplicate_read_result_0, expected_read_result_at_1);
186 EXPECT_EQ(duplicate_read_result_1, expected_read_result_at_4);
187 EXPECT_EQ(duplicate_read_result_2, expected_read_result_at_1);
188
189 // Construct and verify Honk proof
190 bool result = this->construct_and_verify_proof(builder);
191 EXPECT_TRUE(result);
192}
193} // namespace
ECCVMCircuitBuilder CircuitBuilder
static void construct_simple_circuit(MegaBuilder &builder)
Generate a simple test circuit with some ECC op gates and conventional arithmetic gates.
static bool check(const Builder &circuit)
Check the witness satisifies the circuit.
bb::fr ScalarField
Definition bn254.hpp:18
virtual uint32_t get_random_uint32()=0
AluTraceBuilder builder
Definition alu.test.cpp:124
numeric::RNG & engine
testing::Types< UltraFlavor, UltraKeccakFlavor, MegaFlavor > FlavorTypes
RNG & get_debug_randomness(bool reset, std::uint_fast64_t seed)
Definition engine.cpp:245
std::filesystem::path bb_crs_path()
void init_file_crs_factory(const std::filesystem::path &path)
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
TYPED_TEST_SUITE(CommitmentKeyTest, Curves)
BusId
Definition databus.hpp:75
TYPED_TEST(CommitmentKeyTest, CommitToZeroPoly)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static field random_element(numeric::RNG *engine=nullptr) noexcept