Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
block_constraint.test.cpp
Go to the documentation of this file.
2#include "acir_format.hpp"
4
8
9#include <gtest/gtest.h>
10#include <vector>
11
12using namespace acir_format;
13
14class UltraPlonkRAM : public ::testing::Test {
15 protected:
17};
18
19class MegaHonk : public ::testing::Test {
20 public:
26
27 // Construct and verify an MegaHonk proof for the provided circuit
28 static bool prove_and_verify(Builder& circuit)
29 {
30 auto proving_key = std::make_shared<DeciderProvingKey_<Flavor>>(circuit);
31 auto verification_key = std::make_shared<VerificationKey>(proving_key->get_precomputed());
32 Prover prover{ proving_key, verification_key };
33 auto proof = prover.construct_proof();
34
35 Verifier verifier{ verification_key };
36
37 bool result = verifier.template verify_proof<DefaultIO>(proof).result;
38 return result;
39 }
40
41 protected:
43};
44size_t generate_block_constraint(BlockConstraint& constraint, WitnessVector& witness_values)
45{
46 size_t witness_len = 0;
47 witness_values.emplace_back(1);
48 witness_len++;
49
50 fr two = fr::one() + fr::one();
51 poly_triple a0{
52 .a = 0,
53 .b = 0,
54 .c = 0,
55 .q_m = 0,
56 .q_l = two,
57 .q_r = 0,
58 .q_o = 0,
59 .q_c = 0,
60 };
61 fr three = fr::one() + two;
62 poly_triple a1{
63 .a = 0,
64 .b = 0,
65 .c = 0,
66 .q_m = 0,
67 .q_l = 0,
68 .q_r = 0,
69 .q_o = 0,
70 .q_c = three,
71 };
72 poly_triple r1{
73 .a = 0,
74 .b = 0,
75 .c = 0,
76 .q_m = 0,
77 .q_l = fr::one(),
78 .q_r = 0,
79 .q_o = 0,
80 .q_c = fr::neg_one(),
81 };
82 poly_triple r2{
83 .a = 0,
84 .b = 0,
85 .c = 0,
86 .q_m = 0,
87 .q_l = two,
88 .q_r = 0,
89 .q_o = 0,
90 .q_c = fr::neg_one(),
91 };
93 .a = 1,
94 .b = 0,
95 .c = 0,
96 .q_m = 0,
97 .q_l = fr::one(),
98 .q_r = 0,
99 .q_o = 0,
100 .q_c = 0,
101 };
102 witness_values.emplace_back(2);
103 witness_len++;
104 poly_triple z{
105 .a = 2,
106 .b = 0,
107 .c = 0,
108 .q_m = 0,
109 .q_l = fr::one(),
110 .q_r = 0,
111 .q_o = 0,
112 .q_c = 0,
113 };
114 witness_values.emplace_back(3);
115 witness_len++;
116 MemOp op1{
117 .access_type = 0,
118 .index = r1,
119 .value = y,
120 };
121 MemOp op2{
122 .access_type = 0,
123 .index = r2,
124 .value = z,
125 };
126 constraint = BlockConstraint{
127 .init = { a0, a1 },
128 .trace = { op1, op2 },
129 .type = BlockType::ROM,
130 };
131
132 return witness_len;
133}
134
135TEST_F(UltraPlonkRAM, TestBlockConstraint)
136{
137 BlockConstraint block;
138 AcirProgram program;
139 size_t num_variables = generate_block_constraint(block, program.witness);
140 program.constraints = {
141 .varnum = static_cast<uint32_t>(num_variables),
142 .num_acir_opcodes = 7,
143 .public_inputs = {},
144 .block_constraints = { block },
145 .original_opcode_indices = create_empty_original_opcode_indices(),
146 };
148
149 auto builder = create_circuit(program);
150
151 EXPECT_TRUE(CircuitChecker::check(builder));
152}
153
155{
156 BlockConstraint block;
157 AcirProgram program;
158 size_t num_variables = generate_block_constraint(block, program.witness);
159 block.type = BlockType::CallData;
160
161 program.constraints = {
162 .varnum = static_cast<uint32_t>(num_variables),
163 .num_acir_opcodes = 1,
164 .public_inputs = {},
165 .block_constraints = { block },
166 .original_opcode_indices = create_empty_original_opcode_indices(),
167 };
169
170 // Construct a bberg circuit from the acir representation
171 auto circuit = create_circuit<Builder>(program);
172
173 EXPECT_TRUE(CircuitChecker::check(circuit));
174}
175
176TEST_F(MegaHonk, DatabusReturn)
177{
178 BlockConstraint block;
179 AcirProgram program;
180 size_t num_variables = generate_block_constraint(block, program.witness);
181 block.type = BlockType::CallData;
182
183 poly_triple rd_index{
184 .a = static_cast<uint32_t>(num_variables),
185 .b = 0,
186 .c = 0,
187 .q_m = 0,
188 .q_l = 1,
189 .q_r = 0,
190 .q_o = 0,
191 .q_c = 0,
192 };
193 program.witness.emplace_back(0);
194 ++num_variables;
195 auto fr_five = fr(5);
196 poly_triple rd_read{
197 .a = static_cast<uint32_t>(num_variables),
198 .b = 0,
199 .c = 0,
200 .q_m = 0,
201 .q_l = 1,
202 .q_r = 0,
203 .q_o = 0,
204 .q_c = 0,
205 };
206 program.witness.emplace_back(fr_five);
207 poly_triple five{
208 .a = 0,
209 .b = 0,
210 .c = 0,
211 .q_m = 0,
212 .q_l = 0,
213 .q_r = 0,
214 .q_o = 0,
215 .q_c = fr(fr_five),
216 };
217 ++num_variables;
218 MemOp op_rd{
219 .access_type = 0,
220 .index = rd_index,
221 .value = rd_read,
222 };
223 // Initialize the data_bus as [5] and read its value into rd_read
224 auto return_data = BlockConstraint{
225 .init = { five },
226 .trace = { op_rd },
227 .type = BlockType::ReturnData,
228 };
229
230 // Assert that call_data[0]+call_data[1] == return_data[0]
231 poly_triple assert_equal{
232 .a = 1,
233 .b = 2,
234 .c = rd_read.a,
235 .q_m = 0,
236 .q_l = 1,
237 .q_r = 1,
238 .q_o = fr::neg_one(),
239 .q_c = 0,
240 };
241
242 program.constraints = {
243 .varnum = static_cast<uint32_t>(num_variables),
244 .num_acir_opcodes = 2,
245 .public_inputs = {},
246 .poly_triple_constraints = { assert_equal },
247 .block_constraints = { block },
248 .original_opcode_indices = create_empty_original_opcode_indices(),
249 };
251
252 // Construct a bberg circuit from the acir representation
253 auto circuit = create_circuit<Builder>(program);
254
255 EXPECT_TRUE(CircuitChecker::check(circuit));
256}
acir_format::AcirFormatOriginalOpcodeIndices create_empty_original_opcode_indices()
void mock_opcode_indices(acir_format::AcirFormat &constraint_system)
size_t generate_block_constraint(BlockConstraint &constraint, WitnessVector &witness_values)
static void SetUpTestSuite()
static bool prove_and_verify(Builder &circuit)
static void SetUpTestSuite()
The verification key is responsible for storing the commitments to the precomputed (non-witness) poly...
MegaCircuitBuilder CircuitBuilder
static bool check(const Builder &circuit)
Check the witness satisifies the circuit.
AluTraceBuilder builder
Definition alu.test.cpp:123
FF b
UltraCircuitBuilder create_circuit(AcirProgram &program, const ProgramMetadata &metadata)
Specialization for creating an Ultra circuit from an acir program.
bb::SlabVector< bb::fr > WitnessVector
std::filesystem::path bb_crs_path()
void init_file_crs_factory(const std::filesystem::path &path)
TEST_F(BoomerangGoblinRecursiveVerifierTests, graph_description_basic)
Construct and check a goblin recursive verification circuit.
field< Bn254FrParams > fr
Definition fr.hpp:174
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::vector< bb::poly_triple > init
static constexpr field neg_one()
static constexpr field one()