Skip to content

Conversation

Tabaie
Copy link
Contributor

@Tabaie Tabaie commented Sep 24, 2025

Improves the amount of heap allocations in the benchmark of a 2^16 BLS12-377 element long hash down to 1798.62 MB on an hpc6a.48xlarge machine. This constitutes a 20% improvement over the linea-monorepo baseline (2267.45 MB.)

Most of the improvement was achieved by introducing a reusable pool / stack for temporary field element variables, and is generic to any use of the GKR API.
A further reduction was due to a hard-coded implementation of the most commonly used MiMC gate in BLS12-377, which is (state + msg + key)^17.

Log Instance Size computeGJ Solve Total
14 51.05 MB 1102.33 MB 1153.38 MB
15 103.09 MB 1244.49 MB 1347.58 MB
16 228.72 MB 1569.90 MB 1798.62 MB
17 388.39 MB 2943.39 MB 3331.78 MB
18 871.35 MB 4127.38 MB 4998.73 MB
19 1657.63 MB 6413.27 MB 8070.9 MB
20 3352.88 MB 12711.18 MB 16064.06 MB
21 6.63 GB 21.47 GB 28.1 GB
22 13.01 GB 42.69 GB 55.7 GB
23 26.06 GB 83.92 GB 109.98 GB
gkr-mimc mem

I believe that for small instance sizes (< 2^16) the cost is dominated by the GKR verifier and the PLONK prover itself. From that point on, the slope of the fitted line (<1) suggests an at most linear rate of growth.


Note

Introduce a pooled, pointer-based GateAPI (with SumExp17) and plumb it through GKR/MiMC paths to cut heap allocations; update engine hints, templates, SNARK wrapper, and tests/benchmarks.

  • GKR backend/runtime:
    • Add stateful gateAPI with a synchronous memory pool (allocated, nbUsed), pointer receivers, newElement, freeElements, and method cast refactor.
    • Introduce SumExp17 in GateAPI; add FrontendAPIWrapper.SumExp17 for in-circuit use; update interface in std/gkrapi/gkr/types.go.
    • Replace global api usage with local gateAPI instances and pass by pointer in evaluation paths (computeGJ, degree checks, equality tests, hints, Complete).
    • Update SNARK-side calls to wire.Gate.Evaluate to use FrontendAPIWrapper.
    • Engine hints: switch gateAPI to hold mod *big.Int; fix modular reductions.
  • MiMC permutation:
    • Convert round constants to frontend.Variable per curve; register gates using new types.
    • Use SumExp17 path for degree-17 S-box; keep pow5/pow7 forms.
  • Generators/templates and curve backends:
    • Regenerate GKR files across fields to adopt pooled gateAPI and pointer API.
    • Add freeElements() calls in loops hot paths.
  • Tests/benchmarks:
    • Update MiMC tests to new backend APIs; broaden curves/backends; rename hash tree bench to Merkle tree and modernize runner.

Written by Cursor Bugbot for commit f6613ce. This will update automatically on new commits. Configure here.

@Tabaie Tabaie marked this pull request as ready for review September 25, 2025 04:09
@Tabaie Tabaie requested a review from Copilot September 25, 2025 04:09
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR optimizes memory usage in the GKR-MiMC implementation by introducing memory pooling for field element allocations and adding a specialized SumExp17 operation. The changes result in a 20% reduction in heap allocation (from 2267.45 MB to 1798.62 MB) for BLS12-377 benchmarks.

  • Memory pool implementation in gateAPI to reduce heap allocations
  • New SumExp17 method for optimized computation of (a+b+c)^17 operations
  • Refactoring from global to instance-based API usage patterns

Reviewed Changes

Copilot reviewed 33 out of 33 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
std/gkrapi/gkr/types.go Adds SumExp17 method to GateAPI interface
std/gkrapi/compile.go Wraps API with FrontendAPIWrapper for gate evaluation
internal/gkr/gkr.go Adds FrontendAPIWrapper with SumExp17 implementation
std/permutation/gkr-mimc/gkr-mimc.go Optimizes addPow17 function with BLS12-377 specific caching
Multiple internal/gkr/*/gkr.go Implements memory pooling in gateAPI across all curve implementations
Multiple internal/gkr/*/solver_hints.go Updates to use instance-based gateAPI
Test files Renames hashTreeCircuit to merkleTreeCircuit and improves test configuration

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@Tabaie Tabaie requested a review from ivokub September 25, 2025 04:12
cursor[bot]

This comment was marked as outdated.

@ivokub ivokub added the feat: gkr PRs related to GKR label Sep 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: gkr PRs related to GKR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants