Skip to content

perf: embed canonical k=11 params and fixed-base Lagrange tables (cold key build ~5-7x faster) - #12

Open
PastaPastaPasta wants to merge 2 commits into
dashifiedfrom
perf/embedded-params
Open

PastaPastaPasta wants to merge 2 commits into
dashifiedfrom
perf/embedded-params

Conversation

@PastaPastaPasta

@PastaPastaPasta PastaPastaPasta commented Oct 5, 2026 •

Copy link
Copy Markdown
Member

Why

A wallet has to build the Orchard ProvingKey before its first shielded send in each process. On a phone that cold build is seconds of latency on the first Send. Validators build the VerifyingKey at startup.

Most of that time goes to recomputing public, deterministic data:

  • Params::new(11): 2,048 hash-to-curve generators plus an inverse FFT over the curve, ≈300 ms on an M4 Pro.
  • Fixed-base interpolation tables. halo2_gadgets' default FixedPoint::lagrange_coeffs re-derives each fixed base's Lagrange interpolation coefficients from its generator every time. Key generation calls it for each of the 7 fixed-base multiplications in each synthesis, and a proving-key build synthesizes the circuit twice (keygen_vk + keygen_pk). That is ≈260 ms per synthesis.

What

Two commits embed this data as checked-in binary files and decode it instead of deriving it:

  1. perf(circuit): embed canonical k=11 Halo 2 params. src/circuit_data/vesta_params_k11.bin (131,140 B) is the canonical Params::write output, decoded with Params::read (≈22 ms). Both ProvingKey::build_for_version and VerifyingKey::build_for_version use it.
  2. perf(constants): embed fixed-base Lagrange interpolation coefficients. Seven tables (120,064 B) live in src/constants/fixed_bases/lagrange_coeffs/. lagrange_coeffs() overrides on OrchardFixedBasesFull, OrchardBaseFieldBases and OrchardShortScalarBases return them; decoding all seven takes ≈0.25 ms. SpendAuthGBase shares the full SpendAuthG table, since it has the same generator and window count.

Everything is public data: no keys, witnesses, randomness or trusted setup. The embedding is gated on the circuit feature, so builds without it are unaffected. File lengths are checked at compile time.

Keys and proofs are unchanged. Warm proving is unaffected, because the prover never evaluates fixed-column assignments.

Verification

  • embedded_params_match_derivation (always runs): the embedded bytes equal the Params::new(11) serialization byte-for-byte, and round-trip through read/write.
  • lagrange_coeffs_match_default_derivation (always runs): for every enum variant, the override equals compute_lagrange_coeffs(generator, NUM_WINDOWS) for that scalar kind. This covers both the values and which table each base uses.
  • The existing pinned verifying-key descriptions (circuit_description_fixed / _insecure) and the stored proof fixtures still pass, and would independently catch bad data.
  • Regenerate with cargo test --release --lib -- --ignored regenerate_embedded. It reproduces all 8 files byte-for-byte (SHA-256 checked).
  • cargo test --release: 84 passed, 2 ignored (the regenerators). Clippy (--all-features --all-targets) has no warnings; fmt --check and cargo doc are clean; --no-default-features and unstable-voting-circuits build.
  • The tests also pass against halo2_proofs 0.3.5 / pasta_curves 0.5.2.
  • Seeded proofs from an external harness (2 and 6 actions, 9 proofs) are byte-identical to dashified-0.14.1.

Benchmarks

Cold key build in a fresh process for each run.

Setup:

  • Host: Mac Studio, Apple M4 Max; load average ≈9–20 during the run.
  • Release build with fat LTO, one codegen unit and panic=abort, like Platform's release-ios profile.
  • Baseline (dashified-0.14.1) and this branch alternate, 8 runs each per thread count.
Rayon threads ProvingKey::build before → after (median, range) PK build CPU VerifyingKey::build before → after
2 1503 (1465–1534) → 295 (293–299) ms, −80% 2.19 → 0.50 s 1087 → 209 ms
6 1118 (1102–1143) → 167 (162–170) ms, −85% 2.24 → 0.55 s 729 → 100 ms
16 1040 (1018–1108) → 152 (148–172) ms, −85% 2.76 → 0.99 s 654 → 74 ms

In-process breakdown (M4 Pro):

Step Before After
Params new ≈302–329 ms read ≈22 ms
Lagrange tables ≈300 ms per synthesis ≈0.24 ms
keygen_vk 408–513 ms 100–137 ms
keygen_pk 430–499 ms 84–105 ms

Warm 2-action proving: CPU per proof unchanged (3.17 s vs 3.17 s).

Cost

≈245 KiB of read-only data when the circuit feature is enabled. A stripped release binary grows by a net ≈182 KB, because the derivation code is no longer linked.

Related

Part of a series on Dash shielded send latency. The prover-side work starts with dashpay/halo2#1 (compiled evaluator, −20–42% warm proof time). A follow-up PR will switch this crate's halo2 dependencies to dashpay/halo2.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Refactor
    • Proving and verification setup now uses precomputed canonical values, while fixed-base calculations use precomputed coefficient tables.
    • These changes preserve existing keys and calculation results; no user-visible behavior changes.

@coderabbitai

coderabbitai Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Files ignored due to path filters: 8

  • src/circuit_data/vesta_params_k11.bin is excluded by !**/*.bin
  • src/constants/fixed_bases/lagrange_coeffs/commit_ivk_r.bin is excluded by !**/*.bin
  • src/constants/fixed_bases/lagrange_coeffs/note_commit_r.bin is excluded by !**/*.bin
  • src/constants/fixed_bases/lagrange_coeffs/nullifier_k.bin is excluded by !**/*.bin
  • src/constants/fixed_bases/lagrange_coeffs/spend_auth_g.bin is excluded by !**/*.bin
  • src/constants/fixed_bases/lagrange_coeffs/spend_auth_g_short.bin is excluded by !**/*.bin
  • src/constants/fixed_bases/lagrange_coeffs/value_commit_r.bin is excluded by !**/*.bin
  • src/constants/fixed_bases/lagrange_coeffs/value_commit_v_short.bin is excluded by !**/*.bin
Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0df37a94-ba85-44c1-9e2a-f804d5e3228e
Commits

Reviewing files that changed from the base of the PR and between 153b23a and a1e1857.

Files ignored due to path filters: 8

  • src/circuit_data/vesta_params_k11.bin is excluded by !**/*.bin
  • src/constants/fixed_bases/lagrange_coeffs/commit_ivk_r.bin is excluded by !**/*.bin
  • src/constants/fixed_bases/lagrange_coeffs/note_commit_r.bin is excluded by !**/*.bin
  • src/constants/fixed_bases/lagrange_coeffs/nullifier_k.bin is excluded by !**/*.bin
  • src/constants/fixed_bases/lagrange_coeffs/spend_auth_g.bin is excluded by !**/*.bin
  • src/constants/fixed_bases/lagrange_coeffs/spend_auth_g_short.bin is excluded by !**/*.bin
  • src/constants/fixed_bases/lagrange_coeffs/value_commit_r.bin is excluded by !**/*.bin
  • src/constants/fixed_bases/lagrange_coeffs/value_commit_v_short.bin is excluded by !**/*.bin

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

The circuit builders now decode embedded canonical Halo 2 parameters. Fixed-base coefficient lookups now return embedded canonical tables. Tests compare the embedded data with values derived by Halo 2.

Changes: Embedded circuit data

Layer / File(s) Summary
Embedded parameters
src/circuit.rs, CHANGELOG.md
Proving- and verifying-key builders use decoded embedded parameters for K = 11. Tests compare the embedded bytes and their decoded round-trip with freshly derived parameters.
Fixed-base coefficient tables
src/constants/fixed_bases/lagrange_coeffs.rs, src/constants/fixed_bases.rs, CHANGELOG.md
Fixed-base types dispatch to embedded coefficient tables. The table decoder checks the expected byte length and canonical field values. Tests compare each fixed base with Halo 2’s default derivation. An ignored test regenerates the tables.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Merge Risk: Minimal · previous review 153b2

No actionable issue is established for the embedded circuit data; the change is mergeable after normal checks.

Security architecture risk: Low · up to 153b2

This changes how proof-related constants are supplied, not who may create or verify proofs. No new runtime attack path was identified, but the checked-in values were not independently confirmed against their canonical derivation during this review.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — An incorrect embedded release could affect every consumer constructing Orchard keys or using these fixed-base implementations. That exposure requires changing build-time data or the built program; the inspected runtime APIs do not let proof submitters replace the embedded bytes.

Trust Boundaries and Controls

  • observed — Untrusted proof bytes still enter the verification transcript and are checked against the supplied verifying key and public instances. Parameter embedding does not add an alternate verification path or remove the proving-key version check.

Resilience and Maintainability Implications

  • inferred — The derivation-comparison tests provide a control against release-data and dispatch drift. Encoding validation alone would not detect a structurally valid but incorrect table, so confidence in compatibility depends on confirming those comparisons.

Hardening Proposals

  • proposed — Require passing canonical-equivalence checks with the circuit feature enabled when publishing regenerated binaries or changing the parameter and coefficient derivation dependencies. This is an assurance proposal, not an observed missing release control.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 52.63% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 19 functions across 3 files. (1 skipped: … Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)

Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: embedding canonical parameters and fixed-base Lagrange tables to improve cold key-build performance.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 52.63% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 19 functions across 3 files. (1 skipped: 1 unsupported.)

Finishing Touches

Generate docstrings

  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

ℹ️ Recent review info

⚙️ Run configuration

  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 046b1b5f-feca-4222-ba45-9f764c64f54c
📥 Commits

Reviewing files that changed from the base of the PR and between cd044b8 and 153b23a.

⛔ Files ignored due to path filters (8)

  • src/circuit_data/vesta_params_k11.bin is excluded by !**/*.bin
  • src/constants/fixed_bases/lagrange_coeffs/commit_ivk_r.bin is excluded by !**/*.bin
  • src/constants/fixed_bases/lagrange_coeffs/note_commit_r.bin is excluded by !**/*.bin
  • src/constants/fixed_bases/lagrange_coeffs/nullifier_k.bin is excluded by !**/*.bin
  • src/constants/fixed_bases/lagrange_coeffs/spend_auth_g.bin is excluded by !**/*.bin
  • src/constants/fixed_bases/lagrange_coeffs/spend_auth_g_short.bin is excluded by !**/*.bin
  • src/constants/fixed_bases/lagrange_coeffs/value_commit_r.bin is excluded by !**/*.bin
  • src/constants/fixed_bases/lagrange_coeffs/value_commit_v_short.bin is excluded by !**/*.bin
📒 Files selected for processing (4)

  • CHANGELOG.md
  • src/circuit.rs
  • src/constants/fixed_bases.rs
  • src/constants/fixed_bases/lagrange_coeffs.rs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@PastaPastaPasta

Copy link
Copy Markdown
Member Author

CI note: the Code coverage failure is a pre-existing infra issue. This PR did not cause it.

  • The job runs cargo tarpaulin --engine llvm ... --timeout 600 in the floating xd009642/tarpaulin:develop-nightly image. The image changed between PR harden: always enforce canonical proof size in Bundle::try_from_parts #11 (digest sha256:d4859b7b…, 2026-07-13) and this run (sha256:3580ad00…).
  • The new image includes tarpaulin #1891 "Enforce timeouts for LLVM test processes" (merged 2026-09-19). Before that change, --timeout had no effect under --engine llvm. It is now enforced as a wall-clock limit on each test process.
  • Here the orchard lib test binary started at 02:51:33 and was killed at 03:01:33, exactly 600s later, while running circuit::tests::round_trip. All tests up to that point passed, including the new embedded_params_match_derivation.
  • On PR harden: always enforce canonical proof size in Bundle::try_from_parts #11 the same binary ran for more than 22 minutes (18:34:42 → past 18:57) with the same flag and no error. Any commit on dashified would therefore fail this job with the current image, with or without this PR.

All other checks pass (tests on Linux/macOS/Windows, MSRV and beta clippy, no_std builds, rustfmt, intra-doc links, book). I've left the CI config alone in this PR. Possible fixes for a separate PR: raise --timeout (e.g. 3600) to restore the effective old behaviour, pin the image digest, or drop the job as upstream zcash/orchard has done.


🤖 Posted autonomously by Claude on behalf of pasta.

PastaPastaPasta and others added 2 commits November 7, 2026 16:10
ProvingKey::build_for_version and VerifyingKey::build_for_version derived
the public Vesta commitment parameters with Params::new(11) on every call:
2048 hash-to-curve generators plus an inverse FFT over the curve to get the
Lagrange basis. That is roughly 300 ms of a ~1.2 s cold proving-key build
(and ~40% of a verifying-key build) on an M4 Pro, and several times that
on a phone.

The parameters are deterministic public data, so embed their canonical
serialization (Params::write, 131,140 bytes) and decode it with
Params::read, which takes ~22 ms. The keys are unchanged.

embedded_params_match_derivation checks the file byte-for-byte against
Params::new(11) and that it round-trips through Params::read/write. The
ignored test regenerate_embedded_params rewrites it
(cargo test --release --lib -- --ignored regenerate_embedded). The length
is also checked at compile time.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
halo2_gadgets' default FixedPoint::lagrange_coeffs derives each fixed
base's x-coordinate interpolation coefficients from its generator on every
call: a scalar multiplication and normalization per window multiple plus an
interpolation per window. Key generation calls it once per fixed-base
multiplication in the Action circuit (seven per synthesis), and building a
proving key synthesizes the circuit twice (keygen_vk then keygen_pk), so a
cold ProvingKey::build spent ~0.6 s of CPU re-deriving the same public
constants. Witness synthesis during proving does not evaluate fixed
assignments, so proving itself was not affected.

Override lagrange_coeffs for OrchardFixedBasesFull, OrchardBaseFieldBases
and OrchardShortScalarBases to decode embedded canonical encodings instead
(all seven tables decode in ~0.25 ms, so they are not cached). There are
seven tables (117 KiB): the five full-width bases plus the short-scalar
windows for ValueCommitV and SpendAuthG. SpendAuthGBase shares SpendAuthG's
full-width table because it has the same generator and window count.

compute_lagrange_coeffs stays the reference:
lagrange_coeffs_match_default_derivation checks every enum variant's
override against it with halo2_gadgets' own window count for the scalar
kind, which covers the table contents and the dispatch. The ignored test
regenerate_embedded_lagrange_coeffs rewrites the files
(cargo test --release --lib -- --ignored regenerate_embedded). Table
lengths are checked at compile time. The pinned verifying-key descriptions
and stored proofs still match, and seeded proofs are byte-identical to the
previous release.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

This branch has not been deployed

No deployments
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.

1 participant