Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
eccvm_relation_corruption.test.cpp
Go to the documentation of this file.
1
13#include <gtest/gtest.h>
14
15using namespace bb;
16
17namespace {
18
19using Flavor = ECCVMFlavor;
20using FF = typename Flavor::FF;
21using G1 = bb::g1;
22using Fr = typename G1::Fr;
23using Polynomial = typename Flavor::Polynomial;
26
28
34std::vector<Polynomial*> get_msm_polynomials(ProverPolynomials& polys)
35{
36 return {
37 // From WireNonShiftedEntities (columns 21-44)
38 &polys.msm_size_of_msm,
39 &polys.msm_add2,
40 &polys.msm_add3,
41 &polys.msm_add4,
42 &polys.msm_x1,
43 &polys.msm_y1,
44 &polys.msm_x2,
45 &polys.msm_y2,
46 &polys.msm_x3,
47 &polys.msm_y3,
48 &polys.msm_x4,
49 &polys.msm_y4,
50 &polys.msm_collision_x1,
51 &polys.msm_collision_x2,
52 &polys.msm_collision_x3,
53 &polys.msm_collision_x4,
54 &polys.msm_lambda1,
55 &polys.msm_lambda2,
56 &polys.msm_lambda3,
57 &polys.msm_lambda4,
58 &polys.msm_slice1,
59 &polys.msm_slice2,
60 &polys.msm_slice3,
61 &polys.msm_slice4,
62 // From WireToBeShiftedWithoutAccumulatorsEntities (columns 68-77)
63 &polys.msm_transition,
64 &polys.msm_add,
65 &polys.msm_double,
66 &polys.msm_skew,
67 &polys.msm_accumulator_x,
68 &polys.msm_accumulator_y,
69 &polys.msm_count,
70 &polys.msm_round,
71 &polys.msm_add1,
72 &polys.msm_pc,
73 };
74}
75
79ProverPolynomials build_valid_eccvm_msm_state()
80{
81 auto generators = G1::derive_generators("test generators", 3);
82 auto a = generators[0];
83 auto b = generators[1];
86
87 auto op_queue = std::make_shared<ECCOpQueue>();
88 op_queue->mul_accumulate(a, x);
89 op_queue->mul_accumulate(b, y);
90 op_queue->eq_and_reset();
91 op_queue->merge();
92 add_hiding_op_for_test(op_queue);
93
94 ECCVMCircuitBuilder builder{ op_queue };
96}
97
102RelationParameters<FF> compute_full_relation_params(ProverPolynomials& polynomials)
103{
104 const FF beta = FF::random_element(&engine);
105 const FF gamma = FF::random_element(&engine);
106 const FF beta_sqr = beta.sqr();
107 const FF beta_cube = beta_sqr * beta;
108 auto eccvm_set_permutation_delta =
109 gamma * (gamma + beta_sqr) * (gamma + beta_sqr + beta_sqr) * (gamma + beta_sqr + beta_sqr + beta_sqr);
110 eccvm_set_permutation_delta = eccvm_set_permutation_delta.invert();
111
113 .eta = 0,
114 .beta = beta,
115 .gamma = gamma,
116 .public_input_delta = 0,
117 .beta_sqr = beta_sqr,
118 .beta_cube = beta_cube,
119 .eccvm_set_permutation_delta = eccvm_set_permutation_delta,
120 };
121
122 const size_t num_rows = polynomials.get_polynomial_size();
123 const size_t unmasked_witness_size = num_rows - NUM_DISABLED_ROWS_IN_SUMCHECK;
124 compute_logderivative_inverse<FF, ECCVMLookupRelation<FF>>(polynomials, params, unmasked_witness_size);
125 compute_grand_product<Flavor, ECCVMSetRelation<FF>>(polynomials, params, unmasked_witness_size);
126 polynomials.z_perm_shift = Polynomial(polynomials.z_perm.shifted());
127
128 return params;
129}
130
131} // anonymous namespace
132
133class ECCVMRelationCorruptionTests : public ::testing::Test {
134 protected:
136};
137
147TEST_F(ECCVMRelationCorruptionTests, MSMAccumulatorCorruptionAtTransitionRowIsHarmless)
148{
149 auto polynomials = build_valid_eccvm_msm_state();
150 RelationParameters<FF> params{};
151
152 auto baseline = RelationChecker<void>::check<ECCVMMSMRelation<FF>>(polynomials, params, "ECCVMMSMRelation");
153 EXPECT_TRUE(baseline.empty()) << "Baseline MSM relation should pass";
154
155 // Confirm row 1 is indeed the transition row
156 ASSERT_EQ(polynomials.msm_add[1], FF(1)) << "Row 1 should be an active MSM add row";
157 ASSERT_EQ(polynomials.msm_transition[1], FF(1)) << "Row 1 should have msm_transition=1";
158
159 // Corrupt the accumulator at the transition row
160 polynomials.msm_accumulator_x.at(1) = FF::random_element(&engine);
161 polynomials.msm_accumulator_y.at(1) = FF::random_element(&engine);
162 polynomials.set_shifted();
163
164 auto failures = RelationChecker<void>::check<ECCVMMSMRelation<FF>>(polynomials, params, "ECCVMMSMRelation");
165 EXPECT_TRUE(failures.empty()) << "MSM relation should STILL PASS — acc is unused when msm_transition=1";
166}
167
178TEST_F(ECCVMRelationCorruptionTests, MSMAccumulatorCorruptionAtInteriorAndNoOpRows)
179{
180 RelationParameters<FF> params{};
181
182 // --- Part 1: corrupt the accumulator at an interior active MSM row (q_add=1, msm_transition=0) ---
183 {
184 auto polynomials = build_valid_eccvm_msm_state();
185
186 auto baseline = RelationChecker<void>::check<ECCVMMSMRelation<FF>>(polynomials, params, "ECCVMMSMRelation");
187 EXPECT_TRUE(baseline.empty()) << "Baseline MSM relation should pass";
188
189 // Find an interior addition row: q_add=1, msm_transition=0
190 const size_t num_rows = polynomials.get_polynomial_size();
191 size_t active_row = 0;
192 for (size_t i = 1; i < num_rows - 1; i++) {
193 if (polynomials.msm_add[i] == FF(1) && polynomials.msm_transition[i] == FF(0)) {
194 active_row = i;
195 break;
196 }
197 }
198 ASSERT_NE(active_row, 0) << "Should find an interior active MSM add row";
199
200 polynomials.msm_accumulator_x.at(active_row) = FF::random_element(&engine);
201 polynomials.msm_accumulator_y.at(active_row) = FF::random_element(&engine);
202 polynomials.set_shifted();
203
204 auto failures = RelationChecker<void>::check<ECCVMMSMRelation<FF>>(polynomials, params, "ECCVMMSMRelation");
205 EXPECT_FALSE(failures.empty()) << "MSM relation should fail after active-row accumulator corruption";
206 }
207
208 // --- Part 2: corrupt the accumulator at a trailing no-op row ---
209 {
210 auto polynomials = build_valid_eccvm_msm_state();
211
212 // Find the first no-op row (all MSM selectors zero, not lagrange_first)
213 const size_t num_rows = polynomials.get_polynomial_size();
214 size_t no_op_row = 0;
215 for (size_t i = 2; i < num_rows - 1; i++) {
216 if (polynomials.msm_add[i] == FF(0) && polynomials.msm_double[i] == FF(0) &&
217 polynomials.msm_skew[i] == FF(0) && polynomials.msm_transition[i] == FF(0) &&
218 polynomials.lagrange_first[i] == FF(0)) {
219 no_op_row = i;
220 break;
221 }
222 }
223 ASSERT_NE(no_op_row, 0) << "Should find a no-op row in the MSM table";
224
225 polynomials.msm_accumulator_x.at(no_op_row) = FF::random_element(&engine);
226 polynomials.msm_accumulator_y.at(no_op_row) = FF::random_element(&engine);
227 polynomials.set_shifted();
228
229 auto failures = RelationChecker<void>::check<ECCVMMSMRelation<FF>>(polynomials, params, "ECCVMMSMRelation");
230 EXPECT_FALSE(failures.empty()) << "MSM relation should fail after no-op accumulator corruption";
231
232 // The failure should be in subrelations 45 or 46 (the no-op accumulator preservation constraints)
233 bool found_noop_subrelation_failure = failures.contains(45) || failures.contains(46);
234 EXPECT_TRUE(found_noop_subrelation_failure)
235 << "Failure should be detected by subrelations 45/46 (no-op accumulator preservation)";
236 }
237}
238
254TEST_F(ECCVMRelationCorruptionTests, MSMRelationFailsOnShiftedMSMTable)
255{
256 auto polynomials = build_valid_eccvm_msm_state();
257 RelationParameters<FF> params{};
258
259 // Baseline: MSM relation passes on clean data
260 auto baseline = RelationChecker<void>::check<ECCVMMSMRelation<FF>>(polynomials, params, "ECCVMMSMRelation");
261 EXPECT_TRUE(baseline.empty()) << "Baseline MSM relation should pass";
262
263 const size_t num_rows = polynomials.get_polynomial_size();
264 auto msm_polys = get_msm_polynomials(polynomials);
265
266 // Shift every MSM column down by 1: p[k] = p[k-1] for k = num_rows-1 down to 2, then p[1] = 0
267 for (auto* poly : msm_polys) {
268 for (size_t k = num_rows - 1; k >= 2; k--) {
269 poly->at(k) = (*poly)[k - 1];
270 }
271 poly->at(1) = FF(0);
272 }
273
274 // Subrelation 27 enforces: is_not_first_row * msm_transition_shift * (msm_size + pc_shift - pc) = 0
275 // After shifting, row 1 has msm_transition_shift = msm_transition[2] = 1 (old row 1's transition),
276 // but pc[1] = 0 and pc[2] = pc_original[1] (nonzero), with msm_size[1] = 0.
277 // Patch msm_size_of_msm[1] so the pc-continuity constraint is satisfied at the injected row.
278 polynomials.msm_size_of_msm.at(1) = polynomials.msm_pc[1] - polynomials.msm_pc[2];
279
280 // Refresh shifted views
281 polynomials.set_shifted();
282
283 auto failures = RelationChecker<void>::check<ECCVMMSMRelation<FF>>(polynomials, params, "ECCVMMSMRelation");
284 EXPECT_FALSE(failures.empty()) << "MSM relation should fail after shifting MSM table by one row";
285
286 // Log all failing subrelations for visibility
287 for (const auto& [subrelation_idx, row_idx] : failures) {
288 info("Shifted MSM table: subrelation ", subrelation_idx, " first failed at row ", row_idx);
289 }
290
291 // Only subrelations 45 and 46 (no-op accumulator preservation) should fail
292 EXPECT_EQ(failures.size(), 2U) << "Exactly two subrelations should fail (45 and 46)";
293 EXPECT_TRUE(failures.contains(45)) << "Subrelation 45 (no-op acc_x preservation) should fail";
294 EXPECT_TRUE(failures.contains(46)) << "Subrelation 46 (no-op acc_y preservation) should fail";
295
296 // Verify that all other ECCVM relations still pass after the shift.
297 // We compute random Fiat-Shamir challenges and derived polynomials (logderivative inverse, grand product)
298 // so we can also check ECCVMSetRelation and ECCVMLookupRelation.
299 auto full_params = compute_full_relation_params(polynomials);
300
301 // Relations that don't touch MSM columns should be completely unaffected.
302 auto transcript_failures =
303 RelationChecker<void>::check<ECCVMTranscriptRelation<FF>>(polynomials, full_params, "ECCVMTranscriptRelation");
304 EXPECT_TRUE(transcript_failures.empty()) << "ECCVMTranscriptRelation should still pass";
305
306 auto point_table_failures =
307 RelationChecker<void>::check<ECCVMPointTableRelation<FF>>(polynomials, full_params, "ECCVMPointTableRelation");
308 EXPECT_TRUE(point_table_failures.empty()) << "ECCVMPointTableRelation should still pass";
309
310 auto wnaf_failures =
311 RelationChecker<void>::check<ECCVMWnafRelation<FF>>(polynomials, full_params, "ECCVMWnafRelation");
312 EXPECT_TRUE(wnaf_failures.empty()) << "ECCVMWnafRelation should still pass";
313
314 auto bools_failures =
315 RelationChecker<void>::check<ECCVMBoolsRelation<FF>>(polynomials, full_params, "ECCVMBoolsRelation");
316 EXPECT_TRUE(bools_failures.empty()) << "ECCVMBoolsRelation should still pass";
317
318 // The Set relation enforces a multiset equality between MSM output tuples (pc, acc_x, acc_y, msm_size)
319 // and the transcript. Shifting the MSM columns corrupts these tuples, so the grand product (computed
320 // post-shift) reflects mismatched reads/writes and the relation correctly fails. It is possible that with more
321 // care, we could make this also pass.
322 auto set_failures =
323 RelationChecker<void>::check<ECCVMSetRelation<FF>>(polynomials, full_params, "ECCVMSetRelation");
324 EXPECT_FALSE(set_failures.empty()) << "ECCVMSetRelation should also fail (MSM output tuples are shifted)";
325
326 // The Lookup relation's logderivative inverse is computed post-shift, so it adapts to the
327 // shifted column values. The per-row subrelation passes, and the sum-over-trace (linearly
328 // dependent) subrelation also vanishes since the inverse was derived from the current data.
329 auto lookup_failures = RelationChecker<void>::check<ECCVMLookupRelation<FF>, /*has_linearly_dependent=*/true>(
330 polynomials, full_params, "ECCVMLookupRelation");
331 EXPECT_TRUE(lookup_failures.empty()) << "ECCVMLookupRelation should still pass (inverse computed post-shift)";
332}
A container for the prover polynomials.
typename Curve::ScalarField FF
bb::Polynomial< FF > Polynomial
Structured polynomial class that represents the coefficients 'a' of a_0 + a_1 x .....
A debugging utility for checking whether a set of polynomials satisfies the relations for a given Fla...
A wrapper for Relations to expose methods used by the Sumcheck prover or verifier to add the contribu...
#define info(...)
Definition log.hpp:93
AluTraceBuilder builder
Definition alu.test.cpp:124
FF a
FF b
typename ECCVMFlavor::ProverPolynomials ProverPolynomials
numeric::RNG & engine
void add_hiding_op_for_test(const std::shared_ptr< ECCOpQueue > &op_queue)
Set a hiding op on the op_queue for testing.
RNG & get_debug_randomness(bool reset, std::uint_fast64_t seed)
Definition engine.cpp:217
std::filesystem::path bb_crs_path()
void init_file_crs_factory(const std::filesystem::path &path)
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
group< fq, fr, Bn254G1Params > g1
Definition g1.hpp:33
TEST_F(IPATest, ChallengesAreZero)
Definition ipa.test.cpp:142
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
Curve::AffineElement G1
Container for parameters used by the grand product (permutation, lookup) Honk relations.
constexpr field invert() const noexcept
static field random_element(numeric::RNG *engine=nullptr) noexcept
BB_INLINE constexpr field sqr() const noexcept