Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
acir_format.test.cpp
Go to the documentation of this file.
1#include <gtest/gtest.h>
2#include <memory>
3#include <vector>
4
5#include "acir_format.hpp"
9
11
12using namespace bb;
13using namespace bb::crypto;
14using namespace acir_format;
15
16class AcirFormatTests : public ::testing::Test {
17 protected:
19};
20TEST_F(AcirFormatTests, TestASingleConstraintNoPubInputs)
21{
22
23 poly_triple constraint{
24 .a = 0,
25 .b = 1,
26 .c = 2,
27 .q_m = 0,
28 .q_l = 1,
29 .q_r = 1,
30 .q_o = -1,
31 .q_c = 0,
32 };
33
34 AcirFormat constraint_system{
35 .varnum = 4,
36 .num_acir_opcodes = 1,
37 .public_inputs = {},
38 .poly_triple_constraints = { constraint },
39 .original_opcode_indices = create_empty_original_opcode_indices(),
40 };
41 mock_opcode_indices(constraint_system);
42 WitnessVector witness{ 5, 7, 12 };
43 AcirProgram program{ constraint_system, witness };
44 auto builder = create_circuit(program);
45
46 EXPECT_TRUE(CircuitChecker::check(builder));
47}
48
49TEST_F(AcirFormatTests, MsgpackLogicConstraint)
50{
51 auto [actual, expected] = msgpack_roundtrip(LogicConstraint{});
52 EXPECT_EQ(actual, expected);
53}
54TEST_F(AcirFormatTests, TestLogicGateFromNoirCircuit)
55{
64 RangeConstraint range_a{
65 .witness = 0,
66 .num_bits = 32,
67 };
68 RangeConstraint range_b{
69 .witness = 1,
70 .num_bits = 32,
71 };
72
73 LogicConstraint logic_constraint{
76 .result = 2,
77 .num_bits = 32,
78 .is_xor_gate = 1,
79 };
80 poly_triple expr_a{
81 .a = 2,
82 .b = 3,
83 .c = 0,
84 .q_m = 0,
85 .q_l = 1,
86 .q_r = -1,
87 .q_o = 0,
88 .q_c = -10,
89 };
90 poly_triple expr_b{
91 .a = 3,
92 .b = 4,
93 .c = 5,
94 .q_m = 1,
95 .q_l = 0,
96 .q_r = 0,
97 .q_o = -1,
98 .q_c = 0,
99 };
100 poly_triple expr_c{
101 .a = 3,
102 .b = 5,
103 .c = 3,
104 .q_m = 1,
105 .q_l = 0,
106 .q_r = 0,
107 .q_o = -1,
108 .q_c = 0,
109
110 };
111 poly_triple expr_d{
112 .a = 5,
113 .b = 0,
114 .c = 0,
115 .q_m = 0,
116 .q_l = -1,
117 .q_r = 0,
118 .q_o = 0,
119 .q_c = 1,
120 };
121 // EXPR [ (1, _4, _5) (-1, _6) 0 ]
122 // EXPR [ (1, _4, _6) (-1, _4) 0 ]
123 // EXPR [ (-1, _6) 1 ]
124
125 AcirFormat constraint_system{
126 .varnum = 6,
127 .num_acir_opcodes = 7,
128 .public_inputs = { 1 },
129 .logic_constraints = { logic_constraint },
130 .range_constraints = { range_a, range_b },
131 .poly_triple_constraints = { expr_a, expr_b, expr_c, expr_d },
132 .original_opcode_indices = create_empty_original_opcode_indices(),
133 };
134 mock_opcode_indices(constraint_system);
135
136 uint256_t inverse_of_five = fr(5).invert();
137 WitnessVector witness{
138 5, 10, 15, 5, inverse_of_five, 1,
139 };
140 AcirProgram program{ constraint_system, witness };
141 auto builder = create_circuit(program);
142
143 EXPECT_TRUE(CircuitChecker::check(builder));
144}
145
146TEST_F(AcirFormatTests, TestKeccakPermutation)
147{
149 keccak_permutation{
150 .state = {
176 },
177 .result = { 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
178 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50 },
179 };
180
181 AcirFormat constraint_system{
182 .varnum = 51,
183 .num_acir_opcodes = 1,
184 .public_inputs = {},
185 .keccak_permutations = { keccak_permutation },
186 .original_opcode_indices = create_empty_original_opcode_indices(),
187 };
188 mock_opcode_indices(constraint_system);
189
190 WitnessVector witness{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
191 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
192 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50 };
193
194 AcirProgram program{ constraint_system, witness };
195 auto builder = create_circuit(program);
196
197 EXPECT_TRUE(CircuitChecker::check(builder));
198}
199
200TEST_F(AcirFormatTests, TestCollectsGateCounts)
201{
202
203 // Witness 0 + witness 1 = witness 2
204 poly_triple first_gate{
205 .a = 0,
206 .b = 1,
207 .c = 2,
208 .q_m = 0,
209 .q_l = 1,
210 .q_r = 1,
211 .q_o = -1,
212 .q_c = 0,
213 };
214
215 // Witness 1 = 27
216 poly_triple second_gate{
217 .a = 1,
218 .b = 0,
219 .c = 0,
220 .q_m = 0,
221 .q_l = 1,
222 .q_r = 0,
223 .q_o = 0,
224 .q_c = -27,
225 };
226
227 AcirFormat constraint_system{
228 .varnum = 4,
229 .num_acir_opcodes = 2,
230 .public_inputs = {},
231 .poly_triple_constraints = { first_gate, second_gate },
232 .original_opcode_indices = create_empty_original_opcode_indices(),
233 };
234 mock_opcode_indices(constraint_system);
235 WitnessVector witness{ 5, 27, 32 };
236 AcirProgram program{ constraint_system, witness };
237 const ProgramMetadata metadata{ .collect_gates_per_opcode = true };
238 auto builder = create_circuit(program, metadata);
239
240 EXPECT_EQ(program.constraints.gates_per_opcode, std::vector<size_t>({ 2, 1 }));
241}
242
244{
245
246 WitnessVector witness_values;
247 witness_values.emplace_back(fr(0));
248
249 witness_values = {
250 fr(0), fr(1), fr(2), fr(3), fr(4), fr(5), fr(6), fr(7), fr(8), fr(9), fr(10), fr(11), fr(12), fr(13), fr(-91),
251 };
252
253 bb::mul_quad_<fr> quad1 = {
254 .a = 0,
255 .b = 1,
256 .c = 2,
257 .d = 3,
258 .mul_scaling = 0,
259 .a_scaling = fr::one(),
260 .b_scaling = fr::one(),
261 .c_scaling = fr::one(),
262 .d_scaling = fr::one(),
263 .const_scaling = fr(0),
264 };
265
266 bb::mul_quad_<fr> quad2 = {
267 .a = 4,
268 .b = 5,
269 .c = 6,
270 .d = 0,
271 .mul_scaling = 0,
272 .a_scaling = fr::one(),
273 .b_scaling = fr::one(),
274 .c_scaling = fr::one(),
275 .d_scaling = fr(0),
276 .const_scaling = fr(0),
277 };
278
279 bb::mul_quad_<fr> quad3 = {
280 .a = 7,
281 .b = 8,
282 .c = 9,
283 .d = 0,
284 .mul_scaling = 0,
285 .a_scaling = fr::one(),
286 .b_scaling = fr::one(),
287 .c_scaling = fr::one(),
288 .d_scaling = fr(0),
289 .const_scaling = fr(0),
290 };
291 bb::mul_quad_<fr> quad4 = {
292 .a = 10,
293 .b = 11,
294 .c = 12,
295 .d = 0,
296 .mul_scaling = 0,
297 .a_scaling = fr::one(),
298 .b_scaling = fr::one(),
299 .c_scaling = fr::one(),
300 .d_scaling = fr(0),
301 .const_scaling = fr(0),
302 };
303 bb::mul_quad_<fr> quad5 = {
304 .a = 13,
305 .b = 14,
306 .c = 0,
307 .d = 18,
308 .mul_scaling = 0,
309 .a_scaling = fr::one(),
310 .b_scaling = fr::one(),
311 .c_scaling = fr(0),
312 .d_scaling = fr(-1),
313 .const_scaling = fr(0),
314 };
315
316 auto res_x = fr(91);
317 auto assert_equal = poly_triple{
318 .a = 14,
319 .b = 0,
320 .c = 0,
321 .q_m = 0,
322 .q_l = fr::one(),
323 .q_r = 0,
324 .q_o = 0,
325 .q_c = res_x,
326 };
327 auto quad_constraint = { quad1, quad2, quad3, quad4, quad5 };
328 size_t num_variables = witness_values.size();
329 AcirFormat constraint_system{
330 .varnum = static_cast<uint32_t>(num_variables + 1),
331 .num_acir_opcodes = 1,
332 .public_inputs = {},
333 .poly_triple_constraints = { assert_equal },
334 .big_quad_constraints = { quad_constraint },
335 .original_opcode_indices = create_empty_original_opcode_indices(),
336 };
337 mock_opcode_indices(constraint_system);
338
339 AcirProgram program{ constraint_system, witness_values };
340 auto builder = create_circuit(program);
341
342 EXPECT_TRUE(CircuitChecker::check(builder));
343}
acir_format::AcirFormatOriginalOpcodeIndices create_empty_original_opcode_indices()
void mock_opcode_indices(acir_format::AcirFormat &constraint_system)
static void SetUpTestSuite()
static bool check(const Builder &circuit)
Check the witness satisifies the circuit.
AluTraceBuilder builder
Definition alu.test.cpp:123
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.
Entry point for Barretenberg command-line interface.
field< Bn254FrParams > fr
Definition fr.hpp:174
std::array< WitnessOrConstant< bb::fr >, 25 > state
WitnessOrConstant< bb::fr > a
static WitnessOrConstant from_index(uint32_t index)
static constexpr field one()
constexpr field invert() const noexcept
std::pair< T, T > msgpack_roundtrip(const T &object)