Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
translator_recursive_verifier.test.cpp
Go to the documentation of this file.
10#include <gtest/gtest.h>
11namespace bb {
12
19class TranslatorRecursiveTests : public ::testing::Test {
20 public:
30
32
38
40
42
44
45 // Helper function to add no-ops
46 static void add_random_ops(std::shared_ptr<bb::ECCOpQueue>& op_queue, size_t count)
47 {
48 for (size_t i = 0; i < count; i++) {
49 op_queue->random_op_ultra_only();
50 }
51 }
52
53 // Helper function to create an MSM
54 static void add_mixed_ops(std::shared_ptr<bb::ECCOpQueue>& op_queue, size_t count = 100)
55 {
56 auto P1 = InnerG1::random_element();
57 auto P2 = InnerG1::random_element();
58 auto z = InnerFF::random_element();
59 for (size_t i = 0; i < count; i++) {
60 op_queue->add_accumulate(P1);
61 op_queue->mul_accumulate(P2, z);
62 }
63 op_queue->eq_and_reset();
64 }
65
66 // Construct a test circuit based on some random operations
67 static InnerBuilder generate_test_circuit(const InnerBF& batching_challenge_v,
68 const InnerBF& evaluation_challenge_x,
69 const size_t circuit_size_parameter = 500)
70 {
71
72 // Add the same operations to the ECC op queue; the native computation is performed under the hood.
73 auto op_queue = std::make_shared<bb::ECCOpQueue>();
74 op_queue->no_op_ultra_only();
76 add_mixed_ops(op_queue, circuit_size_parameter / 2);
77 op_queue->merge();
78 add_mixed_ops(op_queue, circuit_size_parameter / 2);
80 op_queue->merge(MergeSettings::APPEND, ECCOpQueue::OP_QUEUE_SIZE - op_queue->get_current_subtable_size());
81
82 return InnerBuilder{ batching_challenge_v, evaluation_challenge_x, op_queue };
83 }
84
85 // Helper to create native op queue commitments from proving key
86 static std::array<InnerFlavor::Commitment, InnerFlavor::NUM_OP_QUEUE_WIRES> create_native_op_queue_commitments(
88 {
89 std::array<InnerFlavor::Commitment, InnerFlavor::NUM_OP_QUEUE_WIRES> op_queue_commitments;
90 op_queue_commitments[0] =
91 proving_key->proving_key->commitment_key.commit(proving_key->proving_key->polynomials.op);
92 op_queue_commitments[1] =
93 proving_key->proving_key->commitment_key.commit(proving_key->proving_key->polynomials.x_lo_y_hi);
94 op_queue_commitments[2] =
95 proving_key->proving_key->commitment_key.commit(proving_key->proving_key->polynomials.x_hi_z_1);
96 op_queue_commitments[3] =
97 proving_key->proving_key->commitment_key.commit(proving_key->proving_key->polynomials.y_lo_z_2);
98 return op_queue_commitments;
99 }
100
101 // Helper to convert native op queue commitments to stdlib commitments
102 static std::array<RecursiveFlavor::Commitment, InnerFlavor::NUM_OP_QUEUE_WIRES> create_stdlib_op_queue_commitments(
103 OuterBuilder* builder, const std::array<InnerFlavor::Commitment, InnerFlavor::NUM_OP_QUEUE_WIRES>& native_comms)
104 {
105 std::array<RecursiveFlavor::Commitment, InnerFlavor::NUM_OP_QUEUE_WIRES> stdlib_comms;
106 for (size_t i = 0; i < InnerFlavor::NUM_OP_QUEUE_WIRES; i++) {
107 stdlib_comms[i] = RecursiveFlavor::Commitment::from_witness(builder, native_comms[i]);
108 // Set empty origin tags for commitments (they're free witnesses from merge protocol)
109 stdlib_comms[i].set_origin_tag(OriginTag());
110 }
111 return stdlib_comms;
112 }
113
114 // Helper struct to hold translator verification inputs as stdlib witnesses
119 std::array<RecursiveFlavor::Commitment, InnerFlavor::NUM_OP_QUEUE_WIRES> op_queue_commitments;
120 // Native values for native verifier
122 std::array<InnerFlavor::Commitment, InnerFlavor::NUM_OP_QUEUE_WIRES> native_op_queue_commitments;
123 };
124
125 // Helper to create recursive verifier inputs from native values
127 const InnerProver& prover,
128 const InnerBF& evaluation_challenge_x,
129 const InnerBF& batching_challenge_v)
130 {
131 // Get accumulated_result from the prover
132 bb::fq accumulated_result_native = prover.get_accumulated_result();
133 auto accumulated_result = TranslatorBF::from_witness(builder, accumulated_result_native);
134 accumulated_result.set_origin_tag(OriginTag());
135
136 // Convert challenges to circuit witnesses
137 auto stdlib_evaluation_challenge_x = TranslatorBF::from_witness(builder, evaluation_challenge_x);
138 auto stdlib_batching_challenge_v = TranslatorBF::from_witness(builder, batching_challenge_v);
139 stdlib_evaluation_challenge_x.set_origin_tag(OriginTag());
140 stdlib_batching_challenge_v.set_origin_tag(OriginTag());
141
142 // Create op queue commitments (normally provided by merge protocol)
143 auto native_op_queue_commitments = create_native_op_queue_commitments(prover.key);
144 auto op_queue_commitments = create_stdlib_op_queue_commitments(builder, native_op_queue_commitments);
145
146 return { accumulated_result, stdlib_evaluation_challenge_x, stdlib_batching_challenge_v,
147 op_queue_commitments, accumulated_result_native, native_op_queue_commitments };
148 }
149
150 // Shared helper to create and verify a translator proof recursively
151 // Includes native verification and consistency checks
153 size_t circuit_size_parameter = 500)
154 {
155 using NativeVerifierCommitmentKey = InnerFlavor::VerifierCommitmentKey;
156
157 // Create fake ECCVM proof
158 auto prover_transcript = std::make_shared<Transcript>();
159
160 // Generate challenges
161 InnerBF batching_challenge_v = InnerBF::random_element();
162 InnerBF evaluation_challenge_x = InnerBF::random_element();
163
164 // Create inner translator circuit and generate proof
165 InnerBuilder circuit_builder =
166 generate_test_circuit(batching_challenge_v, evaluation_challenge_x, circuit_size_parameter);
167 auto proving_key = std::make_shared<TranslatorProvingKey>(circuit_builder);
168 InnerProver prover{ proving_key, prover_transcript };
169 auto proof = prover.construct_proof();
170
171 // Set up outer recursive circuit
172 OuterBuilder outer_circuit;
173 stdlib::Proof<OuterBuilder> stdlib_proof(outer_circuit, proof);
174 auto transcript = std::make_shared<RecursiveFlavor::Transcript>(stdlib_proof);
175
176 auto verification_key = std::make_shared<typename InnerFlavor::VerificationKey>(prover.key->proving_key);
177 RecursiveVerifier verifier{ &outer_circuit, verification_key, transcript };
178
179 // Create recursive verifier inputs
180 auto recursive_inputs =
181 create_recursive_verifier_inputs(&outer_circuit, prover, evaluation_challenge_x, batching_challenge_v);
182
183 // Verify proof recursively
184 stdlib::Proof<OuterBuilder> stdlib_proof_for_verifier(outer_circuit, proof);
185 typename RecursiveVerifier::PairingPoints pairing_points =
186 verifier.verify_proof(stdlib_proof_for_verifier,
187 recursive_inputs.evaluation_challenge_x,
188 recursive_inputs.batching_challenge_v,
189 recursive_inputs.accumulated_result,
190 recursive_inputs.op_queue_commitments);
191
193 inputs.pairing_inputs = pairing_points;
195
196 // Verify with native verifier and compare results
197 auto native_verifier_transcript = std::make_shared<Transcript>(proof);
198 InnerVerifier native_verifier(verification_key, native_verifier_transcript);
199 bool native_result = native_verifier.verify_proof(proof,
200 evaluation_challenge_x,
201 batching_challenge_v,
202 recursive_inputs.accumulated_result_native,
203 recursive_inputs.native_op_queue_commitments);
204
205 NativeVerifierCommitmentKey pcs_vkey{};
206 auto recursive_result = pcs_vkey.pairing_check(pairing_points.P0.get_value(), pairing_points.P1.get_value());
207 EXPECT_EQ(recursive_result, native_result);
208
209 // Verify VK consistency
210 EXPECT_EQ(static_cast<uint64_t>(verifier.key->log_circuit_size.get_value()),
211 verification_key->log_circuit_size);
212 EXPECT_EQ(static_cast<uint64_t>(verifier.key->num_public_inputs.get_value()),
213 verification_key->num_public_inputs);
214 for (auto [vk_poly, native_vk_poly] : zip_view(verifier.key->get_all(), verification_key->get_all())) {
215 EXPECT_EQ(vk_poly.get_value(), native_vk_poly);
216 }
217
218 auto outer_proving_key = std::make_shared<OuterProverInstance>(outer_circuit);
219 auto outer_verification_key =
220 std::make_shared<typename OuterFlavor::VerificationKey>(outer_proving_key->get_precomputed());
221
222 return { std::move(outer_circuit), outer_verification_key };
223 }
224
226 {
227 // Use the shared helper to create and verify the recursive circuit
228 auto [outer_circuit, outer_verification_key] = create_recursive_verifier_circuit();
229
230 info("Recursive Verifier: num gates = ", outer_circuit.num_gates());
231 EXPECT_EQ(outer_circuit.failed(), false) << outer_circuit.err();
232
233 // Prove and verify the outer recursive circuit
234 auto prover_instance = std::make_shared<OuterProverInstance>(outer_circuit);
235 OuterProver prover(prover_instance, outer_verification_key);
236 OuterVerifier verifier(outer_verification_key);
237 auto proof = prover.construct_proof();
238 bool verified = verifier.template verify_proof<DefaultIO>(proof).result;
239
240 ASSERT_TRUE(verified);
241 }
242
244 {
245 auto [outer_circuit_256, verification_key_256] = create_recursive_verifier_circuit(256);
246 auto [outer_circuit_512, verification_key_512] = create_recursive_verifier_circuit(512);
247
248 compare_ultra_blocks_and_verification_keys<OuterFlavor>({ outer_circuit_256.blocks, outer_circuit_512.blocks },
249 { verification_key_256, verification_key_512 });
250 };
251};
252
257
262} // namespace bb
Common transcript class for both parties. Stores the data for the current round, as well as the manif...
static const size_t OP_QUEUE_SIZE
A ProverInstance is normally constructed from a finalized circuit and it contains all the information...
TranslatorCircuitBuilder creates a circuit that evaluates the correctness of the evaluation of EccOpQ...
static constexpr size_t NUM_OP_QUEUE_WIRES
TranslatorCircuitBuilder CircuitBuilder
Curve::ScalarField FF
Curve::AffineElement Commitment
bb::VerifierCommitmentKey< Curve > VerifierCommitmentKey
NativeTranscript Transcript
uint256_t get_accumulated_result() const
Extract the accumulated result from the circuit.
std::shared_ptr< TranslatorProvingKey > key
The recursive counterpart of the native Translator flavor.
Test suite for standalone recursive verification of translation proofs.
static void add_mixed_ops(std::shared_ptr< bb::ECCOpQueue > &op_queue, size_t count=100)
static InnerBuilder generate_test_circuit(const InnerBF &batching_challenge_v, const InnerBF &evaluation_challenge_x, const size_t circuit_size_parameter=500)
std::conditional_t< IsMegaBuilder< OuterBuilder >, MegaFlavor, UltraFlavor > OuterFlavor
static RecursiveVerifierInputs create_recursive_verifier_inputs(OuterBuilder *builder, const InnerProver &prover, const InnerBF &evaluation_challenge_x, const InnerBF &batching_challenge_v)
static void add_random_ops(std::shared_ptr< bb::ECCOpQueue > &op_queue, size_t count)
static std::array< RecursiveFlavor::Commitment, InnerFlavor::NUM_OP_QUEUE_WIRES > create_stdlib_op_queue_commitments(OuterBuilder *builder, const std::array< InnerFlavor::Commitment, InnerFlavor::NUM_OP_QUEUE_WIRES > &native_comms)
static std::tuple< OuterBuilder, std::shared_ptr< OuterFlavor::VerificationKey > > create_recursive_verifier_circuit(size_t circuit_size_parameter=500)
static std::array< InnerFlavor::Commitment, InnerFlavor::NUM_OP_QUEUE_WIRES > create_native_op_queue_commitments(const std::shared_ptr< TranslatorProvingKey > &proving_key)
bool verify_proof(const HonkProof &proof, const uint256_t &evaluation_input_x, const BF &batching_challenge_v, const uint256_t &accumulated_result, const std::array< Commitment, TranslatorFlavor::NUM_OP_QUEUE_WIRES > &op_queue_wire_commitments)
This function verifies a TranslatorFlavor Honk proof for given program settings.
A simple wrapper around a vector of stdlib field elements representing a proof.
Definition proof.hpp:19
Manages the data that is propagated on the public inputs of an application/function circuit.
void info(Args... args)
Definition log.hpp:75
AluTraceBuilder builder
Definition alu.test.cpp:124
AvmProvingInputs inputs
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
TEST_F(IPATest, ChallengesAreZero)
Definition ipa.test.cpp:185
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
This file contains part of the logic for the Origin Tag mechanism that tracks the use of in-circuit p...
std::array< RecursiveFlavor::Commitment, InnerFlavor::NUM_OP_QUEUE_WIRES > op_queue_commitments
std::array< InnerFlavor::Commitment, InnerFlavor::NUM_OP_QUEUE_WIRES > native_op_queue_commitments
static field random_element(numeric::RNG *engine=nullptr) noexcept
An object storing two EC points that represent the inputs to a pairing check.
uint32_t set_public()
Set the witness indices for the limbs of the pairing points to public.