Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
mock_circuits.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#pragma once
10
11namespace bb {
12
13namespace {
15}
17 public:
21
28 template <typename Builder>
29 static void add_arithmetic_gates_with_public_inputs(Builder& builder, const size_t num_gates = 4)
30 {
31 // For good measure, include a gate with some public inputs
32 for (size_t i = 0; i < num_gates; ++i) {
36 FF d = a + b + c;
37 uint32_t a_idx = builder.add_public_variable(a);
38 uint32_t b_idx = builder.add_variable(b);
39 uint32_t c_idx = builder.add_variable(c);
40 uint32_t d_idx = builder.add_variable(d);
41
42 builder.create_big_add_gate({ a_idx, b_idx, c_idx, d_idx, FF(1), FF(1), FF(1), FF(-1), FF(0) });
43 }
44 }
45
52 template <typename Builder> static void add_arithmetic_gates(Builder& builder, const size_t num_gates = 4)
53 {
54 for (size_t i = 0; i < num_gates; ++i) {
58 FF d = a + b + c;
59 uint32_t a_idx = builder.add_variable(a);
60 uint32_t b_idx = builder.add_variable(b);
61 uint32_t c_idx = builder.add_variable(c);
62 uint32_t d_idx = builder.add_variable(d);
63
64 builder.create_big_add_gate({ a_idx, b_idx, c_idx, d_idx, FF(1), FF(1), FF(1), FF(-1), FF(0) });
65 }
66 }
67
75 template <typename Builder> static void add_lookup_gates(Builder& builder, size_t num_iterations = 1)
76 {
77 auto UINT32_XOR = plookup::MultiTableId::UINT32_XOR;
78
79 // Each iteration adds 6 lookup gates (due to six 6-bit limbs); the first adds a table of size 4096
80 for (size_t i = 0; i < num_iterations; ++i) {
81 // define some arbitrary inputs to uint32 XOR
82 uint32_t left_value = engine.get_random_uint32();
83 uint32_t right_value = engine.get_random_uint32();
84
85 fr left = fr{ left_value, 0, 0, 0 }.to_montgomery_form();
86 fr right = fr{ right_value, 0, 0, 0 }.to_montgomery_form();
87
88 auto left_idx = builder.add_variable(left);
89 auto right_idx = builder.add_variable(right);
90
91 // perform lookups from the uint32 XOR table
92 auto accumulators = plookup::get_lookup_accumulators(UINT32_XOR, left, right, /*is_2_to_1_lookup*/ true);
93 builder.create_gates_from_plookup_accumulators(UINT32_XOR, accumulators, left_idx, right_idx);
94 }
95 }
96
103 template <typename Builder> static void add_RAM_gates(Builder& builder)
104 {
105 std::array<uint32_t, 3> ram_values{ builder.add_variable(5),
106 builder.add_variable(10),
107 builder.add_variable(20) };
108
109 size_t ram_id = builder.create_RAM_array(3);
110
111 for (size_t i = 0; i < 3; ++i) {
112 builder.init_RAM_element(ram_id, i, ram_values[i]);
113 }
114
115 auto val_idx_1 = builder.read_RAM_array(ram_id, builder.add_variable(1));
116 auto val_idx_2 = builder.read_RAM_array(ram_id, builder.add_variable(2));
117 auto val_idx_3 = builder.read_RAM_array(ram_id, builder.add_variable(0));
118
119 builder.create_big_add_gate({
120 val_idx_1,
121 val_idx_2,
122 val_idx_3,
123 builder.zero_idx,
124 1,
125 1,
126 1,
127 0,
128 -35,
129 });
130 }
131
138 template <typename Builder>
140 const size_t target_log2_dyadic_size = 4,
141 bool include_public_inputs = true)
142 {
143 const size_t target_dyadic_size = 1 << target_log2_dyadic_size;
144 const size_t num_preamble_gates = builder.num_gates;
145 BB_ASSERT_GTE(target_dyadic_size, num_preamble_gates);
146
147 // For good measure, include a gate with some public inputs
148 if (include_public_inputs && target_dyadic_size > num_preamble_gates) {
150 }
151
152 // A proper treatment of this would dynamically calculate how many gates to add given static information about
153 // Builder, but a major overhaul of the execution trace is underway, so we just elect to use a hack. Namely, for
154 // all of builders for which we instantiate this template and for all circuit sizes we care about, to achieve a
155 // desired dyadic circuit size after boilerplate gates, it is sufficient to fill up to OFFSET_HACK-many gates
156 // short of the desired dyadic circuit size.
157 // TODO(https://github.com/AztecProtocol/barretenberg/issues/902)
158 static constexpr size_t OFFSET_HACK = 10;
159
160 // to prevent underflow of the loop upper limit; target size >= 16 should suffice
161 BB_ASSERT_GT(target_dyadic_size, OFFSET_HACK + num_preamble_gates);
162 size_t num_gates_to_add = target_dyadic_size - OFFSET_HACK - 1 - num_preamble_gates;
163
164 // Add arbitrary arithmetic gates to obtain a total of num_gates-many gates
165 add_arithmetic_gates(builder, num_gates_to_add);
166 }
167
174 {
175 // Add a mul accum op, an add accum op and an equality op
176 builder.queue_ecc_add_accum(Point::one() * FF::random_element(&engine));
177 builder.queue_ecc_mul_accum(Point::one() * FF::random_element(&engine), FF::random_element(&engine));
178 builder.queue_ecc_eq();
179 }
180};
181} // namespace bb
#define BB_ASSERT_GTE(left, right,...)
Definition assert.hpp:101
#define BB_ASSERT_GT(left, right,...)
Definition assert.hpp:87
Curve::ScalarField FF
static void add_RAM_gates(Builder &builder)
Add some simple RAM (memory) gates for testing memory read/write functionality.
static void add_arithmetic_gates_with_public_inputs(Builder &builder, const size_t num_gates=4)
Add a specified number of arithmetic gates (with public inputs) to the provided circuit.
Curve::AffineElement Point
static void add_lookup_gates(Builder &builder, size_t num_iterations=1)
Add lookup gates using the uint32 XOR lookup table (table size 4096)
static void construct_arithmetic_circuit(Builder &builder, const size_t target_log2_dyadic_size=4, bool include_public_inputs=true)
Populate a builder with a specified number of arithmetic gates; includes a PI.
static void construct_goblin_ecc_op_circuit(MegaCircuitBuilder &builder)
Populate a builder with some arbitrary goblinized ECC ops, one of each type.
static void add_arithmetic_gates(Builder &builder, const size_t num_gates=4)
Add a specified number of arithmetic gates to the provided circuit.
typename Group::affine_element AffineElement
Definition bn254.hpp:22
bb::fr ScalarField
Definition bn254.hpp:18
AluTraceBuilder builder
Definition alu.test.cpp:123
FF a
FF b
numeric::RNG & engine
RNG & get_debug_randomness(bool reset, std::uint_fast64_t seed)
Definition engine.cpp:190
ReadData< bb::fr > get_lookup_accumulators(const MultiTableId id, const fr &key_a, const fr &key_b, const bool is_2_to_1_lookup)
Given a table ID and the key(s) for a key-value lookup, return the lookup accumulators.
Entry point for Barretenberg command-line interface.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
BB_INLINE constexpr field to_montgomery_form() const noexcept
static field random_element(numeric::RNG *engine=nullptr) noexcept