Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
permutation.test.cpp
Go to the documentation of this file.
5#include "ultra_honk.test.hpp"
6
7using namespace bb;
8
9#ifdef STARKNET_GARAGA_FLAVORS
10using FlavorTypes = testing::Types<UltraFlavor,
14 UltraStarknetFlavor,
15 UltraStarknetZKFlavor>;
16#else
17using FlavorTypes = testing::Types<UltraFlavor, UltraZKFlavor, UltraKeccakFlavor, UltraKeccakZKFlavor>;
18#endif
19template <typename T> using PermutationTests = UltraHonkTests<T>;
21using NonZKFlavorTypes = testing::Types<UltraFlavor, UltraKeccakFlavor>;
22template <typename T> using PermutationNonZKTests = UltraHonkTests<T>;
24
38TYPED_TEST(PermutationTests, NonTrivialTagPermutation)
39{
41
42 // Create two distinct values
44 fr y = -x;
45
46 // first multiset {x, y}
47 auto x1_idx = builder.add_variable(x);
48 auto y1_idx = builder.add_variable(y);
49
50 // second multiset {y, x}
51 auto y2_idx = builder.add_variable(y);
52 auto x2_idx = builder.add_variable(x);
53
54 // Dummy gates to include variables in the trace
55 builder.create_add_gate({ x1_idx, y1_idx, builder.zero_idx(), 1, 1, 0, 0 });
56 builder.create_add_gate({ y2_idx, x2_idx, builder.zero_idx(), 1, 1, 0, 0 });
57
58 // Set up tag transposition: first_tag <-> second_tag
59 auto first_tag = builder.get_new_tag();
60 auto second_tag = builder.get_new_tag();
61 builder.set_tau_transposition(first_tag, second_tag);
62
63 // Assign tags: first_tag -> {x, y}, second_tag -> {y, x}
64 builder.assign_tag(x1_idx, first_tag);
65 builder.assign_tag(y1_idx, first_tag);
66 builder.assign_tag(y2_idx, second_tag);
67 builder.assign_tag(x2_idx, second_tag);
68
69 TestFixture::set_default_pairing_points_and_ipa_claim_and_proof(builder);
70 TestFixture::prove_and_verify(builder, /*expected_result=*/true);
71}
72
84TYPED_TEST(PermutationTests, NonTrivialGeneralizedPerm)
85{
87
89 fr y = -x;
90
91 // Helper to create a pair of equal variables (linked by copy constraint)
92 auto add_equal_pair = [&](fr value) {
93 auto idx1 = builder.add_variable(value);
94 auto idx2 = builder.add_variable(value);
95 builder.assert_equal(idx1, idx2);
96 return std::make_pair(idx1, idx2);
97 };
98
99 // Create pairs of equal variables
100 auto [x1_idx, x1_copy_idx] = add_equal_pair(x);
101 auto [y1_idx, y1_copy_idx] = add_equal_pair(y);
102 auto [x2_idx, x2_copy_idx] = add_equal_pair(x);
103 auto [y2_idx, y2_copy_idx] = add_equal_pair(y);
104
105 // Set up tag transposition for multiset-equality check
106 auto first_tag = builder.get_new_tag();
107 auto second_tag = builder.get_new_tag();
108 builder.set_tau_transposition(first_tag, second_tag);
109
110 // first_tag -> {x, y}, second_tag -> {x, y} (same multisets)
111 builder.assign_tag(x1_idx, first_tag);
112 builder.assign_tag(y1_idx, first_tag);
113 builder.assign_tag(x2_idx, second_tag);
114 builder.assign_tag(y2_idx, second_tag);
115
116 // Dummy gates using copy variables (z1 - z2 = 0)
117 builder.create_add_gate({ x1_copy_idx, x1_idx, builder.zero_idx(), 1, -1, 0, 0 });
118 builder.create_add_gate({ y1_idx, y2_idx, builder.zero_idx(), 1, -1, 0, 0 });
119 builder.create_add_gate({ x2_idx, x2_copy_idx, builder.zero_idx(), 1, -1, 0, 0 });
120
121 TestFixture::set_default_pairing_points_and_ipa_claim_and_proof(builder);
122 TestFixture::prove_and_verify(builder, /*expected_result=*/true);
123}
124
136TYPED_TEST(PermutationTests, BadTagPermutation)
137{
138 // With tags: multisets {x, y} vs {y, x+1} are NOT equal, so verification should FAIL
139 {
141
143 fr y = -x;
144
145 // multiset: {x, y}
146 auto x1_idx = builder.add_variable(x);
147 auto y1_idx = builder.add_variable(y);
148
149 // multiset: {y, x+1}
150 auto y2_idx = builder.add_variable(y);
151 auto x_plus_1_idx = builder.add_variable(x + 1);
152
153 // Dummy gates: x + y = 0, y + (x+1) = 1
154 builder.create_add_gate({ x1_idx, y1_idx, builder.zero_idx(), 1, 1, 0, 0 });
155 builder.create_add_gate({ y2_idx, x_plus_1_idx, builder.zero_idx(), 1, 1, 0, -1 });
156
157 auto first_tag = builder.get_new_tag();
158 auto second_tag = builder.get_new_tag();
159 builder.set_tau_transposition(first_tag, second_tag);
160
161 builder.assign_tag(x1_idx, first_tag);
162 builder.assign_tag(y1_idx, first_tag);
163 builder.assign_tag(y2_idx, second_tag);
164 builder.assign_tag(x_plus_1_idx, second_tag);
165
166 TestFixture::set_default_pairing_points_and_ipa_claim_and_proof(builder);
167 TestFixture::prove_and_verify(builder, /*expected_result=*/false);
168 }
169 // Without tags: same circuit passes, confirming failure above is due to tag mismatch
170 {
172
174 fr y = -x;
175
176 auto x1_idx = builder.add_variable(x);
177 auto y1_idx = builder.add_variable(y);
178 auto y2_idx = builder.add_variable(y);
179 auto x_plus_1_idx = builder.add_variable(x + 1);
180
181 builder.create_add_gate({ x1_idx, y1_idx, builder.zero_idx(), 1, 1, 0, 0 });
182 builder.create_add_gate({ y2_idx, x_plus_1_idx, builder.zero_idx(), 1, 1, 0, -1 });
183
184 TestFixture::set_default_pairing_points_and_ipa_claim_and_proof(builder);
185 TestFixture::prove_and_verify(builder, /*expected_result=*/true);
186 }
187}
188
206TYPED_TEST(PermutationNonZKTests, ZPermZeroedOutFailure)
207{
208 using Flavor = TypeParam;
209 using Builder = typename Flavor::CircuitBuilder;
210
213
214 using Prover = TestFixture::Prover;
215
217
218 auto a = fr::random_element();
219 auto b = fr::random_element();
220 auto c = a + b;
221
222 uint32_t a_idx = builder.add_variable(a);
223 uint32_t a_copy_idx = builder.add_variable(a);
224 uint32_t b_idx = builder.add_variable(b);
225 uint32_t c_idx = builder.add_variable(c);
226
227 builder.create_add_gate({ a_idx, b_idx, c_idx, 1, 1, -1, 0 });
228 builder.create_add_gate({ a_copy_idx, b_idx, c_idx, 1, 1, -1, 0 });
229 builder.assert_equal(a_copy_idx, a_idx);
230
231 TestFixture::set_default_pairing_points_and_ipa_claim_and_proof(builder);
232
233 auto prover_instance = std::make_shared<ProverInstance>(builder);
234 auto verification_key = std::make_shared<VerificationKey>(prover_instance->get_precomputed());
235
236 Prover prover(prover_instance, verification_key);
237 auto proof = prover.construct_proof();
238 auto& z_perm = prover_instance->polynomials.z_perm;
239
240 // First verify that the Permutation relation holds.
241 auto permutation_relation_failures = RelationChecker<Flavor>::template check<UltraPermutationRelation<fr>>(
242 prover_instance->polynomials, prover_instance->relation_parameters, "UltraPermutation - Before Tampering");
243 EXPECT_TRUE(permutation_relation_failures.empty());
244
245 // Tamper: zero-out z_perm
246 for (size_t i = z_perm.start_index(); i < z_perm.end_index(); ++i) {
247 z_perm.at(i) = fr(0);
248 }
249 prover_instance->polynomials.set_shifted();
250 auto tampered_permutation_relation_failures = RelationChecker<Flavor>::template check<UltraPermutationRelation<fr>>(
251 prover_instance->polynomials,
252 prover_instance->relation_parameters,
253 "UltraPermutation - After zeroing out z_perm");
254 // Verify that the Permutation relation now fails
255 EXPECT_FALSE(tampered_permutation_relation_failures.empty());
256}
257
272TYPED_TEST(PermutationNonZKTests, ZPermShiftNotZeroAtLagrangeLastFailure)
273{
274 using Flavor = TypeParam;
275 using Builder = typename Flavor::CircuitBuilder;
276
279
280 using Prover = TestFixture::Prover;
281
283
284 auto a = fr::random_element();
285 auto b = fr::random_element();
286 auto c = a + b;
287
288 uint32_t a_idx = builder.add_variable(a);
289 uint32_t a_copy_idx = builder.add_variable(a);
290 uint32_t b_idx = builder.add_variable(b);
291 uint32_t c_idx = builder.add_variable(c);
292
293 builder.create_add_gate({ a_idx, b_idx, c_idx, 1, 1, -1, 0 });
294 builder.create_add_gate({ a_copy_idx, b_idx, c_idx, 1, 1, -1, 0 });
295 builder.assert_equal(a_copy_idx, a_idx);
296
297 TestFixture::set_default_pairing_points_and_ipa_claim_and_proof(builder);
298
299 auto prover_instance = std::make_shared<ProverInstance>(builder);
300 auto verification_key = std::make_shared<VerificationKey>(prover_instance->get_precomputed());
301
302 Prover prover(prover_instance, verification_key);
303 auto proof = prover.construct_proof();
304
305 // first verify that the Permutation relation holds.
306 auto permutation_relation_failures = RelationChecker<Flavor>::template check<UltraPermutationRelation<fr>>(
307 prover_instance->polynomials, prover_instance->relation_parameters, "UltraPermutation - Before Tampering");
308 EXPECT_TRUE(permutation_relation_failures.empty());
309 // we make z_perm and z_perm_shift full polynomials to tamper with values that are outside the usual allocated
310 // range. This allows us to failure test for the subrelation `z_perm_shift * lagrange_last == 0`.
311 auto& z_perm = prover_instance->polynomials.z_perm;
312 auto last_valid_index = z_perm.end_index();
313 auto& z_perm_shift = prover_instance->polynomials.z_perm_shift;
314 // make the polynomial full to tamper with a last value.
315 prover_instance->polynomials.z_perm = z_perm.full();
316 prover_instance->polynomials.z_perm_shift = z_perm_shift.full();
317
318 ASSERT_EQ(prover_instance->polynomials.lagrange_last.at(last_valid_index - 1), fr(1));
319 ASSERT_EQ(prover_instance->polynomials.z_perm.at(last_valid_index), fr(0));
320 ASSERT_EQ(prover_instance->polynomials.z_perm_shift.at(last_valid_index - 1), fr(0));
321 // Tamper: change `z_perm_shift` to something non-zero when `lagrange_last == 1`.
322 prover_instance->polynomials.z_perm_shift.at(last_valid_index - 1) += fr(1);
323 // Note that `z_perm_shift` and `z_perm` are no longer inextricably linked because we have replaced them by their
324 // full incarnations. Therefore, we still `z_perm.at(last_valid_index) == 0`. This does not effect the test we
325 // wish to check.
326
327 // Verify that the Permutation relation now fails.
328 auto tampered_permutation_relation_failures = RelationChecker<Flavor>::template check<UltraPermutationRelation<fr>>(
329 prover_instance->polynomials,
330 prover_instance->relation_parameters,
331 "UltraPermutation - After incrementing z_perm_shift where lagrange_last is 1");
332 EXPECT_FALSE(tampered_permutation_relation_failures.empty());
333 // the first subrelation first fails at `row_idx == last_valid_index - 1`.
334 ASSERT_EQ(tampered_permutation_relation_failures[1], last_valid_index - 1);
335}
336
348TYPED_TEST(PermutationNonZKTests, SigmaCorruptionFailure)
349{
350 using Flavor = TypeParam;
353 using Prover = typename TestFixture::Prover;
354
356
357 // Create variables with a copy constraint
358 auto a = fr::random_element();
359 auto b = fr::random_element();
360 auto c = a + b;
361
362 uint32_t a_idx = builder.add_variable(a);
363 uint32_t a_copy_idx = builder.add_variable(a);
364 uint32_t b_idx = builder.add_variable(b);
365 uint32_t c_idx = builder.add_variable(c);
366
367 // Gates using a_idx and a_copy_idx (which should be equal)
368 builder.create_add_gate({ a_idx, b_idx, c_idx, 1, 1, -1, 0 });
369 builder.create_add_gate({ a_copy_idx, b_idx, c_idx, 1, 1, -1, 0 });
370 // copy cycle we'll break
371 builder.assert_equal(a_copy_idx, a_idx);
372
373 TestFixture::set_default_pairing_points_and_ipa_claim_and_proof(builder);
374
375 auto prover_instance = std::make_shared<ProverInstance>(builder);
376 auto verification_key = std::make_shared<VerificationKey>(prover_instance->get_precomputed());
377
378 // Construct proof to compute z_perm
379 Prover prover(prover_instance, verification_key);
380 auto proof = prover.construct_proof();
381
382 // The Permutation relation holds before tampering
383 auto permutation_relation_failures = RelationChecker<Flavor>::template check<UltraPermutationRelation<fr>>(
384 prover_instance->polynomials, prover_instance->relation_parameters, "Permutation Relation - Before Tampering");
385 ASSERT_TRUE(permutation_relation_failures.empty());
386
387 // TAMPER
388
389 // Corrupt sigma_1 at a row that's part of the copy cycle.
390 auto& sigma_1 = prover_instance->polynomials.sigma_1;
391 auto& id_1 = prover_instance->polynomials.id_1;
392
393 // Find the first row that's part of a non-trivial cycle (sigma != id)
394 size_t row_to_corrupt = 0;
395 for (size_t row = 1; row < sigma_1.size(); ++row) {
396 if (sigma_1.at(row) != id_1.at(row)) {
397 row_to_corrupt = row;
398 vinfo("Found copy cycle at row ", row, "; will corrupt this one");
399 break;
400 }
401 }
402 ASSERT_NE(row_to_corrupt, 0) << "No copy cycle found in sigma_1!";
403
404 fr original_value = sigma_1.at(row_to_corrupt);
405 sigma_1.at(row_to_corrupt) = original_value + fr(1); // Break the cycle by pointing elsewhere
406 // We perform two distinct tests.
407 //
408 // Failure test 1: make sure that the subrelation fails when when we change sigma_1 without updating z_perm.
409 {
410 auto failures_of_tampered_instance = RelationChecker<Flavor>::template check<UltraPermutationRelation<fr>>(
411 prover_instance->polynomials,
412 prover_instance->relation_parameters,
413 "Permutation Relation - After corrupting sigma_1");
414
415 ASSERT_TRUE(failures_of_tampered_instance.at(0));
416 }
417 // Failure test 2: make sure the subrelation fails when we DO update z_perm
418 {
419 // Sanity check: if we are recompute z_perm, the first different value will be at `row_to_corrupt + 1`. Store
420 // this to ensure that this value indeed changes.
421 auto& z_perm = prover_instance->polynomials.z_perm;
422 auto z_perm_before = z_perm.at(row_to_corrupt + 1);
423
424 // Recompute z_perm with the corrupted sigma.
425 size_t real_circuit_size = prover_instance->get_final_active_wire_idx() + 1;
426 compute_grand_product<Flavor, UltraPermutationRelation<fr>>(
427 prover_instance->polynomials, prover_instance->relation_parameters, real_circuit_size);
428 prover_instance->polynomials.set_shifted(); // Refresh z_perm_shift
429
430 // Verify z_perm actually changed after recomputation with corrupted sigma
431 auto z_perm_after = z_perm.at(row_to_corrupt + 1);
432 ASSERT_NE(z_perm_before, z_perm_after) << "z_perm should change after recomputing with corrupted sigma";
433
434 // After recomputing z_perm, we expect a failure at the very last active wire.
435 auto failures_of_tampered_instance = RelationChecker<Flavor>::template check<UltraPermutationRelation<fr>>(
436 prover_instance->polynomials,
437 prover_instance->relation_parameters,
438 "Permutation Relation - After corrupting sigma_1 and recomputing z_perm");
439
440 ASSERT_EQ(failures_of_tampered_instance.at(0), real_circuit_size - 1)
441 << "Expected failure at row " << (real_circuit_size - 1) << " (the recomputation boundary)";
442 }
443}
444
455TYPED_TEST(PermutationNonZKTests, PublicInputDeltaMismatch)
456{
457 using Flavor = TypeParam;
460 using Prover = typename TestFixture::Prover;
461
463
464 // Add a public input
465 fr public_value = fr(314159);
466 auto pub_var = builder.add_public_variable(public_value);
467
468 // Use the public input in a simple constraint so it appears in the trace
469 auto private_val = fr::random_element();
470 auto private_var = builder.add_variable(private_val);
471 auto result_var = builder.add_variable(public_value + private_val);
472 builder.create_add_gate({ pub_var, private_var, result_var, 1, 1, -1, 0 });
473
474 TestFixture::set_default_pairing_points_and_ipa_claim_and_proof(builder);
475
476 auto prover_instance = std::make_shared<ProverInstance>(builder);
477 auto verification_key = std::make_shared<VerificationKey>(prover_instance->get_precomputed());
478
479 // Construct proof to compute z_perm (with correct public_input_delta)
480 Prover prover(prover_instance, verification_key);
481 auto proof = prover.construct_proof();
482
483 // Verify the permutation relation holds before tampering
484 auto permutation_relation_failures = RelationChecker<Flavor>::template check<UltraPermutationRelation<fr>>(
485 prover_instance->polynomials, prover_instance->relation_parameters, "Permutation Relation - Before Tampering");
486 ASSERT_TRUE(permutation_relation_failures.empty());
487
488 // Store the original public_input_delta
489 fr original_delta = prover_instance->relation_parameters.public_input_delta;
490
491 // TAMPER
492
493 // Recompute public_input_delta with a different public input value
494 // The wire polynomials still contain the original value (314159), but we compute delta as if it were 99999
495 fr tampered_public_val = fr(99999);
496 std::vector<fr> tampered_public_inputs = { tampered_public_val };
497 fr tampered_delta = compute_public_input_delta<Flavor>(tampered_public_inputs,
498 prover_instance->relation_parameters.beta,
499 prover_instance->relation_parameters.gamma,
500 prover_instance->pub_inputs_offset());
501
502 // Sanity check: the tampered delta should differ from the original
503 ASSERT_NE(original_delta, tampered_delta) << "Tampered delta should differ from original";
504
505 // Apply the tampered delta
506 prover_instance->relation_parameters.public_input_delta = tampered_delta;
507
508 // Verify the permutation relation now fails
509 auto failures_of_tampered_instance = RelationChecker<Flavor>::template check<UltraPermutationRelation<fr>>(
510 prover_instance->polynomials,
511 prover_instance->relation_parameters,
512 "Permutation Relation - After tampering with public_input_delta");
513 // The failure should be in subrelation 0 (the recurrence relation) since z_perm was computed with
514 // a different public_input_delta than what we're now using in the relation check
515 ASSERT_TRUE(failures_of_tampered_instance.contains(0)) << "Expected subrelation 0 to fail";
516 size_t final_active_wire_idx = prover_instance->get_final_active_wire_idx();
517 ASSERT_TRUE(failures_of_tampered_instance.at(0) == final_active_wire_idx);
518}
ECCVMCircuitBuilder CircuitBuilder
FixedVKAndHash_< PrecomputedEntities< Commitment >, BF, ECCVMHardcodedVKAndHash > VerificationKey
The verification key stores commitments to the precomputed polynomials used by the verifier.
Base Native verification key class.
Definition flavor.hpp:135
Contains all the information required by a Honk prover to create a proof, constructed from a finalize...
A debugging utility for checking whether a set of polynomials satisfies the relations for a given Fla...
Child class of UltraFlavor that runs with ZK Sumcheck.
#define vinfo(...)
Definition log.hpp:94
AluTraceBuilder builder
Definition alu.test.cpp:124
FF a
FF b
testing::Types< UltraFlavor, UltraKeccakFlavor, MegaFlavor > FlavorTypes
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
TYPED_TEST_SUITE(CommitmentKeyTest, Curves)
field< Bn254FrParams > fr
Definition fr.hpp:155
TYPED_TEST(CommitmentKeyTest, CommitToZeroPoly)
UltraCircuitBuilder_< UltraExecutionTraceBlocks > UltraCircuitBuilder
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
testing::Types< UltraFlavor, UltraKeccakFlavor > NonZKFlavorTypes
static field random_element(numeric::RNG *engine=nullptr) noexcept