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
2#include <gtest/gtest.h>
3
8#include "rom_table.hpp"
9
10using namespace bb;
11
12// Defining ultra-specific types for local testing.
17
18namespace {
20}
22
27TEST(RomTable, TagCorrectness)
28{
29
31 std::vector<field_ct> table_values;
32 // Create random witness elements
36
37 // Tag all 3 with different tags
38 entry_1.set_origin_tag(submitted_value_origin_tag);
39 entry_2.set_origin_tag(challenge_origin_tag);
40 // The last one is "poisoned" (calculating with this element should result in runtime error)
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 with them
48 rom_table_ct table(table_values);
49
50 // Check that the tags of the first two are preserved
51 EXPECT_EQ(table[field_ct(witness_ct(&builder, 0))].get_origin_tag(), submitted_value_origin_tag);
52 EXPECT_EQ(table[field_ct(witness_ct(&builder, 1))].get_origin_tag(), challenge_origin_tag);
53
54#ifndef NDEBUG
55 // Check that computing the sum with the last once crashes the program
56 EXPECT_THROW(table[0] + table[2], std::runtime_error);
57#endif
58}
59
60TEST(RomTable, RomTableReadWriteConsistency)
61{
63
64 std::vector<field_ct> table_values;
65 const size_t table_size = 10;
66 for (size_t i = 0; i < table_size; ++i) {
67 table_values.emplace_back(witness_ct(&builder, bb::fr::random_element()));
68 }
69
70 rom_table_ct table(table_values);
71
72 field_ct result(0);
73 fr expected(0);
74
75 for (size_t i = 0; i < 10; ++i) {
76 field_ct index(witness_ct(&builder, (uint64_t)i));
77
78 if (i % 2 == 0) {
79 const auto before_n = builder.num_gates;
80 const auto to_add = table[index];
81 const auto after_n = builder.num_gates;
82 // should cost 1 gates (the ROM read adds 1 extra gate when the proving key is constructed)
83 // (but not for 1st entry, the 1st ROM read also builts the ROM table, which will cost table_size * 2 gates)
84 if (i != 0) {
85 EXPECT_EQ(after_n - before_n, 1ULL);
86 }
87 result += to_add; // variable lookup
88 } else {
89 const auto before_n = builder.num_gates;
90 const auto to_add = table[i]; // constant lookup
91 const auto after_n = builder.num_gates;
92 // should cost 0 gates. Constant lookups are free
93 EXPECT_EQ(after_n - before_n, 0ULL);
94 result += to_add;
95 }
96 expected += table_values[i].get_value();
97 }
98
99 EXPECT_EQ(result.get_value(), expected);
100
101 bool verified = CircuitChecker::check(builder);
102 EXPECT_EQ(verified, true);
103}
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
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
stdlib::field_t< Builder > field_ct
stdlib::witness_t< Builder > witness_ct
static field random_element(numeric::RNG *engine=nullptr) noexcept