Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
ram_table.test.cpp
Go to the documentation of this file.
1#include <gtest/gtest.h>
2
7#include "ram_table.hpp"
8
9using namespace bb;
10
11template <typename Builder> class RamTableTests : public ::testing::Test {
12 public:
16};
17using BuilderTypes = testing::Types<UltraCircuitBuilder, MegaCircuitBuilder>;
18
19namespace {
21}
23
29TEST(RamTable, TagCorrectness)
30{
36 std::vector<field_ct> table_values;
37
38 // Generate random witnesses
42
43 // Tag them with 3 different tags
44 entry_1.set_origin_tag(submitted_value_origin_tag);
45 entry_2.set_origin_tag(challenge_origin_tag);
46 // The last tag is an instant death tag, that triggers a runtime failure if any computation happens on the element
47 entry_3.set_origin_tag(instant_death_tag);
48
49 table_values.emplace_back(entry_1);
50 table_values.emplace_back(entry_2);
51 table_values.emplace_back(entry_3);
52
53 // Initialize the table
54 ram_table_ct table(table_values);
55
56 // Check that each element has the same tag as original entries
57 EXPECT_EQ(table.read(field_ct(0)).get_origin_tag(), submitted_value_origin_tag);
58 EXPECT_EQ(table.read(field_ct(witness_ct(&builder, 0))).get_origin_tag(), submitted_value_origin_tag);
59 EXPECT_EQ(table.read(field_ct(1)).get_origin_tag(), challenge_origin_tag);
60 EXPECT_EQ(table.read(field_ct(witness_ct(&builder, 1))).get_origin_tag(), challenge_origin_tag);
61
62 // Replace one of the elements in the table with a new one
63 entry_2.set_origin_tag(next_challenge_tag);
64 table.write(field_ct(1), entry_2);
65
66 // Check that the tag has been updated accordingly
67 EXPECT_EQ(table.read(field_ct(1)).get_origin_tag(), next_challenge_tag);
68 EXPECT_EQ(table.read(field_ct(witness_ct(&builder, 1))).get_origin_tag(), next_challenge_tag);
69
70#ifndef NDEBUG
71 // Check that interacting with the poisoned element causes a runtime error
72 EXPECT_THROW(table.read(0) + table.read(2), std::runtime_error);
73#endif
74}
75
77
78TYPED_TEST(RamTableTests, RamTableInitReadConsistency)
79{
80
81 using Builder = TypeParam;
82 using field_ct = typename TestFixture::field_ct;
83 using witness_ct = typename TestFixture::witness_ct;
84 using ram_table_ct = typename TestFixture::ram_table_ct;
86
87 std::vector<field_ct> table_values;
88 const size_t table_size = 10;
89 for (size_t i = 0; i < table_size; ++i) {
90 table_values.emplace_back(witness_ct(&builder, bb::fr::random_element()));
91 }
92
93 ram_table_ct table(table_values);
94
95 field_ct result(0);
96 fr expected(0);
97
98 for (size_t i = 0; i < 10; ++i) {
99 field_ct index(witness_ct(&builder, static_cast<uint64_t>(i)));
100
101 if (i % 2 == 0) {
102 const auto to_add = table.read(index);
103 result += to_add; // variable lookup
104 } else {
105 const auto to_add = table.read(i); // constant lookup
106 result += to_add;
107 }
108 expected += table_values[i].get_value();
109 }
110
111 EXPECT_EQ(result.get_value(), expected);
112
113 bool verified = CircuitChecker::check(builder);
114 EXPECT_EQ(verified, true);
115}
116
117TYPED_TEST(RamTableTests, RamTableReadWriteConsistency)
118{
119 using Builder = TypeParam;
120 using field_ct = typename TestFixture::field_ct;
121 using witness_ct = typename TestFixture::witness_ct;
122 using ram_table_ct = typename TestFixture::ram_table_ct;
124 const size_t table_size = 10;
125 const size_t num_reads = 2 * table_size;
126
127 std::vector<fr> table_values(table_size);
128 std::vector<field_ct> zeros(table_size, field_ct(0));
129 ram_table_ct table(&builder, zeros);
130
131 for (size_t i = 0; i < table_size; ++i) {
132 table.write(i, 0);
133 }
134 field_ct result(0); // tracks a running sum of circuit values to verify correctness of RAM operations
135 fr expected(0); // tracks a running sum of native values to verify correctness of RAM operations
136 // lambda that both initializes and updates our table.
137 const auto update = [&]() {
138 for (size_t i = 0; i < table_size / 2; ++i) {
139 table_values[2 * i] = fr::random_element();
140 table_values[2 * i + 1] = fr::random_element();
141
142 // init with both constant and variable index values
143 table.write(2 * i, table_values[2 * i]);
144 table.write(2 * i + 1, witness_ct(&builder, table_values[2 * i + 1]));
145 }
146 };
147 // lambda that reads from our table
148 const auto read = [&]() {
149 for (size_t i = 0; i < num_reads / 2; ++i) {
150 const size_t index_1 = static_cast<size_t>(engine.get_random_uint32() % table_size);
151 const size_t index_2 = static_cast<size_t>(engine.get_random_uint32() % table_size);
152 // both constant and variable reads
153 result += table.read(witness_ct(&builder, index_1));
154 result += table.read(index_2);
155
156 expected += table_values[index_1];
157 expected += table_values[index_2];
158 }
159 };
160
161 update();
162 read();
163 update();
164 read();
165 update();
166
167 EXPECT_EQ(result.get_value(), expected);
168
169 bool verified = CircuitChecker::check(builder);
170 EXPECT_EQ(verified, true);
171}
static bool check(const Builder &circuit)
Check the witness satisifies the circuit.
virtual uint32_t get_random_uint32()=0
bb::fr get_value() const
Given a := *this, compute its value given by a.v * a.mul + a.add.
Definition field.cpp:828
void set_origin_tag(const OriginTag &new_tag) const
Definition field.hpp:345
field_pt read(const field_pt &index) const
Read a field element from the RAM table at an index value.
void write(const field_pt &index, const field_pt &value)
Write a field element from the RAM table at an index value.
AluTraceBuilder builder
Definition alu.test.cpp:124
numeric::RNG & engine
bn254::witness_ct witness_ct
stdlib::field_t< Builder > field_ct
RNG & get_debug_randomness(bool reset, std::uint_fast64_t seed)
Definition engine.cpp:190
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
TYPED_TEST_SUITE(ShpleminiTest, TestSettings)
void read(uint8_t const *&it, Chonk::VerificationKey &vk)
Definition chonk.hpp:350
UltraCircuitBuilder_< UltraExecutionTraceBlocks > UltraCircuitBuilder
TYPED_TEST(ShpleminiTest, CorrectnessOfMultivariateClaimBatching)
TEST(BoomerangMegaCircuitBuilder, BasicCircuit)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
This file contains part of the logic for the Origin Tag mechanism that tracks the use of in-circuit p...
#define STANDARD_TESTING_TAGS
::testing::Types< UltraCircuitBuilder, MegaCircuitBuilder > BuilderTypes
static field random_element(numeric::RNG *engine=nullptr) noexcept