Skip to content

feat/global-cse-opt#1414

Open
0xParti wants to merge 7 commits intoa16z:mainfrom
defi-wonderland:feat/global-cse-opt-clean
Open

feat/global-cse-opt#1414
0xParti wants to merge 7 commits intoa16z:mainfrom
defi-wonderland:feat/global-cse-opt-clean

Conversation

@0xParti
Copy link
Copy Markdown
Contributor

@0xParti 0xParti commented Apr 7, 2026

Summary

Extends the transpiler's existing per-constraint CSE to global scope, detecting shared TranscriptHash chains across constraints and hoisting them into global variables. Previously each constraint had its own isolated CSE context, duplicating identical Poseidon hash computations. Also adds a Rust/Go cross-validation system for verifying codegen correctness. Builds on #1322 .

Changes

  • Global CSE: Detect shared TranscriptHash chains across constraints and hoist them into global variables, computed once and reused. Previously each constraint had isolated CSE, duplicating identical Poseidon hash computations.
  • Cross-validation system: Compare intermediate assertion values (LHS/RHS) between a concrete AST evaluator in Rust and the generated gnark circuit in Go. Pass --crossval to auto-generate a cross-validation circuit and witness.
  • New files: ast_evaluator.rs (concrete AST evaluation), cross_validation.rs (Rust test), crossval_test.go (Go test)
  • CseContext enum instead of usize::MAX sentinel for CSE scope tracking
  • Dynamic assertion count in crossval_test.go
  • .gitignore for generated Groth16 artifacts
  • Clippy and fmt fixes

Testing

  • cargo clippy and cargo fmt pass
  • cargo test -p transpiler — cross-validation test
  • cd transpiler/go && go test -v -run TestStagesCircuitProveVerify — Groth16 prove/verify
  • cd transpiler/go && go test -v -run TestEndToEndPipeline -timeout 30m — full E2E pipeline

On the fibonacci test example, global CSE reduces constraints from ~2.7M to ~621K–677K. Proof size remains 164 bytes. Cross-validation Rust/Go passes on all intermediate values.

Security Considerations

CSE is a pure optimization: it deduplicates constraint computation but does not change the constraint system's semantics. Each hoisted variable is assigned exactly once and referenced by the same constraints that previously computed it inline, so proof soundness is unaffected. The cross-validation system is test-only infrastructure and does not affect the proof pipeline.

Breaking Changes

None

akoidefi added 6 commits April 7, 2026 15:50
Cross-validation: compare 40 intermediate values (20 assertions x LHS/RHS)
between Rust AST evaluator and Go gnark circuit. Auto-generates crossval
circuit via --crossval flag, eliminating manual copy desync risk.

Codegen fixes from review:
- global CSE only hoists by ref_count, not MAX_INLINE_EXPR_LEN
- assert cse_counter == num_global after global codegen
- unify rewrite_cse_to_slice / rewrite_cse_refs_to_slice
- Don't skip constant constraints in crossval mode
- Inline variables in format! macros (ast_evaluator, gnark_codegen)
- Remove [[bin]] entry for deleted export_assertion_components.rs
- cargo fmt on cross_validation.rs and ast_bundle.rs
…ount)

Replace usize::MAX sentinel with CseContext enum for type-safe global vs
per-constraint CSE distinction. Make crossval_test.go parse assertion count
dynamically from circuit output instead of hardcoding 20.
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 7, 2026

Warning

This PR has more than 500 changed lines and does not include a spec.

Large features and architectural changes benefit from a spec-driven workflow.
See CONTRIBUTING.md for details on how to create a spec.

If this PR is a bug fix, refactor, or doesn't warrant a spec, feel free to ignore this message.

@github-actions github-actions bot added the no-spec PR has no spec file label Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-spec PR has no spec file

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants