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
20using BuilderTypes = testing::Types<UltraCircuitBuilder, MegaCircuitBuilder>;
21namespace {
23}
25
30TEST(TwinRomTable, TagCorrectness)
31{
35 using twin_rom_table_ct = stdlib::twin_rom_table<Builder>;
36 using field_pair_ct = std::array<field_ct, 2>;
38 std::vector<field_pair_ct> table_values;
39
40 // Create random entries
45
46 // Assign different standard tags to them
47 entry_1.set_origin_tag(submitted_value_origin_tag);
48 entry_2.set_origin_tag(challenge_origin_tag);
49 entry_3.set_origin_tag(next_challenge_tag);
50 // The last one is "poisoned" (calculating with this element should result in runtime error)
51 entry_4.set_origin_tag(instant_death_tag);
52
53 // Form entries in the twin table
54 table_values.emplace_back(field_pair_ct{ entry_1, entry_2 });
55 table_values.emplace_back(field_pair_ct{ entry_3, entry_4 });
56
57 // Initialize the table
58 twin_rom_table_ct table(table_values);
59
60 // Check that the tags in positions [0][0], [0][1], [1][0] are preserved
61 EXPECT_EQ(table[field_ct(witness_ct(&builder, 0))][0].get_origin_tag(), submitted_value_origin_tag);
62 EXPECT_EQ(table[field_ct(witness_ct(&builder, 0))][1].get_origin_tag(), challenge_origin_tag);
63 EXPECT_EQ(table[field_ct(1)][0].get_origin_tag(), next_challenge_tag);
64
65#ifndef NDEBUG
66 // Check that working with position [1][1] in debug causes "instant death"
67 EXPECT_THROW(table[1][1] + 1, std::runtime_error);
68#endif
69}
75TYPED_TEST(TwinRomTableTests, ReadWriteConsistency)
76{
77 using Builder = TypeParam;
78 using field_ct = typename TestFixture::field_ct;
79 using witness_ct = typename TestFixture::witness_ct;
80 using twin_rom_table_ct = typename TestFixture::twin_rom_table_ct;
81 using field_pair_ct = typename TestFixture::field_pair_ct;
82
84
85 std::vector<field_pair_ct> table_values;
86 const size_t table_size = 10;
87 // Generate random witness pairs to put in the table
88 for (size_t i = 0; i < table_size; ++i) {
89 table_values.emplace_back(field_pair_ct{ witness_ct(&builder, bb::fr::random_element()),
91 }
92
93 // Initialize the table
94 twin_rom_table_ct table(table_values);
95
96 field_pair_ct result{ field_ct(0), field_ct(0) };
97 std::array<fr, 2> expected{ 0, 0 };
98
99 // Go through the cycle of accessing all entries
100 for (size_t i = 0; i < table_size; ++i) {
101
102 if (i % 2 == 0) {
103 field_ct index(witness_ct(&builder, (uint64_t)i));
104 const auto before_n = builder.num_gates();
105 // Get the entry from the table
106 const auto to_add = table[index];
107 const auto after_n = builder.num_gates();
108 // should cost 1 gates (the ROM read adds 1 extra gate when the proving key is constructed)
109 // (but not for 1st entry, the 1st ROM read also builts the ROM table, which will cost table_size * 2 gates)
110 if (i != 0) {
111 EXPECT_EQ(after_n - before_n, 1ULL);
112 }
113 // Accumulate each of the positions in the result
114 result[0] += to_add[0]; // variable lookup
115 result[1] += to_add[1]; // variable lookup
116 } else {
117 const auto before_n = builder.num_gates();
118 const auto to_add = table[i]; // constant lookup
119 const auto after_n = builder.num_gates();
120 // should cost 0 gates. Constant lookups are free
121 EXPECT_EQ(after_n - before_n, 0ULL);
122 result[0] += to_add[0];
123 result[1] += to_add[1];
124 }
125 // Accumulate original values
126 auto expected_values = table_values[i];
127 expected[0] += expected_values[0].get_value();
128 expected[1] += expected_values[1].get_value();
129 }
130
131 // Check that the sum of the original values is the same as the sum of the ones received from the TwinRomTable
132 // primitive
133 EXPECT_EQ(result[0].get_value(), expected[0]);
134 EXPECT_EQ(result[1].get_value(), expected[1]);
135
136 bool verified = CircuitChecker::check(builder);
137 EXPECT_EQ(verified, true);
138}
std::array< field_ct, 2 > field_pair_ct
static bool check(const Builder &circuit)
Check the witness satisifies the circuit.
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