Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
transcript.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Complete, auditors: [Sergei], commit: 777717f6af324188ecd6bb68c3c86ee7befef94d}
3// external_1: { status: Complete, auditors: [@ed25519 (Spearbit)], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#pragma once
8
20#include "origin_tag.hpp"
22#include <atomic>
23#include <concepts>
24
25namespace bb {
26
27// A concept for detecting whether a type is native or in-circuit
28template <typename T>
30
31// A static counter for the number of transcripts created
32// This is used to generate unique labels for the transcript origin tags
33
34// 'inline' (since C++17) ensures a single shared definition with external linkage.
35inline std::atomic<size_t> unique_transcript_index{ 0 };
36
41template <typename Codec_, typename HashFunction_> class BaseTranscript {
42 public:
43 using Codec = Codec_;
44 using HashFunction = HashFunction_;
45 using DataType = typename Codec::DataType;
46 using Proof = std::vector<DataType>;
47
48 // Detects whether the transcript is in-circuit or not
49 static constexpr bool in_circuit = InCircuit<DataType>;
50 // A `DataType` challenge is split into two limbs that consitute challenge buffer
51 static constexpr size_t CHALLENGE_BUFFER_SIZE = 2;
52
54 {
55 // If we are in circuit, we need to get a unique index for the transcript
56 if constexpr (in_circuit) {
58 }
59 }
60
61 // Verifier-specific constructor.
62 explicit BaseTranscript(const Proof& proof) { load_proof(proof); }
63
64 protected:
65 Proof proof_data; // Contains the raw data sent by the prover.
66
67 private:
68 // Friend function for secure tag context extraction
69 template <typename T> friend OriginTag bb::extract_transcript_tag(const T& transcript);
70
71 // Fiat-Shamir Round Tracking
72 size_t transcript_index = 0; // Unique transcript ID (PRIVATE - access via extract_transcript_tag)
73 size_t round_index = 0; // Current FS round (PRIVATE - access via extract_transcript_tag)
74 bool challenge_generation_phase = false; // Whether currently generating challenges (vs sending/receiving data)
75
76 // Challenge generatopm state==
77 bool is_first_challenge = true; // Indicates if this is the first challenge this transcript is generating
78 DataType previous_challenge{}; // Previous challenge buffer (default-initialized to zeros)
79 std::vector<DataType> current_round_data; // Data for the current round that will be hashed to generate challenges
80
81 // Proof parsing state
83 size_t num_frs_written = 0; // Number of frs written to proof_data by the prover
84 size_t num_frs_read = 0; // Number of frs read from proof_data by the verifier
85
86 // Manifest (debugging tool)
87 bool use_manifest = false; // Indicates whether the manifest is turned on (only for manifest tests)
88 TranscriptManifest manifest; // Records a summary of the transcript interactions
89
99 {
100
101 std::vector<DataType> full_buffer;
102
103 const size_t size_bump = (is_first_challenge) ? 0 : 1;
104
105 full_buffer.resize(current_round_data.size() + size_bump);
106
107 // concatenate the previous challenge (if this is not the first challenge) with the current round data.
108 if (!is_first_challenge) {
109 // if not the first challenge, we can use the previous_challenge
110 full_buffer[0] = previous_challenge;
111 } else {
112 // Prevent challenge generation if this is the first challenge we're generating,
113 // AND nothing was sent by the prover.
115 // Update is_first_challenge for the future
116 is_first_challenge = false;
117 }
118
120 current_round_data.end(),
121 full_buffer.begin() + static_cast<std::ptrdiff_t>(size_bump));
122 current_round_data.clear();
123
124 // Hash the full buffer
125 DataType new_challenge = HashFunction::hash(full_buffer);
126 std::array<DataType, CHALLENGE_BUFFER_SIZE> new_challenges = Codec::split_challenge(new_challenge);
127 // update previous challenge buffer for next time we call this function
128 previous_challenge = new_challenge;
129 return new_challenges;
130 }
131
132 protected:
139 void add_element_frs_to_hash_buffer(const std::string& label, std::span<const DataType> element_frs)
140 {
141 if (use_manifest) {
142 // Add an entry to the current round of the manifest
143 manifest.add_entry(round_index, label, element_frs.size());
144 }
145
146 current_round_data.insert(current_round_data.end(), element_frs.begin(), element_frs.end());
147 }
148
157 template <typename T> void serialize_to_buffer(const T& element, Proof& proof_data)
158 {
159 auto element_frs = Codec::serialize_to_fields(element);
160 proof_data.insert(proof_data.end(), element_frs.begin(), element_frs.end());
161 }
171 template <typename T> T deserialize_from_buffer(const Proof& proof_data, size_t& offset) const
172 {
173 constexpr size_t element_fr_size = Codec::template calc_num_fields<T>();
174 if (offset + element_fr_size > proof_data.size()) {
175 throw_or_abort("Transcript: deserialize_from_buffer out of bounds");
176 }
177
178 auto element_frs = std::span{ proof_data }.subspan(offset, element_fr_size);
179 offset += element_fr_size;
180
181 auto element = Codec::template deserialize_from_fields<T>(element_frs);
182
183 return element;
184 }
185
186 public:
194 std::vector<DataType> export_proof()
195 {
196 std::vector<DataType> result(num_frs_written);
197 std::copy_n(proof_data.begin() + proof_start, num_frs_written, result.begin());
199 num_frs_written = 0;
200 return result;
201 };
202
208 void load_proof(const std::vector<DataType>& proof)
209 {
210 std::copy(proof.begin(), proof.end(), std::back_inserter(proof_data));
211 }
212
213 // Return the size of proof_data
214 size_t get_proof_size() { return proof_data.size(); }
215
216 // Enables the manifest
217 void enable_manifest() { use_manifest = true; }
218
230 {
231 const size_t num_challenges = labels.size();
232
233 if (use_manifest) {
234 // Add challenge labels for current round to the manifest
235 for (const auto& label : labels) {
237 }
238 }
239
240 // In case the transcript is used for recursive verification, we need to sanitize current round data so we don't
241 // get an origin tag violation inside the hasher. We are doing this to ensure that the free witness tagged
242 // elements that are sent to the transcript and are assigned tags externally, don't trigger the origin tag
243 // security mechanism while we are hashing them
244 bb::unset_free_witness_tags<in_circuit, DataType>(current_round_data);
245 // Compute the new challenge buffer from which we derive the challenges.
246
247 // Create challenges from Frs.
249 challenges.resize(num_challenges);
250
251 // Generate the challenges by iteratively hashing over the previous challenge.
252 for (size_t i = 0; i < num_challenges / 2; i += 1) {
254 challenges[2 * i] = Codec::template convert_challenge<ChallengeType>(challenge_buffer[0]);
255 challenges[(2 * i) + 1] = Codec::template convert_challenge<ChallengeType>(challenge_buffer[1]);
256 }
257 if ((num_challenges & 1) == 1) {
259 challenges[num_challenges - 1] = Codec::template convert_challenge<ChallengeType>(challenge_buffer[0]);
260 }
261
262 // Track Fiat-Shamir round transitions: entering challenge generation mode
265 }
266
267 // Assign origin tags to the challenges
268 bb::assign_origin_tag<in_circuit>(challenges, OriginTag(transcript_index, round_index, /*is_submitted=*/false));
269
270 return challenges;
271 }
272
279 template <typename ChallengeType, size_t N>
281 {
282 std::span<const std::string> labels_span{ labels.data(), labels.size() };
283 auto vec = get_challenges<ChallengeType>(labels_span); // calls the const-span overload
285 std::move(vec.begin(), vec.end(), out.begin());
286 return out;
287 }
288
297 template <typename ChallengeType>
298 std::vector<ChallengeType> get_dyadic_powers_of_challenge(const std::string& label, size_t num_challenges)
299 {
300 BB_ASSERT(num_challenges > 0, "get_dyadic_powers_of_challenge called with num_challenges=0");
301 ChallengeType challenge = get_challenge<ChallengeType>(label);
302 std::vector<ChallengeType> pows(num_challenges);
303 pows[0] = challenge;
304 for (size_t i = 1; i < num_challenges; i++) {
305 pows[i] = pows[i - 1].sqr();
306 }
307 return pows;
308 }
309
318 template <class T> void add_to_hash_buffer(const std::string& label, const T& element)
319 {
320 DEBUG_LOG(label, element);
321 // Track Fiat-Shamir round transitions: if we were generating challenges,
322 // now we're adding data, which means a new round has started
325 round_index++;
326 }
327
328 bb::assign_origin_tag<in_circuit>(element, OriginTag(transcript_index, round_index, /*is_submitted=*/true));
329 auto elements = Codec::serialize_to_fields(element);
330
331 add_element_frs_to_hash_buffer(label, elements);
332 }
333
347 template <class T> void send_to_verifier(const std::string& label, const T& element)
348 {
349 DEBUG_LOG(label, element);
350 // Track Fiat-Shamir round transitions: if we were generating challenges,
351 // now we're sending data, which means a new round has started
354 round_index++;
355 }
356
357 auto element_frs = Codec::template serialize_to_fields<T>(element);
358 proof_data.insert(proof_data.end(), element_frs.begin(), element_frs.end());
359 num_frs_written += element_frs.size();
360
361 add_element_frs_to_hash_buffer(label, element_frs);
362 }
363
371 template <class T> T receive_from_prover(const std::string& label)
372 {
373 const size_t element_size = Codec::template calc_num_fields<T>();
374 if (num_frs_read + element_size > proof_data.size()) {
375 throw_or_abort("Transcript: receive_from_prover out of bounds (proof too short)");
376 }
377
378 auto element_frs = std::span{ proof_data }.subspan(num_frs_read, element_size);
379 // Track Fiat-Shamir round transitions: if we were generating challenges,
380 // now we're receiving data, which means a new round has started
383 round_index++;
384 }
385 // Assign an origin tag to the elements going into the hash buffer
386 bb::assign_origin_tag<in_circuit>(element_frs, OriginTag(transcript_index, round_index, /*is_submitted=*/true));
387
388 num_frs_read += element_size;
389
390 add_element_frs_to_hash_buffer(label, element_frs);
391
392 auto element = Codec::template deserialize_from_fields<T>(element_frs);
393 DEBUG_LOG(label, element);
394
395 // Ensure that the element got assigned an origin tag
396 bb::check_origin_tag<in_circuit>(element, OriginTag(transcript_index, round_index, /*is_submitted=*/true));
397
398 return element;
399 }
400
401 template <typename ChallengeType> ChallengeType get_challenge(const std::string& label)
402 {
403 std::span<const std::string> label_span(&label, 1);
404 auto result = get_challenges<ChallengeType>(label_span);
405
406 DEBUG_LOG(label, result);
407 return result[0];
408 }
409
417 const std::shared_ptr<BaseTranscript>& prover_transcript)
418 {
419 // We expect this function to only be used when the transcript has just been exported.
420 BB_ASSERT_EQ(prover_transcript->num_frs_written, 0UL, "Expected to be empty");
421 auto verifier_transcript = std::make_shared<BaseTranscript>(*prover_transcript);
422 verifier_transcript->num_frs_read = static_cast<size_t>(verifier_transcript->proof_start);
423 verifier_transcript->proof_start = 0;
424 return verifier_transcript;
425 }
426
427 // Serialize an element of type T to a vector of fields
428 template <typename T> static std::vector<DataType> serialize(const T& element)
429 {
430 return Codec::serialize_to_fields(element);
431 }
432
433 template <typename T> static T deserialize(std::span<const DataType> frs)
434 {
435 return Codec::template deserialize_from_fields<T>(frs);
436 }
437
438 [[nodiscard]] TranscriptManifest get_manifest() const { return manifest; };
439
440 void print()
441 {
442 if (!use_manifest) {
443 info("Warning: manifest is not enabled!");
444 }
445 manifest.print();
446 }
447
448 // Test-specific utils
449
457 {
458 auto transcript = std::make_shared<BaseTranscript>();
459 constexpr uint32_t init{ 42 }; // arbitrary
460 transcript->send_to_verifier("Init", init);
461 return transcript;
462 };
463
472 {
473 auto verifier_transcript = std::make_shared<BaseTranscript>(transcript->proof_data);
474 [[maybe_unused]] auto _ = verifier_transcript->template receive_from_prover<DataType>("Init");
475 return verifier_transcript;
476 };
477
483 {
484 this->proof_start = start;
485 this->num_frs_written = written;
486 }
487
493
499 const Proof& test_get_proof_data() const { return proof_data; }
500};
501
504
505template <typename Builder>
511
516template <typename Curve, bool = Curve::is_stdlib_type> struct TranscriptFor {
518};
519
520template <typename Curve> struct TranscriptFor<Curve, true> {
522};
523
524template <typename Curve> using TranscriptFor_t = typename TranscriptFor<Curve>::type;
525
526} // namespace bb
#define BB_ASSERT(expression,...)
Definition assert.hpp:70
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:83
Common transcript class for both parties. Stores the data for the current round, as well as the manif...
typename Codec::DataType DataType
static constexpr bool in_circuit
DataType previous_challenge
BaseTranscript(const Proof &proof)
static std::shared_ptr< BaseTranscript > test_prover_init_empty()
For testing: initializes transcript with some arbitrary data so that a challenge can be generated aft...
const Proof & test_get_proof_data() const
T receive_from_prover(const std::string &label)
Reads the next element of type T from the transcript, with a predefined label, only used by verifier.
bool challenge_generation_phase
ChallengeType get_challenge(const std::string &label)
void test_set_proof_parsing_state(std::ptrdiff_t start, size_t written)
Test utility: Set proof parsing state for export after deserialization.
std::vector< DataType > current_round_data
void add_element_frs_to_hash_buffer(const std::string &label, std::span< const DataType > element_frs)
Adds challenge elements to the current_round_buffer and updates the manifest.
Proof & test_get_proof_data()
Test utility: Get mutable reference to proof_data.
void serialize_to_buffer(const T &element, Proof &proof_data)
Serializes object and appends it to proof_data.
static std::shared_ptr< BaseTranscript > test_verifier_init_empty(const std::shared_ptr< BaseTranscript > &transcript)
For testing: initializes transcript based on proof data then receives junk data produced by BaseTrans...
std::vector< ChallengeType > get_dyadic_powers_of_challenge(const std::string &label, size_t num_challenges)
Get a challenge and compute its dyadic powers [δ, δ², δ⁴, ..., δ^(2^(num_challenges-1))].
std::vector< DataType > export_proof()
Return the proof data starting at proof_start.
static T deserialize(std::span< const DataType > frs)
void add_to_hash_buffer(const std::string &label, const T &element)
Adds an element to the transcript.
void send_to_verifier(const std::string &label, const T &element)
Adds a prover message to the transcript, only intended to be used by the prover.
void load_proof(const std::vector< DataType > &proof)
Verifier-specific method. The verifier needs to load a proof or its segment before the verification.
HashFunction_ HashFunction
std::ptrdiff_t proof_start
TranscriptManifest get_manifest() const
std::array< DataType, CHALLENGE_BUFFER_SIZE > get_next_duplex_challenge_buffer()
Compute next challenge c_next = H( Compress(c_prev || round_buffer) )
std::array< ChallengeType, N > get_challenges(const std::array< std::string, N > &labels)
Wrapper around get_challenges to handle array of challenges.
TranscriptManifest manifest
static std::vector< DataType > serialize(const T &element)
std::ptrdiff_t test_get_proof_start() const
Test utility: Get proof_start for validation.
std::vector< DataType > Proof
T deserialize_from_buffer(const Proof &proof_data, size_t &offset) const
Deserializes the frs starting at offset into the typed element and returns that element.
std::vector< ChallengeType > get_challenges(std::span< const std::string > labels)
After all the prover messages have been sent, finalize the round by hashing all the data and then cre...
static constexpr size_t CHALLENGE_BUFFER_SIZE
static std::shared_ptr< BaseTranscript > convert_prover_transcript_to_verifier_transcript(const std::shared_ptr< BaseTranscript > &prover_transcript)
Convert a prover transcript to a verifier transcript.
void add_entry(size_t round, const std::string &element_label, size_t element_size)
void add_challenge(size_t round, const std::string &label)
Add a single challenge label to the manifest for the given round.
stdlib class that evaluates in-circuit poseidon2 hashes, consistent with behavior in crypto::poseidon...
Definition poseidon2.hpp:20
#define DEBUG_LOG(...)
#define info(...)
Definition log.hpp:93
ssize_t offset
Definition engine.cpp:62
const auto init
Definition fr.bench.cpp:135
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
std::atomic< size_t > unique_transcript_index
OriginTag extract_transcript_tag(const TranscriptType &transcript)
Extract origin tag context from a transcript.
BaseTranscript< FrCodec, bb::crypto::Poseidon2< bb::crypto::Poseidon2Bn254ScalarFieldParams > > NativeTranscript
typename TranscriptFor< Curve >::type TranscriptFor_t
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...
StdlibCodec for in-circuit (recursive) verification transcript handling.
Helper to get the appropriate Transcript type for a given Curve.
void throw_or_abort(std::string const &err)