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_for_prover());
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 // MegaZK keeps the kernel_calldata bus (the only bus the hiding kernel needs):
143 // the calldata column + its read_counts ride the oink batch alongside the wires.
144 m.add_entry(round, "KERNEL_CALLDATA", G);
145 m.add_entry(round, "KERNEL_CALLDATA_READ_COUNTS", G);
146 // No "eta" challenge in MegaZK (no memory / log-derivative lookup relation), so W_4
147 // stays in the same FS round as the prior wire/bus commitments — no round break here.
148 m.add_entry(round, "W_4", G);
149 m.add_challenge(round, std::array<std::string, 2>{ "beta", "gamma" });
150 round++;
151
152 // ── Round 1: MegaZK Z_PERM + bus inverses + translator Oink ──────────────
153 // (LOOKUP_INVERSES dropped — MegaZK has no lookup relation.)
154 m.add_entry(round, "KERNEL_CALLDATA_INVERSES", G);
155 m.add_entry(round, "Z_PERM", G);
156 // Translator Oink: vk_hash, masking commitment, 10 wire commitments
157 m.add_entry(round, "vk_hash", Fr);
158 m.add_entry(round, "Gemini:masking_poly_comm", G);
159 for (size_t i = 0; i < 4; ++i) {
160 m.add_entry(round, "CONCATENATED_RANGE_CONSTRAINTS_" + std::to_string(i), G);
161 }
162 m.add_entry(round, "CONCATENATED_NON_RANGE", G);
163 for (size_t i = 0; i < 5; ++i) {
164 m.add_entry(round, "ORDERED_RANGE_CONSTRAINTS_" + std::to_string(i), G);
165 }
166 m.add_challenge(round, std::array<std::string, 2>{ "beta", "gamma" });
167 round++;
168
169 // ── Round 3: translator Z_PERM, then joint alpha + gate challenges ────────
170 m.add_entry(round, "Z_PERM", G);
171 m.add_challenge(round, "Sumcheck:alpha");
172 m.add_challenge(round, "Sumcheck:gate_challenge");
173 round++;
174
175 // ── Round 4: Libra masking commitment + Sum ───────────────────────────────
176 m.add_entry(round, "Libra:concatenation_commitment", G);
177 m.add_entry(round, "Libra:Sum", Fr);
178 m.add_challenge(round, "Libra:Challenge");
179 round++;
180
181 // ── Rounds 5..4+JOINT_LOG_N: joint sumcheck ──────────────────────────────
182 for (size_t i = 0; i < JOINT_LOG_N; ++i) {
183 // Translator mini-circuit evaluations are sent after round LOG_MINI_CIRCUIT_SIZE-1
184 // and appear before univariate_{LOG_MINI} in the manifest.
185 if (i == LOG_MINI) {
186 m.add_entry(round, "Sumcheck:minicircuit_evaluations", Trans::NUM_MINICIRCUIT_EVALUATIONS);
187 }
188 m.add_entry(round, "Sumcheck:univariate_comm_" + std::to_string(i), G);
189 m.add_entry(round, "Sumcheck:univariate_" + std::to_string(i) + "_eval_0", Fr);
190 m.add_entry(round, "Sumcheck:univariate_" + std::to_string(i) + "_eval_1", Fr);
191 m.add_challenge(round, "Sumcheck:u_" + std::to_string(i));
192 round++;
193 }
194
195 // ── Post-sumcheck evaluations ─────────────────────────────────────────────
196 // MegaZK evaluations are sent after all sumcheck rounds (real + virtual).
197 m.add_entry(round, "Sumcheck:evaluations", MZK::NUM_ALL_ENTITIES);
198 m.add_entry(round, "Sumcheck:evaluations_translator", Trans::NUM_FULL_CIRCUIT_EVALUATIONS);
199 m.add_entry(round, "Libra:claimed_evaluation", Fr);
200 m.add_entry(round, "Libra:grand_sum_commitment", G);
201 m.add_entry(round, "Libra:quotient_commitment", G);
202 m.add_challenge(round, "rho");
203 round++;
204
205 // ── Gemini fold commitments ───────────────────────────────────────────────
206 for (size_t i = 1; i < JOINT_LOG_N; ++i) {
207 m.add_entry(round, "Gemini:FOLD_" + std::to_string(i), G);
208 }
209 m.add_challenge(round, "Gemini:r");
210 round++;
211
212 // ── Gemini evaluations + Libra evals ─────────────────────────────────────
213 for (size_t i = 1; i <= JOINT_LOG_N; ++i) {
214 m.add_entry(round, "Gemini:a_" + std::to_string(i), Fr);
215 }
216 m.add_entry(round, "Libra:concatenation_eval", Fr);
217 m.add_entry(round, "Libra:shifted_grand_sum_eval", Fr);
218 m.add_entry(round, "Libra:grand_sum_eval", Fr);
219 m.add_entry(round, "Libra:quotient_eval", Fr);
220 m.add_challenge(round, "Shplonk:nu");
221 round++;
222
223 // ── Shplonk:Q ────────────────────────────────────────────────────────────
224 m.add_entry(round, "Shplonk:Q", G);
225 m.add_challenge(round, "Shplonk:z");
226 round++;
227
228 // ── KZG opening ──────────────────────────────────────────────────────────
229 m.add_entry(round, "KZG:W", G);
230
231 return m;
232 }
233
240 {
241 auto& ck = key->proving_key->commitment_key;
242 auto& polys = key->proving_key->polynomials;
243 return {
244 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)
245 };
246 }
247};
248
249// =============================================================================
250// BatchedHonkTranslatorTests::ProveAndVerify
251// =============================================================================
253{
255 using MegaZKProverInst = ProverInstance_<MegaZKFlavor>;
256 using MegaZKVK = MegaZKFlavor::VerificationKey;
257 using MegaZKVKAndHash = MegaZKFlavor::VKAndHash;
258
259 // -------------------------------------------------------------------------
260 // 1. Translator inputs (random translation challenges — no ECCVM needed).
261 // -------------------------------------------------------------------------
262 const Fq batching_challenge_v = Fq::random_element();
263 const Fq evaluation_input_x = Fq::random_element();
264
265 auto translator_key = build_translator_key(batching_challenge_v, evaluation_input_x);
266
267 // Initialise the translator commitment key (normally done by TranslatorProver ctor).
268 {
269 auto tmp = std::make_shared<Transcript>();
270 TranslatorProver init_prover(translator_key, tmp); // side-effect: initialises commitment_key
271 }
272 const Fq accumulated_result = get_accumulated_result(translator_key);
273 const auto op_queue_wire_commitments = commit_op_queue_wires(translator_key);
274
275 // -------------------------------------------------------------------------
276 // 2. Hiding kernel inputs: pad to JOINT_LOG_N = 17 so hiding_log_n == JOINT_LOG_N.
277 // -------------------------------------------------------------------------
278 MegaCircuitBuilder mega_zk_circuit;
280 // Pad so that hiding_log_n == JOINT_LOG_N. We aim for JOINT_LOG_N-1 as the arithmetic
281 // target because MegaCircuitBuilder's execution-trace overhead grows the dyadic size by one.
282 static constexpr size_t JOINT_LOG_N = BatchedHonkTranslatorProver::JOINT_LOG_N;
283 MockCircuits::construct_arithmetic_circuit(mega_zk_circuit, JOINT_LOG_N - 1, /*include_public_inputs=*/false);
284
285 auto mega_zk_inst = std::make_shared<MegaZKProverInst>(mega_zk_circuit);
286 auto mega_zk_vk = std::make_shared<MegaZKVK>(mega_zk_inst->get_precomputed());
287 auto mega_zk_vk_and_hash = std::make_shared<MegaZKVKAndHash>(mega_zk_vk);
288
289 // -------------------------------------------------------------------------
290 // 3. Prove.
291 // -------------------------------------------------------------------------
292 auto prover_transcript = std::make_shared<Transcript>();
293 BatchedHonkTranslatorProver prover(mega_zk_inst, mega_zk_vk, prover_transcript);
294
295 auto mega_zk_proof = prover.prove_mega_zk_oink();
296 auto joint_proof = prover.prove(translator_key);
297
298 // -------------------------------------------------------------------------
299 // 4. Verify.
300 // -------------------------------------------------------------------------
301 auto verifier_transcript = std::make_shared<Transcript>();
302 BatchedHonkTranslatorVerifier verifier(mega_zk_vk_and_hash, verifier_transcript);
303 verifier.verify_mega_zk_oink(mega_zk_proof);
304 auto result = verifier.verify(
305 joint_proof, evaluation_input_x, batching_challenge_v, accumulated_result, op_queue_wire_commitments);
306
307 EXPECT_TRUE(result.reduction_succeeded);
308 EXPECT_TRUE(result.pairing_points.check());
309}
310
311// =============================================================================
312// BatchedHonkTranslatorTests::VerifierManifestConsistency
313// Checks that the prover and verifier Fiat-Shamir transcripts produce the same
314// manifest (same sequence of send/receive/challenge entries), which pins the
315// joint proof structure and detects any prover/verifier protocol divergence.
316// =============================================================================
317TEST_F(BatchedHonkTranslatorTests, VerifierManifestConsistency)
318{
319 using MegaZKProverInst = ProverInstance_<MegaZKFlavor>;
320 using MegaZKVK = MegaZKFlavor::VerificationKey;
321 using MegaZKVKAndHash = MegaZKFlavor::VKAndHash;
322
323 const Fq batching_challenge_v = Fq::random_element();
324 const Fq evaluation_input_x = Fq::random_element();
325
326 auto translator_key = build_translator_key(batching_challenge_v, evaluation_input_x);
327 {
328 auto tmp = std::make_shared<Transcript>();
329 TranslatorProver init_prover(translator_key, tmp);
330 }
331 const Fq accumulated_result = get_accumulated_result(translator_key);
332 const auto op_queue_wire_commitments = commit_op_queue_wires(translator_key);
333
334 MegaCircuitBuilder mega_zk_circuit;
336
337 auto mega_zk_inst = std::make_shared<MegaZKProverInst>(mega_zk_circuit);
338 auto mega_zk_vk = std::make_shared<MegaZKVK>(mega_zk_inst->get_precomputed());
339 auto mega_zk_vk_and_hash = std::make_shared<MegaZKVKAndHash>(mega_zk_vk);
340
341 // Prove with manifest tracking enabled.
342 auto prover_transcript = std::make_shared<Transcript>();
343 prover_transcript->enable_manifest();
344 BatchedHonkTranslatorProver prover(mega_zk_inst, mega_zk_vk, prover_transcript);
345 auto mega_zk_proof = prover.prove_mega_zk_oink();
346 auto joint_proof = prover.prove(translator_key);
347
348 // Verify with manifest tracking enabled.
349 auto verifier_transcript = std::make_shared<Transcript>();
350 verifier_transcript->enable_manifest();
351 BatchedHonkTranslatorVerifier verifier(mega_zk_vk_and_hash, verifier_transcript);
352 verifier.verify_mega_zk_oink(mega_zk_proof);
353 [[maybe_unused]] auto _ = verifier.verify(
354 joint_proof, evaluation_input_x, batching_challenge_v, accumulated_result, op_queue_wire_commitments);
355
356 auto prover_manifest = prover_transcript->get_manifest();
357 auto verifier_manifest = verifier_transcript->get_manifest();
358
359 ASSERT_GT(prover_manifest.size(), 0);
360 for (size_t round = 0; round < prover_manifest.size(); ++round) {
361 ASSERT_EQ(prover_manifest[round], verifier_manifest[round])
362 << "Prover/verifier manifest discrepancy in round " << round;
363 }
364}
365
366// =============================================================================
367// BatchedHonkTranslatorTests::ProverManifestConsistency
368// Pins the joint transcript structure by comparing the prover manifest against
369// a hard-coded expected manifest built from flavor constants. Detects any
370// structural change in the Fiat-Shamir transcript ordering.
371// =============================================================================
372TEST_F(BatchedHonkTranslatorTests, ProverManifestConsistency)
373{
374 using MegaZKProverInst = ProverInstance_<MegaZKFlavor>;
375 using MegaZKVK = MegaZKFlavor::VerificationKey;
376
377 const Fq batching_challenge_v = Fq::random_element();
378 const Fq evaluation_input_x = Fq::random_element();
379
380 auto translator_key = build_translator_key(batching_challenge_v, evaluation_input_x);
381 {
382 auto tmp = std::make_shared<Transcript>();
383 TranslatorProver init_prover(translator_key, tmp);
384 }
385
386 MegaCircuitBuilder mega_zk_circuit;
388 auto mega_zk_inst = std::make_shared<MegaZKProverInst>(mega_zk_circuit);
389 auto mega_zk_vk = std::make_shared<MegaZKVK>(mega_zk_inst->get_precomputed());
390
391 const size_t num_mega_zk_pub_inputs = mega_zk_inst->num_public_inputs();
392
393 // Prove with manifest tracking enabled.
394 auto prover_transcript = std::make_shared<Transcript>();
395 prover_transcript->enable_manifest();
396 BatchedHonkTranslatorProver prover(mega_zk_inst, mega_zk_vk, prover_transcript);
397 [[maybe_unused]] auto _ = prover.prove_mega_zk_oink();
398 [[maybe_unused]] auto __ = prover.prove(translator_key);
399
400 auto prover_manifest = prover_transcript->get_manifest();
401 auto expected_manifest = build_expected_batched_manifest(num_mega_zk_pub_inputs);
402
403 ASSERT_EQ(prover_manifest.size(), expected_manifest.size()) << "Manifest round count mismatch";
404 for (size_t round = 0; round < expected_manifest.size(); ++round) {
405 ASSERT_EQ(prover_manifest[round], expected_manifest[round]) << "Manifest discrepancy in round " << round;
406 }
407}
408
409// =============================================================================
410// BatchedHonkTranslatorTests::ProveAndVerifySmallHiding
411// Tests the variable circuit size path: hiding_log_n < JOINT_LOG_N.
412// =============================================================================
413TEST_F(BatchedHonkTranslatorTests, ProveAndVerifySmallHiding)
414{
416 using MegaZKProverInst = ProverInstance_<MegaZKFlavor>;
417 using MegaZKVK = MegaZKFlavor::VerificationKey;
418 using MegaZKVKAndHash = MegaZKFlavor::VKAndHash;
419
420 const Fq batching_challenge_v = Fq::random_element();
421 const Fq evaluation_input_x = Fq::random_element();
422
423 auto translator_key = build_translator_key(batching_challenge_v, evaluation_input_x);
424 {
425 auto tmp = std::make_shared<Transcript>();
426 TranslatorProver init_prover(translator_key, tmp);
427 }
428 const Fq accumulated_result = get_accumulated_result(translator_key);
429 const auto op_queue_wire_commitments = commit_op_queue_wires(translator_key);
430
431 // Use a small hiding circuit — NOT padded to JOINT_LOG_N.
432 // hiding_log_n will be well below 17, exercising the variable-size code paths.
433 MegaCircuitBuilder mega_zk_circuit;
435
436 auto mega_zk_inst = std::make_shared<MegaZKProverInst>(mega_zk_circuit);
437 auto mega_zk_vk = std::make_shared<MegaZKVK>(mega_zk_inst->get_precomputed());
438 auto mega_zk_vk_and_hash = std::make_shared<MegaZKVKAndHash>(mega_zk_vk);
439
440 auto prover_transcript = std::make_shared<Transcript>();
441 BatchedHonkTranslatorProver prover(mega_zk_inst, mega_zk_vk, prover_transcript);
442 auto mega_zk_proof = prover.prove_mega_zk_oink();
443 auto joint_proof = prover.prove(translator_key);
444
445 auto verifier_transcript = std::make_shared<Transcript>();
446 BatchedHonkTranslatorVerifier verifier(mega_zk_vk_and_hash, verifier_transcript);
447 verifier.verify_mega_zk_oink(mega_zk_proof);
448 auto result = verifier.verify(
449 joint_proof, evaluation_input_x, batching_challenge_v, accumulated_result, op_queue_wire_commitments);
450
451 EXPECT_TRUE(result.reduction_succeeded);
452 EXPECT_TRUE(result.pairing_points.check());
453}
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.
Hiding-kernel-only Mega variant: runs with ZK Sumcheck and a reduced relation set.
NativeVerificationKey_< PrecomputedEntities< Commitment >, Codec, HashFunction, CommitmentKey > VerificationKey
VKAndHash_< FF, VerificationKey > 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.
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)
static field random_element(numeric::RNG *engine=nullptr) noexcept
VectorField result