Skip to content

Commit de4cc0a

Browse files
authored
feat: fix AVM circuit size (#16332)
Remove circuit size and num_public_inputs from AVM. Use the fixed AVM size everywhere.
2 parents 6831eb6 + 41a7d43 commit de4cc0a

File tree

18 files changed

+59
-128
lines changed

18 files changed

+59
-128
lines changed

barretenberg/cpp/src/barretenberg/client_ivc/client_ivc.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ ClientIVC::ClientIVC(size_t num_circuits, TraceSettings trace_settings)
2828
size_t commitment_key_size =
2929
std::max(trace_settings.dyadic_size(), 1UL << TranslatorFlavor::CONST_TRANSLATOR_LOG_N);
3030
info("BN254 commitment key size: ", commitment_key_size);
31-
// TODO(https://github.com/AztecProtocol/barretenberg/issues/1420): pass commitment keys by value
3231
bn254_commitment_key = CommitmentKey<curve::BN254>(commitment_key_size);
3332
}
3433

@@ -458,7 +457,6 @@ void ClientIVC::accumulate(ClientCircuit& circuit, const std::shared_ptr<MegaVer
458457
// If the current circuit overflows past the current size of the commitment key, reinitialize accordingly.
459458
// TODO(https://github.com/AztecProtocol/barretenberg/issues/1319)
460459
if (proving_key->dyadic_size() > bn254_commitment_key.dyadic_size) {
461-
// TODO(https://github.com/AztecProtocol/barretenberg/issues/1420): pass commitment keys by value
462460
bn254_commitment_key = CommitmentKey<curve::BN254>(proving_key->dyadic_size());
463461
goblin.commitment_key = bn254_commitment_key;
464462
}

barretenberg/cpp/src/barretenberg/dsl/acir_format/avm2_recursion_constraint.cpp

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ namespace {
4646
*/
4747
void create_dummy_vkey_and_proof(Builder& builder,
4848
[[maybe_unused]] size_t proof_size,
49-
size_t public_inputs_size,
5049
const std::vector<field_ct>& key_fields,
5150
const std::vector<field_ct>& proof_fields)
5251
{
@@ -70,27 +69,7 @@ void create_dummy_vkey_and_proof(Builder& builder,
7069
offset++;
7170
};
7271

73-
// Relevant source for proof layout: AvmFlavor::Transcript::serialize_full_transcript()
74-
// TODO(#13390): Revive this assertion (and remove the >= 0 one) once we freeze the number of colums in AVM.
75-
// assert((proof_size - Flavor::NUM_WITNESS_ENTITIES * Flavor::NUM_FRS_COM -
76-
// (Flavor::NUM_ALL_ENTITIES + 1) * Flavor::NUM_FRS_FR - Flavor::NUM_FRS_COM) %
77-
// (Flavor::NUM_FRS_COM + Flavor::NUM_FRS_FR * (Flavor::BATCHED_RELATION_PARTIAL_LENGTH + 1)) ==
78-
// 0);
79-
80-
// Derivation of circuit size based on the proof
81-
// TODO#13390): Revive the following code once we freeze the number of colums in AVM.
82-
// const auto log_circuit_size =
83-
// (proof_size - Flavor::NUM_WITNESS_ENTITIES * Flavor::NUM_FRS_COM -
84-
// (Flavor::NUM_ALL_ENTITIES + 1) * Flavor::NUM_FRS_FR - Flavor::NUM_FRS_COM) /
85-
// (Flavor::NUM_FRS_COM + Flavor::NUM_FRS_FR * (Flavor::BATCHED_RELATION_PARTIAL_LENGTH + 1));
86-
const auto log_circuit_size = numeric::get_msb(avm2::CIRCUIT_SUBGROUP_SIZE);
87-
88-
// First key field is log circuit size
89-
builder.set_variable(key_fields[0].witness_index, log_circuit_size);
90-
// Second key field is number of public inputs
91-
builder.set_variable(key_fields[1].witness_index, public_inputs_size);
92-
93-
size_t offset = 2;
72+
size_t offset = 0;
9473
for (size_t i = 0; i < Flavor::NUM_PRECOMPUTED_ENTITIES; ++i) {
9574
set_dummy_commitment(key_fields, offset);
9675
}
@@ -106,7 +85,7 @@ void create_dummy_vkey_and_proof(Builder& builder,
10685
}
10786

10887
// now the univariates
109-
for (size_t i = 0; i < CONST_PROOF_SIZE_LOG_N * Flavor::BATCHED_RELATION_PARTIAL_LENGTH; i++) {
88+
for (size_t i = 0; i < avm2::MAX_AVM_TRACE_LOG_SIZE * Flavor::BATCHED_RELATION_PARTIAL_LENGTH; i++) {
11089
set_dummy_evaluation_in_proof_fields(offset);
11190
}
11291

@@ -116,12 +95,12 @@ void create_dummy_vkey_and_proof(Builder& builder,
11695
}
11796

11897
// now the gemini fold commitments which are CONST_PROOF_SIZE_LOG_N - 1
119-
for (size_t i = 1; i < CONST_PROOF_SIZE_LOG_N; i++) {
98+
for (size_t i = 1; i < avm2::MAX_AVM_TRACE_LOG_SIZE; i++) {
12099
set_dummy_commitment(proof_fields, offset);
121100
}
122101

123102
// the gemini fold evaluations which are CONST_PROOF_SIZE_LOG_N
124-
for (size_t i = 0; i < CONST_PROOF_SIZE_LOG_N; i++) {
103+
for (size_t i = 0; i < avm2::MAX_AVM_TRACE_LOG_SIZE; i++) {
125104
set_dummy_evaluation_in_proof_fields(offset);
126105
}
127106

@@ -160,7 +139,7 @@ HonkRecursionConstraintOutput<Builder> create_avm2_recursion_constraints_goblin(
160139

161140
// Populate the key fields and proof fields with dummy values to prevent issues (e.g. points must be on curve).
162141
if (!has_valid_witness_assignments) {
163-
create_dummy_vkey_and_proof(builder, input.proof.size(), input.public_inputs.size(), key_fields, proof_fields);
142+
create_dummy_vkey_and_proof(builder, input.proof.size(), key_fields, proof_fields);
164143
}
165144

166145
// Execute the Goblin AVM2 recursive verifier

barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/circuit_builder_base_impl.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ template <typename FF_> void CircuitBuilderBase<FF_>::failure(std::string msg)
236236
{
237237
#ifndef FUZZING_DISABLE_WARNINGS
238238
if (!has_dummy_witnesses) {
239-
// We have a builder failure when we have real witnesses which is a mistake.
240-
info("(Experimental) WARNING: Builder failure when we have real witnesses!"); // not a catch-all error
239+
// Not a catch-all error log. We have a builder failure when we have real witnesses which is a mistake.
240+
info("(Experimental) WARNING: Builder failure when we have real witnesses! Ignore if writing vk.");
241241
}
242242
#endif
243243
_failed = true;

barretenberg/cpp/src/barretenberg/sumcheck/sumcheck.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@ template <typename Flavor> class SumcheckProver {
322322
CommitmentKey ck;
323323

324324
if constexpr (IsGrumpkinFlavor<Flavor>) {
325-
// TODO(https://github.com/AztecProtocol/barretenberg/issues/1420): pass commitment keys by value
326325
ck = CommitmentKey(BATCHED_RELATION_PARTIAL_LENGTH);
327326
// Compute the vector {0, 1, \ldots, BATCHED_RELATION_PARTIAL_LENGTH-1} needed to transform the round
328327
// univariates from Lagrange to monomial basis

barretenberg/cpp/src/barretenberg/vm2/common/constants.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
namespace bb::avm2 {
88

9-
constexpr uint32_t CIRCUIT_SUBGROUP_SIZE = 1 << 21;
9+
constexpr size_t MAX_AVM_TRACE_LOG_SIZE = 21;
10+
constexpr size_t MAX_AVM_TRACE_SIZE = 1 << MAX_AVM_TRACE_LOG_SIZE;
1011

1112
// Also used for op_id in the circuit trace
1213
enum class BitwiseOperation : uint8_t {

barretenberg/cpp/src/barretenberg/vm2/constraining/flavor.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,24 @@ AvmFlavor::ProverPolynomials::ProverPolynomials(ProvingKey& proving_key)
1818
void AvmFlavor::Transcript::deserialize_full_transcript()
1919
{
2020
size_t num_frs_read = 0;
21-
circuit_size = deserialize_from_buffer<uint32_t>(proof_data, num_frs_read);
2221

2322
for (auto& commitment : commitments) {
2423
commitment = deserialize_from_buffer<Commitment>(proof_data, num_frs_read);
2524
}
2625

27-
for (size_t i = 0; i < CONST_PROOF_SIZE_LOG_N; ++i) {
26+
for (size_t i = 0; i < log_circuit_size; ++i) {
2827
sumcheck_univariates.emplace_back(deserialize_from_buffer<bb::Univariate<FF, BATCHED_RELATION_PARTIAL_LENGTH>>(
2928
Transcript::proof_data, num_frs_read));
3029
}
3130

3231
sumcheck_evaluations =
3332
deserialize_from_buffer<std::array<FF, NUM_ALL_ENTITIES>>(Transcript::proof_data, num_frs_read);
3433

35-
for (size_t i = 0; i < CONST_PROOF_SIZE_LOG_N - 1; ++i) {
34+
for (size_t i = 0; i < log_circuit_size - 1; ++i) {
3635
gemini_fold_comms.push_back(deserialize_from_buffer<Commitment>(proof_data, num_frs_read));
3736
}
3837

39-
for (size_t i = 0; i < CONST_PROOF_SIZE_LOG_N; ++i) {
38+
for (size_t i = 0; i < log_circuit_size; ++i) {
4039
gemini_fold_evals.push_back(deserialize_from_buffer<FF>(proof_data, num_frs_read));
4140
}
4241

@@ -50,23 +49,21 @@ void AvmFlavor::Transcript::serialize_full_transcript()
5049
size_t old_proof_length = proof_data.size();
5150
Transcript::proof_data.clear();
5251

53-
serialize_to_buffer(circuit_size, Transcript::proof_data);
54-
5552
for (const auto& commitment : commitments) {
5653
serialize_to_buffer(commitment, Transcript::proof_data);
5754
}
5855

59-
for (size_t i = 0; i < CONST_PROOF_SIZE_LOG_N; ++i) {
56+
for (size_t i = 0; i < log_circuit_size; ++i) {
6057
serialize_to_buffer(sumcheck_univariates[i], Transcript::proof_data);
6158
}
6259

6360
serialize_to_buffer(sumcheck_evaluations, Transcript::proof_data);
6461

65-
for (size_t i = 0; i < CONST_PROOF_SIZE_LOG_N - 1; ++i) {
62+
for (size_t i = 0; i < log_circuit_size - 1; ++i) {
6663
serialize_to_buffer(gemini_fold_comms[i], proof_data);
6764
}
6865

69-
for (size_t i = 0; i < CONST_PROOF_SIZE_LOG_N; ++i) {
66+
for (size_t i = 0; i < log_circuit_size; ++i) {
7067
serialize_to_buffer(gemini_fold_evals[i], proof_data);
7168
}
7269

@@ -95,12 +92,8 @@ AvmFlavor::PartiallyEvaluatedMultivariates::PartiallyEvaluatedMultivariates(cons
9592
}
9693
}
9794

98-
AvmFlavor::ProvingKey::ProvingKey(const size_t circuit_size, const size_t num_public_inputs)
99-
: circuit_size(circuit_size)
100-
, log_circuit_size(numeric::get_msb(circuit_size))
101-
, num_public_inputs(num_public_inputs)
102-
// TODO(https://github.com/AztecProtocol/barretenberg/issues/1420): pass commitment keys by value
103-
, commitment_key(circuit_size + 1) {
95+
AvmFlavor::ProvingKey::ProvingKey()
96+
: commitment_key(circuit_size + 1) {
10497
// The proving key's polynomials are not allocated here because they are later overwritten
10598
// AvmComposer::compute_witness(). We should probably refactor this flow.
10699
};
@@ -112,7 +105,7 @@ AvmFlavor::ProvingKey::ProvingKey(const size_t circuit_size, const size_t num_pu
112105
*/
113106
std::vector<AvmFlavor::FF> AvmFlavor::VerificationKey::to_field_elements() const
114107
{
115-
std::vector<FF> elements = { FF(log_circuit_size), FF(num_public_inputs) };
108+
std::vector<FF> elements;
116109

117110
for (auto const& comm : get_all()) {
118111
std::vector<FF> comm_as_fields = field_conversion::convert_to_bn254_frs(comm);

barretenberg/cpp/src/barretenberg/vm2/constraining/flavor.hpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "barretenberg/transcript/transcript.hpp"
1414

1515
#include "barretenberg/vm2/common/aztec_constants.hpp"
16+
#include "barretenberg/vm2/common/constants.hpp"
1617
#include "barretenberg/vm2/constraining/entities.hpp"
1718
#include "barretenberg/vm2/constraining/flavor_settings.hpp"
1819

@@ -102,7 +103,7 @@ class AvmFlavor {
102103
// to see its value and then update `AVM_V2_PROOF_LENGTH_IN_FIELDS` in constants.nr.
103104
static constexpr size_t COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS =
104105
(NUM_WITNESS_ENTITIES + 1) * NUM_FRS_COM + (NUM_ALL_ENTITIES + 1) * NUM_FRS_FR +
105-
CONST_PROOF_SIZE_LOG_N * (NUM_FRS_COM + NUM_FRS_FR * (BATCHED_RELATION_PARTIAL_LENGTH + 1));
106+
MAX_AVM_TRACE_LOG_SIZE * (NUM_FRS_COM + NUM_FRS_FR * (BATCHED_RELATION_PARTIAL_LENGTH + 1));
106107

107108
static_assert(AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED >= COMPUTED_AVM_PROOF_LENGTH_IN_FIELDS,
108109
"\n The constant AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED is now too short\n"
@@ -197,7 +198,7 @@ class AvmFlavor {
197198

198199
class Transcript : public NativeTranscript {
199200
public:
200-
uint32_t circuit_size;
201+
size_t log_circuit_size = MAX_AVM_TRACE_LOG_SIZE;
201202

202203
std::array<Commitment, NUM_WITNESS_ENTITIES> commitments;
203204

@@ -219,18 +220,13 @@ class AvmFlavor {
219220
using FF = typename Polynomial::FF;
220221
DEFINE_COMPOUND_GET_ALL(PrecomputedEntities<Polynomial>, WitnessEntities<Polynomial>);
221222

222-
ProvingKey() = default;
223-
ProvingKey(const size_t circuit_size, const size_t num_public_inputs);
223+
size_t circuit_size = MAX_AVM_TRACE_SIZE; // Fixed size
224+
size_t log_circuit_size = MAX_AVM_TRACE_LOG_SIZE;
224225

225-
size_t circuit_size = 0;
226-
size_t log_circuit_size = 0;
227-
size_t num_public_inputs = 0;
226+
ProvingKey();
228227

229228
CommitmentKey commitment_key;
230229

231-
// Offset off the public inputs from the start of the execution trace
232-
size_t pub_inputs_offset = 0;
233-
234230
// The number of public inputs has to be the same for all instances because they are
235231
// folded element by element.
236232
std::vector<FF> public_inputs;
@@ -248,19 +244,17 @@ class AvmFlavor {
248244
VerificationKey() = default;
249245

250246
VerificationKey(const std::shared_ptr<ProvingKey>& proving_key)
251-
: NativeVerificationKey_(proving_key->circuit_size, static_cast<size_t>(proving_key->num_public_inputs))
252247
{
248+
this->log_circuit_size = MAX_AVM_TRACE_LOG_SIZE;
253249
for (auto [polynomial, commitment] :
254250
zip_view(proving_key->get_precomputed_polynomials(), this->get_all())) {
255251
commitment = proving_key->commitment_key.commit(polynomial);
256252
}
257253
}
258254

259-
VerificationKey(const size_t circuit_size,
260-
const size_t num_public_inputs,
261-
std::array<Commitment, NUM_PRECOMPUTED_COMMITMENTS> const& precomputed_cmts)
262-
: NativeVerificationKey_(circuit_size, num_public_inputs)
255+
VerificationKey(std::array<Commitment, NUM_PRECOMPUTED_COMMITMENTS> const& precomputed_cmts)
263256
{
257+
this->log_circuit_size = MAX_AVM_TRACE_LOG_SIZE;
264258
for (auto [vk_cmt, cmt] : zip_view(this->get_all(), precomputed_cmts)) {
265259
vk_cmt = cmt;
266260
}

barretenberg/cpp/src/barretenberg/vm2/constraining/polynomials.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ AvmProver::ProverPolynomials compute_polynomials(tracegen::TraceContainer& trace
2929

3030
poly = AvmProver::Polynomial(
3131
/*memory size*/ allocated_size,
32-
/*largest possible index*/ CIRCUIT_SUBGROUP_SIZE,
32+
/*largest possible index*/ MAX_AVM_TRACE_SIZE, // TODO(#16660): use real size?
3333
/*make shiftable with offset*/ 1);
3434
}
3535
}));
@@ -55,7 +55,7 @@ AvmProver::ProverPolynomials compute_polynomials(tracegen::TraceContainer& trace
5555
// WARNING! Column-Polynomials order matters!
5656
Column col = static_cast<Column>(i);
5757
const auto num_rows = trace.get_column_rows(col);
58-
poly = AvmProver::Polynomial::create_non_parallel_zero_init(num_rows, CIRCUIT_SUBGROUP_SIZE);
58+
poly = AvmProver::Polynomial::create_non_parallel_zero_init(num_rows, MAX_AVM_TRACE_SIZE);
5959
});
6060
}));
6161

barretenberg/cpp/src/barretenberg/vm2/constraining/prover.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "barretenberg/honk/proof_system/logderivative_library.hpp"
1010
#include "barretenberg/relations/permutation_relation.hpp"
1111
#include "barretenberg/sumcheck/sumcheck.hpp"
12+
#include "barretenberg/vm2/common/constants.hpp"
1213
#include "barretenberg/vm2/tooling/stats.hpp"
1314

1415
namespace bb::avm2 {
@@ -107,15 +108,15 @@ void AvmProver::execute_relation_check_rounds()
107108

108109
// Generate gate challenges
109110
std::vector<FF> gate_challenges =
110-
transcript->template get_powers_of_challenge<FF>("Sumcheck:gate_challenge", CONST_PROOF_SIZE_LOG_N);
111+
transcript->template get_powers_of_challenge<FF>("Sumcheck:gate_challenge", key->log_circuit_size);
111112

112113
Sumcheck sumcheck(key->circuit_size,
113114
prover_polynomials,
114115
transcript,
115116
alpha,
116117
gate_challenges,
117118
relation_parameters,
118-
CONST_PROOF_SIZE_LOG_N);
119+
key->log_circuit_size);
119120

120121
sumcheck_output = sumcheck.prove();
121122
}

barretenberg/cpp/src/barretenberg/vm2/constraining/recursion/recursive_flavor.hpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,9 @@ class AvmRecursiveFlavor {
6363
class VerificationKey
6464
: public StdlibVerificationKey_<CircuitBuilder, NativeFlavor::PrecomputedEntities<Commitment>> {
6565
public:
66+
size_t log_fixed_circuit_size = MAX_AVM_TRACE_LOG_SIZE;
6667
VerificationKey(CircuitBuilder* builder, const std::shared_ptr<NativeVerificationKey>& native_key)
6768
{
68-
this->log_circuit_size = FF::from_witness(builder, native_key->log_circuit_size);
69-
this->num_public_inputs = FF::from_witness(builder, native_key->num_public_inputs);
70-
7169
for (auto [native_comm, comm] : zip_view(native_key->get_all(), this->get_all())) {
7270
comm = Commitment::from_witness(builder, native_comm);
7371
}
@@ -82,16 +80,8 @@ class AvmRecursiveFlavor {
8280
VerificationKey(CircuitBuilder& builder, std::span<const FF> elements)
8381
{
8482
size_t num_frs_read = 0;
85-
size_t num_frs_FF = stdlib::field_conversion::calc_num_bn254_frs<CircuitBuilder, FF>();
8683
size_t num_frs_Comm = stdlib::field_conversion::calc_num_bn254_frs<CircuitBuilder, Commitment>();
8784

88-
this->log_circuit_size = stdlib::field_conversion::convert_from_bn254_frs<CircuitBuilder, FF>(
89-
builder, elements.subspan(num_frs_read, num_frs_FF));
90-
num_frs_read += num_frs_FF;
91-
this->num_public_inputs = stdlib::field_conversion::convert_from_bn254_frs<CircuitBuilder, FF>(
92-
builder, elements.subspan(num_frs_read, num_frs_FF));
93-
num_frs_read += num_frs_FF;
94-
9585
for (Commitment& comm : this->get_all()) {
9686
comm = stdlib::field_conversion::convert_from_bn254_frs<CircuitBuilder, Commitment>(
9787
builder, elements.subspan(num_frs_read, num_frs_Comm));

0 commit comments

Comments
 (0)