Repository navigation
perf(halo2_proofs): compile polynomial ASTs into a register program (−21–41% Orchard proof time) - #1
PastaPastaPasta wants to merge 1 commit into
Conversation
The prover evaluates the quotient polynomial h(X) (and the compressed lookup expressions) by recursively walking an Ast for every chunk of rows. Every leaf occurrence copies a rotated chunk into a fresh Vec, every node allocates, and shared subtrees and repeated column/rotation queries are recomputed each time they appear. For the Orchard Action circuit this was ~27-31% of proving time. Evaluator::evaluate now lowers the Ast once into a hash-consed DAG, applies exact identities that depend only on the expression shape and public constants (scale by 0/1/-1, adding zero, constant folding, multiplying by a constant polynomial as a scale, the leading zero step of a Horner chain, x + -y as x - y), allocates scratch registers with reuse after a value's last read, and executes the program four independent rows per instruction, reading leaves in place. Instructions refer to constants by index so the instruction stream stays small enough to remain in L1 across row blocks. Field arithmetic is exact, so the output is identical; Orchard proofs created with the same RNG are byte-for-byte unchanged. The recursive evaluator and the chunk helpers only it uses are kept as a test-only reference. New tests compare the compiled evaluator with it and with a direct per-row evaluation on random expressions (shared subtrees, rotations, Horner chains, special scalars) in all three bases, for every chunk size on small domains. The reference's Coeff::linear_term indexed past the end of a chunk of length one; that is fixed so it can serve as a reference. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configuration
📒 Files selected for processing (5)
Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review. 📝 WalkthroughWalkthroughThe polynomial evaluator now compiles expression ASTs into a deduplicated register program and evaluates it in four-row lanes. Basis-specific rotation and ChangesCompiled polynomial evaluation
Priority: ⬆️ High Estimated code review effort: 4 (Complex) | ~45 minutes Change: Refactor Sequence Diagram(s)sequenceDiagram
participant Eval as Evaluator::evaluate
participant Program
participant Polynomials
participant BasisOps
participant Output
Eval->>Program: compile AST into instructions
Eval->>Program: evaluate polynomials on the domain
Program->>Polynomials: load polynomial values for each row
Program->>BasisOps: obtain basis-specific X values
Program->>Output: write valid lane results
Merge Risk: ⚪ Minimal · up to The change rewrites polynomial evaluation as a compiled register program. Proof output is reported unchanged and covered by comparison tests. No concrete merge-blocking issue was found. Security Architecture ReviewSecurity architecture risk: 🔵 Low · up to No new privilege boundary or verification bypass was identified. Residual risk concerns correctness of the replacement proof-generation logic; downstream proof compatibility was not demonstrated by executing tests in this review. Retained concerns Security review detailsSecurity Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Docstring CoverageExplanation Docstring coverage is 61.29% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 62 functions across 4 files. (1 skipped: 1 unsupported.)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
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. Comment |
|
CI status: GitHub Actions has not run on this PR.
Once workflows are enabled, some inherited upstream jobs will not apply to this fork. CI checks run locally instead (head
Across the test runs: 231 tests passed, 0 failed. CodeRabbit had no actionable comments. 🤖 Posted autonomously by Claude on behalf of pasta. |
Switch halo2_proofs, halo2_gadgets and halo2_poseidon from crates.io to https://github.com/dashpay/halo2, which starts from the exact crates.io halo2_proofs 0.3.2 / halo2_gadgets 0.5.0 / halo2_poseidon 0.1.0 sources and adds prover performance work: a compiled polynomial evaluator (dashpay/halo2#1), Booth MSM and GLV IPA generator collapse (dashpay/halo2#2), and concurrent per-circuit proving (dashpay/halo2#3). Together they cut Orchard proving time by 50-61% with byte-identical proofs. The pin points at the integration commit that merges all three; it should move to the dashified merge commit once those PRs land. pasta_curves resolves to 0.5.2 (needed by the GLV collapse). Orchard is the only halo2 user in Platform's dependency graph, so this swaps one copy for another rather than duplicating it. Add examples/prover_bench.rs, the harness used to measure that work: cold key build, warm proof timing for N-action bundles with the Dash memo (every proof is verified), and a seeded mode that prints proof digests to check two prover builds for byte-identical output. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Why
Dash Platform shielded transactions are Orchard bundles proved with this crate (via
dashpay/orchard). On a phone the proof dominates send latency, so it's worth making faster.Profiling a 2-action Orchard proof on an M4 Pro put the quotient-polynomial evaluation (
poly::Evaluator::evaluate, called fromplonk/vanishing/prover.rsfor h(X)) at ~27–31% of proving time (≈143 ms of ≈530 ms; ≈425 ms of ≈1.37 s for 6 actions). The evaluator walks anAstrecursively for every chunk of rows, and does three kinds of wasted work:Vec;For Orchard, the h(X) expression has ~3,000 distinct nodes after de-duplication but is visited far more times than that.
This is the first PR to the new
dashpay/halo2fork. Thedashifiedbase branch is the exact crates.io source that Platform ships today:halo2_proofs0.3.2,halo2_gadgets0.5.0 andhalo2_poseidon0.1.0. Everysrc/file is byte-identical to the published crates.What
Evaluator::evaluatenow compiles theAstonce per call and then executes the compiled program (halo2_proofs/src/poly/evaluator/compiled.rs):Hash-consed DAG. Structurally identical nodes become one node, including repeated column/rotation queries and commutative
a+b/b+a. A pointer memo meansArc-shared subtrees are lowered once.Exact simplifications that depend only on expression shape and public constants (gate constants and transcript challenges):
DistributePowersHorner chain is dropped;x + (−y)becomesx − y.Field arithmetic is exact, so these rewrites cannot change any output.
Register program. One instruction per remaining node, with scratch registers reused after a value's last read. Constants live in a side table, so instructions are ~36 bytes and Orchard's ~2,600-instruction program stays cache-resident.
Row-blocked execution. Each instruction runs on 4 independent rows, giving instruction-level parallelism and amortizing dispatch. Leaves are read in place at
row + rotation_offsetinstead of being copied. Chunks run in parallel as before.Orchard's h(X) program for a 2-action proof: 3,001 DAG nodes → 2,592 instructions (1,050 mul, 429 Horner, 459 add, 420 sub, 234 scale), using 105 registers (≈13 KiB of scratch per worker).
The recursive evaluator, and the chunk helpers only it uses, are kept
#[cfg(test)]as a reference. While writing the tests I fixedCoeff::linear_term, which indexed past the end of a chunk of length one; that code is now test-only.No public API change. The verifier is untouched.
Correctness evidence
halo2_proofs0.3.2. Every timed proof below was also verified.poly::evaluator::tests):compiled_matches_reference_{coeff,lagrange,extended}: 40 random expressions per domain over six(j, k)domains, including sharedArcsubtrees, repeated queries, rotations up to ±3, empty and nested Horner chains, and special scalars 0/1/−1. Each is compared against a direct per-row evaluation built from the old term definitions, and against the old recursive evaluator in the Lagrange bases, for every chunk size from 1 to the polynomial length (partial 4-row blocks, wrap-around).compiler_deduplicates_and_simplifies: instruction counts for dedup and each simplification.coeff_linear_term_short_chunks: regression test for the length-one-chunk bug.cargo test --release -p halo2_proofs(37 + 1 + 8), the same with--no-default-features --features batch(no multicore), andcargo test --release -p halo2_gadgets(38) all pass on the pinned 1.60 toolchain.cargo clippy --all-targets -- -D warningsandcargo fmt --checkare clean.Benchmarks
Orchard proof creation,
dashified-0.14.1circuit with the 36-byte Dash memo. One of these Orchard proofs is what a wallet builds on Send.Setup:
codegen-units = 1,panic = "abort", matching Platform'srelease-iosprofile.Cargo.lockpins today: crates.iohalo2_proofs0.3.2 +pasta_curves0.5.1, orcharddashified-0.14.1. The patched build uses the same pasta.An earlier run against crates.io
halo2_proofs0.3.5 / pasta 0.5.2 (the newest semver-compatible releases) gave the same picture: −20% to −42%.The gain is largest at low thread counts, which is the phone case: an iPhone has 6 cores, 2 of them performance cores.
Evaluator stage alone (2 actions, M4 Pro, 14 threads, loaded host): ~143–215 ms → ~52–63 ms. Instruction encoding was chosen by measurement: compact 4-lane beat the first version (inline constants) and an 8-lane variant.
Harness
A small standalone crate builds an Orchard bundle with N outputs (padded to ≥2 actions), proves it with
Bundle::create_proof, and verifies it.--mode prove: OS RNG, timed.--mode seeded: ChaCha20 seeded RNG; prints the SHA-256 of each proof.Baseline is
orchardat tagdashified-0.14.1with crates.iohalo2_proofs0.3.2 /pasta_curves0.5.1, pinned in the lockfile. The patched build is the same orchard with[patch.crates-io]pointinghalo2_proofs/halo2_gadgets/halo2_poseidonat this branch. The harness will be added todashpay/orchardtogether with the PR that switches it to this fork.Follow-ups (separate PRs)
dashpay/orchard: depend on this fork, and embed canonical params and fixed-base tables to speed up the cold key build.🤖 Generated with Claude Code