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 >:
TestTranscript bb::ECCVMFlavor::IPATranscript bb::avm2::AvmFlavor::Transcript

Public Types

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

Public Member Functions

 BaseTranscript ()
 
std::vector< DataTypeexport_proof ()
 Return the proof data starting at proof_start.
 
void load_proof (const std::vector< DataType > &proof)
 
size_t size_proof_data ()
 Return the size of proof_data.
 
void enable_manifest ()
 Enables the manifest.
 
template<typename ChallengeType , typename... Strings>
std::array< ChallengeType, sizeof...(Strings)> get_challenges (const Strings &... labels)
 After all the prover messages have been sent, finalize the round by hashing all the data and then create the number of requested challenges.
 
template<typename ChallengeType , typename String , std::size_t N>
std::array< ChallengeType, N > get_challenges (std::array< String, N > const &labels)
 Wrapper around get_challenges to handle array of challenges.
 
template<typename ChallengeType >
std::vector< ChallengeType > compute_round_challenge_pows (const size_t num_powers, const ChallengeType &round_challenge)
 Given δ, compute the vector [δ, δ^2,..., δ^2^num_powers].
 
template<typename ChallengeType , typename String >
std::vector< ChallengeType > get_powers_of_challenge (const String &label, size_t num_challenges)
 
template<class T >
void add_to_independent_hash_buffer (const std::string &label, const T &element)
 Adds an element to an independent hash buffer.
 
DataType hash_independent_buffer ()
 Hashes the independent hash buffer and clears it.
 
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)
 
TranscriptManifest get_manifest () const
 
void print ()
 
BaseTranscript branch_transcript ()
 Branch a transcript to perform verifier-only computations.
 

Static Public Member Functions

static DataType hash (const std::vector< DataType > &data)
 Static hash method that forwards to Codec hash.
 
template<typename T >
static std::vector< DataTypeserialize (const T &element)
 Serialize a size_t to a vector of field elements.
 
template<typename T >
static T deserialize (std::span< const DataType > frs)
 
template<typename T >
static size_t calc_num_data_types ()
 
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.
 
static std::shared_ptr< BaseTranscriptprover_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< BaseTranscriptverifier_init_empty (const std::shared_ptr< BaseTranscript > &transcript)
 For testing: initializes transcript based on proof data then receives junk data produced by BaseTranscript::prover_init_empty(). Only intended to be used by Verifier.
 

Public Attributes

size_t transcript_index = 0
 
size_t round_index = 0
 
bool reception_phase = true
 
std::ptrdiff_t proof_start = 0
 
size_t num_frs_written = 0
 
size_t num_frs_read = 0
 
size_t round_number = 0
 

Static Public Attributes

static constexpr bool in_circuit = InCircuit<DataType>
 

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

std::array< DataType, 2 > get_next_duplex_challenge_buffer (size_t num_challenges)
 Compute next challenge c_next = H( Compress(c_prev || round_buffer) )
 

Private Attributes

bool is_first_challenge = true
 
DataType previous_challenge {}
 
std::vector< DataTypecurrent_round_data
 
std::vector< DataTypeindependent_hash_buffer
 
bool use_manifest = false
 
TranscriptManifest manifest
 

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 54 of file transcript.hpp.

Member Typedef Documentation

◆ Codec

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

Definition at line 56 of file transcript.hpp.

◆ DataType

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

Definition at line 57 of file transcript.hpp.

◆ Proof

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

Definition at line 58 of file transcript.hpp.

Constructor & Destructor Documentation

◆ BaseTranscript()

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

Definition at line 73 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 156 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 427 of file transcript.hpp.

◆ add_to_independent_hash_buffer()

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

Adds an element to an independent hash buffer.

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

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

Definition at line 369 of file transcript.hpp.

◆ branch_transcript()

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

Branch a transcript to perform verifier-only computations.

This function takes the current state of a transcript and creates a new transcript that starts from that state. In this way, computations that are not part of the prover's transcript (e.g., computations that can be used to perform calculations more efficiently) will not affect the verifier's transcript.

If transcript = (.., previous_challenge), then for soundness it is enough that branched_transcript = (previous_challenge, ...) However, there are a few implementation details we need to take into account:

  1. branched_transcript will interact with witnesses that come from transcript. To prevent the tool that detects FS bugs from raising an error, we must ensure that branched_transcript.transcript_index = transcript.transcript_index.
  2. To aid debugging, we set branched_transcript.round_index = transcript.round_index, so that it is clear that branched_transcript builds on the current state of transcript.
  3. To aid debugging, we increase transcript.round_index by BRANCHING_JUMP, so that there is a gap between what happens before and after the transcript is branched.
  4. To ensure soundness: a. We add to the hash buffer of branched_transcript the value transcript.previous_challenge b. We enforce ASSERT(current_round_data.empty())
Note
We could remove 4.b and add to the hash buffer of branched_transcript both transcript.previous_challenge and transcript.current_round_data. However, this would conflict with 3 (as the round in transcript is not finished yet). There seems to be no reason why the branching cannot happen after the round is concluded, so we choose this implementation.

The relation between the transcript and the branched transcript is the following:

round_index transcript branched_transcript 0 * 1 | | | | | n * ================= *

n+BRANCHING_JUMP * | n+6 | | | | | ... ... ...

Returns
BaseTranscript

Definition at line 650 of file transcript.hpp.

◆ calc_num_data_types()

template<typename Codec_ , typename HashFunction >
template<typename T >
static size_t bb::BaseTranscript< Codec_, HashFunction >::calc_num_data_types ( )
inlinestatic

Definition at line 262 of file transcript.hpp.

◆ compute_round_challenge_pows()

template<typename Codec_ , typename HashFunction >
template<typename ChallengeType >
std::vector< ChallengeType > bb::BaseTranscript< Codec_, HashFunction >::compute_round_challenge_pows ( const size_t  num_powers,
const ChallengeType &  round_challenge 
)
inline

Given δ, compute the vector [δ, δ^2,..., δ^2^num_powers].

This is Step 2 of the protocol as written in the Protogalaxy paper.

Definition at line 344 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 548 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 257 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 188 of file transcript.hpp.

◆ enable_manifest()

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

Enables the manifest.

Definition at line 234 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 209 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

Definition at line 587 of file transcript.hpp.

◆ get_challenges() [1/2]

template<typename Codec_ , typename HashFunction >
template<typename ChallengeType , typename... Strings>
std::array< ChallengeType, sizeof...(Strings)> bb::BaseTranscript< Codec_, HashFunction >::get_challenges ( const Strings &...  labels)
inline

After all the prover messages have been sent, finalize the round by hashing all the data and then create the number of requested challenges.

Challenges are generated by iteratively hashing over the previous challenge, using get_next_challenge_buffer(). Note that the pairs of challenges will be 128 and 126 bits, as in they will be [128, 126, 128, 126, ...].

Parameters
labelshuman-readable names for the challenges for the manifest
Returns
std::array<Fr, num_challenges> challenges for this round.

Definition at line 275 of file transcript.hpp.

◆ get_challenges() [2/2]

template<typename Codec_ , typename HashFunction >
template<typename ChallengeType , typename String , std::size_t N>
std::array< ChallengeType, N > bb::BaseTranscript< Codec_, HashFunction >::get_challenges ( std::array< String, N > const &  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 333 of file transcript.hpp.

◆ get_manifest()

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

Definition at line 597 of file transcript.hpp.

◆ get_next_duplex_challenge_buffer()

template<typename Codec_ , typename HashFunction >
std::array< DataType, 2 > bb::BaseTranscript< Codec_, HashFunction >::get_next_duplex_challenge_buffer ( size_t  num_challenges)
inlineprivate

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

This function computes a new challenge for the current round using the previous challenge and the current round data, if they exist. It clears the current_round_data if nonempty after computing the challenge to minimize how much we compress. It also sets previous_challenge to the current challenge buffer to set up next function call.

Returns
std::array<DataType, 2>

Definition at line 107 of file transcript.hpp.

◆ get_powers_of_challenge()

template<typename Codec_ , typename HashFunction >
template<typename ChallengeType , typename String >
std::vector< ChallengeType > bb::BaseTranscript< Codec_, HashFunction >::get_powers_of_challenge ( const String &  label,
size_t  num_challenges 
)
inline

Definition at line 356 of file transcript.hpp.

◆ hash()

template<typename Codec_ , typename HashFunction >
static DataType bb::BaseTranscript< Codec_, HashFunction >::hash ( const std::vector< DataType > &  data)
inlinestatic

Static hash method that forwards to Codec hash.

This method allows hash to be called on the Transcript class directly, which is needed for verification key hashing.

Parameters
dataVector of field elements to hash
Returns
Fr Hash result

Definition at line 244 of file transcript.hpp.

◆ hash_independent_buffer()

template<typename Codec_ , typename HashFunction >
DataType bb::BaseTranscript< Codec_, HashFunction >::hash_independent_buffer ( )
inline

Hashes the independent hash buffer and clears it.

Returns
Fr The hash of the independent hash buffer.

Definition at line 405 of file transcript.hpp.

◆ load_proof()

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

Definition at line 218 of file transcript.hpp.

◆ print()

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

Definition at line 599 of file transcript.hpp.

◆ prover_init_empty()

template<typename Codec_ , typename HashFunction >
static std::shared_ptr< BaseTranscript > bb::BaseTranscript< Codec_, HashFunction >::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 564 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 493 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 471 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

Serialize a size_t to a vector of field elements.

Parameters
element
Returns
std::vector<DataType>

Definition at line 252 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 174 of file transcript.hpp.

◆ size_proof_data()

template<typename Codec_ , typename HashFunction >
size_t bb::BaseTranscript< Codec_, HashFunction >::size_proof_data ( )
inline

Return the size of proof_data.

Returns
size_t

Definition at line 228 of file transcript.hpp.

◆ verifier_init_empty()

template<typename Codec_ , typename HashFunction >
static std::shared_ptr< BaseTranscript > bb::BaseTranscript< Codec_, HashFunction >::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::prover_init_empty(). Only intended to be used by Verifier.

Parameters
transcript
Returns
BaseTranscript

Definition at line 579 of file transcript.hpp.

Member Data Documentation

◆ current_round_data

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

Definition at line 90 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 61 of file transcript.hpp.

◆ independent_hash_buffer

template<typename Codec_ , typename HashFunction >
std::vector<DataType> bb::BaseTranscript< Codec_, HashFunction >::independent_hash_buffer
private

Definition at line 92 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 87 of file transcript.hpp.

◆ manifest

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

Definition at line 97 of file transcript.hpp.

◆ num_frs_read

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

Definition at line 83 of file transcript.hpp.

◆ num_frs_written

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

Definition at line 82 of file transcript.hpp.

◆ previous_challenge

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

Definition at line 88 of file transcript.hpp.

◆ proof_data

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

Definition at line 148 of file transcript.hpp.

◆ proof_start

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

Definition at line 81 of file transcript.hpp.

◆ reception_phase

template<typename Codec_ , typename HashFunction >
bool bb::BaseTranscript< Codec_, HashFunction >::reception_phase = true

Definition at line 71 of file transcript.hpp.

◆ round_index

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

Definition at line 68 of file transcript.hpp.

◆ round_number

template<typename Codec_ , typename HashFunction >
size_t bb::BaseTranscript< Codec_, HashFunction >::round_number = 0

Definition at line 84 of file transcript.hpp.

◆ transcript_index

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

Definition at line 64 of file transcript.hpp.

◆ use_manifest

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

Definition at line 94 of file transcript.hpp.


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