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