Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
recursive_flavor.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Planned, auditors: [Federico], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#pragma once
8
9#include <cstdint>
10
17
18namespace bb::avm2 {
19
21 public:
29
32
33 // Native one is used!
35
37
41
43 using SubrelationSeparators = std::array<FF, NUM_SUBRELATIONS - 1>;
44
45 // This flavor would not be used with ZK Sumcheck
46 static constexpr bool HasZK = false;
47
48 // To achieve fixed proof size and that the recursive verifier circuit is constant, we are using padding in Sumcheck
49 // and Shplemini
50 static constexpr bool USE_PADDING = true;
51
57 public:
59 using Base::Base;
60 };
61
62 class VerificationKey : public StdlibVerificationKey_<CircuitBuilder,
63 NativeFlavor::PrecomputedEntities<Commitment>,
64 NativeVerificationKey,
65 VKSerializationMode::NO_METADATA> {
66 public:
68 {
69 for (auto [native_comm, comm] : zip_view(native_key->get_all(), this->get_all())) {
70 comm = Commitment::from_witness(builder, native_comm);
71 }
72 }
73
81 {
82 using Codec = stdlib::StdlibCodec<FF>;
83
84 size_t num_frs_read = 0;
85 size_t num_frs_Comm = Codec::template calc_num_fields<Commitment>();
86
87 for (Commitment& comm : this->get_all()) {
88 comm =
89 Codec::template deserialize_from_fields<Commitment>(elements.subspan(num_frs_read, num_frs_Comm));
90 num_frs_read += num_frs_Comm;
91 }
92 }
93
94 FF hash_with_origin_tagging([[maybe_unused]] const OriginTag& tag) const override
95 {
96 throw_or_abort("Not intended to be used because vk is hardcoded in circuit.");
97 }
98
104 {
105 for (Commitment& commitment : this->get_all()) {
106 commitment.fix_witness();
107 }
108 }
109 };
110 template <typename Builder> class TemplatedTranscript : public StdlibTranscript<Builder> {
113
114 private:
127 const stdlib::Proof<Builder>& stdlib_proof,
128 const std::vector<std::vector<stdlib::field_t<Builder>>>& public_inputs,
129 const bool enable_manifest = false)
130 {
132 auto native_vk_hash = native_vk->hash();
133 FF vk_hash = FF::from_witness(&builder, native_vk_hash);
134 vk_hash.fix_witness();
135
137 if (enable_manifest) {
138 transcript->enable_manifest();
139 }
140
141 transcript->load_proof(stdlib_proof);
142
143 transcript->add_to_hash_buffer("avm_vk_hash", vk_hash);
144
145 for (size_t i = 0; i < AVM_NUM_PUBLIC_INPUT_COLUMNS; i++) {
146 for (size_t j = 0; j < public_inputs[i].size(); j++) {
147 transcript->add_to_hash_buffer("public_input_" + std::to_string(i) + "_" + std::to_string(j),
148 public_inputs[i][j]);
149 }
150 }
151
152 size_t proof_idx = 0;
153 std::span<const stdlib::field_t<Builder>> proof_span = stdlib_proof;
154
155 constexpr size_t num_frs_comm =
156 Base::Codec::template calc_num_fields<typename Base::Codec::bn254_commitment>();
157 for (size_t idx = WIRE_START_IDX; idx < WIRE_START_IDX + NUM_WIRE_ENTITIES; idx++) {
158 transcript->add_element_frs_to_hash_buffer(COLUMN_NAMES[idx],
159 proof_span.subspan(proof_idx, num_frs_comm));
160 proof_idx += num_frs_comm;
161 }
162
163 [[maybe_unused]] auto [_beta, _gamma] =
164 transcript->template get_challenges<FF>(std::array<std::string, 2>{ "beta", "gamma" });
165
166 for (size_t idx = DERIVED_START_IDX; idx < DERIVED_START_IDX + NUM_DERIVED_ENTITIES; idx++) {
167 transcript->add_element_frs_to_hash_buffer(COLUMN_NAMES[idx],
168 proof_span.subspan(proof_idx, num_frs_comm));
169 proof_idx += num_frs_comm;
170 }
171
172 [[maybe_unused]] const FF _alpha = transcript->template get_challenge<FF>("Sumcheck:alpha");
173
174 [[maybe_unused]] const FF _initial_gate_challenge =
175 transcript->template get_challenge<FF>("Sumcheck:gate_challenge");
176
177 for (size_t i = 0; i < native_vk->log_circuit_size; i++) {
178 std::string round_univariate_label = "Sumcheck:univariate_" + std::to_string(i);
179 transcript->add_element_frs_to_hash_buffer(
180 round_univariate_label, proof_span.subspan(proof_idx, AvmFlavor::BATCHED_RELATION_PARTIAL_LENGTH));
182 [[maybe_unused]] FF _round_challenge =
183 transcript->template get_challenge<FF>("Sumcheck:u_" + std::to_string(i));
184 }
185
186 transcript->add_element_frs_to_hash_buffer("Sumcheck:evaluations",
187 proof_span.subspan(proof_idx, NUM_ALL_ENTITIES));
188 proof_idx += NUM_ALL_ENTITIES;
189
190 std::vector<std::string> unshifted_batching_challenge_labels;
191 unshifted_batching_challenge_labels.reserve(NUM_UNSHIFTED_ENTITIES - 1);
192 for (size_t idx = 0; idx < NUM_UNSHIFTED_ENTITIES - 1; idx++) {
193 unshifted_batching_challenge_labels.push_back("rho_" + std::to_string(idx));
194 }
195 std::vector<std::string> shifted_batching_challenge_labels;
196 shifted_batching_challenge_labels.reserve(NUM_WIRES_TO_BE_SHIFTED);
197 for (size_t idx = 0; idx < NUM_WIRES_TO_BE_SHIFTED; idx++) {
198 shifted_batching_challenge_labels.push_back("rho_" + std::to_string(NUM_UNSHIFTED_ENTITIES - 1 + idx));
199 }
200
201 [[maybe_unused]] auto _unshifted_challenges =
202 transcript->template get_challenges<FF>(unshifted_batching_challenge_labels);
203 [[maybe_unused]] auto _shifted_challenges =
204 transcript->template get_challenges<FF>(shifted_batching_challenge_labels);
205
206 [[maybe_unused]] const FF _gemini_batching_challenge = transcript->template get_challenge<FF>("rho");
207
208 for (size_t i = 1; i < native_vk->log_circuit_size; ++i) {
209 transcript->add_element_frs_to_hash_buffer("Gemini:FOLD_" + std::to_string(i),
210 proof_span.subspan(proof_idx, num_frs_comm));
211 proof_idx += num_frs_comm;
212 }
213
214 [[maybe_unused]] const FF _gemini_evaluation_challenge = transcript->template get_challenge<FF>("Gemini:r");
215
216 for (size_t i = 1; i <= native_vk->log_circuit_size; ++i) {
217 transcript->add_to_hash_buffer("Gemini:a_" + std::to_string(i), proof_span[proof_idx++]);
218 }
219
220 [[maybe_unused]] const FF _shplonk_batching_challenge =
221 transcript->template get_challenge<FF>("Shplonk:nu");
222
223 transcript->add_element_frs_to_hash_buffer("Shplonk:Q", proof_span.subspan(proof_idx, num_frs_comm));
224 proof_idx += num_frs_comm;
225
226 [[maybe_unused]] const FF _shplonk_evaluation_challenge =
227 transcript->template get_challenge<FF>("Shplonk:z");
228
229 transcript->add_element_frs_to_hash_buffer("KZG:W", proof_span.subspan(proof_idx, num_frs_comm));
230 proof_idx += num_frs_comm;
231
232 [[maybe_unused]] const FF _masking_challenge =
233 transcript->template get_challenge<FF>("KZG:masking_challenge");
234
235 return transcript;
236 };
237
245 const std::shared_ptr<TemplatedTranscript<Builder>>& transcript, const stdlib::Proof<Builder>& stdlib_proof)
246 {
247 if (stdlib_proof.size() == AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED) {
248 // If the proof is padded, we need to add the padding values to the transcript because recursive
249 // verification doesn't do that
250 transcript->add_element_frs_to_hash_buffer(
251 "proof_padding",
252 std::span(stdlib_proof)
256 }
257 return transcript->template get_challenge<stdlib::field_t<Builder>>("final_transcript_state");
258 };
259
260 public:
268 const stdlib::Proof<Builder>& stdlib_proof,
269 const std::vector<std::vector<stdlib::field_t<Builder>>>& public_inputs)
270 {
271 auto transcript = perform_avm_transcript_operations(builder, stdlib_proof, public_inputs);
272 return pad_and_hash_avm_transcript(transcript, stdlib_proof);
273 }
274
281 const std::shared_ptr<TemplatedTranscript<Builder>>& transcript, const stdlib::Proof<Builder>& stdlib_proof)
282 {
283 return pad_and_hash_avm_transcript(transcript, stdlib_proof);
284 }
285
293 const stdlib::Proof<Builder>& stdlib_proof,
294 const std::vector<std::vector<stdlib::field_t<Builder>>>& public_inputs)
295 {
296 auto transcript = perform_avm_transcript_operations(builder, stdlib_proof, public_inputs, true);
297
298 return { pad_and_hash_avm_transcript(transcript, stdlib_proof), transcript };
299 }
300 };
301
306};
307
308} // namespace bb::avm2
#define AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED
#define AVM_NUM_PUBLIC_INPUT_COLUMNS
Common transcript class for both parties. Stores the data for the current round, as well as the manif...
Base Stdlib verification key class.
Definition flavor.hpp:425
Representation of the Grumpkin Verifier Commitment Key inside a bn254 circuit.
tuple_cat_t< MainRelations_< FF_ >, LookupRelations_< FF_ > > Relations_
Definition flavor.hpp:81
static constexpr size_t COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS
Definition flavor.hpp:102
static constexpr size_t NUM_SUBRELATIONS
Definition flavor.hpp:84
AvmFlavorSettings::VerifierCommitmentKey VerifierCommitmentKey
Definition flavor.hpp:43
static constexpr size_t BATCHED_RELATION_PARTIAL_LENGTH
Definition flavor.hpp:93
static constexpr size_t NUM_ALL_ENTITIES
Definition flavor.hpp:58
A field element for each entity of the flavor. These entities represent the prover polynomials evalua...
static std::pair< stdlib::field_t< Builder >, std::shared_ptr< TemplatedTranscript< Builder > > > hash_avm_transcript_for_testing(Builder &builder, const stdlib::Proof< Builder > &stdlib_proof, const std::vector< std::vector< stdlib::field_t< Builder > > > &public_inputs)
Testing method to hash the transcript after having replicated the operations performed on the AVM tra...
static stdlib::field_t< Builder > pad_and_hash_avm_transcript(const std::shared_ptr< TemplatedTranscript< Builder > > &transcript, const stdlib::Proof< Builder > &stdlib_proof)
Hash a transcript that has recorded the operations performed during AVM proof verification.
static stdlib::field_t< Builder > hash_avm_transcript(Builder &builder, const stdlib::Proof< Builder > &stdlib_proof, const std::vector< std::vector< stdlib::field_t< Builder > > > &public_inputs)
Construct a transcript replicating the operations performed on the AVM transcript during proof verifi...
static std::shared_ptr< TemplatedTranscript< Builder > > perform_avm_transcript_operations(Builder &builder, const stdlib::Proof< Builder > &stdlib_proof, const std::vector< std::vector< stdlib::field_t< Builder > > > &public_inputs, const bool enable_manifest=false)
Replicate the operations performed on the AVM transcript during proof verification.
static stdlib::field_t< Builder > hash_avm_transcript(const std::shared_ptr< TemplatedTranscript< Builder > > &transcript, const stdlib::Proof< Builder > &stdlib_proof)
Hash the AVM verifier transcript after having performed proof verification. Then, hash the transcript...
void fix_witness()
Fixes witnesses of VK to be constants.
VerificationKey(CircuitBuilder *builder, const std::shared_ptr< NativeVerificationKey > &native_key)
FF hash_with_origin_tagging(const OriginTag &tag) const override
Tag VK components and hash.
VerificationKey(std::span< const FF > elements)
Deserialize a verification key from a vector of field elements.
static constexpr size_t NUM_SUBRELATIONS
NativeFlavor::Relations_< FF > Relations
AvmRecursiveFlavorSettings::BF BF
static constexpr size_t NUM_RELATIONS
static constexpr size_t BATCHED_RELATION_PARTIAL_LENGTH
AvmRecursiveFlavorSettings::GroupElement GroupElement
static constexpr size_t NUM_ALL_ENTITIES
static constexpr bool USE_PADDING
AvmRecursiveFlavorSettings::FF FF
std::array< FF, NUM_SUBRELATIONS - 1 > SubrelationSeparators
AvmRecursiveFlavorSettings::Commitment Commitment
static constexpr std::array< Commitment, VerificationKey::NUM_PRECOMPUTED_COMMITMENTS > get_all()
A simple wrapper around a vector of stdlib field elements representing a proof.
Definition proof.hpp:19
static field_t from_witness(Builder *ctx, const bb::fr &input)
Definition field.hpp:455
AluTraceBuilder builder
Definition alu.test.cpp:124
Base class templates for structures that contain data parameterized by the fundamental polynomials of...
constexpr auto NUM_WIRE_ENTITIES
Definition columns.hpp:42
constexpr auto NUM_UNSHIFTED_ENTITIES
Definition columns.hpp:47
constexpr auto NUM_WIRES_TO_BE_SHIFTED
Definition columns.hpp:45
constexpr auto NUM_DERIVED_ENTITIES
Definition columns.hpp:43
constexpr auto DERIVED_START_IDX
Definition columns.hpp:68
const std::vector< std::string > & COLUMN_NAMES
Definition columns.hpp:84
constexpr auto WIRE_START_IDX
Definition columns.hpp:64
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::string to_string(bb::avm2::ValueTag tag)
void throw_or_abort(std::string const &err)