Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
avm2_recursion_constraint.test.cpp
Go to the documentation of this file.
18
19#include <gtest/gtest.h>
20#include <memory>
21#include <vector>
22
23using namespace acir_format;
24using namespace bb;
25using namespace bb::avm2;
26
28 public:
31
33 using FF = Builder::FF;
34
36 public:
37 enum class Target : uint8_t { None, PublicInputs, Proof };
39 {
41 return targets;
42 };
43 static std::vector<std::string> get_labels()
44 {
45 std::vector<std::string> labels = { "None", "PublicInputs", "Proof" };
46 return labels;
47 };
48 };
49
51 {
52 auto [trace, public_inputs] = avm2::testing::get_minimal_trace_with_pi();
53
54 AvmProver prover;
55 auto proof = prover.prove(std::move(trace));
56
57 const bool verified = prover.verify(proof, public_inputs);
58 EXPECT_TRUE(verified) << "native proof verification failed";
59
60 auto public_inputs_flat = PublicInputs::columns_to_flat(public_inputs.to_columns());
61 public_inputs_flat.resize(AVM_PUBLIC_INPUTS_COLUMNS_COMBINED_LENGTH, FF::zero()); // Pad public inputs
62
63 return { proof, public_inputs_flat };
64 }
65
67
68 static void generate_constraints(AcirConstraint& avm_recursion_constraint, WitnessVector& witness_values)
69 {
70 const auto [proof, public_inputs_flat] = create_avm_data();
71 avm_recursion_constraint = RecursionConstraint{
72 .key = {}, // Unused, the key is hard-coded in the circuit
73 .proof = add_to_witness_and_track_indices(witness_values, proof),
74 .public_inputs = add_to_witness_and_track_indices(witness_values, public_inputs_flat),
75 .key_hash = IS_CONSTANT, // Unused, the key hash is hard-coded in the circuit
76 .proof_type = AVM,
78 };
79 }
80
82 AcirConstraint constraint, WitnessVector witness_values, const InvalidWitness::Target& invalid_witness_target)
83 {
84 switch (invalid_witness_target) {
86 break;
88 // Tamper with the public inputs
89 witness_values[constraint.public_inputs[0]] += FF::one();
90 break;
91 }
93 // Tamper with the proof by changing one of the univariate coefficients
94 witness_values[constraint.proof[FrCodec::calc_num_fields<AvmFlavor::Commitment>() *
96 break;
97 }
98 }
99
100 return { constraint, witness_values };
101 }
102};
103
104class AvmRecursionConstraintTest : public ::testing::Test, public TestClass<AvmRecursionConstraintTestingFunctions> {
105 protected:
107};
108
109TEST_F(AvmRecursionConstraintTest, GenerateVKFromConstraints)
110{
112 GTEST_SKIP() << "Skipping slow test";
113 }
114 // AVM constraints are always proven with UltraRollupVerifier
115 [[maybe_unused]] size_t num_gates = test_vk_independence<UltraFlavor>();
116
117 // TODO(fcarreiro): Re-enable when the VK is fixed.
118 // EXPECT_EQ(num_gates, FINALIZED_GOBLIN_AVM_GATE_COUNT);
119}
120
122{
124 GTEST_SKIP() << "Skipping slow test";
125 }
126 std::vector<std::string> _ = test_tampering();
127}
128
129// TODO(fcarreiro): Re-enable when the VK is fixed.
130TEST_F(AvmRecursionConstraintTest, DISABLED_GateCountAndVKCheck)
131{
133 GTEST_SKIP() << "Skipping slow test";
134 }
136
137 AcirConstraint constraint;
138 WitnessVector witness;
139 Base::generate_constraints(constraint, witness);
140
142
143 AcirProgram program = { acir_format, {} };
144 ProgramMetadata metadata = Base::generate_metadata();
145 metadata.collect_gates_per_opcode = true;
146 auto builder = create_circuit<Builder>(program, metadata);
147
148 EXPECT_EQ(program.constraints.gates_per_opcode.size(), 1);
150
151 auto prover_instance = std::make_shared<ProverInstance>(builder);
152 auto vk = std::make_shared<typename UltraFlavor::VerificationKey>(prover_instance->get_precomputed());
153
154 static constexpr FF EXPECTED_OUTER_VK_HASH =
155 FF("0x28f5195fba68d3a530560eef5c0ade56761c5b51e8c07436a672c57b7b7e7cd8");
156 EXPECT_EQ(vk->hash(), EXPECTED_OUTER_VK_HASH)
157 << "The VK hash of the outer circuit in the Goblinized AVM recursive verifier has changed. If this is "
158 "expected, update the expected value in the test.";
159}
160
161class AvmRecursionInnerCircuitTests : public ::testing::Test {
162 public:
167
168 static constexpr FF EXPECTED_INNER_VK_HASH =
169 FF("0x222aceaf07c4ffd9f4d2c87318ce3008375836c922e095587ed8ce91ae20b705");
170 static constexpr size_t EXPECT_GATE_COUNT = 550463;
171
173
176 const HonkProof& proof,
177 const std::vector<FF>& public_inputs_flat)
178 {
179 std::vector<field_t<Builder>> stdlib_public_inputs_flat;
180 stdlib_public_inputs_flat.reserve(AVM_PUBLIC_INPUTS_COLUMNS_COMBINED_LENGTH);
181 for (const auto public_input : public_inputs_flat) {
182 stdlib_public_inputs_flat.emplace_back(field_t<Builder>::from_witness(&outer_builder, public_input));
183 }
184 stdlib::Proof<Builder> stdlib_proof;
185 stdlib_proof.reserve(AVM_V2_PROOF_LENGTH_IN_FIELDS);
186 for (const auto proof_element : proof) {
187 stdlib_proof.emplace_back(field_t<Builder>::from_witness(&outer_builder, proof_element));
188 }
189
191 PublicInputs::flat_to_columns<field_t<Builder>>(stdlib_public_inputs_flat);
192 auto [mega_proof, goblin_proof, mega_vk] =
194 public_inputs);
195
196 return { stdlib_proof, public_inputs, { mega_proof, goblin_proof, mega_vk } };
197 }
198};
199
200// TODO(fcarreiro): Re-enable when the VK is fixed.
201TEST_F(AvmRecursionInnerCircuitTests, DISABLED_GateCountAndVKCheck)
202{
203 using MegaAvmProverInstance = ProverInstance_<MegaAvmFlavor>;
204 using MegaAvmVerificationKey = MegaAvmFlavor::VerificationKey;
205
207 GTEST_SKIP() << "Skipping slow test";
208 }
209 const auto [proof, public_inputs_flat] = AvmRecursionConstraintTestingFunctions::create_avm_data();
210
211 Builder outer_builder;
212
213 std::vector<field_t<Builder>> stdlib_public_inputs_flat;
214 stdlib_public_inputs_flat.reserve(AVM_PUBLIC_INPUTS_COLUMNS_COMBINED_LENGTH);
215 for (const auto public_input : public_inputs_flat) {
216 stdlib_public_inputs_flat.emplace_back(field_t<Builder>::from_witness(&outer_builder, public_input));
217 }
218 stdlib::Proof<Builder> stdlib_proof;
219 stdlib_proof.reserve(AVM_V2_PROOF_LENGTH_IN_FIELDS);
220 for (const auto proof_element : proof) {
221 stdlib_proof.emplace_back(field_t<Builder>::from_witness(&outer_builder, proof_element));
222 }
223
225 PublicInputs::flat_to_columns<field_t<Builder>>(stdlib_public_inputs_flat);
226
227 MegaCircuitBuilder inner_builder;
229 inner_builder, stdlib_proof, public_inputs);
230
231 auto mega_proving_key = std::make_shared<MegaAvmProverInstance>(inner_builder);
232 auto mega_vk = std::make_shared<MegaAvmVerificationKey>(mega_proving_key->get_precomputed());
233
234 EXPECT_EQ(mega_vk->hash(), EXPECTED_INNER_VK_HASH)
235 << "The VK hash of the inner circuit in the Goblinized AVM recursive verifier has changed. If this is "
236 "expected, update the expected value in the test.";
237 EXPECT_EQ(inner_builder.get_num_finalized_gates_inefficient(), EXPECT_GATE_COUNT);
238}
239
246{
248 GTEST_SKIP() << "Skipping slow test";
249 }
250 const auto [proof, public_inputs_flat] = AvmRecursionConstraintTestingFunctions::create_avm_data();
251
252 {
253 Builder outer_builder;
254 auto [stdlib_proof, public_inputs, inner_prover_output] =
255 create_and_prove_inner_circuit(outer_builder, proof, public_inputs_flat);
256
257 auto mega_proof_tampered = inner_prover_output.mega_proof;
258 mega_proof_tampered[0] += FF::one(); // Tamper with the first public input
259
260 TwoLayerAvmRecursiveVerifier goblin_avm_verifier(outer_builder);
261 TwoLayerAvmRecursiveVerifierOutput output = goblin_avm_verifier.construct_outer_recursive_verification_circuit(
262 stdlib_proof,
263 public_inputs,
264 { mega_proof_tampered, inner_prover_output.goblin_proof, inner_prover_output.mega_vk });
265
266 EXPECT_TRUE(outer_builder.failed());
267 }
268
269 {
270 Builder outer_builder;
271 auto [stdlib_proof, public_inputs, inner_prover_output] =
272 create_and_prove_inner_circuit(outer_builder, proof, public_inputs_flat);
273
274 // Tamper with one of the ECCVM op evaluations. The `op` evaluation is located 3 evaluations before the end of
275 // pre-IPA proof (followed by x_lo_y_hi, x_hi_z_1, y_lo_z_2 evaluations). See also
276 // GoblinAvmRecursiveVerifierTests::tamper_with_eccvm_op_eval for reference
277 static constexpr size_t evals_after_op = 3; // x_lo_y_hi, x_hi_z_1, y_lo_z_2
278 const size_t op_eval_idx = inner_prover_output.goblin_proof.eccvm_proof.size() - evals_after_op;
279
280 auto goblin_proof_tampered = inner_prover_output.goblin_proof;
281 goblin_proof_tampered.eccvm_proof[op_eval_idx] += FF(1);
282
283 TwoLayerAvmRecursiveVerifier goblin_avm_verifier(outer_builder);
284 TwoLayerAvmRecursiveVerifierOutput output = goblin_avm_verifier.construct_outer_recursive_verification_circuit(
285 stdlib_proof,
286 public_inputs,
287 { inner_prover_output.mega_proof, goblin_proof_tampered, inner_prover_output.mega_vk });
288
289 EXPECT_TRUE(outer_builder.failed());
290 }
291
292 {
293 Builder outer_builder;
294 auto [stdlib_proof, public_inputs, inner_prover_output] =
295 create_and_prove_inner_circuit(outer_builder, proof, public_inputs_flat);
296
297 auto mega_vk_tampered = inner_prover_output.mega_vk;
298 mega_vk_tampered->q_m() =
299 mega_vk_tampered->q_m() + MegaAvmFlavor::Commitment::one(); // Tamper with q_m commitment
300
301 TwoLayerAvmRecursiveVerifier goblin_avm_verifier(outer_builder);
302 TwoLayerAvmRecursiveVerifierOutput output = goblin_avm_verifier.construct_outer_recursive_verification_circuit(
303 stdlib_proof,
304 public_inputs,
305 { inner_prover_output.mega_proof, inner_prover_output.goblin_proof, mega_vk_tampered });
306
307 EXPECT_TRUE(outer_builder.failed());
308 }
309}
static std::pair< AvmProver::Proof, std::vector< FF > > create_avm_data()
static std::pair< AcirConstraint, WitnessVector > invalidate_witness(AcirConstraint constraint, WitnessVector witness_values, const InvalidWitness::Target &invalid_witness_target)
static void generate_constraints(AcirConstraint &avm_recursion_constraint, WitnessVector &witness_values)
static std::tuple< stdlib::Proof< Builder >, std::vector< std::vector< field_t< Builder > > >, InnerProverOutput > create_and_prove_inner_circuit(Builder &outer_builder, const HonkProof &proof, const std::vector< FF > &public_inputs_flat)
TwoLayerAvmRecursiveVerifier::InnerProverOutput InnerProverOutput
NativeVerificationKey_< PrecomputedEntities< Commitment >, Codec, HashFunction, CommitmentKey > VerificationKey
Contains all the information required by a Honk prover to create a proof, constructed from a finalize...
typename ExecutionTrace::FF FF
static constexpr size_t NUM_WITNESS_ENTITIES
Definition flavor.hpp:63
bool verify(const Proof &proof, const PublicInputs &pi)
Proof prove(tracegen::TraceContainer &&trace)
Recursive verifier of AVM2 proofs that utilizes the Goblin mechanism for efficient EC operations.
static void construct_inner_recursive_verification_circuit(MegaCircuitBuilder &inner_builder, const stdlib::Proof< UltraCircuitBuilder > &stdlib_proof, const std::vector< std::vector< UltraFF > > &public_inputs)
Construct the inner recursive verification circuit for the AVM2 recursive verifier.
static InnerProverOutput construct_and_prove_inner_recursive_verification_circuit(const stdlib::Proof< UltraCircuitBuilder > &stdlib_proof, const std::vector< std::vector< UltraFF > > &public_inputs)
Construct and prove the inner Mega-arithmetized AVM recursive verifier circuit.
TwoLayerAvmRecursiveVerifierOutput construct_outer_recursive_verification_circuit(const stdlib::Proof< UltraCircuitBuilder > &stdlib_proof, const std::vector< std::vector< UltraFF > > &public_inputs, const InnerProverOutput &inner_output) const
Construct the outer circuit which recursively verifies a Mega proof and a Goblin proof.
A simple wrapper around a vector of stdlib field elements representing a proof.
Definition proof.hpp:20
AluTraceBuilder builder
Definition alu.test.cpp:124
TestTraceContainer trace
AcirFormat constraint_to_acir_format(const ConstraintType &constraint)
Convert an AcirConstraint (single or vector) to AcirFormat by going through the full ACIR serde flow.
constexpr size_t GOBLIN_AVM_GATE_COUNT
std::vector< bb::fr > WitnessVector
std::vector< uint32_t > add_to_witness_and_track_indices(std::vector< bb::fr > &witness, const T &input)
Append values to a witness vector and track their indices.
Definition utils.hpp:90
bool skip_slow_tests()
Check if slow tests should be skipped.
Definition fixtures.cpp:244
std::pair< tracegen::TraceContainer, PublicInputs > get_minimal_trace_with_pi()
Definition fixtures.cpp:230
AvmFlavorSettings::FF FF
Definition field.hpp:10
std::filesystem::path bb_crs_path()
void init_file_crs_factory(const std::filesystem::path &path)
TEST_F(BoomerangGoblinRecursiveVerifierTests, graph_description_basic)
Construct and check a goblin recursive verification circuit.
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
std::vector< fr > HonkProof
Definition proof.hpp:15
UltraCircuitBuilder_< UltraExecutionTraceBlocks > UltraCircuitBuilder
VerifierCommitmentKey< Curve > vk
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
Barretenberg's representation of ACIR constraints.
std::vector< size_t > gates_per_opcode
Struct containing both the constraints to be added to the circuit and the witness vector.
Metadata required to create a circuit.
RecursionConstraint struct contains information required to recursively verify a proof.
static WitnessOrConstant from_constant(FF value)
static std::vector< FF > columns_to_flat(std::vector< std::vector< FF > > const &columns)
Definition avm_io.cpp:292