Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
twin_rom_table.test.cpp
Go to the documentation of this file.
1
2#include <array>
3#include <gtest/gtest.h>
4
9#include "twin_rom_table.hpp"
10
11using namespace bb;
12
13// Defining ultra-specific types for local testing.
19
20namespace {
22}
24
29TEST(TwinRomTable, TagCorrectness)
30{
31
33 std::vector<field_pair_ct> table_values;
34
35 // Create random entries
40
41 // Assign different standard tags to them
42 entry_1.set_origin_tag(submitted_value_origin_tag);
43 entry_2.set_origin_tag(challenge_origin_tag);
44 entry_3.set_origin_tag(next_challenge_tag);
45
46 // Assign the instant death tag to one of the
47 // entries
48 // It causes an error in Debug if it is being merged with another tag (when arithmetic actions are being performed
49 // on it)
50 entry_4.set_origin_tag(instant_death_tag);
51
52 // Form entries in the twin table
53 table_values.emplace_back(field_pair_ct{ entry_1, entry_2 });
54 table_values.emplace_back(field_pair_ct{ entry_3, entry_4 });
55
56 // Initialize the table
57 twin_rom_table_ct table(table_values);
58
59 // Check that the tags in positions [0][0], [0][1], [1][0] are preserved
60 EXPECT_EQ(table[field_ct(witness_ct(&builder, 0))][0].get_origin_tag(), submitted_value_origin_tag);
61 EXPECT_EQ(table[field_ct(witness_ct(&builder, 0))][1].get_origin_tag(), challenge_origin_tag);
62 EXPECT_EQ(table[field_ct(1)][0].get_origin_tag(), next_challenge_tag);
63
64#ifndef NDEBUG
65 // Check that working with position [1][1] in debug causes "instant death"
66 EXPECT_THROW(table[1][1] + 1, std::runtime_error);
67#endif
68}
69
74TEST(TwinRomTable, ReadWriteConsistency)
75{
77
78 std::vector<field_pair_ct> table_values;
79 const size_t table_size = 10;
80 // Generate random witness pairs to put in the table
81 for (size_t i = 0; i < table_size; ++i) {
82 table_values.emplace_back(field_pair_ct{ witness_ct(&builder, bb::fr::random_element()),
84 }
85
86 // Initialize the table
87 twin_rom_table_ct table(table_values);
88
89 field_pair_ct result{ field_ct(0), field_ct(0) };
90 std::array<fr, 2> expected{ 0, 0 };
91
92 // Go throught the cycle of accessing all entries
93 for (size_t i = 0; i < 10; ++i) {
94 field_ct index(witness_ct(&builder, (uint64_t)i));
95
96 if (i % 2 == 0) {
97 const auto before_n = builder.num_gates;
98 // Get the entry from the table
99 const auto to_add = table[index];
100 const auto after_n = builder.num_gates;
101 // should cost 1 gates (the ROM read adds 1 extra gate when the proving key is constructed)
102 // (but not for 1st entry, the 1st ROM read also builts the ROM table, which will cost table_size * 2 gates)
103 if (i != 0) {
104 EXPECT_EQ(after_n - before_n, 1ULL);
105 }
106 // Accumulate each of the positions in the result
107 result[0] += to_add[0]; // variable lookup
108 result[1] += to_add[1]; // variable lookup
109 } else {
110 const auto before_n = builder.num_gates;
111 const auto to_add = table[i]; // constant lookup
112 const auto after_n = builder.num_gates;
113 // should cost 0 gates. Constant lookups are free
114 EXPECT_EQ(after_n - before_n, 0ULL);
115 result[0] += to_add[0];
116 result[1] += to_add[1];
117 }
118 // Accumulate original values
119 auto expected_values = table_values[i];
120 expected[0] += expected_values[0].get_value();
121 expected[1] += expected_values[1].get_value();
122 }
123
124 // Check that the sum of the original values is the same as the sum of the ones received from the TwinRomTable
125 // primitive
126 EXPECT_EQ(result[0].get_value(), expected[0]);
127 EXPECT_EQ(result[1].get_value(), expected[1]);
128
129 bool verified = CircuitChecker::check(builder);
130 EXPECT_EQ(verified, true);
131}
static bool check(const Builder &circuit)
Check the witness satisifies the circuit.
void set_origin_tag(const OriginTag &new_tag) const
Definition field.hpp:332
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.
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
static field random_element(numeric::RNG *engine=nullptr) noexcept
stdlib::field_t< Builder > field_ct
std::array< field_ct, 2 > field_pair_ct
stdlib::witness_t< Builder > witness_ct