Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
eccvm_recursive_verifier.test.cpp
Go to the documentation of this file.
10
11#include <gtest/gtest.h>
12
13namespace {
15}
16namespace bb {
17class ECCVMRecursiveTests : public ::testing::Test {
18 public:
29
32
34
40
43
50 static InnerBuilder generate_circuit(numeric::RNG* engine = nullptr, const size_t num_iterations = 1)
51 {
52 using Curve = curve::BN254;
53 using G1 = Curve::Element;
54 using Fr = Curve::ScalarField;
56
58 G1 a = G1::random_element(engine);
59 G1 b = G1::random_element(engine);
60 G1 c = G1::random_element(engine);
63 for (size_t idx = 0; idx < num_iterations; idx++) {
64 op_queue->add_accumulate(a);
65 op_queue->mul_accumulate(a, x);
66 op_queue->mul_accumulate(b, x);
67 op_queue->mul_accumulate(b, y);
68 op_queue->add_accumulate(a);
69 op_queue->mul_accumulate(b, x);
70 op_queue->eq_and_reset();
71 op_queue->add_accumulate(c);
72 op_queue->mul_accumulate(a, x);
73 op_queue->mul_accumulate(b, x);
74 op_queue->eq_and_reset();
75 op_queue->mul_accumulate(a, x);
76 op_queue->mul_accumulate(b, x);
77 op_queue->mul_accumulate(c, x);
78 op_queue->merge();
79 }
80 // Set hiding op for ECCVM ZK (required before ECCVMCircuitBuilder construction)
81 op_queue->append_hiding_op(Fq::random_element(engine), Fq::random_element(engine));
82 InnerBuilder builder{ op_queue };
83 return builder;
84 }
85
87 {
89 std::shared_ptr<Transcript> prover_transcript = std::make_shared<Transcript>();
90 InnerProver prover(builder, prover_transcript);
91 auto [proof, opening_claim] = prover.construct_proof();
92
93 // Compute IPA proof
94 auto ipa_transcript = std::make_shared<Transcript>();
95 PCS::compute_opening_proof(prover.key->commitment_key, opening_claim, ipa_transcript);
96 HonkProof ipa_proof = ipa_transcript->export_proof();
97
98 auto verification_key = std::make_shared<InnerFlavor::VerificationKey>();
99
100 info("ECCVM Recursive Verifier");
101 OuterBuilder outer_circuit;
102 auto stdlib_proof = stdlib::Proof<OuterBuilder>(outer_circuit, proof);
104 RecursiveVerifier verifier{ stdlib_verifier_transcript, stdlib_proof };
105 verifier.get_transcript()->enable_manifest();
106 [[maybe_unused]] auto recursive_result = verifier.reduce_to_ipa_opening();
108
109 info("Recursive Verifier: num gates = ", outer_circuit.get_num_finalized_gates_inefficient());
110
111 // Check for a failure flag in the recursive verifier circuit
112 EXPECT_EQ(outer_circuit.failed(), false) << outer_circuit.err();
113
114 bool result = CircuitChecker::check(outer_circuit);
115 EXPECT_TRUE(result);
116
117 std::shared_ptr<Transcript> verifier_transcript = std::make_shared<Transcript>();
118 InnerVerifier native_verifier(verifier_transcript, proof);
119 verifier_transcript->enable_manifest();
120 auto native_result = native_verifier.reduce_to_ipa_opening();
121
122 // Verify IPA
123 auto ipa_verify_transcript = std::make_shared<Transcript>();
124 ipa_verify_transcript->load_proof(ipa_proof);
126 bool ipa_verified = IPA<curve::Grumpkin>::reduce_verify(ipa_vk, native_result.ipa_claim, ipa_verify_transcript);
127 EXPECT_TRUE(ipa_verified && native_result.reduction_succeeded);
128 auto recursive_manifest = verifier.get_transcript()->get_manifest();
129 auto native_manifest = native_verifier.get_transcript()->get_manifest();
130
131 ASSERT_GT(recursive_manifest.size(), 0);
132 for (size_t i = 0; i < recursive_manifest.size(); ++i) {
133 EXPECT_EQ(recursive_manifest[i], native_manifest[i])
134 << "Recursive Verifier/Verifier manifest discrepency in round " << i;
135 }
136
137 // Ensure verification key commitments are the same
138 for (auto [vk_poly, native_vk_poly] :
139 zip_view(verifier.get_verification_key()->get_all(), verification_key->get_all())) {
140 EXPECT_EQ(vk_poly.get_value(), native_vk_poly);
141 }
142
143 // Construct a full proof from the recursive verifier circuit
144 {
145 auto prover_instance = std::make_shared<OuterProverInstance>(outer_circuit);
146 auto verification_key = std::make_shared<OuterFlavor::VerificationKey>(prover_instance->get_precomputed());
147 auto vk_and_hash = std::make_shared<OuterFlavor::VKAndHash>(verification_key);
148 OuterProver prover(prover_instance, verification_key);
149 OuterVerifier verifier(vk_and_hash);
150 auto proof = prover.construct_proof();
151 bool verified = verifier.verify_proof(proof).result;
152
153 ASSERT_TRUE(verified);
154 }
155
156 // Check that the size of the recursive verifier is consistent with historical expectation
158 << "Ultra-arithmetized ECCVM Recursive verifier gate count changed! Update this value if you are sure this "
159 "is expected.";
160 }
161
163 {
165 builder.op_queue->add_erroneous_equality_op_for_testing();
166 builder.op_queue->merge();
167 std::shared_ptr<Transcript> prover_transcript = std::make_shared<Transcript>();
168 InnerProver prover(builder, prover_transcript);
169 auto [proof, opening_claim] = prover.construct_proof();
170
171 // Compute IPA proof
172 auto ipa_transcript = std::make_shared<Transcript>();
173 PCS::compute_opening_proof(prover.key->commitment_key, opening_claim, ipa_transcript);
174 HonkProof ipa_proof = ipa_transcript->export_proof();
175
176 auto verification_key = std::make_shared<InnerFlavor::VerificationKey>();
177
178 OuterBuilder outer_circuit;
179 auto stdlib_proof = stdlib::Proof<OuterBuilder>(outer_circuit, proof);
180
182 RecursiveVerifier verifier{ stdlib_verifier_transcript, stdlib_proof };
183 [[maybe_unused]] auto output = verifier.reduce_to_ipa_opening();
185 info("Recursive Verifier: estimated num finalized gates = ",
187
188 // Check for a failure flag in the recursive verifier circuit
189 EXPECT_FALSE(CircuitChecker::check(outer_circuit));
190 }
191
198 {
200 std::shared_ptr<Transcript> prover_transcript = std::make_shared<Transcript>();
201 InnerProver prover(builder, prover_transcript);
202 auto [proof, opening_claim] = prover.construct_proof();
203
204 ASSERT_EQ(proof.size(), InnerFlavor::PROOF_LENGTH);
205
206 StructuredProof<InnerFlavor> structured_proof;
207 auto proof_data = prover.transcript->test_get_proof_data();
208 structured_proof.deserialize(proof_data, /*num_public_inputs=*/0, CONST_ECCVM_LOG_N);
209 structured_proof.serialize(proof_data, CONST_ECCVM_LOG_N);
210
211 auto original_data = prover.transcript->test_get_proof_data();
212 ASSERT_EQ(proof_data.size(), original_data.size());
213 EXPECT_EQ(proof_data, original_data);
214 }
215
216 enum class TamperType {
217 MODIFY_SUMCHECK_UNIVARIATE, // Tests committed sumcheck first-round sum constraint (circuit FAIL)
218 MODIFY_SUMCHECK_EVAL, // Tests Gemini consistency constraint (circuit FAIL)
219 MODIFY_IPA_CLAIM, // Tests IPA opening (circuit PASS, IPA FAIL)
220 MODIFY_TRANSLATION_EVAL, // Tests translation masking consistency constraint (circuit FAIL)
221 MODIFY_LIBRA_EVAL, // Tests Libra SmallSubgroupIPA consistency constraint (circuit FAIL)
222 END
223 };
224
225 static void tamper_eccvm_proof(InnerProver& prover,
226 typename InnerFlavor::Transcript::Proof& proof,
227 TamperType tamper_type)
228 {
229 using FF = InnerFF;
230 static constexpr size_t FIRST_WITNESS_INDEX = InnerFlavor::NUM_PRECOMPUTED_ENTITIES;
231
232 StructuredProof<InnerFlavor> structured_proof;
233 structured_proof.deserialize(
234 prover.transcript->test_get_proof_data(), /*num_public_inputs=*/0, CONST_ECCVM_LOG_N);
235
236 switch (tamper_type) {
238 // Committed sumcheck: break the first-round sum by modifying eval_0 without compensating eval_1.
239 // Preserving the sum would only break IPA opening (external), not any in-circuit constraint.
240 structured_proof.sumcheck_round_eval_0s[0] += FF::random_element();
241 break;
243 structured_proof.sumcheck_evaluations[FIRST_WITNESS_INDEX] = FF::random_element();
244 break;
246 // Modify the final Shplonk Q commitment — bypasses circuit constraints but corrupts IPA opening claim.
247 structured_proof.final_shplonk_q_comm = structured_proof.final_shplonk_q_comm * FF(2);
248 break;
250 structured_proof.translation_op_eval = FF::random_element();
251 break;
253 structured_proof.libra_quotient_eval = FF::random_element();
254 break;
255 case TamperType::END:
256 break;
257 }
258
259 structured_proof.serialize(prover.transcript->test_get_proof_data(), CONST_ECCVM_LOG_N);
260 prover.transcript->test_set_proof_parsing_state(0, InnerFlavor::PROOF_LENGTH);
261 proof = prover.export_proof();
262 }
263
265 {
266 for (size_t idx = 0; idx < static_cast<size_t>(TamperType::END); idx++) {
267 TamperType tamper_type = static_cast<TamperType>(idx);
268
270 std::shared_ptr<Transcript> prover_transcript = std::make_shared<Transcript>();
271 InnerProver prover(builder, prover_transcript);
272 auto [proof, opening_claim] = prover.construct_proof();
273
274 // Compute IPA proof from the genuine opening claim (needed for MODIFY_IPA_CLAIM case)
275 auto ipa_transcript = std::make_shared<Transcript>();
276 PCS::compute_opening_proof(prover.key->commitment_key, opening_claim, ipa_transcript);
277 HonkProof ipa_proof = ipa_transcript->export_proof();
278
279 // Tamper with the proof
280 tamper_eccvm_proof(prover, proof, tamper_type);
281
282 OuterBuilder outer_circuit;
283 auto stdlib_proof = stdlib::Proof<OuterBuilder>(outer_circuit, proof);
285 RecursiveVerifier verifier{ stdlib_verifier_transcript, stdlib_proof };
286 auto recursive_result = verifier.reduce_to_ipa_opening();
288
289 if (tamper_type == TamperType::MODIFY_IPA_CLAIM) {
290 // Modifying the final Shplonk Q bypasses circuit constraints but causes IPA failure
291 EXPECT_TRUE(CircuitChecker::check(outer_circuit));
292
293 // Verify IPA fails with the tampered opening claim
294 VerifierCommitmentKey<InnerFlavor::Curve> native_pcs_vk(1UL << CONST_ECCVM_LOG_N);
296 &outer_circuit, 1UL << CONST_ECCVM_LOG_N, native_pcs_vk);
297 auto stdlib_ipa_proof = stdlib::Proof<OuterBuilder>(outer_circuit, ipa_proof);
298 auto ipa_verify_transcript = std::make_shared<StdlibTranscript>(stdlib_ipa_proof);
300 stdlib_pcs_vkey, recursive_result.ipa_claim, ipa_verify_transcript));
301 } else {
302 // All other tamper types should cause a circuit constraint violation
303 EXPECT_FALSE(CircuitChecker::check(outer_circuit)) << "Expected circuit failure for TamperType " << idx;
304 }
305 }
306 }
307
309 {
310
311 // Retrieves the trace blocks (each consisting of a specific gate) from the recursive verifier circuit
312 auto get_blocks = [](size_t inner_size)
314 auto inner_circuit = generate_circuit(&engine, inner_size);
315 std::shared_ptr<Transcript> prover_transcript = std::make_shared<Transcript>();
316 InnerProver inner_prover(inner_circuit, prover_transcript);
317
318 auto [proof, opening_claim] = inner_prover.construct_proof();
319
320 // Compute IPA proof
321 auto ipa_transcript = std::make_shared<Transcript>();
322 PCS::compute_opening_proof(inner_prover.key->commitment_key, opening_claim, ipa_transcript);
323 HonkProof ipa_proof = ipa_transcript->export_proof();
324
325 // Create a recursive verification circuit for the proof of the inner circuit
326 OuterBuilder outer_circuit;
327 auto stdlib_proof = stdlib::Proof<OuterBuilder>(outer_circuit, proof);
328
330 RecursiveVerifier verifier{ stdlib_verifier_transcript, stdlib_proof };
331
332 [[maybe_unused]] auto recursive_opening_claim = verifier.reduce_to_ipa_opening();
334
335 auto outer_proving_key = std::make_shared<OuterProverInstance>(outer_circuit);
336 auto outer_verification_key =
337 std::make_shared<OuterFlavor::VerificationKey>(outer_proving_key->get_precomputed());
338
339 return { outer_circuit.blocks, outer_verification_key };
340 };
341
342 auto [blocks_20, verification_key_20] = get_blocks(20);
343 auto [blocks_40, verification_key_40] = get_blocks(40);
344
345 compare_ultra_blocks_and_verification_keys<OuterFlavor>({ blocks_20, blocks_40 },
346 { verification_key_20, verification_key_40 });
347 };
348};
349
354
355TEST_F(ECCVMRecursiveTests, SingleRecursiveVerificationFailure)
356{
358};
359
364
369
374} // namespace bb
bb::field< bb::Bn254FrParams > FF
Definition field.cpp:24
Common transcript class for both parties. Stores the data for the current round, as well as the manif...
const std::string & err() const
The proving key is responsible for storing the polynomials used by the prover.
static constexpr size_t ECCVM_FIXED_SIZE
typename Curve::ScalarField FF
ECCVMCircuitBuilder CircuitBuilder
typename G1::affine_element Commitment
typename Curve::BaseField BF
static constexpr size_t PROOF_LENGTH
static constexpr size_t NUM_PRECOMPUTED_ENTITIES
FixedVKAndHash_< PrecomputedEntities< Commitment >, BF, ECCVMHardcodedVKAndHash > VerificationKey
The verification key stores commitments to the precomputed polynomials used by the verifier.
BaseTranscript< Codec, HashFunction > Transcript
std::shared_ptr< Transcript > transcript
std::pair< Proof, OpeningClaim > construct_proof()
std::shared_ptr< ProvingKey > key
StdlibTranscript< CircuitBuilder > Transcript
static void tamper_eccvm_proof(InnerProver &prover, typename InnerFlavor::Transcript::Proof &proof, TamperType tamper_type)
static InnerBuilder generate_circuit(numeric::RNG *engine=nullptr, const size_t num_iterations=1)
Adds operations in BN254 to the op_queue and then constructs and ECCVM circuit from the op_queue.
static void test_structured_proof_round_trip()
Verify that StructuredProof<ECCVMFlavor> can round-trip serialize/deserialize a proof.
std::conditional_t< IsMegaBuilder< OuterBuilder >, MegaFlavor, UltraFlavor > OuterFlavor
Unified ECCVM verifier class for both native and recursive verification.
std::shared_ptr< Transcript > get_transcript() const
ReductionResult reduce_to_ipa_opening()
Reduce the ECCVM proof to an IPA opening claim.
Simple verification key class for fixed-size circuits (ECCVM, Translator, AVM).
Definition flavor.hpp:101
IPA (inner product argument) commitment scheme class.
Definition ipa.hpp:86
Contains all the information required by a Honk prover to create a proof, constructed from a finalize...
static bool check(const Builder &circuit)
Check the witness satisifies the circuit.
size_t get_num_finalized_gates() const override
Get the number of gates in a finalized circuit.
size_t get_num_finalized_gates_inefficient(bool ensure_nonzero=true) const
Get the number of gates in the finalized version of the circuit.
Output verify_proof(const Proof &proof)
Perform ultra verification.
Representation of the Grumpkin Verifier Commitment Key inside a bn254 circuit.
typename Group::element Element
Definition grumpkin.hpp:64
A simple wrapper around a vector of stdlib field elements representing a proof.
Definition proof.hpp:19
static void add_default(Builder &builder)
Add default public inputs when they are not present.
#define info(...)
Definition log.hpp:93
AluTraceBuilder builder
Definition alu.test.cpp:124
FF a
FF b
numeric::RNG & engine
constexpr size_t ECCVM_RECURSIVE_VERIFIER_GATE_COUNT
RNG & get_debug_randomness(bool reset, std::uint_fast64_t seed)
Definition engine.cpp:217
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
std::vector< fr > HonkProof
Definition proof.hpp:15
ECCVMVerifier_< ECCVMRecursiveFlavor > ECCVMRecursiveVerifier
TEST_F(IPATest, ChallengesAreZero)
Definition ipa.test.cpp:155
ECCVMVerifier_< ECCVMFlavor > ECCVMVerifier
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
Curve::AffineElement G1
Test utility for deserializing/serializing proof data into typed structures.
static field random_element(numeric::RNG *engine=nullptr) noexcept