Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
honk_transcript.test.cpp
Go to the documentation of this file.
16
17#include <gtest/gtest.h>
18
19using namespace bb;
20
21#ifdef STARKNET_GARAGA_FLAVORS
22using FlavorTypes = ::testing::Types<UltraFlavor,
24 UltraStarknetFlavor,
25 UltraStarknetZKFlavor,
30#else
32 ::testing::Types<UltraFlavor, UltraKeccakFlavor, UltraZKFlavor, UltraKeccakZKFlavor, MegaFlavor, MegaZKFlavor>;
33#endif
34
35template <typename Flavor> class HonkTranscriptTests : public ::testing::Test {
36 public:
38
40 using FF = Flavor::FF;
45 using IO = DefaultIO;
48
61 {
62 TranscriptManifest manifest_expected;
63
64 const size_t virtual_log_n = Flavor::USE_PADDING ? Flavor::VIRTUAL_LOG_N : log_n;
65
66 size_t MAX_PARTIAL_RELATION_LENGTH = Flavor::BATCHED_RELATION_PARTIAL_LENGTH;
67 // Size of types is number of bb::frs needed to represent the types
68 // UltraKeccak uses uint256_t for commitments and frs, so we need to handle that differently.
69 size_t data_types_per_Frs = [] {
70 if constexpr (IsKeccakFlavor<Flavor>) {
71 return U256Codec::calc_num_fields<FF>();
72 } else {
73 return FrCodec::calc_num_fields<FF>();
74 }
75 }();
76 size_t data_types_per_G = [] {
77 if constexpr (IsKeccakFlavor<Flavor>) {
78 return U256Codec::calc_num_fields<Commitment>();
79 } else {
80 return FrCodec::calc_num_fields<Commitment>();
81 }
82 }();
83 size_t frs_per_uni = MAX_PARTIAL_RELATION_LENGTH * data_types_per_Frs;
84 size_t frs_per_evals = (Flavor::NUM_ALL_ENTITIES)*data_types_per_Frs;
85
86 size_t round = 0;
87 manifest_expected.add_entry(round, "vk_hash", data_types_per_Frs);
88
89 manifest_expected.add_entry(round, "public_input_0", data_types_per_Frs);
90 constexpr size_t PUBLIC_INPUTS_SIZE = IO::PUBLIC_INPUTS_SIZE;
91 for (size_t i = 0; i < PUBLIC_INPUTS_SIZE; i++) {
92 manifest_expected.add_entry(round, "public_input_" + std::to_string(1 + i), data_types_per_Frs);
93 }
94
95 // For flavors with Gemini masking: masking polynomial commitment is sent at end of oink
96 if constexpr (flavor_has_gemini_masking<Flavor>()) {
97 manifest_expected.add_entry(round, "Gemini:masking_poly_comm", data_types_per_G);
98 }
99 manifest_expected.add_entry(round, "W_L", data_types_per_G);
100 manifest_expected.add_entry(round, "W_R", data_types_per_G);
101 manifest_expected.add_entry(round, "W_O", data_types_per_G);
102
103 if constexpr (Flavor::HasEccOpQueue) {
104 manifest_expected.add_entry(round, "ECC_OP_WIRE_1", data_types_per_G);
105 manifest_expected.add_entry(round, "ECC_OP_WIRE_2", data_types_per_G);
106 manifest_expected.add_entry(round, "ECC_OP_WIRE_3", data_types_per_G);
107 manifest_expected.add_entry(round, "ECC_OP_WIRE_4", data_types_per_G);
108 }
109 // Bus list is flavor-specific: MegaZK keeps only kernel_calldata (the hiding kernel's
110 // sole bus); MegaFlavor carries the full seven-bus set.
111 constexpr std::array<const char*, 7> ALL_BUSES = { "KERNEL_CALLDATA", "FIRST_APP_CALLDATA",
112 "SECOND_APP_CALLDATA", "THIRD_APP_CALLDATA",
113 "FOURTH_APP_CALLDATA", "FIFTH_APP_CALLDATA",
114 "RETURN_DATA" };
115 if constexpr (Flavor::HasDataBus) {
116 for (size_t i = 0; i < Flavor::NUM_BUS_COLUMNS; ++i) {
117 manifest_expected.add_entry(round, ALL_BUSES[i], data_types_per_G);
118 manifest_expected.add_entry(round, std::string(ALL_BUSES[i]) + "_READ_COUNTS", data_types_per_G);
119 }
120 }
121
122 // eta and the ROM-LogUp offset rom_logup_gamma are sampled only when the flavor carries the
123 // memory relation; flavors without it (e.g. MegaZK) skip the challenges entirely, and the W_4 commit stays in
124 // the same Fiat-Shamir round as the prior wire/bus commitments.
125 if constexpr (Flavor::HasMemory) {
126 manifest_expected.add_challenge(round, std::array{ "eta", "rom_logup_gamma" });
127 round++;
128 }
129 if constexpr (Flavor::HasLogDerivLookup) {
130 manifest_expected.add_entry(round, "LOOKUP_READ_COUNTS", data_types_per_G);
131 manifest_expected.add_entry(round, "LOOKUP_READ_TAGS", data_types_per_G);
132 }
133 manifest_expected.add_entry(round, "W_4", data_types_per_G);
134 manifest_expected.add_challenge(round, std::array{ "beta", "gamma" });
135
136 round++;
137 if constexpr (Flavor::HasLogDerivLookup) {
138 manifest_expected.add_entry(round, "LOOKUP_INVERSES", data_types_per_G);
139 }
140 if constexpr (Flavor::HasDataBus) {
141 for (size_t i = 0; i < Flavor::NUM_BUS_COLUMNS; ++i) {
142 manifest_expected.add_entry(round, std::string(ALL_BUSES[i]) + "_INVERSES", data_types_per_G);
143 }
144 }
145 manifest_expected.add_entry(round, "Z_PERM", data_types_per_G);
146
147 manifest_expected.add_challenge(round, "alpha");
148 manifest_expected.add_challenge(round, "Sumcheck:gate_challenge");
149 round++;
150
151 if constexpr (Flavor::HasZK) {
152 manifest_expected.add_entry(round, "Libra:concatenation_commitment", data_types_per_G);
153 manifest_expected.add_entry(round, "Libra:Sum", data_types_per_Frs);
154 manifest_expected.add_challenge(round, "Libra:Challenge");
155 round++;
156 }
157
158 for (size_t i = 0; i < virtual_log_n; ++i) {
159 std::string idx = std::to_string(i);
160 manifest_expected.add_entry(round, "Sumcheck:univariate_" + idx, frs_per_uni);
161 std::string label = "Sumcheck:u_" + idx;
162 manifest_expected.add_challenge(round, label);
163 round++;
164 }
165
166 manifest_expected.add_entry(round, "Sumcheck:evaluations", frs_per_evals);
167
168 if constexpr (Flavor::HasZK) {
169 manifest_expected.add_entry(round, "Libra:claimed_evaluation", data_types_per_Frs);
170 manifest_expected.add_entry(round, "Libra:grand_sum_commitment", data_types_per_G);
171 manifest_expected.add_entry(round, "Libra:quotient_commitment", data_types_per_G);
172 }
173
174 manifest_expected.add_challenge(round, "rho");
175
176 round++;
177 for (size_t i = 1; i < virtual_log_n; ++i) {
178 std::string idx = std::to_string(i);
179 manifest_expected.add_entry(round, "Gemini:FOLD_" + idx, data_types_per_G);
180 }
181 manifest_expected.add_challenge(round, "Gemini:r");
182 round++;
183 for (size_t i = 1; i <= virtual_log_n; ++i) {
184 std::string idx = std::to_string(i);
185 manifest_expected.add_entry(round, "Gemini:a_" + idx, data_types_per_Frs);
186 }
187
188 if constexpr (Flavor::HasZK) {
189 manifest_expected.add_entry(round, "Libra:concatenation_eval", data_types_per_Frs);
190 manifest_expected.add_entry(round, "Libra:shifted_grand_sum_eval", data_types_per_Frs);
191 manifest_expected.add_entry(round, "Libra:grand_sum_eval", data_types_per_Frs);
192 manifest_expected.add_entry(round, "Libra:quotient_eval", data_types_per_Frs);
193 }
194
195 manifest_expected.add_challenge(round, "Shplonk:nu");
196 round++;
197 manifest_expected.add_entry(round, "Shplonk:Q", data_types_per_G);
198 manifest_expected.add_challenge(round, "Shplonk:z");
199
200 round++;
201 manifest_expected.add_entry(round, "KZG:W", data_types_per_G);
202
203 return manifest_expected;
204 }
205
207 {
208 FF a = 1;
209 builder.add_variable(a);
210 builder.add_public_variable(a);
212 }
213
214 Proof export_serialized_proof(Prover& prover, const size_t num_public_inputs, const size_t log_n)
215 {
216 // reset internal variables needed for exporting the proof
217 // Note: this excludes IPA proof length since export_proof appends it separately
218 size_t proof_length = ProofLength::Honk<Flavor>::LENGTH_WITHOUT_PUB_INPUTS(log_n) + num_public_inputs;
219 prover.get_transcript()->test_set_proof_parsing_state(0, proof_length);
220 return prover.export_proof();
221 }
222};
223
225
230TYPED_TEST(HonkTranscriptTests, ProverManifestConsistency)
231{
232 // Construct a simple circuit of size n = 8 (i.e. the minimum circuit size)
233 auto builder = typename TestFixture::Builder();
234 TestFixture::generate_test_circuit(builder);
235
236 // Automatically generate a transcript manifest by constructing a proof
238 auto verification_key = std::make_shared<typename TestFixture::VerificationKey>(prover_instance->get_precomputed());
239 typename TestFixture::Prover prover(prover_instance, verification_key);
240 prover.get_transcript()->enable_manifest();
241 auto proof = prover.construct_proof();
242
243 // Check that the prover generated manifest agrees with the manifest hard coded in this suite
244 auto manifest_expected = TestFixture::construct_honk_manifest(prover.log_dyadic_size());
245 auto prover_manifest = prover.get_transcript()->get_manifest();
246 // Note: a manifest can be printed using manifest.print()
247 manifest_expected.print();
248 prover_manifest.print();
249 ASSERT_GT(manifest_expected.size(), 0);
250 for (size_t round = 0; round < manifest_expected.size(); ++round) {
251 if (prover_manifest[round] != manifest_expected[round]) {
252 info("Prover manifest discrepency in round ", round);
253 info("Prover manifest:");
254 prover_manifest[round].print();
255 info("Expected manifest:");
256 manifest_expected[round].print();
257 FAIL();
258 }
259 }
260}
261
266TYPED_TEST(HonkTranscriptTests, VerifierManifestConsistency)
267{
268 // Construct a simple circuit of size n = 8 (i.e. the minimum circuit size)
269 auto builder = typename TestFixture::Builder();
270 TestFixture::generate_test_circuit(builder);
271
272 // Automatically generate a transcript manifest in the prover by constructing a proof
274 auto verification_key = std::make_shared<typename TestFixture::VerificationKey>(prover_instance->get_precomputed());
275 auto vk_and_hash = std::make_shared<typename TypeParam::VKAndHash>(verification_key);
276 typename TestFixture::Prover prover(prover_instance, verification_key);
277 prover.get_transcript()->enable_manifest();
278 auto proof = prover.construct_proof();
279
280 // Automatically generate a transcript manifest in the verifier by verifying a proof
281 auto verifier_transcript = std::make_shared<typename TypeParam::Transcript>();
282 verifier_transcript->enable_manifest();
283 typename TestFixture::Verifier verifier(vk_and_hash, verifier_transcript);
284 [[maybe_unused]] auto _ = verifier.verify_proof(proof);
285
286 // Check consistency between the manifests generated by the prover and verifier
287 auto prover_manifest = prover.get_transcript()->get_manifest();
288 auto verifier_manifest = verifier.get_transcript()->get_manifest();
289
290 // Note: a manifest can be printed using manifest.print()
291 ASSERT_GT(prover_manifest.size(), 0);
292 for (size_t round = 0; round < prover_manifest.size(); ++round) {
293 ASSERT_EQ(prover_manifest[round], verifier_manifest[round])
294 << "Prover/Verifier manifest discrepency in round " << round;
295 }
296}
297
302TYPED_TEST(HonkTranscriptTests, ChallengeGenerationTest)
303{
304 using Flavor = TypeParam;
305 using FF = Flavor::FF;
306 // initialized with random value sent to verifier
307 auto transcript = TypeParam::Transcript::test_prover_init_empty();
308 // test a bunch of challenges
309 std::vector<std::string> challenge_labels{ "a", "b", "c", "d", "e", "f" };
310 auto challenges = transcript->template get_challenges<FF>(challenge_labels);
311 // check they are not 0
312 for (size_t i = 0; i < challenges.size(); ++i) {
313 ASSERT_NE(challenges[i], 0) << "Challenge " << i << " is 0";
314 }
315 constexpr uint32_t random_val{ 17 }; // arbitrary
316 transcript->send_to_verifier("random val", random_val);
317 // test more challenges
318 challenge_labels = { "a", "b", "c" };
319 challenges = transcript->template get_challenges<FF>(challenge_labels);
320 ASSERT_NE(challenges[0], 0) << "Challenge a is 0";
321 ASSERT_NE(challenges[1], 0) << "Challenge b is 0";
322 ASSERT_NE(challenges[2], 0) << "Challenge c is 0";
323}
324
326{
327 using Flavor = TypeParam;
328 using FF = Flavor::FF;
329 using Commitment = Flavor::Commitment;
330 // Construct a simple circuit of size n = 8 (i.e. the minimum circuit size)
331 auto builder = typename TestFixture::Builder();
332 TestFixture::generate_test_circuit(builder);
333
334 // Automatically generate a transcript manifest by constructing a proof
336 auto verification_key = std::make_shared<typename TestFixture::VerificationKey>(prover_instance->get_precomputed());
337 auto vk_and_hash = std::make_shared<typename TypeParam::VKAndHash>(verification_key);
338 typename TestFixture::Prover prover(prover_instance, verification_key);
339 auto proof = prover.construct_proof();
340 typename TestFixture::Verifier verifier(vk_and_hash);
341 EXPECT_TRUE(verifier.verify_proof(proof).result);
342
343 const size_t virtual_log_n = Flavor::USE_PADDING ? Flavor::VIRTUAL_LOG_N : prover_instance->log_dyadic_size();
344
345 // Use StructuredProof test utility to deserialize/serialize proof data
346 StructuredProof<Flavor> proof_structure;
347
348 // try deserializing and serializing with no changes and check proof is still valid
349 proof_structure.deserialize(
350 prover.get_transcript()->test_get_proof_data(), verification_key->num_public_inputs, virtual_log_n);
351 proof_structure.serialize(prover.get_transcript()->test_get_proof_data(), virtual_log_n);
352
353 proof = TestFixture::export_serialized_proof(prover, prover_instance->num_public_inputs(), virtual_log_n);
354 // we have changed nothing so proof is still valid
355 typename TestFixture::Verifier verifier2(vk_and_hash);
356 EXPECT_TRUE(verifier2.verify_proof(proof).result);
357
358 Commitment one_group_val = Commitment::one();
359 FF rand_val = FF::random_element();
360 proof_structure.z_perm_comm = one_group_val * rand_val; // choose random object to modify
361 proof = TestFixture::export_serialized_proof(prover, prover_instance->num_public_inputs(), virtual_log_n);
362 // we have not serialized it back to the proof so it should still be fine
363 typename TestFixture::Verifier verifier3(vk_and_hash);
364 EXPECT_TRUE(verifier3.verify_proof(proof).result);
365
366 proof_structure.serialize(prover.get_transcript()->test_get_proof_data(), virtual_log_n);
367 proof = TestFixture::export_serialized_proof(prover, prover_instance->num_public_inputs(), virtual_log_n);
368 // the proof is now wrong after serializing it
369 typename TestFixture::Verifier verifier4(vk_and_hash);
370 EXPECT_FALSE(verifier4.verify_proof(proof).result);
371
372 proof_structure.deserialize(
373 prover.get_transcript()->test_get_proof_data(), verification_key->num_public_inputs, virtual_log_n);
374 EXPECT_EQ(static_cast<Commitment>(proof_structure.z_perm_comm), one_group_val * rand_val);
375}
typename Flavor::Transcript::Proof Proof
TranscriptManifest construct_honk_manifest(const size_t &log_n)
Construct a manifest for a Honk proof (Ultra or Mega)
void generate_test_circuit(Builder &builder)
Proof export_serialized_proof(Prover &prover, const size_t num_public_inputs, const size_t log_n)
Flavor::Commitment Commitment
Manages the data that is propagated on the public inputs of an application/function circuit.
static constexpr size_t PUBLIC_INPUTS_SIZE
static void add_default(Builder &builder)
Add default IO values to a circuit builder (for native tests)
static constexpr bool HasZK
typename Curve::ScalarField FF
static constexpr size_t NUM_ALL_ENTITIES
ECCVMCircuitBuilder CircuitBuilder
typename G1::affine_element Commitment
FixedVKAndHash_< PrecomputedEntities< Commitment >, BF, ECCVMHardcodedVKAndHash > VerificationKey
The verification key stores commitments to the precomputed polynomials used by the verifier.
static constexpr size_t BATCHED_RELATION_PARTIAL_LENGTH
static constexpr bool USE_PADDING
Simple verification key class for fixed-size circuits (ECCVM, Translator, AVM).
Definition flavor.hpp:104
Hiding-kernel-only Mega variant: runs with ZK Sumcheck and a reduced relation set.
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.
const std::shared_ptr< Transcript > & get_transcript() const
Proof export_proof()
Export the complete proof, including IPA proof for rollup circuits.
Child class of UltraFlavor that runs with ZK Sumcheck.
#define info(...)
Definition log.hpp:93
AluTraceBuilder builder
Definition alu.test.cpp:124
FF a
std::string label
Base class templates shared across Honk flavors.
testing::Types< UltraFlavor, UltraKeccakFlavor, MegaFlavor > FlavorTypes
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
TYPED_TEST_SUITE(CommitmentKeyTest, Curves)
TYPED_TEST(CommitmentKeyTest, CommitToZeroPoly)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::string to_string(bb::avm2::ValueTag tag)
static constexpr size_t LENGTH_WITHOUT_PUB_INPUTS(size_t log_n)
Test utility for deserializing/serializing proof data into typed structures.
static field random_element(numeric::RNG *engine=nullptr) noexcept