Skip to content

Commit 4644a84

Browse files
feat: reduce CRS and polynomial memory for non-ZK proofs (#20731)
## Summary - Size the CRS at `max_end_index` (actual data extent) rather than `dyadic_size` for non-ZK proofs - Make Shplonk quotient polynomial `round_up_power_2` conditional on having non-dyadic claims (Libra/sumcheck), which only exist in the ZK path - Size PolynomialBatcher's temporary polynomials (`batched_unshifted`, `batched_to_be_shifted`, `A_0_pos`, `A_0_neg`) at `actual_data_size` rather than `full_batched_size` ## Benchmark results Chonk AMM flow (`ecdsar1+amm_add_liquidity_1_recursions+sponsored_fpc`), `max_end_index=278,429` vs `dyadic_size=524,288`: ``` bb prove --scheme chonk --ivc_inputs_path .../ecdsar1+amm_add_liquidity_1_recursions+sponsored_fpc/ivc-inputs.msgpack -o /tmp/out ``` | | Run 1 (KB) | Run 2 (KB) | Run 3 (KB) | Avg (MiB) | |---|---|---|---|---| | Before | 702,660 | 704,044 | 691,328 | 682.7 | | After | 657,096 | 672,916 | 677,508 | 654.8 | | **Savings** | **45,564** | **31,128** | **13,820** | **~28 MiB (~4.1%)** | ## Test plan - [x] `ultra_honk_bench` at 2^20 passes (no crash) - [x] `ultra_honk_tests`: 260 passed, 5 skipped - [x] `commitment_schemes_tests`: 82 passed, 2 skipped - [ ] CI passes
1 parent c572e95 commit 4644a84

6 files changed

Lines changed: 34 additions & 14 deletions

File tree

barretenberg/cpp/src/barretenberg/commitment_schemes/gemini/gemini.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ template <typename Curve> class GeminiProver_ {
125125
class PolynomialBatcher {
126126

127127
size_t full_batched_size = 0; // size of the full batched polynomial (generally the circuit size)
128+
size_t actual_data_size_ = 0; // max end_index across all polynomials (actual data extent)
128129

129130
Polynomial batched_unshifted; // linear combination of unshifted polynomials
130131
Polynomial batched_to_be_shifted_by_one; // linear combination of to-be-shifted polynomials
@@ -133,10 +134,11 @@ template <typename Curve> class GeminiProver_ {
133134
RefVector<Polynomial> unshifted; // set of unshifted polynomials
134135
RefVector<Polynomial> to_be_shifted_by_one; // set of polynomials to be left shifted by 1
135136

136-
PolynomialBatcher(const size_t full_batched_size)
137+
PolynomialBatcher(const size_t full_batched_size, const size_t actual_data_size = 0)
137138
: full_batched_size(full_batched_size)
138-
, batched_unshifted(full_batched_size)
139-
, batched_to_be_shifted_by_one(Polynomial::shiftable(full_batched_size))
139+
, actual_data_size_(actual_data_size == 0 ? full_batched_size : actual_data_size)
140+
, batched_unshifted(actual_data_size_, full_batched_size)
141+
, batched_to_be_shifted_by_one(Polynomial::shiftable(actual_data_size_, full_batched_size))
140142
{}
141143

142144
bool has_unshifted() const { return unshifted.size() > 0; }
@@ -191,8 +193,8 @@ template <typename Curve> class GeminiProver_ {
191193
*/
192194
std::pair<Polynomial, Polynomial> compute_partially_evaluated_batch_polynomials(const Fr& r_challenge)
193195
{
194-
// Initialize A₀₊ and compute A₀₊ += F as necessary
195-
Polynomial A_0_pos(full_batched_size); // A₀₊
196+
// Initialize A₀₊ with only the actual data extent; virtual zeroes cover the rest
197+
Polynomial A_0_pos(actual_data_size_, full_batched_size); // A₀₊
196198

197199
if (has_unshifted()) {
198200
A_0_pos += batched_unshifted; // A₀₊ += F

barretenberg/cpp/src/barretenberg/commitment_schemes/shplonk/shplonk.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ template <typename Curve> class ShplonkProver_ {
6161
max_poly_size = std::max(max_poly_size, claim.polynomial.size());
6262
}
6363
}
64-
// The polynomials in Sumcheck Round claims and Libra opening claims are generally not dyadic,
65-
// so we round up to the next power of 2.
66-
max_poly_size = numeric::round_up_power_2(max_poly_size);
67-
6864
// Q(X) = ∑ⱼ νʲ ⋅ ( fⱼ(X) − vⱼ) / ( X − xⱼ )
6965
Polynomial Q(max_poly_size);
7066
Polynomial tmp(max_poly_size);

barretenberg/cpp/src/barretenberg/flavor/prover_polynomials.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ class ProverPolynomialsBase : public AllEntitiesBase {
6464
}
6565
}
6666

67+
// Returns the maximum end_index across all polynomials (i.e. the actual data extent)
68+
[[nodiscard]] size_t max_end_index() const
69+
{
70+
size_t result = 0;
71+
for (const auto& poly : this->get_all()) {
72+
result = std::max(result, poly.end_index());
73+
}
74+
return result;
75+
}
76+
6777
void increase_polynomials_virtual_size(const size_t size_in)
6878
{
6979
for (auto& polynomial : this->get_all()) {

barretenberg/cpp/src/barretenberg/polynomials/polynomial.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,13 @@ template <typename Fr> Polynomial<Fr>& Polynomial<Fr>::operator+=(PolynomialSpan
186186

187187
template <typename Fr> Fr Polynomial<Fr>::evaluate(const Fr& z) const
188188
{
189-
BB_ASSERT(size() == virtual_size());
190-
return polynomial_arithmetic::evaluate(data(), z, size());
189+
// Evaluate only the backing data; virtual zeroes beyond backing contribute nothing.
190+
// When start_index > 0, multiply by z^start_index to account for the offset.
191+
Fr result = polynomial_arithmetic::evaluate(data(), z, size());
192+
if (start_index() > 0) {
193+
result *= z.pow(start_index());
194+
}
195+
return result;
191196
}
192197

193198
template <typename Fr> Fr Polynomial<Fr>::evaluate_mle(std::span<const Fr> evaluation_points, bool shift) const

barretenberg/cpp/src/barretenberg/ultra_honk/oink_prover.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace bb {
2222
template <typename Flavor> void OinkProver<Flavor>::prove()
2323
{
2424
BB_BENCH_NAME("OinkProver::prove");
25-
commitment_key = CommitmentKey(prover_instance->dyadic_size());
25+
commitment_key = CommitmentKey(prover_instance->polynomials.max_end_index());
2626
send_vk_hash_and_public_inputs();
2727
commit_to_masking_poly();
2828
commit_to_wires();

barretenberg/cpp/src/barretenberg/ultra_honk/ultra_prover.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,15 @@ template <typename Flavor> void UltraProver_<Flavor>::generate_gate_challenges()
6060

6161
template <typename Flavor> typename UltraProver_<Flavor>::Proof UltraProver_<Flavor>::construct_proof()
6262
{
63-
size_t key_size = prover_instance->dyadic_size();
63+
// The CRS only needs to accommodate the actual data extent (max_end_index) rather than the
64+
// full dyadic_size. All committed polynomials fit within this bound: witness/selector polys
65+
// have backing ≤ max_end_index, Gemini fold polys have size ≤ dyadic_size/2 < max_end_index,
66+
// Shplonk quotient Q is sized at max(claim sizes), and KZG opening proof is sized at Q.size().
67+
// For ZK, the gemini_masking_poly (at dyadic_size) is already reflected in max_end_index.
68+
size_t key_size = prover_instance->polynomials.max_end_index();
6469
if constexpr (Flavor::HasZK) {
70+
// SmallSubgroupIPA commits fixed-size polynomials (up to SUBGROUP_SIZE + 3). Ensure the
71+
// CRS is large enough for tiny test circuits where max_end_index may be smaller.
6572
constexpr size_t log_subgroup_size = static_cast<size_t>(numeric::get_msb(Curve::SUBGROUP_SIZE));
6673
key_size = std::max(key_size, size_t{ 1 } << (log_subgroup_size + 1));
6774
}
@@ -120,7 +127,7 @@ template <typename Flavor> void UltraProver_<Flavor>::execute_pcs()
120127

121128
auto& ck = commitment_key;
122129

123-
PolynomialBatcher polynomial_batcher(prover_instance->dyadic_size());
130+
PolynomialBatcher polynomial_batcher(prover_instance->dyadic_size(), prover_instance->polynomials.max_end_index());
124131
polynomial_batcher.set_unshifted(prover_instance->polynomials.get_unshifted());
125132
polynomial_batcher.set_to_be_shifted_by_one(prover_instance->polynomials.get_to_be_shifted());
126133

0 commit comments

Comments
 (0)