Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
batched_honk_translator.test.cpp
Go to the documentation of this file.
1
16
25
26#include <gtest/gtest.h>
27
28using namespace bb;
29
30class BatchedHonkTranslatorTests : public ::testing::Test {
31 public:
33 using Fq = TranslatorFlavor::BF; // BN254 base field (= Grumpkin scalar field)
36
38
39 // -------------------------------------------------------------------------
40 // Translator helpers (adapted from translator.test.cpp)
41 // -------------------------------------------------------------------------
42
43 static void add_random_ops(std::shared_ptr<ECCOpQueue>& op_queue, size_t count)
44 {
45 for (size_t i = 0; i < count; i++) {
46 op_queue->random_op_ultra_only();
47 }
48 }
49
50 static void add_mixed_ops(std::shared_ptr<ECCOpQueue>& op_queue, size_t count = 100)
51 {
52 auto P1 = G1::random_element();
53 auto P2 = G1::random_element();
54 auto z = FF::random_element();
55 for (size_t i = 0; i < count; i++) {
56 op_queue->add_accumulate(P1);
57 op_queue->mul_accumulate(P2, z);
58 }
59 op_queue->eq_and_reset();
60 }
61
67 const Fq& evaluation_input_x,
68 size_t circuit_size_param = 500)
69 {
70 auto op_queue = std::make_shared<ECCOpQueue>();
71 // Construct zk_columns
72 op_queue->construct_zk_columns();
73 // Table with correct final structure for translator
74 add_mixed_ops(op_queue, circuit_size_param / 2);
76 // Merge with fixed append
77 op_queue->merge_fixed_append(op_queue->get_append_offset());
78
79 TranslatorCircuitBuilder circuit(batching_challenge_v, evaluation_input_x, op_queue);
81 }
82
88 {
89 const size_t RESULT_ROW = TranslatorFlavor::RESULT_ROW;
90 auto& polys = key->proving_key->polynomials;
91 return Fq(uint256_t(polys.accumulators_binary_limbs_0[RESULT_ROW]) +
92 (uint256_t(polys.accumulators_binary_limbs_1[RESULT_ROW]) << 68) +
93 (uint256_t(polys.accumulators_binary_limbs_2[RESULT_ROW]) << 136) +
94 (uint256_t(polys.accumulators_binary_limbs_3[RESULT_ROW]) << 204));
95 }
96
118 static TranscriptManifest build_expected_batched_manifest(const size_t num_mega_zk_pub_inputs)
119 {
120 using MZK = MegaZKFlavor;
121 using Trans = TranslatorFlavor;
122
123 constexpr size_t G = FrCodec::calc_num_fields<MZK::Commitment>(); // 4
124 constexpr size_t Fr = 1;
125 constexpr size_t JOINT_LOG_N = Trans::CONST_TRANSLATOR_LOG_N; // 17
126 constexpr size_t LOG_MINI = Trans::LOG_MINI_CIRCUIT_SIZE; // 13
127
129 size_t round = 0;
130
131 // ── Round 0: MegaZK Oink ──────────────────────────────────────────────────
132 m.add_entry(round, "vk_hash", Fr);
133 for (size_t i = 0; i < num_mega_zk_pub_inputs; ++i) {
134 m.add_entry(round, "public_input_" + std::to_string(i), Fr);
135 }
136 m.add_entry(round, "W_L", G);
137 m.add_entry(round, "W_R", G);
138 m.add_entry(round, "W_O", G);
139 for (size_t i = 1; i <= 4; ++i) {
140 m.add_entry(round, "ECC_OP_WIRE_" + std::to_string(i), G);
141 }
142 // DataBus entities:
143 for (const auto& label : { "KERNEL_CALLDATA",
144 "KERNEL_CALLDATA_READ_COUNTS",
145 "FIRST_APP_CALLDATA",
146 "FIRST_APP_CALLDATA_READ_COUNTS",
147 "SECOND_APP_CALLDATA",
148 "SECOND_APP_CALLDATA_READ_COUNTS",
149 "THIRD_APP_CALLDATA",
150 "THIRD_APP_CALLDATA_READ_COUNTS",
151 "RETURN_DATA",
152 "RETURN_DATA_READ_COUNTS" }) {
153 m.add_entry(round, label, G);
154 }
155 m.add_challenge(round, "eta");
156 round++;
157
158 // ── Round 1: MegaZK lookup counts/tags/W_4 ───────────────────────────────
159 m.add_entry(round, "LOOKUP_READ_COUNTS", G);
160 m.add_entry(round, "LOOKUP_READ_TAGS", G);
161 m.add_entry(round, "W_4", G);
162 m.add_challenge(round, std::array<std::string, 2>{ "beta", "gamma" });
163 round++;
164
165 // ── Round 2: MegaZK logderiv inverses + Z_PERM + translator Oink ─────────
166 m.add_entry(round, "LOOKUP_INVERSES", G);
167 m.add_entry(round, "KERNEL_CALLDATA_INVERSES", G);
168 m.add_entry(round, "FIRST_APP_CALLDATA_INVERSES", G);
169 m.add_entry(round, "SECOND_APP_CALLDATA_INVERSES", G);
170 m.add_entry(round, "THIRD_APP_CALLDATA_INVERSES", G);
171 m.add_entry(round, "RETURN_DATA_INVERSES", G);
172 m.add_entry(round, "Z_PERM", G);
173 // Translator Oink: vk_hash, masking commitment, 10 wire commitments
174 m.add_entry(round, "vk_hash", Fr);
175 m.add_entry(round, "Gemini:masking_poly_comm", G);
176 for (size_t i = 0; i < 4; ++i) {
177 m.add_entry(round, "CONCATENATED_RANGE_CONSTRAINTS_" + std::to_string(i), G);
178 }
179 m.add_entry(round, "CONCATENATED_NON_RANGE", G);
180 for (size_t i = 0; i < 5; ++i) {
181 m.add_entry(round, "ORDERED_RANGE_CONSTRAINTS_" + std::to_string(i), G);
182 }
183 m.add_challenge(round, std::array<std::string, 2>{ "beta", "gamma" });
184 round++;
185
186 // ── Round 3: translator Z_PERM, then joint alpha + gate challenges ────────
187 m.add_entry(round, "Z_PERM", G);
188 m.add_challenge(round, "Sumcheck:alpha");
189 m.add_challenge(round, "Sumcheck:gate_challenge");
190 round++;
191
192 // ── Round 4: Libra masking commitment + Sum ───────────────────────────────
193 m.add_entry(round, "Libra:concatenation_commitment", G);
194 m.add_entry(round, "Libra:Sum", Fr);
195 m.add_challenge(round, "Libra:Challenge");
196 round++;
197
198 // ── Rounds 5..4+JOINT_LOG_N: joint sumcheck ──────────────────────────────
199 for (size_t i = 0; i < JOINT_LOG_N; ++i) {
200 // Translator mini-circuit evaluations are sent after round LOG_MINI_CIRCUIT_SIZE-1
201 // and appear before univariate_{LOG_MINI} in the manifest.
202 if (i == LOG_MINI) {
203 m.add_entry(round, "Sumcheck:minicircuit_evaluations", Trans::NUM_MINICIRCUIT_EVALUATIONS);
204 }
205 m.add_entry(round, "Sumcheck:univariate_comm_" + std::to_string(i), G);
206 m.add_entry(round, "Sumcheck:univariate_" + std::to_string(i) + "_eval_0", Fr);
207 m.add_entry(round, "Sumcheck:univariate_" + std::to_string(i) + "_eval_1", Fr);
208 m.add_challenge(round, "Sumcheck:u_" + std::to_string(i));
209 round++;
210 }
211
212 // ── Post-sumcheck evaluations ─────────────────────────────────────────────
213 // MegaZK evaluations are sent after all sumcheck rounds (real + virtual).
214 m.add_entry(round, "Sumcheck:evaluations", MZK::NUM_ALL_ENTITIES);
215 m.add_entry(round, "Sumcheck:evaluations_translator", Trans::NUM_FULL_CIRCUIT_EVALUATIONS);
216 m.add_entry(round, "Libra:claimed_evaluation", Fr);
217 m.add_entry(round, "Libra:grand_sum_commitment", G);
218 m.add_entry(round, "Libra:quotient_commitment", G);
219 m.add_challenge(round, "rho");
220 round++;
221
222 // ── Gemini fold commitments ───────────────────────────────────────────────
223 for (size_t i = 1; i < JOINT_LOG_N; ++i) {
224 m.add_entry(round, "Gemini:FOLD_" + std::to_string(i), G);
225 }
226 m.add_challenge(round, "Gemini:r");
227 round++;
228
229 // ── Gemini evaluations + Libra evals ─────────────────────────────────────
230 for (size_t i = 1; i <= JOINT_LOG_N; ++i) {
231 m.add_entry(round, "Gemini:a_" + std::to_string(i), Fr);
232 }
233 m.add_entry(round, "Libra:concatenation_eval", Fr);
234 m.add_entry(round, "Libra:shifted_grand_sum_eval", Fr);
235 m.add_entry(round, "Libra:grand_sum_eval", Fr);
236 m.add_entry(round, "Libra:quotient_eval", Fr);
237 m.add_challenge(round, "Shplonk:nu");
238 round++;
239
240 // ── Shplonk:Q ────────────────────────────────────────────────────────────
241 m.add_entry(round, "Shplonk:Q", G);
242 m.add_challenge(round, "Shplonk:z");
243 round++;
244
245 // ── KZG opening ──────────────────────────────────────────────────────────
246 m.add_entry(round, "KZG:W", G);
247
248 return m;
249 }
250
257 {
258 auto& ck = key->proving_key->commitment_key;
259 auto& polys = key->proving_key->polynomials;
260 return {
261 ck.commit(polys.op), ck.commit(polys.x_lo_y_hi), ck.commit(polys.x_hi_z_1), ck.commit(polys.y_lo_z_2)
262 };
263 }
264};
265
266// =============================================================================
267// BatchedHonkTranslatorTests::ProveAndVerify
268// =============================================================================
270{
272 using MegaZKProverInst = ProverInstance_<MegaZKFlavor>;
273 using MegaZKVK = MegaZKFlavor::VerificationKey;
274 using MegaZKVKAndHash = MegaZKFlavor::VKAndHash;
275
276 // -------------------------------------------------------------------------
277 // 1. Translator inputs (random translation challenges — no ECCVM needed).
278 // -------------------------------------------------------------------------
279 const Fq batching_challenge_v = Fq::random_element();
280 const Fq evaluation_input_x = Fq::random_element();
281
282 auto translator_key = build_translator_key(batching_challenge_v, evaluation_input_x);
283
284 // Initialise the translator commitment key (normally done by TranslatorProver ctor).
285 {
286 auto tmp = std::make_shared<Transcript>();
287 TranslatorProver init_prover(translator_key, tmp); // side-effect: initialises commitment_key
288 }
289 const Fq accumulated_result = get_accumulated_result(translator_key);
290 const auto op_queue_wire_commitments = commit_op_queue_wires(translator_key);
291
292 // -------------------------------------------------------------------------
293 // 2. Hiding kernel inputs: pad to JOINT_LOG_N = 17 so hiding_log_n == JOINT_LOG_N.
294 // -------------------------------------------------------------------------
295 MegaCircuitBuilder mega_zk_circuit;
297 // Pad so that hiding_log_n == JOINT_LOG_N. We aim for JOINT_LOG_N-1 as the arithmetic
298 // target because MegaCircuitBuilder's execution-trace overhead grows the dyadic size by one.
299 static constexpr size_t JOINT_LOG_N = BatchedHonkTranslatorProver::JOINT_LOG_N;
300 MockCircuits::construct_arithmetic_circuit(mega_zk_circuit, JOINT_LOG_N - 1, /*include_public_inputs=*/false);
301
302 auto mega_zk_inst = std::make_shared<MegaZKProverInst>(mega_zk_circuit);
303 auto mega_zk_vk = std::make_shared<MegaZKVK>(mega_zk_inst->get_precomputed());
304 auto mega_zk_vk_and_hash = std::make_shared<MegaZKVKAndHash>(mega_zk_vk);
305
306 // -------------------------------------------------------------------------
307 // 3. Prove.
308 // -------------------------------------------------------------------------
309 auto prover_transcript = std::make_shared<Transcript>();
310 BatchedHonkTranslatorProver prover(mega_zk_inst, mega_zk_vk, prover_transcript);
311
312 auto mega_zk_proof = prover.prove_mega_zk_oink();
313 auto joint_proof = prover.prove(translator_key);
314
315 // -------------------------------------------------------------------------
316 // 4. Verify.
317 // -------------------------------------------------------------------------
318 auto verifier_transcript = std::make_shared<Transcript>();
319 BatchedHonkTranslatorVerifier verifier(mega_zk_vk_and_hash, verifier_transcript);
320 verifier.verify_mega_zk_oink(mega_zk_proof);
321 auto result = verifier.verify(
322 joint_proof, evaluation_input_x, batching_challenge_v, accumulated_result, op_queue_wire_commitments);
323
324 EXPECT_TRUE(result.reduction_succeeded);
325 EXPECT_TRUE(result.pairing_points.check());
326}
327
328// =============================================================================
329// BatchedHonkTranslatorTests::VerifierManifestConsistency
330// Checks that the prover and verifier Fiat-Shamir transcripts produce the same
331// manifest (same sequence of send/receive/challenge entries), which pins the
332// joint proof structure and detects any prover/verifier protocol divergence.
333// =============================================================================
334TEST_F(BatchedHonkTranslatorTests, VerifierManifestConsistency)
335{
336 using MegaZKProverInst = ProverInstance_<MegaZKFlavor>;
337 using MegaZKVK = MegaZKFlavor::VerificationKey;
338 using MegaZKVKAndHash = MegaZKFlavor::VKAndHash;
339
340 const Fq batching_challenge_v = Fq::random_element();
341 const Fq evaluation_input_x = Fq::random_element();
342
343 auto translator_key = build_translator_key(batching_challenge_v, evaluation_input_x);
344 {
345 auto tmp = std::make_shared<Transcript>();
346 TranslatorProver init_prover(translator_key, tmp);
347 }
348 const Fq accumulated_result = get_accumulated_result(translator_key);
349 const auto op_queue_wire_commitments = commit_op_queue_wires(translator_key);
350
351 MegaCircuitBuilder mega_zk_circuit;
353
354 auto mega_zk_inst = std::make_shared<MegaZKProverInst>(mega_zk_circuit);
355 auto mega_zk_vk = std::make_shared<MegaZKVK>(mega_zk_inst->get_precomputed());
356 auto mega_zk_vk_and_hash = std::make_shared<MegaZKVKAndHash>(mega_zk_vk);
357
358 // Prove with manifest tracking enabled.
359 auto prover_transcript = std::make_shared<Transcript>();
360 prover_transcript->enable_manifest();
361 BatchedHonkTranslatorProver prover(mega_zk_inst, mega_zk_vk, prover_transcript);
362 auto mega_zk_proof = prover.prove_mega_zk_oink();
363 auto joint_proof = prover.prove(translator_key);
364
365 // Verify with manifest tracking enabled.
366 auto verifier_transcript = std::make_shared<Transcript>();
367 verifier_transcript->enable_manifest();
368 BatchedHonkTranslatorVerifier verifier(mega_zk_vk_and_hash, verifier_transcript);
369 verifier.verify_mega_zk_oink(mega_zk_proof);
370 [[maybe_unused]] auto _ = verifier.verify(
371 joint_proof, evaluation_input_x, batching_challenge_v, accumulated_result, op_queue_wire_commitments);
372
373 auto prover_manifest = prover_transcript->get_manifest();
374 auto verifier_manifest = verifier_transcript->get_manifest();
375
376 ASSERT_GT(prover_manifest.size(), 0);
377 for (size_t round = 0; round < prover_manifest.size(); ++round) {
378 ASSERT_EQ(prover_manifest[round], verifier_manifest[round])
379 << "Prover/verifier manifest discrepancy in round " << round;
380 }
381}
382
383// =============================================================================
384// BatchedHonkTranslatorTests::ProverManifestConsistency
385// Pins the joint transcript structure by comparing the prover manifest against
386// a hard-coded expected manifest built from flavor constants. Detects any
387// structural change in the Fiat-Shamir transcript ordering.
388// =============================================================================
389TEST_F(BatchedHonkTranslatorTests, ProverManifestConsistency)
390{
391 using MegaZKProverInst = ProverInstance_<MegaZKFlavor>;
392 using MegaZKVK = MegaZKFlavor::VerificationKey;
393
394 const Fq batching_challenge_v = Fq::random_element();
395 const Fq evaluation_input_x = Fq::random_element();
396
397 auto translator_key = build_translator_key(batching_challenge_v, evaluation_input_x);
398 {
399 auto tmp = std::make_shared<Transcript>();
400 TranslatorProver init_prover(translator_key, tmp);
401 }
402
403 MegaCircuitBuilder mega_zk_circuit;
405 auto mega_zk_inst = std::make_shared<MegaZKProverInst>(mega_zk_circuit);
406 auto mega_zk_vk = std::make_shared<MegaZKVK>(mega_zk_inst->get_precomputed());
407
408 const size_t num_mega_zk_pub_inputs = mega_zk_inst->num_public_inputs();
409
410 // Prove with manifest tracking enabled.
411 auto prover_transcript = std::make_shared<Transcript>();
412 prover_transcript->enable_manifest();
413 BatchedHonkTranslatorProver prover(mega_zk_inst, mega_zk_vk, prover_transcript);
414 [[maybe_unused]] auto _ = prover.prove_mega_zk_oink();
415 [[maybe_unused]] auto __ = prover.prove(translator_key);
416
417 auto prover_manifest = prover_transcript->get_manifest();
418 auto expected_manifest = build_expected_batched_manifest(num_mega_zk_pub_inputs);
419
420 ASSERT_EQ(prover_manifest.size(), expected_manifest.size()) << "Manifest round count mismatch";
421 for (size_t round = 0; round < expected_manifest.size(); ++round) {
422 ASSERT_EQ(prover_manifest[round], expected_manifest[round]) << "Manifest discrepancy in round " << round;
423 }
424}
425
426// =============================================================================
427// BatchedHonkTranslatorTests::ProveAndVerifySmallHiding
428// Tests the variable circuit size path: hiding_log_n < JOINT_LOG_N.
429// =============================================================================
430TEST_F(BatchedHonkTranslatorTests, ProveAndVerifySmallHiding)
431{
433 using MegaZKProverInst = ProverInstance_<MegaZKFlavor>;
434 using MegaZKVK = MegaZKFlavor::VerificationKey;
435 using MegaZKVKAndHash = MegaZKFlavor::VKAndHash;
436
437 const Fq batching_challenge_v = Fq::random_element();
438 const Fq evaluation_input_x = Fq::random_element();
439
440 auto translator_key = build_translator_key(batching_challenge_v, evaluation_input_x);
441 {
442 auto tmp = std::make_shared<Transcript>();
443 TranslatorProver init_prover(translator_key, tmp);
444 }
445 const Fq accumulated_result = get_accumulated_result(translator_key);
446 const auto op_queue_wire_commitments = commit_op_queue_wires(translator_key);
447
448 // Use a small hiding circuit — NOT padded to JOINT_LOG_N.
449 // hiding_log_n will be well below 17, exercising the variable-size code paths.
450 MegaCircuitBuilder mega_zk_circuit;
452
453 auto mega_zk_inst = std::make_shared<MegaZKProverInst>(mega_zk_circuit);
454 auto mega_zk_vk = std::make_shared<MegaZKVK>(mega_zk_inst->get_precomputed());
455 auto mega_zk_vk_and_hash = std::make_shared<MegaZKVKAndHash>(mega_zk_vk);
456
457 auto prover_transcript = std::make_shared<Transcript>();
458 BatchedHonkTranslatorProver prover(mega_zk_inst, mega_zk_vk, prover_transcript);
459 auto mega_zk_proof = prover.prove_mega_zk_oink();
460 auto joint_proof = prover.prove(translator_key);
461
462 auto verifier_transcript = std::make_shared<Transcript>();
463 BatchedHonkTranslatorVerifier verifier(mega_zk_vk_and_hash, verifier_transcript);
464 verifier.verify_mega_zk_oink(mega_zk_proof);
465 auto result = verifier.verify(
466 joint_proof, evaluation_input_x, batching_challenge_v, accumulated_result, op_queue_wire_commitments);
467
468 EXPECT_TRUE(result.reduction_succeeded);
469 EXPECT_TRUE(result.pairing_points.check());
470}
TEST_F(BatchedHonkTranslatorTests, ProveAndVerify)
static std::array< TranslatorFlavor::Commitment, TranslatorFlavor::NUM_OP_QUEUE_WIRES > commit_op_queue_wires(const std::shared_ptr< TranslatorProvingKey > &key)
Commit to the four op-queue wire polynomials.
static void add_mixed_ops(std::shared_ptr< ECCOpQueue > &op_queue, size_t count=100)
static Fq get_accumulated_result(const std::shared_ptr< TranslatorProvingKey > &key)
Read accumulated_result from the translator witness polynomials.
static std::shared_ptr< TranslatorProvingKey > build_translator_key(const Fq &batching_challenge_v, const Fq &evaluation_input_x, size_t circuit_size_param=500)
Build a translator circuit on a fresh op queue with random challenges.
static void add_random_ops(std::shared_ptr< ECCOpQueue > &op_queue, size_t count)
static TranscriptManifest build_expected_batched_manifest(const size_t num_mega_zk_pub_inputs)
Build the expected transcript manifest for a BatchedHonkTranslator proof.
Common transcript class for both parties. Stores the data for the current round, as well as the manif...
Prover for the batched MegaZK circuit + translator sumcheck and PCS.
HonkProof prove(std::shared_ptr< TranslatorProvingKey > translator_proving_key)
Verifier for the batched MegaZK circuit + translator sumcheck and PCS.
ReductionResult verify(const Proof &joint_proof, const TransBF &evaluation_input_x, const TransBF &batching_challenge_v, const TransBF &accumulated_result, const std::array< Commitment, TranslatorFlavor::NUM_OP_QUEUE_WIRES > &op_queue_wire_commitments)
Phase 2: Verify translator Oink + joint sumcheck + joint PCS.
OinkResult verify_mega_zk_oink(const Proof &mega_zk_proof)
Phase 1: Verify the MegaZK Oink phase on the shared transcript.
static void construct_simple_circuit(MegaBuilder &builder)
Generate a simple test circuit with some ECC op gates and conventional arithmetic gates.
Child class of MegaFlavor that runs with ZK Sumcheck.
MegaFlavor::VKAndHash VKAndHash
static void construct_arithmetic_circuit(Builder &builder, const size_t target_log2_dyadic_size=4, bool include_public_inputs=true)
Populate a builder with a specified number of arithmetic gates; includes a PI.
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...
void add_entry(size_t round, const std::string &element_label, size_t element_size)
void add_challenge(size_t round, const std::string &label)
Add a single challenge label to the manifest for the given round.
TranslatorCircuitBuilder creates a circuit that evaluates the correctness of the evaluation of EccOpQ...
Curve::ScalarField FF
Curve::AffineElement Commitment
static constexpr size_t RESULT_ROW
#define G(r, i, a, b, c, d)
Definition blake2s.cpp:116
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
BaseTranscript< FrCodec, bb::crypto::Poseidon2< bb::crypto::Poseidon2Bn254ScalarFieldParams > > NativeTranscript
CommitmentKey< Curve > ck
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::string to_string(bb::avm2::ValueTag tag)
Curve::ScalarField Fr
static field random_element(numeric::RNG *engine=nullptr) noexcept