# Contributing to Sparkle Thanks for contributing! This page is the entrance for every kind of change. Pick yours, run the minimal local check, and know which CI gate must be green before a release. Nothing here needs private notes or the author's machine. ## Where do I start? | I want to … | Start here | You bring | |---|---|---| | **Fix a bug** (compiler, DSL, simulation, an IP) | Reproduce it with the smallest Lean file you can; see [Reporting a bug](#reporting-a-bug) and [When something is genuinely blocked](#when-something-is-genuinely-blocked). Synthesis errors: [Troubleshooting_Synthesis.md](docs/reference/Troubleshooting_Synthesis.md) | a failing test (or check file) that passes after the fix | | **Extend the proofs** (a new circuit shape for `#ved_u1`, a lemma, a new entry) | [docs/VED.md](docs/VED.md) (what is supported), [PublicAPI.md](docs/reference/PublicAPI.md) (what is public, what the theorem says), then [What a proof extension must bring](#what-a-proof-extension-must-bring) | the five items of that section | | **Keep downstream projects working** (the public imports, names, the statement) | [PublicAPI.md](docs/reference/PublicAPI.md), sections 1, 6, 9; [How_To_Use.md](docs/reference/How_To_Use.md); the fixture [`Tests/Downstream/ProofConsumer`](Tests/Downstream/ProofConsumer) | an unchanged fixture, or a stated breaking change with a migration note | | **Improve the docs** | [docs/README.md](docs/README.md) for the layout; examples must be ones CI builds | working links, and examples that run | | **Add an IP** | [Quick checklist for "I added a new IP"](#quick-checklist-for-i-added-a-new-ip) below | spec, sim test, `#synthesizeVerilog` check, CI entry | For the snapshot reproduction of the 0.2 research results (not the current development entrance), see [docs/research/CONTRIBUTING-research.md](docs/research/CONTRIBUTING-research.md). ## Which check for which change Run the **minimal local check** while you work; the **CI gate** is what must be green on the pull request (and for a release candidate). You are not asked to run every IP's CI locally. Build by name; most test libraries and executables are not default targets. | You changed | Minimal local check | CI gate that must pass | |---|---|---| | Proof code: `Tools/Ved*`, `Tools/Deep*`, `SparkleProof/`, `Tests/Ved/`, `Tests/ProofCorpus/` | `lake build Tests.Ved.` (for example `Tests.Ved.U1EntryCheck`); `lake build SparkleProof.Single`; `python3 -I scripts/ci/ved_paths.py --check` | `VED` workflow (`ved.yml`, jobs in `ved-run.yml`, run on every supported Lean version): `ved-pr` on a pull request, `ved-full` before a release, and `ved-corpus` (the proof corpus, `python3 -I scripts/proof/run_core.py --out DIR`) | | The public API: names, statements, the `SparkleProof.*` imports, `docs/reference/PublicAPI.md` | `lake build Tests.Ved.SparkleProofFacadeCheck`; `scripts/ci/proof_consumer.sh URL SHA WORKDIR` against a committed SHA | `Downstream Smoke Test` (`downstream-smoke.yml`): the `proof-consumer` job; and the VED gate above | | The compiler or the DSL: `Sparkle/`, `Sparkle.lean` | the nearest test under `Tests/` (`lake build Tests.…`, `lake exe …`); `lake exe test` for the release gate | `Build` (`build.yml`), `IP Tests` (`ip-tests.yml`), and `VED` (the compiler is in its path filter) | | An IP: `IP/…` | its `*-test` executable, `lake exe my-cipher-test` style, with the `#synthesizeVerilog` check in its test file | `IP Tests` (`ip-tests.yml`): your entry in the matrix; the co-simulation executable (`verilator/`, `*-jit-test`) where the IP has one | | Docs only: `*.md`, `docs/` | `python3 -I scripts/research/check_links.py` (it checks the pages it lists: README, this file, `How_To_Use.md`, `PublicAPI.md`, the IP catalog index, `Ch01b`, `Ch07c`, the tutorial index and `docs/research/`; other pages are not checked); for a tutorial chapter, edit `docs/tutorial/md/Ch*.md`, then `bash docs/tutorial/build-from-md.sh` (needs `xlean-convert` from xeus-lean) and `lake build TutorialNotebooks`, the same two commands CI runs | `Docs` (`docs.yml`: links and anchors, coverage tables), and `tutorial-notebooks` in `build.yml` for tutorial chapters | | `lakefile.lean`, `lean-toolchain`, `lake-manifest.json` | `lake build` of the targets you touched; `python3 -I scripts/ci/ved_paths.py --check` | all of the above; the toolchain is not changed together with other work | If a gate is skipped by CI (for example a path filter), that is not a substitute for it: say in the pull request which gate you ran by hand. ## What a proof extension must bring A new accepted shape, a new entry, or a change to what a theorem says comes with **all five**: 1. **A success example**: a small declaration the entry accepts, as a `Tests/Ved/*Check.lean` file (and, if it is part of the public surface, a case in `Tests/ProofCorpus`), built by CI. 2. **A refusal example**: a nearby declaration that must be refused, with the refusal text pinned (`#guard_msgs (error)`), so a change that silently accepts it, or that turns a resource limit into an "expected refusal", fails. 3. **A fixed statement**: the theorem's type is written out in the check (`example : U1Stmt … := f.ved_u1` or the statement of your entry), not read back from what the entry produced; a change in what the theorem means is a change in the public API (PublicAPI.md, section 6). 4. **An axiom audit**: `#print axioms` pinned to `[propext, Classical.choice, Quot.sound]` and the strict audit (`#ved_strict_audit`) on the result. No `sorry`, no added axiom, no `native_decide`, no exception to the audit to make a proof go through. 5. **A support-matrix update**: the shape, the entry and its status (public / experimental) in [docs/VED.md](docs/VED.md), and in [PublicAPI.md](docs/reference/PublicAPI.md) if a public name is added. `scripts/ci/ved_paths.py --check` must pass (a new file the checks import must be in the workflow's path filter; `--write` regenerates it). ## Reporting a bug For a problem found in Sparkle itself, or in a project that depends on it, an issue needs enough to reproduce it without asking you anything: * **Version**: the Sparkle release tag or the full commit SHA you depend on (the `rev` in your `lakefile.toml`, and `lake-manifest.json`). * **Toolchain**: the content of `lean-toolchain`, and `lake --version`. * **OS**: the OS and its version (and `cc --version`; the build links C code). * **Minimal code**: the smallest Lean file, or Lake project, that shows it. For a downstream project, the fresh-directory version of it (see [How_To_Use.md](docs/reference/How_To_Use.md)). * **Command**: exactly what you ran (`lake build …`, `lake env lean …`). * **Expected** and **actual**: what you thought would happen, and the full output (error text, the `VED U1 REFUSED f [stage]` line, the exit code), not a paraphrase. ## What is where | Directory | Role | Stability | |---|---|---| | `Sparkle/` | The library: Signal DSL, IR, compiler (`Sparkle.Compiler.Elab`), simulation, JIT | The DSL and `#synthesizeVerilog` are what users write against; changes need a test. Not a promise in the sense of [PublicAPI.md](docs/reference/PublicAPI.md), which covers the proof side | | `SparkleProof/` | The public proof facade: `SparkleProof.Core` and `SparkleProof.Single` | **Public**: kept in 0.2.x (PublicAPI.md, sections 2 and 6) | | `Tools/` | The implementation of the verified entries (`Tools.Ved*`, `Tools.Deep*`), the SV parser, macros | Mostly **internal** or experimental; only the three compatibility imports named in PublicAPI.md are kept | | `IP/` | IP cores | Each IP is as stable as its own test | | `Tests/` | Tests, the `Tests/Ved` checks, the proof corpus (`Tests/ProofCorpus`), the downstream fixture (`Tests/Downstream`) | Test code is not an API; the fixture is the contract's executable example | | `docs/` | Hand-written documentation; [docs/VED.md](docs/VED.md) is the current support, `docs/reference/` the reference | Current | | `docs/research/` | Pages and measurements of the 0.2 research snapshot, each tied to a named revision | **Snapshot**: not updated for current code; read the revision box first | | `notes/` | Development work records and plans | **Not maintained as documentation**; do not rely on it, and do not cite it for a user-facing claim | AI coding agents: follow this file; there is no separate rule set for them. ## Quick checklist for "I added a new IP" If you've written `IP//.lean` and want it on the main branch, you need **all** of the following. Skipping any of these means the IP isn't actually covered — `lake build` passing is type-checking, not behaviour. 1. **Pure-data spec** (oracle) 2. **Sim test** that compares HW vs spec cycle-by-cycle 3. **`#synthesizeVerilog` check** in the test file 4. **`lakefile.lean` entries** for the lib + the test exe 5. **`.github/workflows/ip-tests.yml` matrix entry** 6. **(optional) `Tests/AllTests.lean` import** if the test should also run inside the `lake exe test` release gate Each step is detailed below. See `IP/Bus/CANHW.lean` + `Tests/IP/Bus/CANHWTest.lean` for a small, complete example. --- ## 1. Pure-data spec (the oracle) A new HW IP is only "done" when there's something to compare its cycle-by-cycle behaviour against. The cleanest way is a pure-data Lean function that mirrors the spec: ```lean -- IP/Crypto/MyCipher.lean namespace Sparkle.IP.Crypto.MyCipher /-- Pure-data encryption — the reference semantics. -/ def encrypt (key : BitVec 128) (block : BitVec 128) : BitVec 128 := -- … plain Lean, no Signal types … /-- Signal-DSL HW version — must produce the same output. -/ def encryptHW {dom : DomainConfig} (key : Signal dom (BitVec 128)) (block : Signal dom (BitVec 128)) : Signal dom (BitVec 128) := circuit do -- … the actual hardware … -- ``` If the IP is **inherently** byte-stream-shaped (a UART, a parser, a network protocol) the oracle can be a pure `List UInt8 → List UInt8` function and the sim test feeds both with the same stimulus. See `IP/Net/Memcached.lean` (pure parser + KV store) ↔ `IP/Net/MemcachedHW.lean` (Signal-DSL BRAM-backed engine). ## 2. Sim test (cycle-accurate) A `Tests/IP//Test.lean` file with a `def main : IO Unit` that: 1. Drives the HW `Signal.circuit do` with a known input stream. 2. Samples the output cycle-by-cycle via `.val t`. 3. Asserts every output matches the pure-data oracle's prediction for the same input. ```lean -- Tests/IP/Crypto/MyCipherTest.lean import IP.Crypto.MyCipher namespace Sparkle.Tests.IP.Crypto.MyCipherTest def main : IO Unit := do let key : Signal defaultDomain (BitVec 128) := Signal.pure 0xdeadbeef…#128 let block : Signal defaultDomain (BitVec 128) := Signal.pure 0xcafe…#128 let hw := encryptHW key block -- HW engine takes N cycles; sample at cycle N. let got := hw.val 10 let expected := encrypt 0xdeadbeef…#128 0xcafe…#128 if got = expected then IO.println " ✓ HW matches oracle" else IO.println s!" ✗ mismatch: got=0x{Nat.toDigits 16 got.toNat |>.toString.toLower}" IO.Process.exit 1 end Sparkle.Tests.IP.Crypto.MyCipherTest ``` Plus a one-line driver under `Tests/Drivers/`: ```lean -- Tests/Drivers/MyCipherTestMain.lean import Tests.IP.Crypto.MyCipherTest def main : IO Unit := Sparkle.Tests.IP.Crypto.MyCipherTest.main ``` ## 3. `#synthesizeVerilog` check Inside the same test file, add a synthesis check. The synth elaborator is **separate from the Lean type checker** — a `def` that type-checks can still fail to compile to Verilog. Without this check, sim-pass / synth-fail regressions slip past. ```lean section SynthesisChecks open Sparkle.Core.Domain open Sparkle.Core.Signal open Sparkle.IP.Crypto.MyCipher private def synth_encryptHW (key : Signal defaultDomain (BitVec 128)) (block : Signal defaultDomain (BitVec 128)) : Signal defaultDomain (BitVec 128) := encryptHW key block #synthesizeVerilog synth_encryptHW end SynthesisChecks ``` If `#synthesizeVerilog` fails, look up the error in [`docs/reference/Troubleshooting_Synthesis.md`](docs/reference/Troubleshooting_Synthesis.md): its sections "Synthesis Compiler Patterns" (what is and is not supported, with fix patterns), "Pattern Matching on Tuples" and "If-Then-Else in Signal Contexts" cover the usual causes. The synth check runs at **`lake build` time** of the test module, so it's automatically covered once you wire the test into `lakefile.lean` (next step). ## 4. `lakefile.lean` entries Two changes: ```lean -- (a) make sure the IP family lib's `roots` covers your file lean_lib «IP.Crypto» where roots := #[`IP.Crypto] -- already there for most families -- (b) add the test executable lean_exe «my-cipher-test» where root := `Tests.Drivers.MyCipherTestMain supportInterpreter := true ``` `supportInterpreter := true` is required so the test driver can evaluate `Signal.val t` via the native FFI sim path. ## 5. `.github/workflows/ip-tests.yml` Add the test executable to the matrix: ```yaml matrix: exe: # … existing entries … - my-cipher-test # under the right topic group ``` The CI workflow runs each entry as `lake exe ` in its own parallel shard. Without this, `lake build` will still type-check your test, but the cycle-by-cycle assertions never actually execute in CI — DSL changes that break your IP's runtime semantics won't be caught. Verify the matrix list matches `lakefile.lean`'s `lean_exe` entries (minus the bins / docs / traces — see the yaml's comment for the explicit skip list). ## 6. (optional) `Tests/AllTests.lean` If the test is fast (< 1s) and you want it as part of the release-gate `lake exe test`, add an import and a call: ```lean import Tests.IP.Crypto.MyCipherTest -- in `def main : IO UInt32 := do` Sparkle.Tests.IP.Crypto.MyCipherTest.main IO.println "" ``` For slow tests (BitVec 128 sim, long FSM horizons) **don't** add them to AllTests — they'll bloat the release-gate wall clock. The per-IP CI matrix is enough. --- ## What NOT to do * Don't commit IP code without a sim test. "Builds clean" proves type-checking; it doesn't prove the HW does what the docstring claims. * Don't skip `#synthesizeVerilog`. Sim-correct but synth-broken IPs accumulate silently and surface as a giant pile of work when someone tries to actually flash an FPGA. * Don't add an IP and forget the CI matrix entry. The current cohort took six months and revealed that 26 test files were never running in CI because nobody plugged them in. The matrix is the single source of truth — if it's not there, the test isn't running. ## Common synth-elaborator gotchas These patterns sim-pass but break `#synthesizeVerilog`: | Pattern | Fix | | -------------------------------------- | ----------------------------------------------------- | | `sig.map (fun _ => true)` | drop or use `Signal.pure true` | | `sig.map (fun b => if b then a else b)`| `Signal.mux sig (Signal.pure a) (Signal.pure b)` | | `(· != ·) <$> a <*> b` | `(fun a b => !(a == b)) <$> a <*> b` | | `Bool.not <$> sig` | `(fun b => !b) <$> sig` | | `return (a, b)` from `circuit do` | wrap in a `structure` with `HasDomain` (see Ethernet.RxOut) | | `Signal.map identity-lambda inner` | pass `inner` directly | When `#synthesizeVerilog` fails, set `set_option trace.sparkle.compiler true` to see which expression the elaborator gets stuck on, then check the table above. The elaborator also has a runtime cap (`SPARKLE_TRANSLATE_LIMIT`, default 500k) — if your IP hits it, the error message names the deepest hint so you can grep the trace. ## When something is genuinely blocked If you find a bug in Sparkle itself (Compiler/Elab, Core/Signal, Core/CircuitMonad), please open an issue with: * The failing IP (minimal repro if possible). * `SPARKLE_PROFILE=1` output of `/tmp/sparkle-profile.log`. * Any trace excerpts that show the failure point. The Compiler bug class is small but bites hard. For a worked example of narrowing a synthesis hang down to a minimal repro, read the header of [`Tests/Compiler/MultiOutputSubModuleHangRepro.lean`](Tests/Compiler/MultiOutputSubModuleHangRepro.lean) (what works, what hangs, the `SPARKLE_PROFILE=1` evidence) and run it with `lake exe multi-output-submodule-hang-repro`. --- Welcome aboard.