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 proof.resize(AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED, FF::zero()); // Pad proof
57
58 const bool verified = prover.verify(proof, public_inputs);
59 EXPECT_TRUE(verified) << "native proof verification failed";
60
61 auto public_inputs_flat = PublicInputs::columns_to_flat(public_inputs.to_columns());
62 public_inputs_flat.resize(AVM_PUBLIC_INPUTS_COLUMNS_COMBINED_LENGTH, FF::zero()); // Pad public inputs
63
64 return { proof, public_inputs_flat };
65 }
66
68
69 static void generate_constraints(AcirConstraint& avm_recursion_constraint, WitnessVector& witness_values)
70 {
71 const auto [proof, public_inputs_flat] = create_avm_data();
72 avm_recursion_constraint = RecursionConstraint{
73 .key = {}, // Unused, the key is hard-coded in the circuit
74 .proof = add_to_witness_and_track_indices(witness_values, proof),
75 .public_inputs = add_to_witness_and_track_indices(witness_values, public_inputs_flat),
76 .key_hash = IS_CONSTANT, // Unused, the key hash is hard-coded in the circuit
77 .proof_type = AVM,
79 };
80 }
81
83 AcirConstraint constraint, WitnessVector witness_values, const InvalidWitness::Target& invalid_witness_target)
84 {
85 switch (invalid_witness_target) {
87 break;
89 // Tamper with the public inputs
90 witness_values[constraint.public_inputs[0]] += FF::one();
91 break;
92 }
94 // Tamper with the proof by changing one of the univariate coefficients
95 witness_values[constraint.proof[FrCodec::calc_num_fields<AvmFlavor::Commitment>() *
97 break;
98 }
99 }
100
101 return { constraint, witness_values };
102 }
103};
104
105class AvmRecursionConstraintTest : public ::testing::Test, public TestClass<AvmRecursionConstraintTestingFunctions> {
106 protected:
108};
109
110TEST_F(AvmRecursionConstraintTest, GenerateVKFromConstraints)
111{
113 GTEST_SKIP() << "Skipping slow test";
114 }
115 // AVM constraints are always proven with UltraRollupFlavor (they are part of the base rollup circuit)
116 size_t num_gates = test_vk_independence<UltraRollupFlavor>();
117
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
130{
132 GTEST_SKIP() << "Skipping slow test";
133 }
135
136 AcirConstraint constraint;
137 WitnessVector witness;
138 Base::generate_constraints(constraint, witness);
139
140 AcirFormat acir_format = constraint_to_acir_format(constraint, static_cast<uint32_t>(witness.size() - 1));
141
142 AcirProgram program = { acir_format, {} };
143 ProgramMetadata metadata = Base::generate_metadata();
144 metadata.collect_gates_per_opcode = true;
145 auto builder = create_circuit<Builder>(program, metadata);
146
147 EXPECT_EQ(program.constraints.gates_per_opcode.size(), 1);
149
150 auto prover_instance = std::make_shared<ProverInstance>(builder);
151 auto vk = std::make_shared<typename UltraRollupFlavor::VerificationKey>(prover_instance->get_precomputed());
152
153 // TODO(fcarreiro): Re-enable when the VK is fixed.
154 // static constexpr FF EXPECTED_OUTER_VK_HASH =
155 // FF("0x195059523571dbadeae1b213250567e17b4994568b736b73a1aae2b0c65fd2cd");
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("0x01caba77a068a59190885beaf6c30240dcd92b0515064b36dd4a3035b39154ea");
170
172
175 const HonkProof& proof,
176 const std::vector<FF>& public_inputs_flat)
177 {
178 std::vector<field_t<Builder>> stdlib_public_inputs_flat;
179 stdlib_public_inputs_flat.reserve(AVM_PUBLIC_INPUTS_COLUMNS_COMBINED_LENGTH);
180 for (const auto public_input : public_inputs_flat) {
181 stdlib_public_inputs_flat.emplace_back(field_t<Builder>::from_witness(&outer_builder, public_input));
182 }
183 stdlib::Proof<Builder> stdlib_proof;
184 stdlib_proof.reserve(AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED);
185 for (const auto proof_element : proof) {
186 stdlib_proof.emplace_back(field_t<Builder>::from_witness(&outer_builder, proof_element));
187 }
188
189 AvmGoblinRecursiveVerifier goblin_avm_verifier(outer_builder);
191 PublicInputs::flat_to_columns<field_t<Builder>>(stdlib_public_inputs_flat);
192 auto [mega_proof, goblin_proof, mega_vk] =
193 goblin_avm_verifier.construct_and_prove_inner_recursive_verification_circuit(stdlib_proof, public_inputs);
194
195 return { stdlib_proof, public_inputs, { mega_proof, goblin_proof, mega_vk } };
196 }
197};
198
199// TODO(fcarreiro): Re-enable when the VK is fixed.
201{
203 GTEST_SKIP() << "Skipping slow test";
204 }
205 const auto [proof, public_inputs_flat] = AvmRecursionConstraintTestingFunctions::create_avm_data();
206
207 Builder outer_builder;
208 auto [_stdlib_proof, _public_inputs, inner_prover_output] =
209 create_and_prove_inner_circuit(outer_builder, proof, public_inputs_flat);
210 EXPECT_EQ(inner_prover_output.mega_vk->hash(), EXPECTED_INNER_VK_HASH)
211 << "The VK hash of the inner circuit in the Goblinized AVM recursive verifier has changed. If this is "
212 "expected, update the expected value in the test.";
213}
214
221{
223 GTEST_SKIP() << "Skipping slow test";
224 }
225 const auto [proof, public_inputs_flat] = AvmRecursionConstraintTestingFunctions::create_avm_data();
226
227 {
228 Builder outer_builder;
229 auto [stdlib_proof, public_inputs, inner_prover_output] =
230 create_and_prove_inner_circuit(outer_builder, proof, public_inputs_flat);
231
232 auto mega_proof_tampered = inner_prover_output.mega_proof;
233 mega_proof_tampered[0] += FF::one(); // Tamper with the first public input
234
235 AvmGoblinRecursiveVerifier goblin_avm_verifier(outer_builder);
236 RecursiveAvmGoblinOutput output = goblin_avm_verifier.construct_outer_recursive_verification_circuit(
237 stdlib_proof,
238 public_inputs,
239 { mega_proof_tampered, inner_prover_output.goblin_proof, inner_prover_output.mega_vk });
240
241 EXPECT_TRUE(outer_builder.failed());
242 }
243
244 {
245 Builder outer_builder;
246 auto [stdlib_proof, public_inputs, inner_prover_output] =
247 create_and_prove_inner_circuit(outer_builder, proof, public_inputs_flat);
248
249 auto goblin_proof_tampered = inner_prover_output.goblin_proof;
250 goblin_proof_tampered.merge_proof[0] -= FF::one(); // Tamper with merge proof shift size
251
252 AvmGoblinRecursiveVerifier goblin_avm_verifier(outer_builder);
253 RecursiveAvmGoblinOutput output = goblin_avm_verifier.construct_outer_recursive_verification_circuit(
254 stdlib_proof,
255 public_inputs,
256 { inner_prover_output.mega_proof, goblin_proof_tampered, inner_prover_output.mega_vk });
257
258 EXPECT_TRUE(outer_builder.failed());
259 }
260
261 {
262 Builder outer_builder;
263 auto [stdlib_proof, public_inputs, inner_prover_output] =
264 create_and_prove_inner_circuit(outer_builder, proof, public_inputs_flat);
265
266 auto mega_vk_tampered = inner_prover_output.mega_vk;
267 mega_vk_tampered->q_m = mega_vk_tampered->q_m + MegaAvmFlavor::Commitment::one(); // Tamper with q_m commitment
268
269 AvmGoblinRecursiveVerifier goblin_avm_verifier(outer_builder);
270 RecursiveAvmGoblinOutput output = goblin_avm_verifier.construct_outer_recursive_verification_circuit(
271 stdlib_proof,
272 public_inputs,
273 { inner_prover_output.mega_proof, inner_prover_output.goblin_proof, mega_vk_tampered });
274
275 EXPECT_TRUE(outer_builder.failed());
276 }
277}
#define AVM_PUBLIC_INPUTS_COLUMNS_COMBINED_LENGTH
#define AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED
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)
AvmGoblinRecursiveVerifier::InnerProverOutput InnerProverOutput
A ProverInstance is normally constructed from a finalized circuit and it contains all the information...
typename ExecutionTrace::FF FF
static constexpr size_t NUM_WITNESS_ENTITIES
Definition flavor.hpp:55
Recursive verifier of AVM2 proofs that utilizes the Goblin mechanism for efficient EC operations.
InnerProverOutput construct_and_prove_inner_recursive_verification_circuit(const stdlib::Proof< UltraBuilder > &stdlib_proof, const std::vector< std::vector< UltraFF > > &public_inputs) const
Construct and prove the inner Mega-arithmetized AVM recursive verifier circuit.
stdlib::recursion::honk::UltraRecursiveVerifierOutput< UltraBuilder > RecursiveAvmGoblinOutput
RecursiveAvmGoblinOutput construct_outer_recursive_verification_circuit(const stdlib::Proof< UltraBuilder > &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.
bool verify(const Proof &proof, const PublicInputs &pi)
Proof prove(tracegen::TraceContainer &&trace)
A simple wrapper around a vector of stdlib field elements representing a proof.
Definition proof.hpp:19
AluTraceBuilder builder
Definition alu.test.cpp:124
TestTraceContainer trace
constexpr size_t FINALIZED_GOBLIN_AVM_GATE_COUNT
AcirFormat constraint_to_acir_format(const ConstraintType &constraint, uint32_t varnum)
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:199
std::pair< tracegen::TraceContainer, PublicInputs > get_minimal_trace_with_pi()
Definition fixtures.cpp:183
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:289
Output type for recursive ultra verification.