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.
15
16#include <gtest/gtest.h>
17
18using namespace bb;
19
20#ifdef STARKNET_GARAGA_FLAVORS
21using FlavorTypes = ::testing::Types<UltraFlavor,
23 UltraStarknetFlavor,
24 UltraStarknetZKFlavor,
29#else
31 ::testing::Types<UltraFlavor, UltraKeccakFlavor, UltraZKFlavor, UltraKeccakZKFlavor, MegaFlavor, MegaZKFlavor>;
32#endif
33
34template <typename Flavor> class HonkTranscriptTests : public ::testing::Test {
35 public:
37
39 using FF = Flavor::FF;
44 using IO = DefaultIO;
47
60 {
61 TranscriptManifest manifest_expected;
62
63 const size_t virtual_log_n = Flavor::USE_PADDING ? Flavor::VIRTUAL_LOG_N : log_n;
64
65 size_t MAX_PARTIAL_RELATION_LENGTH = Flavor::BATCHED_RELATION_PARTIAL_LENGTH;
66 // Size of types is number of bb::frs needed to represent the types
67 // UltraKeccak uses uint256_t for commitments and frs, so we need to handle that differently.
68 size_t data_types_per_Frs = [] {
69 if constexpr (IsKeccakFlavor<Flavor>) {
70 return U256Codec::calc_num_fields<FF>();
71 } else {
72 return FrCodec::calc_num_fields<FF>();
73 }
74 }();
75 size_t data_types_per_G = [] {
76 if constexpr (IsKeccakFlavor<Flavor>) {
77 return U256Codec::calc_num_fields<Commitment>();
78 } else {
79 return FrCodec::calc_num_fields<Commitment>();
80 }
81 }();
82 size_t frs_per_uni = MAX_PARTIAL_RELATION_LENGTH * data_types_per_Frs;
83 size_t frs_per_evals = (Flavor::NUM_ALL_ENTITIES)*data_types_per_Frs;
84
85 size_t round = 0;
86 manifest_expected.add_entry(round, "vk_hash", data_types_per_Frs);
87
88 manifest_expected.add_entry(round, "public_input_0", data_types_per_Frs);
89 constexpr size_t PUBLIC_INPUTS_SIZE = IO::PUBLIC_INPUTS_SIZE;
90 for (size_t i = 0; i < PUBLIC_INPUTS_SIZE; i++) {
91 manifest_expected.add_entry(round, "public_input_" + std::to_string(1 + i), data_types_per_Frs);
92 }
93
94 // For ZK flavors: Gemini masking polynomial commitment is sent at end of oink
95 if constexpr (Flavor::HasZK) {
96 manifest_expected.add_entry(round, "Gemini:masking_poly_comm", data_types_per_G);
97 }
98 manifest_expected.add_entry(round, "W_L", data_types_per_G);
99 manifest_expected.add_entry(round, "W_R", data_types_per_G);
100 manifest_expected.add_entry(round, "W_O", data_types_per_G);
101
102 // Mega-specific witness commitments: ECC op wires and databus polynomials
103 if constexpr (IsMegaFlavor<Flavor>) {
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 manifest_expected.add_entry(round, "CALLDATA", data_types_per_G);
109 manifest_expected.add_entry(round, "CALLDATA_READ_COUNTS", data_types_per_G);
110 manifest_expected.add_entry(round, "CALLDATA_READ_TAGS", data_types_per_G);
111 manifest_expected.add_entry(round, "SECONDARY_CALLDATA", data_types_per_G);
112 manifest_expected.add_entry(round, "SECONDARY_CALLDATA_READ_COUNTS", data_types_per_G);
113 manifest_expected.add_entry(round, "SECONDARY_CALLDATA_READ_TAGS", data_types_per_G);
114 manifest_expected.add_entry(round, "RETURN_DATA", data_types_per_G);
115 manifest_expected.add_entry(round, "RETURN_DATA_READ_COUNTS", data_types_per_G);
116 manifest_expected.add_entry(round, "RETURN_DATA_READ_TAGS", data_types_per_G);
117 }
118
119 manifest_expected.add_challenge(round, "eta");
120
121 round++;
122 manifest_expected.add_entry(round, "LOOKUP_READ_COUNTS", data_types_per_G);
123 manifest_expected.add_entry(round, "LOOKUP_READ_TAGS", data_types_per_G);
124 manifest_expected.add_entry(round, "W_4", data_types_per_G);
125 manifest_expected.add_challenge(round, std::array{ "beta", "gamma" });
126
127 round++;
128 manifest_expected.add_entry(round, "LOOKUP_INVERSES", data_types_per_G);
129 // Mega-specific databus inverse commitments
130 if constexpr (IsMegaFlavor<Flavor>) {
131 manifest_expected.add_entry(round, "CALLDATA_INVERSES", data_types_per_G);
132 manifest_expected.add_entry(round, "SECONDARY_CALLDATA_INVERSES", data_types_per_G);
133 manifest_expected.add_entry(round, "RETURN_DATA_INVERSES", data_types_per_G);
134 }
135 manifest_expected.add_entry(round, "Z_PERM", data_types_per_G);
136
137 manifest_expected.add_challenge(round, "alpha");
138 manifest_expected.add_challenge(round, "Sumcheck:gate_challenge");
139 round++;
140
141 if constexpr (Flavor::HasZK) {
142 manifest_expected.add_entry(round, "Libra:concatenation_commitment", data_types_per_G);
143 manifest_expected.add_entry(round, "Libra:Sum", data_types_per_Frs);
144 manifest_expected.add_challenge(round, "Libra:Challenge");
145 round++;
146 }
147
148 for (size_t i = 0; i < virtual_log_n; ++i) {
149 std::string idx = std::to_string(i);
150 manifest_expected.add_entry(round, "Sumcheck:univariate_" + idx, frs_per_uni);
151 std::string label = "Sumcheck:u_" + idx;
152 manifest_expected.add_challenge(round, label);
153 round++;
154 }
155
156 manifest_expected.add_entry(round, "Sumcheck:evaluations", frs_per_evals);
157
158 if constexpr (Flavor::HasZK) {
159 manifest_expected.add_entry(round, "Libra:claimed_evaluation", data_types_per_Frs);
160 manifest_expected.add_entry(round, "Libra:grand_sum_commitment", data_types_per_G);
161 manifest_expected.add_entry(round, "Libra:quotient_commitment", data_types_per_G);
162 }
163
164 manifest_expected.add_challenge(round, "rho");
165
166 round++;
167 for (size_t i = 1; i < virtual_log_n; ++i) {
168 std::string idx = std::to_string(i);
169 manifest_expected.add_entry(round, "Gemini:FOLD_" + idx, data_types_per_G);
170 }
171 manifest_expected.add_challenge(round, "Gemini:r");
172 round++;
173 for (size_t i = 1; i <= virtual_log_n; ++i) {
174 std::string idx = std::to_string(i);
175 manifest_expected.add_entry(round, "Gemini:a_" + idx, data_types_per_Frs);
176 }
177
178 if constexpr (Flavor::HasZK) {
179 manifest_expected.add_entry(round, "Libra:concatenation_eval", data_types_per_Frs);
180 manifest_expected.add_entry(round, "Libra:shifted_grand_sum_eval", data_types_per_Frs);
181 manifest_expected.add_entry(round, "Libra:grand_sum_eval", data_types_per_Frs);
182 manifest_expected.add_entry(round, "Libra:quotient_eval", data_types_per_Frs);
183 }
184
185 manifest_expected.add_challenge(round, "Shplonk:nu");
186 round++;
187 manifest_expected.add_entry(round, "Shplonk:Q", data_types_per_G);
188 manifest_expected.add_challenge(round, "Shplonk:z");
189
190 round++;
191 manifest_expected.add_entry(round, "KZG:W", data_types_per_G);
192 manifest_expected.add_challenge(round, "KZG:masking_challenge");
193
194 return manifest_expected;
195 }
196
198 {
199 FF a = 1;
200 builder.add_variable(a);
201 builder.add_public_variable(a);
203 }
204
205 Proof export_serialized_proof(Prover& prover, const size_t num_public_inputs, const size_t log_n)
206 {
207 // reset internal variables needed for exporting the proof
208 // Note: this excludes IPA proof length since export_proof appends it separately
209 size_t proof_length = ProofLength::Honk<Flavor>::LENGTH_WITHOUT_PUB_INPUTS(log_n) + num_public_inputs;
210 prover.get_transcript()->test_set_proof_parsing_state(0, proof_length);
211 return prover.export_proof();
212 }
213};
214
216
221TYPED_TEST(HonkTranscriptTests, ProverManifestConsistency)
222{
223 // Construct a simple circuit of size n = 8 (i.e. the minimum circuit size)
224 auto builder = typename TestFixture::Builder();
225 TestFixture::generate_test_circuit(builder);
226
227 // Automatically generate a transcript manifest by constructing a proof
229 auto verification_key = std::make_shared<typename TestFixture::VerificationKey>(prover_instance->get_precomputed());
230 typename TestFixture::Prover prover(prover_instance, verification_key);
231 prover.get_transcript()->enable_manifest();
232 auto proof = prover.construct_proof();
233
234 // Check that the prover generated manifest agrees with the manifest hard coded in this suite
235 auto manifest_expected = TestFixture::construct_honk_manifest(prover.log_dyadic_size());
236 auto prover_manifest = prover.get_transcript()->get_manifest();
237 // Note: a manifest can be printed using manifest.print()
238 manifest_expected.print();
239 prover_manifest.print();
240 ASSERT_GT(manifest_expected.size(), 0);
241 for (size_t round = 0; round < manifest_expected.size(); ++round) {
242 if (prover_manifest[round] != manifest_expected[round]) {
243 info("Prover manifest discrepency in round ", round);
244 info("Prover manifest:");
245 prover_manifest[round].print();
246 info("Expected manifest:");
247 manifest_expected[round].print();
248 FAIL();
249 }
250 }
251}
252
257TYPED_TEST(HonkTranscriptTests, VerifierManifestConsistency)
258{
259 // Construct a simple circuit of size n = 8 (i.e. the minimum circuit size)
260 auto builder = typename TestFixture::Builder();
261 TestFixture::generate_test_circuit(builder);
262
263 // Automatically generate a transcript manifest in the prover by constructing a proof
265 auto verification_key = std::make_shared<typename TestFixture::VerificationKey>(prover_instance->get_precomputed());
266 auto vk_and_hash = std::make_shared<typename TypeParam::VKAndHash>(verification_key);
267 typename TestFixture::Prover prover(prover_instance, verification_key);
268 prover.get_transcript()->enable_manifest();
269 auto proof = prover.construct_proof();
270
271 // Automatically generate a transcript manifest in the verifier by verifying a proof
272 auto verifier_transcript = std::make_shared<typename TypeParam::Transcript>();
273 verifier_transcript->enable_manifest();
274 typename TestFixture::Verifier verifier(vk_and_hash, verifier_transcript);
275 [[maybe_unused]] auto _ = verifier.verify_proof(proof);
276
277 // Check consistency between the manifests generated by the prover and verifier
278 auto prover_manifest = prover.get_transcript()->get_manifest();
279 auto verifier_manifest = verifier.get_transcript()->get_manifest();
280
281 // Note: a manifest can be printed using manifest.print()
282 ASSERT_GT(prover_manifest.size(), 0);
283 for (size_t round = 0; round < prover_manifest.size(); ++round) {
284 ASSERT_EQ(prover_manifest[round], verifier_manifest[round])
285 << "Prover/Verifier manifest discrepency in round " << round;
286 }
287}
288
293TYPED_TEST(HonkTranscriptTests, ChallengeGenerationTest)
294{
295 using Flavor = TypeParam;
296 using FF = Flavor::FF;
297 // initialized with random value sent to verifier
298 auto transcript = TypeParam::Transcript::test_prover_init_empty();
299 // test a bunch of challenges
300 std::vector<std::string> challenge_labels{ "a", "b", "c", "d", "e", "f" };
301 auto challenges = transcript->template get_challenges<FF>(challenge_labels);
302 // check they are not 0
303 for (size_t i = 0; i < challenges.size(); ++i) {
304 ASSERT_NE(challenges[i], 0) << "Challenge " << i << " is 0";
305 }
306 constexpr uint32_t random_val{ 17 }; // arbitrary
307 transcript->send_to_verifier("random val", random_val);
308 // test more challenges
309 challenge_labels = { "a", "b", "c" };
310 challenges = transcript->template get_challenges<FF>(challenge_labels);
311 ASSERT_NE(challenges[0], 0) << "Challenge a is 0";
312 ASSERT_NE(challenges[1], 0) << "Challenge b is 0";
313 ASSERT_NE(challenges[2], 0) << "Challenge c is 0";
314}
315
317{
318 using Flavor = TypeParam;
319 using FF = Flavor::FF;
320 using Commitment = Flavor::Commitment;
321 // Construct a simple circuit of size n = 8 (i.e. the minimum circuit size)
322 auto builder = typename TestFixture::Builder();
323 TestFixture::generate_test_circuit(builder);
324
325 // Automatically generate a transcript manifest by constructing a proof
327 auto verification_key = std::make_shared<typename TestFixture::VerificationKey>(prover_instance->get_precomputed());
328 auto vk_and_hash = std::make_shared<typename TypeParam::VKAndHash>(verification_key);
329 typename TestFixture::Prover prover(prover_instance, verification_key);
330 auto proof = prover.construct_proof();
331 typename TestFixture::Verifier verifier(vk_and_hash);
332 EXPECT_TRUE(verifier.verify_proof(proof).result);
333
334 const size_t virtual_log_n = Flavor::USE_PADDING ? Flavor::VIRTUAL_LOG_N : prover_instance->log_dyadic_size();
335
336 // Use StructuredProof test utility to deserialize/serialize proof data
337 StructuredProof<Flavor> proof_structure;
338
339 // try deserializing and serializing with no changes and check proof is still valid
340 proof_structure.deserialize(
341 prover.get_transcript()->test_get_proof_data(), verification_key->num_public_inputs, virtual_log_n);
342 proof_structure.serialize(prover.get_transcript()->test_get_proof_data(), virtual_log_n);
343
344 proof = TestFixture::export_serialized_proof(prover, prover_instance->num_public_inputs(), virtual_log_n);
345 // we have changed nothing so proof is still valid
346 typename TestFixture::Verifier verifier2(vk_and_hash);
347 EXPECT_TRUE(verifier2.verify_proof(proof).result);
348
349 Commitment one_group_val = Commitment::one();
350 FF rand_val = FF::random_element();
351 proof_structure.z_perm_comm = one_group_val * rand_val; // choose random object to modify
352 proof = TestFixture::export_serialized_proof(prover, prover_instance->num_public_inputs(), virtual_log_n);
353 // we have not serialized it back to the proof so it should still be fine
354 typename TestFixture::Verifier verifier3(vk_and_hash);
355 EXPECT_TRUE(verifier3.verify_proof(proof).result);
356
357 proof_structure.serialize(prover.get_transcript()->test_get_proof_data(), virtual_log_n);
358 proof = TestFixture::export_serialized_proof(prover, prover_instance->num_public_inputs(), virtual_log_n);
359 // the proof is now wrong after serializing it
360 typename TestFixture::Verifier verifier4(vk_and_hash);
361 EXPECT_FALSE(verifier4.verify_proof(proof).result);
362
363 proof_structure.deserialize(
364 prover.get_transcript()->test_get_proof_data(), verification_key->num_public_inputs, virtual_log_n);
365 EXPECT_EQ(static_cast<Commitment>(proof_structure.z_perm_comm), one_group_val * rand_val);
366}
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:101
Child class of MegaFlavor that runs with ZK Sumcheck.
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
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