Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
bb::BaseTranscript< Codec_, HashFunction_ > Class Template Reference

Common transcript class for both parties. Stores the data for the current round, as well as the manifest. More...

#include <transcript.hpp>

Inheritance diagram for bb::BaseTranscript< Codec_, HashFunction_ >:
bb::ECCVMFlavor::IPATranscript bb::avm2::AvmFlavor::Transcript bb::avm2::AvmRecursiveFlavor::TemplatedTranscript< Builder >

Public Types

using Codec = Codec_
 
using HashFunction = HashFunction_
 
using DataType = typename Codec::DataType
 
using Proof = std::vector< DataType >
 

Public Member Functions

 BaseTranscript ()
 
 BaseTranscript (const Proof &proof)
 
std::vector< DataTypeexport_proof ()
 Return the proof data starting at proof_start.
 
void load_proof (const std::vector< DataType > &proof)
 Verifier-specific method. The verifier needs to load a proof or its segment before the verification.
 
size_t get_proof_size ()
 
void enable_manifest ()
 
template<typename ChallengeType >
std::vector< ChallengeType > generate_challenges (std::span< const std::string > labels, auto &&fill)
 Shared core of get_challenges / get_short_challenges: the bookkeeping common to every challenge round (manifest, free-witness sanitization, phase flag, origin tags). The full and short paths differ only in how the challenge vector is filled, which the caller supplies via fill.
 
template<typename ChallengeType >
std::vector< ChallengeType > get_short_challenges (std::span< const std::string > labels)
 Generate short (127-bit) challenges for the given labels.
 
template<typename ChallengeType >
std::vector< ChallengeType > get_challenges (std::span< const std::string > labels)
 Generate full-width (~254-bit) challenges for the given labels (the default).
 
template<typename ChallengeType , size_t N>
std::array< ChallengeType, Nget_short_challenges (const std::array< std::string, N > &labels)
 Wrapper around get_short_challenges to handle array of challenges.
 
template<typename ChallengeType , size_t N>
std::array< ChallengeType, Nget_challenges (const std::array< std::string, N > &labels)
 Wrapper around get_challenges to handle array of challenges.
 
template<typename ChallengeType >
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))].
 
template<class T >
void add_to_hash_buffer (const std::string &label, const T &element)
 Adds an element to the transcript.
 
template<class T >
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.
 
template<class 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.
 
template<typename ChallengeType >
ChallengeType get_challenge (const std::string &label)
 Generate a single full-width (~254-bit) challenge for label (the default). See get_challenges.
 
template<typename ChallengeType >
ChallengeType get_short_challenge (const std::string &label)
 Generate a single short (127-bit) challenge for label. See get_short_challenges.
 
const TranscriptManifestget_manifest () const
 
void print ()
 
void test_set_proof_parsing_state (std::ptrdiff_t start, size_t written)
 Test utility: Set proof parsing state for export after deserialization.
 
std::ptrdiff_t test_get_proof_start () const
 Test utility: Get proof_start for validation.
 
Prooftest_get_proof_data ()
 Test utility: Get mutable reference to proof_data.
 
const Prooftest_get_proof_data () const
 

Static Public Member Functions

static std::shared_ptr< BaseTranscriptconvert_prover_transcript_to_verifier_transcript (const std::shared_ptr< BaseTranscript > &prover_transcript)
 Convert a prover transcript to a verifier transcript.
 
template<typename T >
static std::vector< DataTypeserialize (const T &element)
 
template<typename T >
static T deserialize (std::span< const DataType > frs)
 
static std::shared_ptr< BaseTranscripttest_prover_init_empty ()
 For testing: initializes transcript with some arbitrary data so that a challenge can be generated after initialization. Only intended to be used by Prover.
 
static std::shared_ptr< BaseTranscripttest_verifier_init_empty (const std::shared_ptr< BaseTranscript > &transcript)
 For testing: initializes transcript based on proof data then receives junk data produced by BaseTranscript::test_prover_init_empty(). Only intended to be used by Verifier.
 

Static Public Attributes

static constexpr bool in_circuit = InCircuit<DataType>
 
static constexpr size_t CHALLENGE_BUFFER_SIZE = 2
 

Protected Member Functions

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.
 
template<typename T >
void serialize_to_buffer (const T &element, Proof &proof_data)
 Serializes object and appends it to proof_data.
 
template<typename 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.
 

Protected Attributes

Proof proof_data
 

Private Member Functions

DataType get_next_challenge_hash ()
 Compute the next challenge c_next = H( Compress(c_prev || round_buffer) ).
 
std::array< DataType, CHALLENGE_BUFFER_SIZEget_next_duplex_challenge_buffer ()
 Compute the next challenge and split it into the two 127-bit limbs of the short-challenge buffer.
 

Private Attributes

size_t transcript_index = 0
 
size_t round_index = 0
 
bool challenge_generation_phase = false
 
bool is_first_challenge = true
 
DataType previous_challenge {}
 
std::vector< DataTypecurrent_round_data
 
std::ptrdiff_t proof_start = 0
 
size_t num_frs_written = 0
 
size_t num_frs_read = 0
 
bool use_manifest = false
 
TranscriptManifest manifest
 

Friends

template<typename T >
OriginTag bb::extract_transcript_tag (const T &transcript)
 

Detailed Description

template<typename Codec_, typename HashFunction_>
class bb::BaseTranscript< Codec_, HashFunction_ >

Common transcript class for both parties. Stores the data for the current round, as well as the manifest.

Definition at line 41 of file transcript.hpp.

Member Typedef Documentation

◆ Codec

template<typename Codec_ , typename HashFunction_ >
using bb::BaseTranscript< Codec_, HashFunction_ >::Codec = Codec_

Definition at line 43 of file transcript.hpp.

◆ DataType

template<typename Codec_ , typename HashFunction_ >
using bb::BaseTranscript< Codec_, HashFunction_ >::DataType = typename Codec::DataType

Definition at line 45 of file transcript.hpp.

◆ HashFunction

template<typename Codec_ , typename HashFunction_ >
using bb::BaseTranscript< Codec_, HashFunction_ >::HashFunction = HashFunction_

Definition at line 44 of file transcript.hpp.

◆ Proof

template<typename Codec_ , typename HashFunction_ >
using bb::BaseTranscript< Codec_, HashFunction_ >::Proof = std::vector<DataType>

Definition at line 46 of file transcript.hpp.

Constructor & Destructor Documentation

◆ BaseTranscript() [1/2]

template<typename Codec_ , typename HashFunction_ >
bb::BaseTranscript< Codec_, HashFunction_ >::BaseTranscript ( )
inline

Definition at line 53 of file transcript.hpp.

◆ BaseTranscript() [2/2]

template<typename Codec_ , typename HashFunction_ >
bb::BaseTranscript< Codec_, HashFunction_ >::BaseTranscript ( const Proof proof)
inlineexplicit

Definition at line 62 of file transcript.hpp.

Member Function Documentation

◆ add_element_frs_to_hash_buffer()

template<typename Codec_ , typename HashFunction_ >
void bb::BaseTranscript< Codec_, HashFunction_ >::add_element_frs_to_hash_buffer ( const std::string &  label,
std::span< const DataType element_frs 
)
inlineprotected

Adds challenge elements to the current_round_buffer and updates the manifest.

Parameters
labelof the element sent
element_frsserialized

Definition at line 147 of file transcript.hpp.

◆ add_to_hash_buffer()

template<typename Codec_ , typename HashFunction_ >
template<class T >
void bb::BaseTranscript< Codec_, HashFunction_ >::add_to_hash_buffer ( const std::string &  label,
const T &  element 
)
inline

Adds an element to the transcript.

Serializes the element to frs and adds it to the current_round_data buffer. Does NOT add the element to the proof.

Parameters
labelHuman-readable name for the challenge.
elementElement to be added.

Definition at line 365 of file transcript.hpp.

◆ convert_prover_transcript_to_verifier_transcript()

template<typename Codec_ , typename HashFunction_ >
static std::shared_ptr< BaseTranscript > bb::BaseTranscript< Codec_, HashFunction_ >::convert_prover_transcript_to_verifier_transcript ( const std::shared_ptr< BaseTranscript< Codec_, HashFunction_ > > &  prover_transcript)
inlinestatic

Convert a prover transcript to a verifier transcript.

Parameters
prover_transcriptThe prover transcript to convert
Returns
std::shared_ptr<BaseTranscript> The verifier transcript

Definition at line 479 of file transcript.hpp.

◆ deserialize()

template<typename Codec_ , typename HashFunction_ >
template<typename T >
static T bb::BaseTranscript< Codec_, HashFunction_ >::deserialize ( std::span< const DataType frs)
inlinestatic

Definition at line 496 of file transcript.hpp.

◆ deserialize_from_buffer()

template<typename Codec_ , typename HashFunction_ >
template<typename T >
T bb::BaseTranscript< Codec_, HashFunction_ >::deserialize_from_buffer ( const Proof proof_data,
size_t &  offset 
) const
inlineprotected

Deserializes the frs starting at offset into the typed element and returns that element.

Using the template parameter and the offset argument, this function deserializes the frs with from_buffer and then increments the offset appropriately based on the number of frs that were deserialized.

Template Parameters
T
Parameters
proof_data
offset
Returns
T

Definition at line 179 of file transcript.hpp.

◆ enable_manifest()

template<typename Codec_ , typename HashFunction_ >
void bb::BaseTranscript< Codec_, HashFunction_ >::enable_manifest ( )
inline

Definition at line 225 of file transcript.hpp.

◆ export_proof()

template<typename Codec_ , typename HashFunction_ >
std::vector< DataType > bb::BaseTranscript< Codec_, HashFunction_ >::export_proof ( )
inline

Return the proof data starting at proof_start.

This function returns the elements of the transcript in the interval [proof_start : proof_start + num_frs_written] and then updates proof_start. It is useful for when two provers share a transcript, as calling export_proof at the end of each provers' code returns the slices T_1, T_2 of the transcript that must be loaded by the verifiers via load_proof.

Definition at line 202 of file transcript.hpp.

◆ generate_challenges()

template<typename Codec_ , typename HashFunction_ >
template<typename ChallengeType >
std::vector< ChallengeType > bb::BaseTranscript< Codec_, HashFunction_ >::generate_challenges ( std::span< const std::string >  labels,
auto &&  fill 
)
inline

Shared core of get_challenges / get_short_challenges: the bookkeeping common to every challenge round (manifest, free-witness sanitization, phase flag, origin tags). The full and short paths differ only in how the challenge vector is filled, which the caller supplies via fill.

Parameters
fillCallback that populates the (pre-sized) challenge vector; it owns the squeeze loop.

Definition at line 235 of file transcript.hpp.

◆ get_challenge()

template<typename Codec_ , typename HashFunction_ >
template<typename ChallengeType >
ChallengeType bb::BaseTranscript< Codec_, HashFunction_ >::get_challenge ( const std::string &  label)
inline

Generate a single full-width (~254-bit) challenge for label (the default). See get_challenges.

Parameters
labelHuman-readable name for the challenge (recorded in the manifest).

Definition at line 452 of file transcript.hpp.

◆ get_challenges() [1/2]

template<typename Codec_ , typename HashFunction_ >
template<typename ChallengeType , size_t N>
std::array< ChallengeType, N > bb::BaseTranscript< Codec_, HashFunction_ >::get_challenges ( const std::array< std::string, N > &  labels)
inline

Wrapper around get_challenges to handle array of challenges.

Parameters
arrayof labels human-readable names for the challenges for the manifest
Returns
std::array<ChallengeType, N> challenges for this round.

Definition at line 327 of file transcript.hpp.

◆ get_challenges() [2/2]

template<typename Codec_ , typename HashFunction_ >
template<typename ChallengeType >
std::vector< ChallengeType > bb::BaseTranscript< Codec_, HashFunction_ >::get_challenges ( std::span< const std::string >  labels)
inline

Generate full-width (~254-bit) challenges for the given labels (the default).

One transcript hash yields one full field challenge.

Parameters
labelshuman-readable names for the challenges for the manifest
Returns
std::vector<ChallengeType> challenges for this round.

Definition at line 297 of file transcript.hpp.

◆ get_dyadic_powers_of_challenge()

template<typename Codec_ , typename HashFunction_ >
template<typename ChallengeType >
std::vector< ChallengeType > bb::BaseTranscript< Codec_, HashFunction_ >::get_dyadic_powers_of_challenge ( const std::string &  label,
size_t  num_challenges 
)
inline

Get a challenge and compute its dyadic powers [δ, δ², δ⁴, ..., δ^(2^(num_challenges-1))].

Generates num_challenges elements where each element is the square of the previous one. This is Step 2 of the protocol as written in the Protogalaxy paper.

Parameters
labelHuman-readable name for the challenge
num_challengesNumber of power-of-2 powers to generate
Returns
Vector of num_challenges elements: [δ, δ², δ⁴, δ⁸, ...]

Definition at line 345 of file transcript.hpp.

◆ get_manifest()

template<typename Codec_ , typename HashFunction_ >
const TranscriptManifest & bb::BaseTranscript< Codec_, HashFunction_ >::get_manifest ( ) const
inline

Definition at line 501 of file transcript.hpp.

◆ get_next_challenge_hash()

template<typename Codec_ , typename HashFunction_ >
DataType bb::BaseTranscript< Codec_, HashFunction_ >::get_next_challenge_hash ( )
inlineprivate

Compute the next challenge c_next = H( Compress(c_prev || round_buffer) ).

Computes a new challenge for the current round using the previous challenge and the current round data, if they exist. Clears current_round_data after hashing to minimize how much we compress, and sets previous_challenge to the new challenge to set up the next call.

Returns
DataType

Definition at line 97 of file transcript.hpp.

◆ get_next_duplex_challenge_buffer()

template<typename Codec_ , typename HashFunction_ >
std::array< DataType, CHALLENGE_BUFFER_SIZE > bb::BaseTranscript< Codec_, HashFunction_ >::get_next_duplex_challenge_buffer ( )
inlineprivate

Compute the next challenge and split it into the two 127-bit limbs of the short-challenge buffer.

Thin wrapper around get_next_challenge_hash.

Returns
std::array<DataType, CHALLENGE_BUFFER_SIZE>

Definition at line 135 of file transcript.hpp.

◆ get_proof_size()

template<typename Codec_ , typename HashFunction_ >
size_t bb::BaseTranscript< Codec_, HashFunction_ >::get_proof_size ( )
inline

Definition at line 222 of file transcript.hpp.

◆ get_short_challenge()

template<typename Codec_ , typename HashFunction_ >
template<typename ChallengeType >
ChallengeType bb::BaseTranscript< Codec_, HashFunction_ >::get_short_challenge ( const std::string &  label)
inline

Generate a single short (127-bit) challenge for label. See get_short_challenges.

Definition at line 464 of file transcript.hpp.

◆ get_short_challenges() [1/2]

template<typename Codec_ , typename HashFunction_ >
template<typename ChallengeType , size_t N>
std::array< ChallengeType, N > bb::BaseTranscript< Codec_, HashFunction_ >::get_short_challenges ( const std::array< std::string, N > &  labels)
inline

Wrapper around get_short_challenges to handle array of challenges.

Definition at line 311 of file transcript.hpp.

◆ get_short_challenges() [2/2]

template<typename Codec_ , typename HashFunction_ >
template<typename ChallengeType >
std::vector< ChallengeType > bb::BaseTranscript< Codec_, HashFunction_ >::get_short_challenges ( std::span< const std::string >  labels)
inline

Generate short (127-bit) challenges for the given labels.

Each transcript hash is split into two 127-bit limbs, so two challenges are produced per hash. get_challenges (full width) is the default.

Parameters
labelshuman-readable names for the challenges for the manifest
Returns
std::vector<ChallengeType> challenges for this round.

Definition at line 272 of file transcript.hpp.

◆ load_proof()

template<typename Codec_ , typename HashFunction_ >
void bb::BaseTranscript< Codec_, HashFunction_ >::load_proof ( const std::vector< DataType > &  proof)
inline

Verifier-specific method. The verifier needs to load a proof or its segment before the verification.

Parameters
proof

Definition at line 216 of file transcript.hpp.

◆ print()

template<typename Codec_ , typename HashFunction_ >
void bb::BaseTranscript< Codec_, HashFunction_ >::print ( )
inline

Definition at line 503 of file transcript.hpp.

◆ receive_from_prover()

template<typename Codec_ , typename HashFunction_ >
template<class T >
T bb::BaseTranscript< Codec_, HashFunction_ >::receive_from_prover ( const std::string &  label)
inline

Reads the next element of type T from the transcript, with a predefined label, only used by verifier.

Parameters
labelHuman readable name for the challenge.
Returns
deserialized element of type T

Definition at line 418 of file transcript.hpp.

◆ send_to_verifier()

template<typename Codec_ , typename HashFunction_ >
template<class T >
void bb::BaseTranscript< Codec_, HashFunction_ >::send_to_verifier ( const std::string &  label,
const T &  element 
)
inline

Adds a prover message to the transcript, only intended to be used by the prover.

Serializes the provided object into proof_data, and updates the current round state in add_element_frs_to_hash_buffer.

Parameters
labelDescription/name of the object being added.
elementSerializable object that will be added to the transcript
Todo:
Use a concept to only allow certain types to be passed. Requirements are that the object should be serializable.

Definition at line 394 of file transcript.hpp.

◆ serialize()

template<typename Codec_ , typename HashFunction_ >
template<typename T >
static std::vector< DataType > bb::BaseTranscript< Codec_, HashFunction_ >::serialize ( const T &  element)
inlinestatic

Definition at line 491 of file transcript.hpp.

◆ serialize_to_buffer()

template<typename Codec_ , typename HashFunction_ >
template<typename T >
void bb::BaseTranscript< Codec_, HashFunction_ >::serialize_to_buffer ( const T &  element,
Proof proof_data 
)
inlineprotected

Serializes object and appends it to proof_data.

Calls to_buffer on element to serialize, and modifies proof_data object by appending the serialized frs to it.

Template Parameters
T
Parameters
element
proof_data

Definition at line 165 of file transcript.hpp.

◆ test_get_proof_data() [1/2]

template<typename Codec_ , typename HashFunction_ >
Proof & bb::BaseTranscript< Codec_, HashFunction_ >::test_get_proof_data ( )
inline

Test utility: Get mutable reference to proof_data.

Used by test utilities that need to deserialize/serialize proof structure

Definition at line 561 of file transcript.hpp.

◆ test_get_proof_data() [2/2]

template<typename Codec_ , typename HashFunction_ >
const Proof & bb::BaseTranscript< Codec_, HashFunction_ >::test_get_proof_data ( ) const
inline

Definition at line 562 of file transcript.hpp.

◆ test_get_proof_start()

template<typename Codec_ , typename HashFunction_ >
std::ptrdiff_t bb::BaseTranscript< Codec_, HashFunction_ >::test_get_proof_start ( ) const
inline

Test utility: Get proof_start for validation.

Used by test fixtures to verify transcript conversion

Definition at line 555 of file transcript.hpp.

◆ test_prover_init_empty()

template<typename Codec_ , typename HashFunction_ >
static std::shared_ptr< BaseTranscript > bb::BaseTranscript< Codec_, HashFunction_ >::test_prover_init_empty ( )
inlinestatic

For testing: initializes transcript with some arbitrary data so that a challenge can be generated after initialization. Only intended to be used by Prover.

Returns
BaseTranscript

Definition at line 519 of file transcript.hpp.

◆ test_set_proof_parsing_state()

template<typename Codec_ , typename HashFunction_ >
void bb::BaseTranscript< Codec_, HashFunction_ >::test_set_proof_parsing_state ( std::ptrdiff_t  start,
size_t  written 
)
inline

Test utility: Set proof parsing state for export after deserialization.

Used by test utilities that need to re-export proofs after tampering

Definition at line 545 of file transcript.hpp.

◆ test_verifier_init_empty()

template<typename Codec_ , typename HashFunction_ >
static std::shared_ptr< BaseTranscript > bb::BaseTranscript< Codec_, HashFunction_ >::test_verifier_init_empty ( const std::shared_ptr< BaseTranscript< Codec_, HashFunction_ > > &  transcript)
inlinestatic

For testing: initializes transcript based on proof data then receives junk data produced by BaseTranscript::test_prover_init_empty(). Only intended to be used by Verifier.

Parameters
transcript
Returns
BaseTranscript

Definition at line 534 of file transcript.hpp.

Friends And Related Symbol Documentation

◆ bb::extract_transcript_tag

template<typename Codec_ , typename HashFunction_ >
template<typename T >
OriginTag bb::extract_transcript_tag ( const T &  transcript)
friend

Member Data Documentation

◆ CHALLENGE_BUFFER_SIZE

template<typename Codec_ , typename HashFunction_ >
constexpr size_t bb::BaseTranscript< Codec_, HashFunction_ >::CHALLENGE_BUFFER_SIZE = 2
staticconstexpr

Definition at line 51 of file transcript.hpp.

◆ challenge_generation_phase

template<typename Codec_ , typename HashFunction_ >
bool bb::BaseTranscript< Codec_, HashFunction_ >::challenge_generation_phase = false
private

Definition at line 74 of file transcript.hpp.

◆ current_round_data

template<typename Codec_ , typename HashFunction_ >
std::vector<DataType> bb::BaseTranscript< Codec_, HashFunction_ >::current_round_data
private

Definition at line 79 of file transcript.hpp.

◆ in_circuit

template<typename Codec_ , typename HashFunction_ >
constexpr bool bb::BaseTranscript< Codec_, HashFunction_ >::in_circuit = InCircuit<DataType>
staticconstexpr

Definition at line 49 of file transcript.hpp.

◆ is_first_challenge

template<typename Codec_ , typename HashFunction_ >
bool bb::BaseTranscript< Codec_, HashFunction_ >::is_first_challenge = true
private

Definition at line 77 of file transcript.hpp.

◆ manifest

template<typename Codec_ , typename HashFunction_ >
TranscriptManifest bb::BaseTranscript< Codec_, HashFunction_ >::manifest
private

Definition at line 88 of file transcript.hpp.

◆ num_frs_read

template<typename Codec_ , typename HashFunction_ >
size_t bb::BaseTranscript< Codec_, HashFunction_ >::num_frs_read = 0
private

Definition at line 84 of file transcript.hpp.

◆ num_frs_written

template<typename Codec_ , typename HashFunction_ >
size_t bb::BaseTranscript< Codec_, HashFunction_ >::num_frs_written = 0
private

Definition at line 83 of file transcript.hpp.

◆ previous_challenge

template<typename Codec_ , typename HashFunction_ >
DataType bb::BaseTranscript< Codec_, HashFunction_ >::previous_challenge {}
private

Definition at line 78 of file transcript.hpp.

◆ proof_data

template<typename Codec_ , typename HashFunction_ >
Proof bb::BaseTranscript< Codec_, HashFunction_ >::proof_data
protected

Definition at line 65 of file transcript.hpp.

◆ proof_start

template<typename Codec_ , typename HashFunction_ >
std::ptrdiff_t bb::BaseTranscript< Codec_, HashFunction_ >::proof_start = 0
private

Definition at line 82 of file transcript.hpp.

◆ round_index

template<typename Codec_ , typename HashFunction_ >
size_t bb::BaseTranscript< Codec_, HashFunction_ >::round_index = 0
private

Definition at line 73 of file transcript.hpp.

◆ transcript_index

template<typename Codec_ , typename HashFunction_ >
size_t bb::BaseTranscript< Codec_, HashFunction_ >::transcript_index = 0
private

Definition at line 72 of file transcript.hpp.

◆ use_manifest

template<typename Codec_ , typename HashFunction_ >
bool bb::BaseTranscript< Codec_, HashFunction_ >::use_manifest = false
private

Definition at line 87 of file transcript.hpp.


The documentation for this class was generated from the following file: