Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
flavor.cpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Completed, auditors: [Federico], commit: 0e37cb8}
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6#include "flavor.hpp"
8
9namespace bb::avm2 {
10
11AvmFlavor::ProverPolynomials::ProverPolynomials(ProvingKey& proving_key)
12{
13 for (auto [prover_poly, key_poly] : zip_view(this->get_unshifted(), proving_key.get_all())) {
15 prover_poly = key_poly.share();
16 }
17 for (auto [prover_poly, key_poly] : zip_view(this->get_shifted(), proving_key.get_to_be_shifted())) {
18 BB_ASSERT_EQ(flavor_get_label(*this, prover_poly), (flavor_get_label(proving_key, key_poly) + "_shift"));
19 prover_poly = key_poly.shifted();
20 }
21}
22
23// Deserializes the proof from proof_data. Length must match COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS in flavor.hpp.
24void AvmFlavor::Transcript::deserialize_full_transcript()
25{
26 size_t num_frs_read = 0;
27
28 for (auto& commitment : commitments) {
29 commitment = deserialize_from_buffer<Commitment>(proof_data, num_frs_read);
30 }
31
32 for (size_t i = 0; i < log_circuit_size; ++i) {
33 sumcheck_univariates.emplace_back(deserialize_from_buffer<bb::Univariate<FF, BATCHED_RELATION_PARTIAL_LENGTH>>(
34 Transcript::proof_data, num_frs_read));
35 }
36
37 sumcheck_evaluations =
38 deserialize_from_buffer<std::array<FF, NUM_ALL_ENTITIES>>(Transcript::proof_data, num_frs_read);
39
40 for (size_t i = 0; i < log_circuit_size - 1; ++i) {
41 gemini_fold_comms.push_back(deserialize_from_buffer<Commitment>(proof_data, num_frs_read));
42 }
43
44 for (size_t i = 0; i < log_circuit_size; ++i) {
45 gemini_fold_evals.push_back(deserialize_from_buffer<FF>(proof_data, num_frs_read));
46 }
47
48 shplonk_q_comm = deserialize_from_buffer<Commitment>(proof_data, num_frs_read);
49
50 kzg_w_comm = deserialize_from_buffer<Commitment>(proof_data, num_frs_read);
51}
52
53// Serializes the proof to proof_data. Length must match COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS in flavor.hpp.
54void AvmFlavor::Transcript::serialize_full_transcript()
55{
56 size_t old_proof_length = proof_data.size();
58
59 for (const auto& commitment : commitments) {
60 serialize_to_buffer(commitment, Transcript::proof_data);
61 }
62
63 for (size_t i = 0; i < log_circuit_size; ++i) {
64 serialize_to_buffer(sumcheck_univariates[i], Transcript::proof_data);
65 }
66
67 serialize_to_buffer(sumcheck_evaluations, Transcript::proof_data);
68
69 for (size_t i = 0; i < log_circuit_size - 1; ++i) {
70 serialize_to_buffer(gemini_fold_comms[i], proof_data);
71 }
72
73 for (size_t i = 0; i < log_circuit_size; ++i) {
74 serialize_to_buffer(gemini_fold_evals[i], proof_data);
75 }
76
77 serialize_to_buffer(shplonk_q_comm, proof_data);
78 serialize_to_buffer(kzg_w_comm, proof_data);
79
80 // sanity check to make sure we generate the same length of proof as before.
81 BB_ASSERT_EQ(proof_data.size(), old_proof_length);
82}
83
84AvmFlavor::ProverPolynomials::ProverPolynomials(const ProverPolynomials& full_polynomials, size_t circuit_size)
85{
86 for (auto [poly, full_poly] : zip_view(get_all(), full_polynomials.get_all())) {
87 // After the initial sumcheck round, the new size is CEIL(size/2).
88 size_t desired_size = (full_poly.end_index() / 2) + (full_poly.end_index() % 2);
89 poly = Polynomial(desired_size, circuit_size / 2);
90 }
91}
92
94 : commitment_key(circuit_size) {
95 // The proving key's polynomials are not allocated here because they are later overwritten
96 // AvmComposer::compute_witness(). We should probably refactor this flow.
97 };
98
100{
101 current_edge = edge_idx;
102 // If the current edge changed, we need to clear all the cached univariates.
103 dirty = true;
104}
105
108{
109 const auto& multivariate = multivariates.get(c);
110 if (multivariate.is_empty() || multivariate.end_index() < current_edge) {
111 static const auto zero_univariate = bb::Univariate<FF, MAX_PARTIAL_RELATION_LENGTH>::zero();
112 return zero_univariate;
113 } else {
114 auto& mutable_entities = const_cast<decltype(entities)&>(entities);
115 if (dirty) {
116 // If the current edge changed, we need to clear all the cached univariates.
117 for (auto& extended_ptr : mutable_entities) {
118 extended_ptr.reset();
119 }
120 dirty = false;
121 }
122 auto& extended_ptr = mutable_entities[static_cast<size_t>(c)];
123 if (extended_ptr.get() == nullptr) {
125 bb::Univariate<FF, 2>({ multivariate[current_edge], multivariate[current_edge + 1] })
126 .template extend_to<MAX_PARTIAL_RELATION_LENGTH>());
127 }
128 return *extended_ptr;
129 }
130}
131
132} // namespace bb::avm2
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:83
A univariate polynomial represented by its values on {0, 1,..., domain_end - 1}.
static Univariate zero()
std::span< DataType > get_all()
Definition flavor.hpp:132
DataType & get(ColumnAndShifts c)
Definition flavor.hpp:145
const bb::Univariate< FF, MAX_PARTIAL_RELATION_LENGTH > & get(ColumnAndShifts c) const
Definition flavor.cpp:107
A container for the prover polynomials handles.
Definition flavor.hpp:249
std::span< Polynomial > get_all()
Definition flavor.hpp:210
AvmFlavorSettings::Polynomial Polynomial
Definition flavor.hpp:44
ColumnAndShifts
Definition columns.hpp:34
std::string flavor_get_label(Container &&container, const Element &element)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13