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#pragma once
4
5namespace bb {
6
7namespace {
9}
11 public:
15
22 template <typename Builder>
23 static void add_arithmetic_gates_with_public_inputs(Builder& builder, const size_t num_gates = 4)
24 {
25 // For good measure, include a gate with some public inputs
26 for (size_t i = 0; i < num_gates; ++i) {
30 FF d = a + b + c;
31 uint32_t a_idx = builder.add_public_variable(a);
32 uint32_t b_idx = builder.add_variable(b);
33 uint32_t c_idx = builder.add_variable(c);
34 uint32_t d_idx = builder.add_variable(d);
35
36 builder.create_big_add_gate({ a_idx, b_idx, c_idx, d_idx, FF(1), FF(1), FF(1), FF(-1), FF(0) });
37 }
38 }
39
46 template <typename Builder> static void add_arithmetic_gates(Builder& builder, const size_t num_gates = 4)
47 {
48 for (size_t i = 0; i < num_gates; ++i) {
52 FF d = a + b + c;
53 uint32_t a_idx = builder.add_variable(a);
54 uint32_t b_idx = builder.add_variable(b);
55 uint32_t c_idx = builder.add_variable(c);
56 uint32_t d_idx = builder.add_variable(d);
57
58 builder.create_big_add_gate({ a_idx, b_idx, c_idx, d_idx, FF(1), FF(1), FF(1), FF(-1), FF(0) });
59 }
60 }
61
69 template <typename Builder> static void add_lookup_gates(Builder& builder, size_t num_iterations = 1)
70 {
71 auto UINT32_XOR = plookup::MultiTableId::UINT32_XOR;
72
73 // Each iteration adds 6 lookup gates (due to six 6-bit limbs); the first adds a table of size 4096
74 for (size_t i = 0; i < num_iterations; ++i) {
75 // define some arbitrary inputs to uint32 XOR
76 uint32_t left_value = engine.get_random_uint32();
77 uint32_t right_value = engine.get_random_uint32();
78
79 fr left = fr{ left_value, 0, 0, 0 }.to_montgomery_form();
80 fr right = fr{ right_value, 0, 0, 0 }.to_montgomery_form();
81
82 auto left_idx = builder.add_variable(left);
83 auto right_idx = builder.add_variable(right);
84
85 // perform lookups from the uint32 XOR table
86 auto accumulators = plookup::get_lookup_accumulators(UINT32_XOR, left, right, /*is_2_to_1_lookup*/ true);
87 builder.create_gates_from_plookup_accumulators(UINT32_XOR, accumulators, left_idx, right_idx);
88 }
89 }
90
97 template <typename Builder> static void add_RAM_gates(Builder& builder)
98 {
99 std::array<uint32_t, 3> ram_values{ builder.add_variable(5),
100 builder.add_variable(10),
101 builder.add_variable(20) };
102
103 size_t ram_id = builder.create_RAM_array(3);
104
105 for (size_t i = 0; i < 3; ++i) {
106 builder.init_RAM_element(ram_id, i, ram_values[i]);
107 }
108
109 auto val_idx_1 = builder.read_RAM_array(ram_id, builder.add_variable(1));
110 auto val_idx_2 = builder.read_RAM_array(ram_id, builder.add_variable(2));
111 auto val_idx_3 = builder.read_RAM_array(ram_id, builder.add_variable(0));
112
113 builder.create_big_add_gate({
114 val_idx_1,
115 val_idx_2,
116 val_idx_3,
117 builder.zero_idx(),
118 1,
119 1,
120 1,
121 0,
122 -35,
123 });
124 }
125
132 template <typename Builder>
134 const size_t target_log2_dyadic_size = 4,
135 bool include_public_inputs = true)
136 {
137 const size_t target_dyadic_size = 1 << target_log2_dyadic_size;
138 const size_t num_preamble_gates = builder.num_gates();
139 BB_ASSERT_GTE(target_dyadic_size, num_preamble_gates);
140
141 // For good measure, include a gate with some public inputs
142 if (include_public_inputs && target_dyadic_size > num_preamble_gates) {
144 }
145
146 // A proper treatment of this would dynamically calculate how many gates to add given static information about
147 // Builder, but a major overhaul of the execution trace is underway, so we just elect to use a hack. Namely, for
148 // all of builders for which we instantiate this template and for all circuit sizes we care about, to achieve a
149 // desired dyadic circuit size after boilerplate gates, it is sufficient to fill up to OFFSET_HACK-many gates
150 // short of the desired dyadic circuit size.
151 // TODO(https://github.com/AztecProtocol/barretenberg/issues/902)
152 static constexpr size_t OFFSET_HACK = 10;
153
154 // to prevent underflow of the loop upper limit; target size >= 16 should suffice
155 BB_ASSERT_GT(target_dyadic_size, OFFSET_HACK + num_preamble_gates);
156 size_t num_gates_to_add = target_dyadic_size - OFFSET_HACK - 1 - num_preamble_gates;
157
158 // Add arbitrary arithmetic gates to obtain a total of num_gates-many gates
159 add_arithmetic_gates(builder, num_gates_to_add);
160 }
161
168 {
169 // Add a mul accum op, an add accum op and an equality op
170 builder.queue_ecc_add_accum(Point::one() * FF::random_element(&engine));
171 builder.queue_ecc_mul_accum(Point::one() * FF::random_element(&engine), FF::random_element(&engine));
172 builder.queue_ecc_eq();
173 }
174};
175} // namespace bb
#define BB_ASSERT_GTE(left, right,...)
Definition assert.hpp:138
#define BB_ASSERT_GT(left, right,...)
Definition assert.hpp:123
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:124
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.
Definition api.hpp:5
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