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// Defining ultra-specific types for local testing.
15
16namespace {
18}
20
26TEST(RamTable, TagCorrectness)
27{
28
30 std::vector<field_ct> table_values;
31
32 // Generate random witnesses
36
37 // Tag them with 3 different tags
38 entry_1.set_origin_tag(submitted_value_origin_tag);
39 entry_2.set_origin_tag(challenge_origin_tag);
40 // The last tag is an instant death tag, that triggers a runtime failure if any computation happens on the element
41 entry_3.set_origin_tag(instant_death_tag);
42
43 table_values.emplace_back(entry_1);
44 table_values.emplace_back(entry_2);
45 table_values.emplace_back(entry_3);
46
47 // Initialize the table
48 ram_table_ct table(table_values);
49
50 // Check that each element has the same tag as original entries
51 EXPECT_EQ(table.read(field_ct(0)).get_origin_tag(), submitted_value_origin_tag);
52 EXPECT_EQ(table.read(field_ct(witness_ct(&builder, 0))).get_origin_tag(), submitted_value_origin_tag);
53 EXPECT_EQ(table.read(field_ct(1)).get_origin_tag(), challenge_origin_tag);
54 EXPECT_EQ(table.read(field_ct(witness_ct(&builder, 1))).get_origin_tag(), challenge_origin_tag);
55
56 // Replace one of the elements in the table with a new one
57 entry_2.set_origin_tag(next_challenge_tag);
58 table.write(field_ct(1), entry_2);
59
60 // Check that the tag has been updated accordingly
61 EXPECT_EQ(table.read(field_ct(1)).get_origin_tag(), next_challenge_tag);
62 EXPECT_EQ(table.read(field_ct(witness_ct(&builder, 1))).get_origin_tag(), next_challenge_tag);
63
64#ifndef NDEBUG
65 // Check that interacting with the poisoned element causes a runtime error
66 EXPECT_THROW(table.read(0) + table.read(2), std::runtime_error);
67#endif
68}
69
70TEST(RamTable, RamTableInitReadConsistency)
71{
73
74 std::vector<field_ct> table_values;
75 const size_t table_size = 10;
76 for (size_t i = 0; i < table_size; ++i) {
77 table_values.emplace_back(witness_ct(&builder, bb::fr::random_element()));
78 }
79
80 ram_table_ct table(table_values);
81
82 field_ct result(0);
83 fr expected(0);
84
85 for (size_t i = 0; i < 10; ++i) {
86 field_ct index(witness_ct(&builder, (uint64_t)i));
87
88 if (i % 2 == 0) {
89 const auto to_add = table.read(index);
90 result += to_add; // variable lookup
91 } else {
92 const auto to_add = table.read(i); // constant lookup
93 result += to_add;
94 }
95 expected += table_values[i].get_value();
96 }
97
98 EXPECT_EQ(result.get_value(), expected);
99
100 bool verified = CircuitChecker::check(builder);
101 EXPECT_EQ(verified, true);
102}
103
104TEST(RamTable, RamTableReadWriteConsistency)
105{
107 const size_t table_size = 10;
108
109 std::vector<fr> table_values(table_size);
110
111 ram_table_ct table(&builder, table_size);
112
113 for (size_t i = 0; i < table_size; ++i) {
114 table.write(i, 0);
115 }
116 field_ct result(0);
117 fr expected(0);
118
119 const auto update = [&]() {
120 for (size_t i = 0; i < table_size / 2; ++i) {
121 table_values[2 * i] = fr::random_element();
122 table_values[2 * i + 1] = fr::random_element();
123
124 // init with both constant and variable values
125 table.write(2 * i, table_values[2 * i]);
126 table.write(2 * i + 1, witness_ct(&builder, table_values[2 * i + 1]));
127 }
128 };
129
130 const auto read = [&]() {
131 for (size_t i = 0; i < table_size / 2; ++i) {
132 const size_t index = table_size - 2 - (i * 2); // access in something other than basic incremental order
133
134 result += table.read(witness_ct(&builder, index));
135 result += table.read(index + 1);
136
137 expected += table_values[index];
138 expected += table_values[index + 1];
139 }
140 };
141
142 update();
143 read();
144 update();
145 read();
146 update();
147
148 EXPECT_EQ(result.get_value(), expected);
149
150 bool verified = CircuitChecker::check(builder);
151 EXPECT_EQ(verified, true);
152}
static bool check(const Builder &circuit)
Check the witness satisifies the circuit.
bb::fr get_value() const
Given a := *this, compute its value given by a.v * a.mul + a.add.
Definition field.cpp:827
void set_origin_tag(const OriginTag &new_tag) const
Definition field.hpp:332
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:123
numeric::RNG & engine
RNG & get_debug_randomness(bool reset, std::uint_fast64_t seed)
Definition engine.cpp:190
Entry point for Barretenberg command-line interface.
void read(B &it, field2< base_field, Params > &value)
TEST(MegaCircuitBuilder, CopyConstructor)
UltraCircuitBuilder_< UltraExecutionTraceBlocks > UltraCircuitBuilder
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
stdlib::field_t< Builder > field_ct
stdlib::witness_t< Builder > witness_ct
static field random_element(numeric::RNG *engine=nullptr) noexcept