Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
rom_table.test.cpp
Go to the documentation of this file.
1#include "rom_table.hpp"
5
6#include <gtest/gtest.h>
7using namespace bb;
8
9template <typename Builder> class RomTableTests : public ::testing::Test {
10 public:
14};
15using BuilderTypes = testing::Types<UltraCircuitBuilder, MegaCircuitBuilder>;
16namespace {
18}
20
25TEST(RomTable, TagCorrectness)
26{
32 std::vector<field_ct> table_values;
33 // Create random witness elements
37
38 // Tag all 3 with different tags
39 entry_1.set_origin_tag(submitted_value_origin_tag);
40 entry_2.set_origin_tag(challenge_origin_tag);
41 // The last one is "poisoned" (calculating with this element should result in runtime error)
42 entry_3.set_origin_tag(instant_death_tag);
43
44 table_values.emplace_back(entry_1);
45 table_values.emplace_back(entry_2);
46 table_values.emplace_back(entry_3);
47
48 // Initialize the table with them
49 rom_table_ct table(table_values);
50
51 // Check that the tags of the first two are preserved
52 EXPECT_EQ(table[field_ct(witness_ct(&builder, 0))].get_origin_tag(), submitted_value_origin_tag);
53 EXPECT_EQ(table[field_ct(witness_ct(&builder, 1))].get_origin_tag(), challenge_origin_tag);
54
55#ifndef NDEBUG
56 // Check that computing the sum with the last once crashes the program
57 EXPECT_THROW(table[0] + table[2], std::runtime_error);
58#endif
59}
60
62// tests basic functionality, as well as the number of gates added per ROM read (not including the
63// finalization/processing): one gate per variable lookup, zero gates per constant lookup.
64TYPED_TEST(RomTableTests, RomTableReadWriteConsistency)
65{
66 using Builder = TypeParam;
67 using field_ct = typename TestFixture::field_ct;
68 using witness_ct = typename TestFixture::witness_ct;
69 using rom_table_ct = typename TestFixture::rom_table_ct;
70
72
73 const size_t table_size = 10;
74 std::vector<field_ct> table_values;
75 for (size_t i = 0; i < table_size; ++i) {
76 table_values.emplace_back(witness_ct(&builder, bb::fr::random_element()));
77 }
78
79 rom_table_ct table(table_values);
80
81 field_ct result(0);
82 fr expected(0);
83
84 for (size_t i = 0; i < table_size; ++i) {
85 // if `i` is even, do a variable lookup (i.e., the index witness is _not constant_), if `i` is odd, do a
86 // constant lookup.
87 if (i % 2 == 0) {
88 field_ct index(witness_ct(&builder, static_cast<uint64_t>(i)));
89 const auto before_n = builder.num_gates();
90 const auto to_add = table[index];
91 const auto after_n = builder.num_gates();
92 // should cost 1 gate (the ROM read adds 1 extra gate when the proving key is constructed, i.e., before
93 // finalization), but not for first entry, the first ROM read also builts the ROM table, which will cost
94 // table_size * 2 gates.
95 if (i != 0) {
96 EXPECT_EQ(after_n - before_n, 1ULL);
97 }
98 result += to_add; // variable lookup
99 } else {
100 const auto before_n = builder.num_gates();
101 const auto to_add = table[i]; // constant lookup
102 const auto after_n = builder.num_gates();
103 // should cost 0 gates. Constant lookups are free
104 EXPECT_EQ(after_n - before_n, 0ULL);
105 result += to_add;
106 }
107 expected += table_values[i].get_value();
108 }
109
110 EXPECT_EQ(result.get_value(), expected);
111 EXPECT_EQ(CircuitChecker::check(builder), true);
112}
113// tests that copying the ROM table works as expected.
115{
116 using Builder = TypeParam;
117 using field_ct = typename TestFixture::field_ct;
118 using witness_ct = typename TestFixture::witness_ct;
119 using rom_table_ct = typename TestFixture::rom_table_ct;
120
122
123 std::vector<field_ct> table_values;
124 const size_t table_size = 5;
125 for (size_t i = 0; i < table_size; ++i) {
126 table_values.emplace_back(witness_ct(&builder, bb::fr::random_element()));
127 }
128
129 rom_table_ct table(table_values);
130 const auto copied_rom_table = table;
131 field_ct result(0);
132 fr expected(0);
133
134 for (size_t i = 0; i < table_size; ++i) {
135
136 field_ct index(witness_ct(&builder, static_cast<uint64_t>(i)));
137 const auto to_add = (i % 2 == 0) ? copied_rom_table[index] : table[index];
138 result += to_add;
139 expected += table_values[i].get_value();
140 }
141 EXPECT_EQ(result.get_value(), expected);
142
143 bool verified = CircuitChecker::check(builder);
144 EXPECT_EQ(verified, true);
145}
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:828
void set_origin_tag(const OriginTag &new_tag) const
Definition field.hpp:345
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)
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