Skip to content

Improve circuit setup phase - #74

Merged
frozenspider merged 4 commits into
mainfrom
fs/bigint-to-fq
Sep 22, 2026
Merged

frozenspider merged 4 commits into
mainfrom
fs/bigint-to-fq

Conversation

@frozenspider

@frozenspider frozenspider commented Sep 21, 2026

Copy link
Copy Markdown
Collaborator

Resolves #45, also partially implements #44

This PR makes R1CS circuit setup faster, use less peak memory and do significantly less allocation:
(Using circuit_setup benchmark for measurement)

sha256-2kb Before After
Time ~700 ms ~300 ms
Max alloc 260 MB 140 MB
Total alloc 2505 MB 372 MB

Summary of changes:

  • Tweaked bigint_to_fq to use LazyLock to only map Q100 ($2^{100} - 15$) to BigInt once instead of on every invocation. Part in matrix setup: 92% -> 88%.
  • Parallelized SparseMatrix::map_values_with.
  • Replaced BTreeMap/BTreeSet in LinearCombination and BoolLinearCombination with Vec that are kept sorted and deduplicated.
  • Improved efficiency of ConstraintGenerator::bitz_unsigned.
  • Additionally, map_values_with now takes a function by value instead of function reference - this is a style change, performance is unaffected.

Sampling with:

$ CARGO_PROFILE_BENCH_DEBUG=line-tables-only RUSTFLAGS="-C target-cpu=native -C force-frame-pointers=yes" cargo bench -p bitz-cli --bench circuits --no-run
$ samply record -- target/release/deps/circuits-<HASH> --bench circuit_setup --sample-count 20 --sample-size 1

Final measurements done with:

RUSTFLAGS="-C target-cpu=native -C force-frame-pointers=yes" cargo bench -p bitz-cli --bench circuits -- circuit_setup --sample-count 20 --sample-size 1

@frozenspider frozenspider changed the title [WIP] Improve prime field mapping in R1CS matrix setup [WIP] Improve circuit setup. Sep 21, 2026
@frozenspider frozenspider changed the title [WIP] Improve circuit setup. Improve circuit setup phase Sep 21, 2026
@frozenspider
frozenspider marked this pull request as ready for review September 21, 2026 19:55

@zkfriendly zkfriendly left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, I was able to reproduce the results

@zkfriendly

Copy link
Copy Markdown
Collaborator

Noticed the CI is failing; can we address that here before merging?

@frozenspider

Copy link
Copy Markdown
Collaborator Author

@zkfriendly: this is a separate issue with our test suite being quite borked (see #63, but it's not just that).
Addressing it might not be trivial, so I'd prefer to do it separately.

@frozenspider
frozenspider merged commit 31faeef into main Sep 22, 2026
2 of 3 checks passed
@frozenspider
frozenspider deleted the fs/bigint-to-fq branch September 22, 2026 13:00
@frozenspider frozenspider mentioned this pull request Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hoist the modulus out of bigint_to_fq and parallelise the lowering pass

2 participants