feat: add file-backed constraint system loading#1781
Draft
sergeytimoshin wants to merge 2 commits into
Draft
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds file-backed loading for serialized constraint systems:
ReadFromFile(path)support to curve-typed constraint systems.ReadFrompath on unsupported platforms.groth16.ReadCSFromFile(curveID, path)for R1CS.plonk.ReadCSFromFile(curveID, path)for SparseR1CS.Why
Today
ReadFromreads the whole serialized constraint-system payload into a heap[]bytebefore decoding it. For large circuits this creates a temporary memory spike: the process holds both the serialized payload copy and the decoded constraint-system structures during load.When the source is already a file, this PR removes that intermediate heap copy on mmap-capable platforms. The decoded R1CS/SparseR1CS still owns normal Go heap structures after loading, so the mmap can be closed before returning. This is intentionally a load-time peak-memory reduction, not a long-lived mmap-backed constraint-system representation.
This is useful independently of mmap-backed Groth16 proving keys: users who persist compiled R1CS or SparseR1CS artifacts can reduce load-time memory pressure even when using the existing proving-key formats.
API Notes
The PR does not add methods to
constraint.ConstraintSystem. That avoids breaking external implementations of the interface. Instead, the fast file path is exposed through backend-level helpers next to the existingNewCSconstructors, and the generated concrete types also getReadFromFile.Benchmark
Local benchmark on Apple M4 Max, darwin/arm64:
The benchmark shows the expected reduction in heap bytes from avoiding the serialized-payload copy. Runtime is comparable; allocation count has two extra small allocations from file mapping.
Validation
Passed:
Checklist Notes
ReadCSFromFileand generatedReadFromFile; the PR body explains intended usage and limitations.