Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
translator_recursive_verifier.test.cpp
Go to the documentation of this file.
12#include <gtest/gtest.h>
13namespace bb {
14
21class TranslatorRecursiveTests : public ::testing::Test {
22 public:
32
34
40
42
44
46
47 // Helper function to add no-ops
48 static void add_random_ops(std::shared_ptr<bb::ECCOpQueue>& op_queue, size_t count)
49 {
50 for (size_t i = 0; i < count; i++) {
51 op_queue->random_op_ultra_only();
52 }
53 }
54
55 // Helper function to create an MSM
56 static void add_mixed_ops(std::shared_ptr<bb::ECCOpQueue>& op_queue, size_t count = 100)
57 {
58 auto P1 = InnerG1::random_element();
59 auto P2 = InnerG1::random_element();
60 auto z = InnerFF::random_element();
61 for (size_t i = 0; i < count; i++) {
62 op_queue->add_accumulate(P1);
63 op_queue->mul_accumulate(P2, z);
64 }
65 op_queue->eq_and_reset();
66 }
67
68 // Construct a test circuit based on some random operations
69 static InnerBuilder generate_test_circuit(const InnerBF& batching_challenge_v,
70 const InnerBF& evaluation_challenge_x,
71 const size_t circuit_size_parameter = 500)
72 {
73
74 // Add the same operations to the ECC op queue; the native computation is performed under the hood.
75 auto op_queue = std::make_shared<bb::ECCOpQueue>();
76 // Construct zk columns
77 op_queue->construct_zk_columns();
78 add_mixed_ops(op_queue, circuit_size_parameter / 2);
80 op_queue->merge_fixed_append(op_queue->get_append_offset());
81
82 return InnerBuilder{ batching_challenge_v, evaluation_challenge_x, op_queue };
83 }
84
85 // Helper to create native op queue commitments from proving key
86 static std::array<InnerFlavor::Commitment, InnerFlavor::NUM_OP_QUEUE_WIRES> create_native_op_queue_commitments(
88 {
89 std::array<InnerFlavor::Commitment, InnerFlavor::NUM_OP_QUEUE_WIRES> op_queue_commitments;
90 op_queue_commitments[0] =
91 proving_key->proving_key->commitment_key.commit(proving_key->proving_key->polynomials.op);
92 op_queue_commitments[1] =
93 proving_key->proving_key->commitment_key.commit(proving_key->proving_key->polynomials.x_lo_y_hi);
94 op_queue_commitments[2] =
95 proving_key->proving_key->commitment_key.commit(proving_key->proving_key->polynomials.x_hi_z_1);
96 op_queue_commitments[3] =
97 proving_key->proving_key->commitment_key.commit(proving_key->proving_key->polynomials.y_lo_z_2);
98 return op_queue_commitments;
99 }
100
101 // Helper to convert native op queue commitments to stdlib commitments
102 static std::array<RecursiveFlavor::Commitment, InnerFlavor::NUM_OP_QUEUE_WIRES> create_stdlib_op_queue_commitments(
103 OuterBuilder* builder, const std::array<InnerFlavor::Commitment, InnerFlavor::NUM_OP_QUEUE_WIRES>& native_comms)
104 {
105 std::array<RecursiveFlavor::Commitment, InnerFlavor::NUM_OP_QUEUE_WIRES> stdlib_comms;
106 for (size_t i = 0; i < InnerFlavor::NUM_OP_QUEUE_WIRES; i++) {
107 stdlib_comms[i] = RecursiveFlavor::Commitment::from_witness(builder, native_comms[i]);
108 // Set empty origin tags for commitments (they're free witnesses from merge protocol)
109 stdlib_comms[i].set_origin_tag(OriginTag::constant());
110 }
111 return stdlib_comms;
112 }
113
114 // Helper struct to hold translator verification inputs as stdlib witnesses
119 std::array<RecursiveFlavor::Commitment, InnerFlavor::NUM_OP_QUEUE_WIRES> op_queue_commitments;
120 // Native values for native verifier
122 std::array<InnerFlavor::Commitment, InnerFlavor::NUM_OP_QUEUE_WIRES> native_op_queue_commitments;
123 };
124
125 // Helper to create recursive verifier inputs from native values
127 const InnerProver& prover,
128 const InnerBF& evaluation_challenge_x,
129 const InnerBF& batching_challenge_v)
130 {
131 // Get accumulated_result from the prover
132 bb::fq accumulated_result_native = prover.get_accumulated_result();
133 auto accumulated_result = TranslatorBF::from_witness(builder, accumulated_result_native);
134 accumulated_result.set_origin_tag(OriginTag::constant());
135
136 // Convert challenges to circuit witnesses
137 auto stdlib_evaluation_challenge_x = TranslatorBF::from_witness(builder, evaluation_challenge_x);
138 auto stdlib_batching_challenge_v = TranslatorBF::from_witness(builder, batching_challenge_v);
139 stdlib_evaluation_challenge_x.set_origin_tag(OriginTag::constant());
140 stdlib_batching_challenge_v.set_origin_tag(OriginTag::constant());
141
142 // Create op queue commitments (normally provided by merge protocol)
143 auto native_op_queue_commitments = create_native_op_queue_commitments(prover.key);
144 auto op_queue_commitments = create_stdlib_op_queue_commitments(builder, native_op_queue_commitments);
145
146 return { accumulated_result, stdlib_evaluation_challenge_x, stdlib_batching_challenge_v,
147 op_queue_commitments, accumulated_result_native, native_op_queue_commitments };
148 }
149
150 // Shared helper to create and verify a translator proof recursively
151 // Includes native verification and consistency checks
153 size_t circuit_size_parameter = 500)
154 {
155
156 // Create fake ECCVM proof
157 auto prover_transcript = std::make_shared<Transcript>();
158
159 // Generate challenges
160 InnerBF batching_challenge_v = InnerBF::random_element();
161 InnerBF evaluation_challenge_x = InnerBF::random_element();
162
163 // Create inner translator circuit and generate proof
164 InnerBuilder circuit_builder =
165 generate_test_circuit(batching_challenge_v, evaluation_challenge_x, circuit_size_parameter);
166 auto proving_key = std::make_shared<TranslatorProvingKey>(circuit_builder);
167 InnerProver prover{ proving_key, prover_transcript };
168 auto proof = prover.construct_proof();
169
170 // Set up outer recursive circuit
171 OuterBuilder outer_circuit;
172 stdlib::Proof<OuterBuilder> stdlib_proof(outer_circuit, proof);
173 auto transcript = std::make_shared<RecursiveFlavor::Transcript>(stdlib_proof);
174
175 // Create recursive verifier inputs
176 auto recursive_inputs =
177 create_recursive_verifier_inputs(&outer_circuit, prover, evaluation_challenge_x, batching_challenge_v);
178
179 // Verify proof recursively
180 stdlib::Proof<OuterBuilder> stdlib_proof_for_verifier(outer_circuit, proof);
181 RecursiveVerifier verifier{ transcript,
182 stdlib_proof_for_verifier,
183 recursive_inputs.evaluation_challenge_x,
184 recursive_inputs.batching_challenge_v,
185 recursive_inputs.accumulated_result,
186 recursive_inputs.op_queue_commitments };
187 auto recursive_result = verifier.reduce_to_pairing_check();
188
190 inputs.pairing_inputs = recursive_result.pairing_points;
192
193 // Verify with native verifier and compare results
194 auto native_verifier_transcript = std::make_shared<Transcript>(proof);
195 InnerVerifier native_verifier(native_verifier_transcript,
196 proof,
197 evaluation_challenge_x,
198 batching_challenge_v,
199 recursive_inputs.accumulated_result_native,
200 recursive_inputs.native_op_queue_commitments);
201 auto native_result = native_verifier.reduce_to_pairing_check();
202 bool native_verified = native_result.pairing_points.check() && native_result.reduction_succeeded;
203
204 auto recursive_verified = recursive_result.pairing_points.check();
205 EXPECT_EQ(recursive_verified, native_verified);
206
207 // Verify VK commitments consistency between recursive and native verifiers
208 auto recursive_vk = verifier.get_verification_key();
209 auto native_vk = native_verifier.get_verification_key();
210 for (auto [vk_poly, native_vk_poly] : zip_view(recursive_vk->get_all(), native_vk->get_all())) {
211 EXPECT_EQ(vk_poly.get_value(), native_vk_poly);
212 }
213
214 auto outer_proving_key = std::make_shared<OuterProverInstance>(outer_circuit);
215 auto outer_verification_key =
216 std::make_shared<typename OuterFlavor::VerificationKey>(outer_proving_key->get_precomputed());
217
218 return { std::move(outer_circuit), outer_verification_key };
219 }
220
222 {
223 // Use the shared helper to create and verify the recursive circuit
224 auto [outer_circuit, outer_verification_key] = create_recursive_verifier_circuit();
225
226 info("Recursive Verifier: num gates = ", outer_circuit.get_num_finalized_gates_inefficient());
227 EXPECT_EQ(outer_circuit.failed(), false) << outer_circuit.err();
228
229 // Prove and verify the outer recursive circuit
230 auto prover_instance = std::make_shared<OuterProverInstance>(outer_circuit);
231 auto vk_and_hash = std::make_shared<OuterFlavor::VKAndHash>(outer_verification_key);
232 OuterProver prover(prover_instance, outer_verification_key);
233 OuterVerifier verifier(vk_and_hash);
234 auto proof = prover.construct_proof();
235 bool verified = verifier.verify_proof(proof).result;
236
237 ASSERT_TRUE(verified);
238 }
239
246 {
247 InnerBF batching_challenge_v = InnerBF::random_element();
248 InnerBF evaluation_challenge_x = InnerBF::random_element();
249
250 InnerBuilder circuit_builder = generate_test_circuit(batching_challenge_v, evaluation_challenge_x);
251 auto prover_transcript = std::make_shared<Transcript>();
252 auto proving_key = std::make_shared<TranslatorProvingKey>(circuit_builder);
253 InnerProver prover{ proving_key, prover_transcript };
254 auto proof = prover.construct_proof();
255
256 ASSERT_EQ(proof.size(), InnerFlavor::PROOF_LENGTH);
257
258 StructuredProof<InnerFlavor> structured_proof;
259 auto proof_data = prover.transcript->test_get_proof_data();
260 structured_proof.deserialize(proof_data, /*num_public_inputs=*/0, InnerFlavor::CONST_TRANSLATOR_LOG_N);
261 structured_proof.serialize(proof_data, InnerFlavor::CONST_TRANSLATOR_LOG_N);
262
263 auto original_data = prover.transcript->test_get_proof_data();
264 ASSERT_EQ(proof_data.size(), original_data.size());
265 EXPECT_EQ(proof_data, original_data);
266 }
267
268 enum class TamperType {
269 MODIFY_SUMCHECK_UNIVARIATE, // Tests sumcheck round consistency constraint (circuit FAIL)
270 MODIFY_SUMCHECK_EVAL, // Tests final relation check constraint (circuit FAIL)
271 MODIFY_KZG_WITNESS, // Tests pairing check (circuit PASS, pairing FAIL)
272 MODIFY_LIBRA_EVAL, // Tests Libra consistency constraint (circuit FAIL)
273 END
274 };
275
277 typename InnerFlavor::Transcript::Proof& proof,
278 TamperType tamper_type)
279 {
280 using FF = InnerFF;
281 static constexpr size_t LOG_N = InnerFlavor::CONST_TRANSLATOR_LOG_N;
282
283 StructuredProof<InnerFlavor> structured_proof;
284 structured_proof.deserialize(prover.transcript->test_get_proof_data(), /*num_public_inputs=*/0, LOG_N);
285
286 switch (tamper_type) {
288 FF delta = FF::random_element();
289 structured_proof.sumcheck_univariates[0].value_at(0) += delta;
290 structured_proof.sumcheck_univariates[0].value_at(1) -= delta;
291 break;
292 }
294 structured_proof.full_circuit_evaluations[0] = FF::random_element();
295 break;
297 structured_proof.kzg_w_comm = structured_proof.kzg_w_comm * FF::random_element();
298 break;
300 structured_proof.libra_quotient_eval = FF::random_element();
301 break;
302 case TamperType::END:
303 break;
304 }
305
306 structured_proof.serialize(prover.transcript->test_get_proof_data(), LOG_N);
307 prover.transcript->test_set_proof_parsing_state(0, InnerFlavor::PROOF_LENGTH);
308 proof = prover.export_proof();
309 }
310
312 {
313 for (size_t idx = 0; idx < static_cast<size_t>(TamperType::END); idx++) {
314 TamperType tamper_type = static_cast<TamperType>(idx);
315
316 // Generate challenges
317 InnerBF batching_challenge_v = InnerBF::random_element();
318 InnerBF evaluation_challenge_x = InnerBF::random_element();
319
320 // Create inner translator circuit and generate proof
321 InnerBuilder circuit_builder = generate_test_circuit(batching_challenge_v, evaluation_challenge_x);
322 auto proving_key = std::make_shared<TranslatorProvingKey>(circuit_builder);
323 auto prover_transcript = std::make_shared<Transcript>();
324 InnerProver prover{ proving_key, prover_transcript };
325 auto proof = prover.construct_proof();
326
327 // Tamper with the proof
328 tamper_translator_proof(prover, proof, tamper_type);
329
330 // Set up outer recursive circuit
331 OuterBuilder outer_circuit;
332
333 // Create recursive verifier inputs (unaffected by proof tampering)
334 auto recursive_inputs =
335 create_recursive_verifier_inputs(&outer_circuit, prover, evaluation_challenge_x, batching_challenge_v);
336
337 // Create recursive verifier and verify tampered proof
338 stdlib::Proof<OuterBuilder> stdlib_proof(outer_circuit, proof);
339 auto transcript = std::make_shared<RecursiveFlavor::Transcript>(stdlib_proof);
340 stdlib::Proof<OuterBuilder> stdlib_proof_for_verifier(outer_circuit, proof);
341 RecursiveVerifier verifier{ transcript,
342 stdlib_proof_for_verifier,
343 recursive_inputs.evaluation_challenge_x,
344 recursive_inputs.batching_challenge_v,
345 recursive_inputs.accumulated_result,
346 recursive_inputs.op_queue_commitments };
347 auto recursive_result = verifier.reduce_to_pairing_check();
348
349 if (tamper_type == TamperType::MODIFY_KZG_WITNESS) {
350 // KZG witness tampering bypasses circuit constraints but causes pairing failure
351 EXPECT_TRUE(CircuitChecker::check(outer_circuit));
352 EXPECT_FALSE(recursive_result.pairing_points.check());
353 } else {
354 // All other tamper types should cause a circuit constraint violation
355 EXPECT_FALSE(CircuitChecker::check(outer_circuit));
356 }
357 }
358 }
359
361 {
362 auto [outer_circuit_256, verification_key_256] = create_recursive_verifier_circuit(256);
363 auto [outer_circuit_512, verification_key_512] = create_recursive_verifier_circuit(512);
364
365 compare_ultra_blocks_and_verification_keys<OuterFlavor>({ outer_circuit_256.blocks, outer_circuit_512.blocks },
366 { verification_key_256, verification_key_512 });
367 };
368
375 {
376 auto prover_transcript = std::make_shared<Transcript>();
377
378 InnerBF batching_challenge_v = InnerBF::random_element();
379 InnerBF evaluation_challenge_x = InnerBF::random_element();
380
381 InnerBuilder circuit_builder = generate_test_circuit(batching_challenge_v, evaluation_challenge_x);
382 auto proving_key = std::make_shared<TranslatorProvingKey>(circuit_builder);
383 InnerProver prover{ proving_key, prover_transcript };
384 auto proof = prover.construct_proof();
385
386 // Set up outer recursive circuit
387 OuterBuilder outer_circuit;
388 stdlib::Proof<OuterBuilder> stdlib_proof(outer_circuit, proof);
389 auto transcript = std::make_shared<RecursiveFlavor::Transcript>(stdlib_proof);
390
391 // Create recursive verifier inputs
392 auto recursive_inputs =
393 create_recursive_verifier_inputs(&outer_circuit, prover, evaluation_challenge_x, batching_challenge_v);
394
395 // Verify proof recursively
396 stdlib::Proof<OuterBuilder> stdlib_proof_for_verifier(outer_circuit, proof);
397 RecursiveVerifier verifier{ transcript,
398 stdlib_proof_for_verifier,
399 recursive_inputs.evaluation_challenge_x,
400 recursive_inputs.batching_challenge_v,
401 recursive_inputs.accumulated_result,
402 recursive_inputs.op_queue_commitments };
403 auto recursive_result = verifier.reduce_to_pairing_check();
404
405 recursive_result.pairing_points.fix_witness();
406
408 inputs.pairing_inputs = recursive_result.pairing_points;
410
411 EXPECT_EQ(outer_circuit.failed(), false) << outer_circuit.err();
412 outer_circuit.finalize_circuit();
413
414 // Run static analysis - no variable should appear in only one gate
415 auto graph = cdg::UltraStaticAnalyzer(outer_circuit);
416 auto [cc, variables_in_one_gate] = graph.analyze_circuit(/*filter_cc=*/true);
417
418 EXPECT_EQ(variables_in_one_gate.size(), 0);
419 }
420};
421
426
431
436
441
446} // namespace bb
Common transcript class for both parties. Stores the data for the current round, as well as the manif...
const std::string & err() const
Contains all the information required by a Honk prover to create a proof, constructed from a finalize...
TranslatorCircuitBuilder creates a circuit that evaluates the correctness of the evaluation of EccOpQ...
static bool check(const Builder &circuit)
Check the witness satisifies the circuit.
BaseTranscript< Codec, HashFunction > Transcript
static constexpr size_t CONST_TRANSLATOR_LOG_N
static constexpr size_t NUM_OP_QUEUE_WIRES
static constexpr size_t PROOF_LENGTH
TranslatorCircuitBuilder CircuitBuilder
Curve::ScalarField FF
Curve::AffineElement Commitment
uint256_t get_accumulated_result() const
Extract the accumulated result from the circuit.
std::shared_ptr< TranslatorProvingKey > key
std::shared_ptr< Transcript > transcript
The recursive counterpart of the native Translator flavor.
Test suite for standalone recursive verification of translation proofs.
static void add_mixed_ops(std::shared_ptr< bb::ECCOpQueue > &op_queue, size_t count=100)
static InnerBuilder generate_test_circuit(const InnerBF &batching_challenge_v, const InnerBF &evaluation_challenge_x, const size_t circuit_size_parameter=500)
std::conditional_t< IsMegaBuilder< OuterBuilder >, MegaFlavor, UltraFlavor > OuterFlavor
static RecursiveVerifierInputs create_recursive_verifier_inputs(OuterBuilder *builder, const InnerProver &prover, const InnerBF &evaluation_challenge_x, const InnerBF &batching_challenge_v)
static void test_static_analysis()
Static analysis (boomerang detection) of the translator recursive verifier circuit.
static void add_random_ops(std::shared_ptr< bb::ECCOpQueue > &op_queue, size_t count)
static void test_structured_proof_round_trip()
Verify that StructuredProof<TranslatorFlavor> can round-trip serialize/deserialize a proof.
static std::array< RecursiveFlavor::Commitment, InnerFlavor::NUM_OP_QUEUE_WIRES > create_stdlib_op_queue_commitments(OuterBuilder *builder, const std::array< InnerFlavor::Commitment, InnerFlavor::NUM_OP_QUEUE_WIRES > &native_comms)
static std::tuple< OuterBuilder, std::shared_ptr< OuterFlavor::VerificationKey > > create_recursive_verifier_circuit(size_t circuit_size_parameter=500)
static std::array< InnerFlavor::Commitment, InnerFlavor::NUM_OP_QUEUE_WIRES > create_native_op_queue_commitments(const std::shared_ptr< TranslatorProvingKey > &proving_key)
static void tamper_translator_proof(InnerProver &prover, typename InnerFlavor::Transcript::Proof &proof, TamperType tamper_type)
Translator verifier class that verifies the proof of the Translator circuit.
ReductionResult reduce_to_pairing_check()
Reduce the translator proof to a pairing check.
std::shared_ptr< VerificationKey > get_verification_key() const
Get the verification key.
Output verify_proof(const Proof &proof)
Perform ultra verification.
A simple wrapper around a vector of stdlib field elements representing a proof.
Definition proof.hpp:19
Manages the data that is propagated on the public inputs of an application/function circuit.
#define info(...)
Definition log.hpp:93
AluTraceBuilder builder
Definition alu.test.cpp:124
AvmProvingInputs inputs
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
TranslatorVerifier_< TranslatorRecursiveFlavor > TranslatorRecursiveVerifier
TEST_F(IPATest, ChallengesAreZero)
Definition ipa.test.cpp:155
TranslatorVerifier_< TranslatorFlavor > TranslatorVerifier
StaticAnalyzer_< bb::fr, bb::UltraCircuitBuilder > UltraStaticAnalyzer
Definition graph.hpp:188
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
This file contains part of the logic for the Origin Tag mechanism that tracks the use of in-circuit p...
static OriginTag constant()
Test utility for deserializing/serializing proof data into typed structures.
std::array< RecursiveFlavor::Commitment, InnerFlavor::NUM_OP_QUEUE_WIRES > op_queue_commitments
std::array< InnerFlavor::Commitment, InnerFlavor::NUM_OP_QUEUE_WIRES > native_op_queue_commitments
static field random_element(numeric::RNG *engine=nullptr) noexcept
uint32_t set_public(Builder *ctx=nullptr)
Set the witness indices for the pairing points to public.