diff --git a/.cargo/config.toml b/.cargo/config.toml index 6d36286517..6f6806a2f2 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -2,7 +2,7 @@ testall = ["test", "--release", "--workspace"] clippyall = ["clippy", "--release", "--workspace", "--all-targets", "--", "-D", "warnings"] docall = ["doc", "--release", "--workspace", "--no-deps"] -leanvm = ["run", "--release", "-p", "leanvm-cli", "--"] +leanvm-bench = ["run", "--release", "-p", "leanvm-bench", "--"] [build] rustdocflags = ["-D", "warnings"] diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index fc417204e6..f63d8113be 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -39,6 +39,19 @@ jobs: run: cargo test --release -p keccak256 env: RUSTFLAGS: -C target-cpu=x86-64 + - uses: actions/setup-go@v5 + with: + go-version-file: bins/leanvm-sidecar/clients/go/go.mod + # Against the sidecar `cargo testall` built. + - name: Test (the sidecar's Go client) + working-directory: bins/leanvm-sidecar/clients/go + run: | + test -z "$(gofmt -l .)" + go vet ./... + socket=$RUNNER_TEMP/sidecar.sock + ../../../../target/release/leanvm-sidecar --socket "$socket" & + until [ -S "$socket" ]; do kill -0 $! && sleep 1; done + LEANVM_SIDECAR_SOCKET=$socket go test -count=1 ./... # The NEON kernels, as bench.yml's arm64 testbed builds them: the x86 job's haswell reaches none of them. test-arm64: @@ -64,8 +77,10 @@ jobs: - uses: Swatinem/rust-cache@v2 - name: Clippy run: cargo clippyall - - name: Clippy (the API on the experimental prover) - run: cargo clippy --release -p lean_vm --all-targets --features experimental_prover -- -D warnings + - name: Clippy (the API and the sidecar on the experimental prover) + run: cargo clippy --release -p lean_vm -p leanvm-sidecar --all-targets --features experimental_prover -- -D warnings + - name: Clippy (the API in its sidecar mode) + run: cargo clippy --release -p lean_vm --all-targets --features sidecar -- -D warnings # Clippy type-checks every arm the ISA selects, so it also does `cargo check`'s work. # Nothing runs: GitHub's x86-64 runners do not reliably have AVX-512. diff --git a/AGENTS.md b/AGENTS.md index cb61c980ed..8f209136c0 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -18,12 +18,12 @@ The fields are `K = GF(2)[x]/(x^64 + x^4 + x^3 + x + 1)` and `E = K[y]/(y^2 + x* ## Layout -The root `Cargo.toml` is workspace-only: libraries in `crates/`, the CLI in `bins/`. The prover exists twice, in two trees that share no prover crate: +The root `Cargo.toml` is workspace-only: libraries in `crates/`, the benchmark CLI in `bench/`, the binaries users run in `bins/`. The prover exists twice, in two trees that share no prover crate: - `crates/stable_prover/` (packages `stable_*`): the reference prover, written to be read: plain, safe code, as short as the protocol allows, and slower than experimental. Architecture-specific code lives in one SIMD layer, `primitives::field::lanes` (eight field elements at a time, `F128x8`, `F64x8`, `Acc8`, `Wide8`, and the byte maps and transposes over 64 bytes, each with one portable body that defines it and NEON, AVX2+VPCLMULQDQ and AVX-512+VPCLMULQDQ arms and the portable one), and in the shared `keccak256`'s multi-way permutation; the hot loops are written over them and nothing else in stable has `core::arch` or `cfg(target_feature)`. No uninitialized buffers, no non-temporal stores, no cache-tiled pass plans, no per-machine tuning. It keeps the whole level-0 codeword and every Merkle level, so its peak memory at the largest stacks is a multiple of experimental's on x86. An optimization enters it only where leaving it out costs a measurable share of a proof and its simple form stays short. - `crates/experimental_prover/` (packages `experimental_*`): the optimized prover, where performance work lands first. The conventions below about NEON, AVX-512, streaming stores and the like are about this tree. -Both prove the same protocol and give the same proof bytes, pinned by `bins/leanvm/tests/identical_proofs.rs`; a protocol change lands in both trees, the three verifiers and `formal/`, while an optimization lands in experimental alone. What is not prover code exists once (table below), the guest (`guests/lean_ethereum.py`) and the aggregation included. It depends on the stable tree only where it needs the protocol's structure or a primitive (`aggregation`, below; `lean_da`, its NTT), importing rather than restating them, and proves only through `isa::Prover`, which each tree implements once beside `cpu::prove` (`lean_vm_core::StableProver`, `lean_vm_core::ExperimentalProver`): proving a program, its thread pool, and the deferred claims' batching kernels (`combine_rows`, `dot_rows`). `formal/`'s Rust helpers that need a prover tree build against stable, but for `whir-fold-table`, which exports the WHIR security search that only experimental's `pcs` keeps (stable reads the verifier's table). Within a tree, crates name each other through Cargo renames (`pcs = { package = "stable_pcs", path = "../pcs" }`), so sources say `pcs::`, and the shared `fiat_shamir_prover` likewise as `fiat_shamir`; an integration test reaches its own crate with `extern crate stable_pcs as pcs;`, and a doctest names it in full. The shared crates, leaves first: +Both prove the same protocol and give the same proof bytes, pinned by `bench/tests/identical_proofs.rs`; a protocol change lands in both trees, the three verifiers and `formal/`, while an optimization lands in experimental alone. What is not prover code exists once (table below), the guest (`guests/lean_ethereum.py`) and the aggregation included. It depends on the stable tree only where it needs the protocol's structure or a primitive (`aggregation`, below; `lean_da`, its NTT), importing rather than restating them, and proves only through `isa::Prover`, which each tree implements once beside `cpu::prove` (`lean_vm_core::StableProver`, `lean_vm_core::ExperimentalProver`): proving a program, its thread pool, and the deferred claims' batching kernels (`combine_rows`, `dot_rows`). `formal/`'s Rust helpers that need a prover tree build against stable, but for `whir-fold-table`, which exports the WHIR security search that only experimental's `pcs` keeps (stable reads the verifier's table). Within a tree, crates name each other through Cargo renames (`pcs = { package = "stable_pcs", path = "../pcs" }`), so sources say `pcs::`, and the shared `fiat_shamir_prover` likewise as `fiat_shamir`; an integration test reaches its own crate with `extern crate stable_pcs as pcs;`, and a doctest names it in full. The shared crates, leaves first: | crate | role | | ----------------- | ---------------------------------------------------------------------- | @@ -34,10 +34,13 @@ Both prove the same protocol and give the same proof bytes, pinned by `bins/lean | `zkdsl_compiler` | zkDSL → ISA; its tests run the stable prover | | `lean_xmss` | leanXMSS over Keccak-256 | | `lean_sphincs` | leanSPHINCS over Keccak-256 | -| `bench` | benchmark harness (`Plan`, `Timing`, `--tracing`, Bencher output, the jemalloc allocator); never linked by a prover | +| `allocator` | the jemalloc global allocator and its configuration (below), for the benchmark CLI, the benches and the sidecar; never linked by a library | +| `bench_harness` | benchmark harness (`Plan`, `Timing`, `--tracing`, Bencher output); never linked by a prover | | `lean_da` | additive Reed-Solomon blob encoding (the stable tree's NTT), commitments, membership vectors | +| `claims` | `AggregateClaims` and `OutputClaims`, and the claims' byte form: what both the library and the sidecar's client name | | `aggregation` | recursive signature and DA aggregation, generic over the prover: the guest's constants and witness, and the entry points | -| `lean_vm` | the public API, not generic: it proves with the stable tree, or with the `experimental_prover` feature the experimental one | +| `sidecar` | proving and verifying in another process: the protocol of `bins/leanvm-sidecar` and its Rust client, which builds no prover; with the `server` feature, the server (generic over the prover) | +| `lean_vm` | the public API, not generic: it proves and verifies in the process, with the stable tree, or with the `experimental_prover` feature the experimental one; with the `sidecar` feature, it neither proves nor verifies itself, and does both through the sidecar's client | `aggregation` proves on whichever prover it is given, but derives the guest's constants and a child's witness from the stable tree, the reference: its bus blocks and their coordinates (`lean_vm_core::leaf`, whose term order the guest's arrays follow), the stacking, the grinds, the WHIR shapes and the NTT twiddles; both trees lay a proof out alike. @@ -51,7 +54,7 @@ Each tree holds, in dependency order, leaves first: | `keccak_gkr` | the prover of the VM's `KECCAK` argument | | `lean_vm_core` | arithmetization: tables, bus, constraints, `cpu::execute` and `cpu::prove` of an `isa::Program` and its witness streams; `cpu::verify` wraps `verifier`; the tree's `isa::Prover` | -Paths below such as `crates/lean_vm_core` mean the crate in either tree. `crates/lean_vm/src/lib.rs` is the only thing a user imports: every other crate is `publish = false`, so a new user-facing item is a re-export there, and `crates/lean_vm/tests/api.rs` is its end-to-end use. A re-exported type brings its `pub` methods along, so one the workspace needs but users should not see is `#[doc(hidden)] pub fn __name`, the double underscore marking it internal at every call site. The guest is zkDSL, `guests/lean_ethereum.py`. The CLI is `bins/leanvm`, run with `cargo leanvm --prover stable|experimental ` (`aggregate`, `recursion`, `fibonacci`; the prover is required, with no default), the benchmarks generic over the prover. +Paths below such as `crates/lean_vm_core` mean the crate in either tree. `crates/lean_vm/src/lib.rs` is the only thing a user imports: every other crate is `publish = false`, so a new user-facing item is a re-export there, and `crates/lean_vm/tests/api.rs` is its end-to-end use. A re-exported type brings its `pub` methods along, so one the workspace needs but users should not see is `#[doc(hidden)] pub fn __name`, the double underscore marking it internal at every call site. The guest is zkDSL, `guests/lean_ethereum.py`. The benchmark CLI is `bench/` (`leanvm-bench`), run with `cargo leanvm-bench --prover stable|experimental ` (`aggregate`, `recursion`, `fibonacci`; the prover is required, with no default), the benchmarks generic over the prover. The sidecar is `bins/leanvm-sidecar`, which serves `aggregate` over a Unix socket so that clients in any language get the prover in a process of its own; its `README.md` is the protocol's specification, so a change to the protocol in `crates/sidecar` (`wire.rs` and the server's half in `server.rs`), or to the claims' byte form (`AggregateClaims::to_bytes`), lands there and in its Go client (`clients/go`), which CI tests against the sidecar. It also verifies, for clients that do not verify themselves. ## Building and testing @@ -67,11 +70,11 @@ cargo fmt --all # max_width = 120 cargo machete --with-metadata # unused dependencies, dev ones included ``` -An x86-only `cfg` arm never compiles on an Apple machine, so type-check the other target before pushing anything `cfg`-gated (needs only `rustup target add x86_64-unknown-linux-gnu`; `bench` and the CLI are left out because jemalloc's build script wants a cross `gcc`): +An x86-only `cfg` arm never compiles on an Apple machine, so type-check the other target before pushing anything `cfg`-gated (needs only `rustup target add x86_64-unknown-linux-gnu`; `allocator`, the benchmark CLI and the sidecar are left out because jemalloc's build script wants a cross `gcc`): ```bash RUSTFLAGS="-C target-cpu=znver4" \ - cargo check --release --workspace --exclude leanvm-cli --exclude bench --target x86_64-unknown-linux-gnu + cargo check --release --workspace --exclude leanvm-bench --exclude leanvm-sidecar --exclude allocator --target x86_64-unknown-linux-gnu ``` `RUSTFLAGS` replaces `.cargo/config.toml`'s `target-cpu=native`; a target-specific `CARGO_TARGET_*_RUSTFLAGS` would be merged with it and hand the Apple CPU to the x86 target. @@ -80,15 +83,15 @@ To confirm an arm is reached rather than silently skipped, put a `compile_error! Heavy benches are `benches/` targets, run with `cargo bench -p --bench `: `experimental_keccak_gkr`'s `hash_batch` and `experimental_pcs`'s `throughput` (and their `stable_*` copies), and `keccak256`'s `hash_throughput`. Slow checks are `#[ignore]`d tests, run by name with `-- --ignored --nocapture`: `print_whir_query_counts`, `print_whir_query_table`, `encoding_grinding_bits`. `aggregate_two_to_one` is the fast end-to-end check. -Where a proof's time goes is the `tracing` span tree, and only that: `--tracing` on the CLI, `BENCH_TRACING=1` on the benches, `RUST_LOG` for the level. A new stage worth timing gets an `info_span!` (`stage!` in `lean_vm_core`), never an `Instant` behind an env var. +Where a proof's time goes is the `tracing` span tree, and only that: `--tracing` on the benchmark CLI, `BENCH_TRACING=1` on the benches, `RUST_LOG` for the level. A new stage worth timing gets an `info_span!` (`stage!` in `lean_vm_core`), never an `Instant` behind an env var. ## Benchmarking ```bash -cargo leanvm --prover experimental aggregate --xmss 900 --log-inv-rate 1 --repeat 3 -cargo leanvm --prover experimental aggregate --sphincs 410 --log-inv-rate 1 --repeat 3 -cargo leanvm --prover experimental recursion --n 2 --xmss-per-leaf 900 --log-inv-rate 2 --repeat 3 -cargo leanvm --prover stable aggregate --xmss 900 --log-inv-rate 1 --repeat 3 +cargo leanvm-bench --prover experimental aggregate --xmss 900 --log-inv-rate 1 --repeat 3 +cargo leanvm-bench --prover experimental aggregate --sphincs 410 --log-inv-rate 1 --repeat 3 +cargo leanvm-bench --prover experimental recursion --n 2 --xmss-per-leaf 900 --log-inv-rate 2 --repeat 3 +cargo leanvm-bench --prover stable aggregate --xmss 900 --log-inv-rate 1 --repeat 3 ``` `aggregate` takes a count per scheme (both default to zero) and `--blobs`; `recursion` likewise takes `--sphincs-per-leaf` and `--blobs-per-leaf`. Compare schemes and changes by the reported VM cycle counts. @@ -97,7 +100,7 @@ CI compares every PR with its base, never with a history: `bench.yml` runs both ## The allocator -The prover's buffers are plain `Vec`s and the libraries impose no allocator. A proof allocates and frees gigabytes, so the CLI and the benches install jemalloc with `dirty_decay_ms:-1` (`bench::Jemalloc`), which keeps freed pages mapped for the next proof at the cost of resident memory, and on Linux with `thp:always`, which backs its extents with transparent huge pages. Experimental skips a buffer's zero-fill with `primitives::uninit_vec` or `filled_vec` (`T: Plain`, every element written before it is read); stable never reads uninitialized memory, and writes a large buffer's first pass with the pool (`parallel::filled`, or `parallel::map_collect` when every element is computed anyway), since a zero-fill on one thread costs a whole-proof factor on many cores. Both drop large buffers where their last use ends. +The prover's buffers are plain `Vec`s and the libraries impose no allocator. A proof allocates and frees gigabytes, so the benchmark CLI, the sidecar and the benches install jemalloc with `dirty_decay_ms:-1` (`allocator::Jemalloc`), which keeps freed pages mapped for the next proof at the cost of resident memory, and on Linux with `thp:always`, which backs its extents with transparent huge pages. Experimental skips a buffer's zero-fill with `primitives::uninit_vec` or `filled_vec` (`T: Plain`, every element written before it is read); stable never reads uninitialized memory, and writes a large buffer's first pass with the pool (`parallel::filled`, or `parallel::map_collect` when every element is computed anyway), since a zero-fill on one thread costs a whole-proof factor on many cores. Both drop large buffers where their last use ends. ## The thread pool (`parallel`) @@ -105,7 +108,7 @@ No rayon. Every parallel site is "N independent items, each writing its own disj - Nested dispatch panics, because it would deadlock the dispatch lock. Each pool only sees its own nesting, so a task of either pool must not dispatch on either: inside a `P::map_collect` task, the shared crates call no dispatching helper of the stable tree. - One experimental site runs beside the pool: `keccak::while_executing` spawns half the pool's count of plain threads while a program executes, beside the pool's dispatches of a signature loop's segments and of their Keccak blocks, and gone when it ends. Stable builds the Keccak witness after execution, in the pool. -- `LEANVM_NUM_THREADS` sets the worker count, and `1` is strictly sequential. In experimental, workers fill the performance cores before the efficiency cores, and the default is one worker per logical CPU (on Linux, per physical core the process may use), holding back one performance worker when efficiency workers exist. In stable the default is `available_parallelism()`, halved where Linux reports SMT active. +- The thread count is set in code, before a pool starts (`isa::Prover::set_num_threads`, which `lean_vm::setup` and the benchmark CLI's and the sidecar's `--threads` call); `1` is strictly sequential. In experimental, workers fill the performance cores before the efficiency cores, and the default is one worker per logical CPU (on Linux, per physical core the process may use), holding back one performance worker when efficiency workers exist. In stable the default is `available_parallelism()`, halved where Linux reports SMT active. ## Three verifiers, one protocol @@ -134,7 +137,7 @@ In the recursion program, the prover sends the verifier advice, called hints. Hi - **A hot NEON helper that is `#[inline(always)]` stays so.** A `#[target_feature]` function cannot be forced inline, and LLVM then leaves field helpers out of line in hot loops. Split a product's halves by hand rather than with `array::map`. A helper that needs `unsafe` is an `unsafe fn` its callers wrap. - **On Zen 4, 512-bit cross-lane moves are half-rate**: prefer kernels where both qwords of every 128-bit lane carry a product and nothing crosses lanes. - **On Zen 5 the carry-less multiplier is the scarce unit**: reduce modulo `x^64 + x^4 + x^3 + x + 1` with shifts and XORs (`gf2_64::reduce`), and count CLMUL instructions, not products. The exception is the scalar `F64` product (`gf2_64::x86_64::mul`): one CLMUL fold, then a byte lookup of its spill (CLHASH's reduction). -- **`crates/verifier` is still auto-vectorized**: on AVX-512, LLVM turns table lookups into gathers that lose to scalar loads. Check its x86 disassembly after touching a lookup. +- **`crates/verifier` is still auto-vectorized**: on AVX-512, LLVM turns table lookups into gathers that lose to scalar loads. Check its x86 disassembly after touching a lookup. Tuned for Zen 4 (`znver4`, which `native` is there), the SLP vectorizer also packs the Keccak permutation's lanes into vectors and spends the gain on shuffles, slower than scalar; no portable rewrite tried avoids it without slowing other targets, and `-C llvm-args=-slp-max-vf=2` measures the verifier without it. - **Fiat-Shamir:** `ProverState::add_scalar` and `Transcript::next_scalar` bind into the transcript as a side effect, and the public statement seeds it at construction. Never re-observe data that rode the stream. - **Provers and verifiers derive the layout identically** from announced sizes: a change to `placements_of` or the schema lands in both provers and all three verifiers (`crates/verifier`'s `layout.rs` is the native one). `col_kappas` is derived from `col_kappa_sources`; keep it that way. - **The opening's lane fold binds the committed witness's top variables** (`whir::commit` documents the layout), so every verifier rotates the terminal point left by `INITIAL_FOLDING_FACTOR` before evaluating the basis, while every other read of the opening's point stays in round order. @@ -151,7 +154,6 @@ In the recursion program, the prover sends the verifier advice, called hints. Hi | var | effect | | ------------------------------------------------------------------------------ | ----------------------------------------------------- | -| `LEANVM_NUM_THREADS` | worker count; `1` = sequential | | `BENCH_TRACING`, `BENCH_REPEAT`, `BENCH_COOLDOWN` | `--tracing`, `--repeat`, `--cooldown` for the benches | | `KECCAK_N_LOG`, `KECCAK_DEPTH` | keccak_gkr batch size, rounds between committed states | | `PCS_LOG_N`, `PCS_LOG_INV_RATE`, `PCS_POINT_CLAIMS`, `PCS_RING_CLAIMS` | PCS throughput bench: size, rate, claims | diff --git a/Cargo.lock b/Cargo.lock index abba83434d..b549c86c91 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,6 +6,7 @@ version = 4 name = "aggregation" version = "0.1.0" dependencies = [ + "claims", "fiat_shamir_prover", "isa", "keccak256", @@ -30,6 +31,13 @@ dependencies = [ "memchr", ] +[[package]] +name = "allocator" +version = "0.1.0" +dependencies = [ + "tikv-jemallocator", +] + [[package]] name = "ansi_term" version = "0.12.1" @@ -90,12 +98,11 @@ dependencies = [ ] [[package]] -name = "bench" +name = "bench_harness" version = "0.1.0" dependencies = [ "isa", "libc", - "tikv-jemallocator", "tracing-forest", "tracing-subscriber", ] @@ -125,6 +132,14 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" +[[package]] +name = "claims" +version = "0.1.0" +dependencies = [ + "lean_sphincs", + "lean_xmss", +] + [[package]] name = "clap" version = "4.6.1" @@ -210,7 +225,8 @@ dependencies = [ name = "experimental_keccak_gkr" version = "0.1.0" dependencies = [ - "bench", + "allocator", + "bench_harness", "experimental_keccak_gkr", "experimental_parallel", "experimental_pcs", @@ -246,7 +262,8 @@ dependencies = [ name = "experimental_pcs" version = "0.1.0" dependencies = [ - "bench", + "allocator", + "bench_harness", "experimental_parallel", "experimental_primitives", "fiat_shamir_prover", @@ -334,7 +351,7 @@ dependencies = [ name = "keccak256" version = "0.1.0" dependencies = [ - "bench", + "bench_harness", "sha3", "stable_parallel", "verifier", @@ -370,12 +387,15 @@ name = "lean_vm" version = "0.1.0" dependencies = [ "aggregation", + "claims", "experimental_lean_vm_core", "isa", "lean_da", "lean_sphincs", "lean_xmss", + "sidecar", "stable_lean_vm_core", + "verifier", ] [[package]] @@ -387,11 +407,12 @@ dependencies = [ ] [[package]] -name = "leanvm-cli" +name = "leanvm-bench" version = "0.1.0" dependencies = [ "aggregation", - "bench", + "allocator", + "bench_harness", "clap", "experimental_lean_vm_core", "isa", @@ -405,6 +426,19 @@ dependencies = [ "zkdsl_compiler", ] +[[package]] +name = "leanvm-sidecar" +version = "0.1.0" +dependencies = [ + "aggregation", + "allocator", + "clap", + "experimental_lean_vm_core", + "lean_vm", + "sidecar", + "stable_lean_vm_core", +] + [[package]] name = "libc" version = "0.2.186" @@ -563,6 +597,20 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" +[[package]] +name = "sidecar" +version = "0.1.0" +dependencies = [ + "aggregation", + "claims", + "isa", + "lean_da", + "lean_sphincs", + "lean_xmss", + "libc", + "verifier", +] + [[package]] name = "smallvec" version = "1.15.2" @@ -573,7 +621,8 @@ checksum = "8ed6a63f02c8539c91a8685a86f4099661ba3da017932f6ebbea6de3f0fa7c90" name = "stable_keccak_gkr" version = "0.1.0" dependencies = [ - "bench", + "allocator", + "bench_harness", "fiat_shamir_prover", "isa", "stable_keccak_gkr", @@ -607,7 +656,8 @@ version = "0.1.0" name = "stable_pcs" version = "0.1.0" dependencies = [ - "bench", + "allocator", + "bench_harness", "fiat_shamir_prover", "isa", "stable_parallel", diff --git a/Cargo.toml b/Cargo.toml index f6cd3f37a7..37c7f5f870 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,8 @@ [workspace] members = [ "crates/verifier", - "crates/bench", + "crates/allocator", + "crates/bench_harness", "crates/keccak256", "crates/fiat_shamir_prover", "crates/isa", @@ -9,10 +10,13 @@ members = [ "crates/lean_xmss", "crates/lean_sphincs", "crates/lean_da", + "crates/claims", "crates/aggregation", "crates/lean_vm", + "crates/sidecar", "crates/stable_prover/*", "crates/experimental_prover/*", + "bench", "bins/*", ] resolver = "3" @@ -26,8 +30,13 @@ edition = "2024" needless_range_loop = "allow" too_many_arguments = "allow" +# With its `sidecar` feature, `lean_vm` still builds its prover, unused; CI's `cargo machete` finds unused dependencies. +[workspace.lints.cargo] +unused_dependencies = "allow" + [workspace.dependencies] -bench = { path = "crates/bench" } +allocator = { path = "crates/allocator" } +bench_harness = { path = "crates/bench_harness" } verifier = { path = "crates/verifier" } keccak256 = { path = "crates/keccak256" } fiat_shamir_prover = { path = "crates/fiat_shamir_prover" } @@ -36,7 +45,9 @@ zkdsl_compiler = { path = "crates/zkdsl_compiler" } lean_xmss = { path = "crates/lean_xmss" } lean_sphincs = { path = "crates/lean_sphincs" } lean_da = { path = "crates/lean_da" } +claims = { path = "crates/claims" } aggregation = { path = "crates/aggregation" } +sidecar = { path = "crates/sidecar" } libc = "0.2" rand = "0.9" sha3 = "0.11" diff --git a/README.md b/README.md index 0e5762fd0f..61b27dce6f 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ In the short term, we provide two Rust implementations of the prover, `stable` ( The leanXMSS parameters are specified in [leanXMSS.pdf](https://github.com/leanEthereum/leanVM/releases/download/doc-latest/leanXMSS.pdf), with a [security proof in Lean 4](formal/statement.tex). **This is not the definitive parameter set.** ```bash -cargo leanvm --prover experimental aggregate --xmss 1400 --log-inv-rate 2 --repeat 3 +cargo leanvm-bench --prover experimental aggregate --xmss 1400 --log-inv-rate 2 --repeat 3 ``` ``` @@ -87,7 +87,7 @@ aggregation, 1,400 leanXMSS signatures The leanSPHINCS parameters are specified in [leanSPHINCS.pdf](https://github.com/leanEthereum/leanVM/releases/download/doc-latest/leanSPHINCS.pdf), with a [security proof in Lean 4](formal/statement.tex). **This is not the definitive parameter set.** ```bash -cargo leanvm --prover experimental aggregate --sphincs 500 --log-inv-rate 1 --repeat 3 +cargo leanvm-bench --prover experimental aggregate --sphincs 500 --log-inv-rate 1 --repeat 3 ``` ``` @@ -103,7 +103,7 @@ aggregation, 500 leanSPHINCS signatures ### data availability ```bash -cargo leanvm --prover experimental aggregate --blobs 32 --log-inv-rate 1 --repeat 3 +cargo leanvm-bench --prover experimental aggregate --blobs 32 --log-inv-rate 1 --repeat 3 ``` ``` @@ -119,7 +119,7 @@ aggregation, 32 blobs ### recursion ```bash -cargo leanvm --prover experimental recursion --n 2 --xmss-per-leaf 1400 --leaf-log-inv-rate 2 --log-inv-rate 1 --repeat 3 +cargo leanvm-bench --prover experimental recursion --n 2 --xmss-per-leaf 1400 --leaf-log-inv-rate 2 --log-inv-rate 1 --repeat 3 ``` ``` @@ -154,7 +154,7 @@ Keccak GKR batch proving, 1,048,576 permutations, a state committed every 12 rou ### Fibonacci ```bash -cargo leanvm --prover experimental fibonacci --n 2000000 --log-inv-rate 1 --repeat 3 +cargo leanvm-bench --prover experimental fibonacci --n 2000000 --log-inv-rate 1 --repeat 3 ``` ``` diff --git a/bins/leanvm/Cargo.toml b/bench/Cargo.toml similarity index 66% rename from bins/leanvm/Cargo.toml rename to bench/Cargo.toml index 6a3666c993..fb2cf98efc 100644 --- a/bins/leanvm/Cargo.toml +++ b/bench/Cargo.toml @@ -1,11 +1,11 @@ [package] -name = "leanvm-cli" +name = "leanvm-bench" version.workspace = true edition.workspace = true publish = false [[bin]] -name = "leanvm" +name = "leanvm-bench" path = "src/main.rs" # A binary, nothing to document. doc = false @@ -21,9 +21,10 @@ lean_da.workspace = true zkdsl_compiler.workspace = true lean_xmss.workspace = true lean_sphincs.workspace = true -stable_lean_vm_core = { path = "../../crates/stable_prover/lean_vm_core" } -experimental_lean_vm_core = { path = "../../crates/experimental_prover/lean_vm_core" } -bench.workspace = true +stable_lean_vm_core = { path = "../crates/stable_prover/lean_vm_core" } +experimental_lean_vm_core = { path = "../crates/experimental_prover/lean_vm_core" } +allocator.workspace = true +bench_harness.workspace = true rand.workspace = true tracing.workspace = true clap = { version = "4", features = ["derive"] } diff --git a/bins/leanvm/bench.sh b/bench/bench.sh similarity index 80% rename from bins/leanvm/bench.sh rename to bench/bench.sh index d34c68f793..6b62878f57 100755 --- a/bins/leanvm/bench.sh +++ b/bench/bench.sh @@ -1,13 +1,13 @@ #!/usr/bin/env bash # Runs leanXMSS aggregation and 16-to-1 recursion at 4 and 8 threads and at the default pool ("full"), and prints a table of # proving time and peak RSS per benchmark and thread count. -# Usage: [BENCH_THREADS="4 8 full"] bins/leanvm/bench.sh stable|experimental [log file] (default: .build/bench-.log) +# Usage: [BENCH_THREADS="4 8 full"] bench/bench.sh stable|experimental [log file] (default: .build/bench-.log) set -uo pipefail PROVER=${1:-} -case $PROVER in stable | experimental) ;; *) echo "usage: bins/leanvm/bench.sh stable|experimental [log file]" >&2; exit 2 ;; esac +case $PROVER in stable | experimental) ;; *) echo "usage: bench/bench.sh stable|experimental [log file]" >&2; exit 2 ;; esac -cd "$(dirname "$0")/../.." +cd "$(dirname "$0")/.." mkdir -p .build LOG="${2:-$PWD/.build/bench-$(date +%Y%m%d-%H%M%S).log}" RESULTS=$(mktemp) @@ -27,17 +27,17 @@ measure() { run() { local label=$1 threads=$2; shift 2 - local pool=(env LEANVM_NUM_THREADS="$threads") - [ "$threads" = full ] && pool=(env -u LEANVM_NUM_THREADS) - echo "=== ${pool[*]:1} leanvm --prover $PROVER $* --cooldown 5 ===" | tee -a "$LOG" - "${pool[@]}" "${CARGO_TARGET_DIR:-target}/release/leanvm" --prover "$PROVER" "$@" --cooldown 5 2>&1 | tee "$OUT" | tee -a "$LOG" + local count=$threads + [ "$threads" = full ] && count=0 + echo "=== leanvm-bench --prover $PROVER $* --threads $count --cooldown 5 ===" | tee -a "$LOG" + "${CARGO_TARGET_DIR:-target}/release/leanvm-bench" --prover "$PROVER" "$@" --threads "$count" --cooldown 5 2>&1 | tee "$OUT" | tee -a "$LOG" printf '%s\t%s\t%s\n' "$label" "$threads" "$(measure)" >>"$RESULTS" echo | tee -a "$LOG" } read -ra THREADS <<<"${BENCH_THREADS:-4 8 full}" -cargo build --release -p leanvm-cli || exit 1 +cargo build --release -p leanvm-bench || exit 1 for t in "${THREADS[@]}"; do run "aggregate, 1400 leanXMSS, rate 2" "$t" aggregate --xmss 1400 --log-inv-rate 2 --repeat 3 diff --git a/bins/leanvm/src/benchmark.rs b/bench/src/benchmark.rs similarity index 80% rename from bins/leanvm/src/benchmark.rs rename to bench/src/benchmark.rs index 93a88a662d..cf5af42242 100644 --- a/bins/leanvm/src/benchmark.rs +++ b/bench/src/benchmark.rs @@ -3,7 +3,7 @@ //! signature counts and a blob count; recursion accepts these counts per leaf. use aggregation::{AggregateClaims, AggregateProof, OutputClaims, aggregate, signers_cache}; -use bench::{Plan, Timing}; +use bench_harness::{Plan, Timing}; use isa::{Prover, Stats, pretty_f64, pretty_integer}; use lean_xmss::{LeanXmssPublicKey, LeanXmssSignature}; use rand::{Rng, SeedableRng, rngs::StdRng}; @@ -79,7 +79,7 @@ fn describe(n_lean_xmss: usize, n_lean_sphincs: usize) -> String { /// Prove signatures and `n_blobs` blobs in one leaf, then verify it. /// /// Proving runs one discarded warmup pass followed by `plan.repeat` measured -/// passes; see [`bench`] for why the first pass is not +/// passes; see [`bench_harness`] for why the first pass is not /// representative and why the cooldown matters. pub fn run_aggregation( n_lean_xmss: usize, @@ -99,7 +99,7 @@ pub fn run_aggregation( // Only the final measured pass of each stage is traced: the tree describes the // proof the reported timings are about, instead of repeating itself per pass. let ((claims, sig, stats), prove_time) = plan.warm_then_measure(|last| { - let _quiet = (!last).then(bench::suppress_tracing); + let _quiet = (!last).then(bench_harness::suppress_tracing); aggregate::

( &[], raw_lean_xmss.clone(), @@ -111,7 +111,7 @@ pub fn run_aggregation( .expect("leaf aggregates") }); let (_, verify_time) = Plan::new(plan.repeat, 0).measure_quiet(|last| { - let _quiet = (!last).then(bench::suppress_tracing); + let _quiet = (!last).then(bench_harness::suppress_tracing); assert!(sig.verify(&claims), "the leaf aggregate verifies"); }); drop(trace_span); @@ -183,14 +183,14 @@ pub fn run_recursion( let children = leaves_from_subprocess(n); if enable_tracing { - bench::init_tracing(); + bench_harness::init_tracing(); } let ((claims, sig, stats), prove_time) = plan.warm_then_measure(|last| { - let _quiet = (!last).then(bench::suppress_tracing); + let _quiet = (!last).then(bench_harness::suppress_tracing); aggregate::

(&children, vec![], vec![], None, OutputClaims::All, log_inv_rate).expect("node aggregates") }); let (_, verify_time) = Plan::new(plan.repeat, 0).measure_quiet(|last| { - let _quiet = (!last).then(bench::suppress_tracing); + let _quiet = (!last).then(bench_harness::suppress_tracing); assert!(sig.verify(&claims), "the recursive aggregate verifies"); }); @@ -232,8 +232,8 @@ pub fn run_recursion( ); } -/// Prove a recursion step's `n` leaves and write them to `out`: each leaf's claims, then its proof's -/// bytes after their length. +/// Prove a recursion step's `n` leaves and write them to `out`: each leaf's claims and its proof's bytes, each after +/// its length. fn write_leaves( n: usize, per_leaf: usize, @@ -255,73 +255,27 @@ fn write_leaves( log_inv_rate, ) .expect("leaf aggregates"); + // The proof carries no claims; the parent trusts its subprocess for them, standing in for a receiver that + // knows them. let (claims, leaf, _) = leaf; - write_claims(&mut bytes, &claims); - let proof = leaf.to_bytes(); - bytes.extend((proof.len() as u64).to_le_bytes()); - bytes.extend(proof); + for part in [claims.to_bytes(), leaf.to_bytes()] { + bytes.extend((part.len() as u64).to_le_bytes()); + bytes.extend(part); + } } std::fs::write(out, bytes).expect("the leaves are written"); } -/// A leaf's claims in the leaves' file: each list as its length and its `(key, message)` pairs, then -/// a 0/1 byte for whether there is a DA root and 32 root bytes, zero without one. The proof carries -/// no claims; the parent trusts its subprocess for them, standing in for a receiver that knows them. -fn write_claims(out: &mut Vec, claims: &AggregateClaims) { - out.extend((claims.lean_xmss.len() as u64).to_le_bytes()); - for (pk, message) in &claims.lean_xmss { - out.extend(pk.to_bytes()); - out.extend(message); - } - out.extend((claims.lean_sphincs.len() as u64).to_le_bytes()); - for (pk, message) in &claims.lean_sphincs { - out.extend(pk.to_bytes()); - out.extend(message); - } - out.push(u8::from(claims.da_commitment.is_some())); - out.extend(claims.da_commitment.unwrap_or_default()); -} - /// The leaves' file, read back: the bytes not yet read. struct LeavesFile<'a>(&'a [u8]); impl<'a> LeavesFile<'a> { - fn take(&mut self, n: usize) -> &'a [u8] { - let (head, rest) = self.0.split_at(n); + /// The next length-prefixed part. + fn part(&mut self) -> &'a [u8] { + let (len, rest) = self.0.split_first_chunk().expect("a length"); + let (part, rest) = rest.split_at(u64::from_le_bytes(*len) as usize); self.0 = rest; - head - } - - fn array(&mut self) -> [u8; N] { - self.take(N).try_into().expect("N bytes") - } - - fn count(&mut self) -> usize { - u64::from_le_bytes(self.array()) as usize - } - - /// The inverse of [`write_claims`]. - fn claims(&mut self) -> AggregateClaims { - let n = self.count(); - let lean_xmss = (0..n) - .map(|_| (LeanXmssPublicKey::from_bytes(&self.array()), self.array())) - .collect(); - let n = self.count(); - let lean_sphincs = (0..n) - .map(|_| { - ( - lean_sphincs::LeanSphincsPublicKey::from_bytes(&self.array()), - self.array(), - ) - }) - .collect(); - let [has_root] = self.array(); - let root = self.array(); - AggregateClaims { - lean_xmss, - lean_sphincs, - da_commitment: (has_root == 1).then_some(root), - } + part } } @@ -351,12 +305,8 @@ fn leaves_from_subprocess(n: usize) -> Vec<(AggregateClaims, AggregateProof)> { let mut file = LeavesFile(&bytes); let mut leaves = Vec::with_capacity(n); while !file.0.is_empty() { - let claims = file.claims(); - let len = file.count(); - leaves.push(( - claims, - AggregateProof::from_bytes(file.take(len)).expect("a leaf proof"), - )); + let claims = AggregateClaims::from_bytes(file.part()).expect("a leaf's claims"); + leaves.push((claims, AggregateProof::from_bytes(file.part()).expect("a leaf proof"))); } assert_eq!(leaves.len(), n, "one proof per leaf"); leaves diff --git a/bins/leanvm/src/fibonacci.rs b/bench/src/fibonacci.rs similarity index 94% rename from bins/leanvm/src/fibonacci.rs rename to bench/src/fibonacci.rs index 56314d415d..55311dd401 100644 --- a/bins/leanvm/src/fibonacci.rs +++ b/bench/src/fibonacci.rs @@ -1,7 +1,7 @@ //! Fibonacci in the exponent: the demo benchmark (`buff[g^k] = g^{F(k)}`, //! recurrence `buff[i·g²] = buff[i·g] · buff[i]`). -use bench::Plan; +use bench_harness::Plan; use isa::{Prover, pretty_f64, pretty_integer}; use verifier::{E, K}; use zkdsl_compiler::{compile, parse}; @@ -9,7 +9,7 @@ use zkdsl_compiler::{compile, parse}; /// Prove and verify Fibonacci-in-the-exponent over a `HeapBuf` (an unrolled /// `mul_range` recurrence), binding `g^{F(n)}` as the public input. Prints the /// benchmark report. Proving runs one discarded warmup pass followed by -/// `plan.repeat` measured passes (see [`bench`]). +/// `plan.repeat` measured passes (see [`bench_harness`]). pub fn run_fibonacci(n: usize, log_inv_rate: usize, plan: Plan) { let trace_span = tracing::info_span!("Fibonacci", n, log_inv_rate).entered(); @@ -19,11 +19,11 @@ pub fn run_fibonacci(n: usize, log_inv_rate: usize, plan: Plan) { // Only the final measured pass of each stage is traced (see `run_aggregation`). let ((proof, stats), prove_time) = plan.warm_then_measure(|last| { - let _quiet = (!last).then(bench::suppress_tracing); + let _quiet = (!last).then(bench_harness::suppress_tracing); P::prove(&program, &[], pi, log_inv_rate).expect("the Fibonacci program proves") }); let (_, verify_time) = Plan::new(plan.repeat, 0).measure_quiet(|last| { - let _quiet = (!last).then(bench::suppress_tracing); + let _quiet = (!last).then(bench_harness::suppress_tracing); verifier::verify_execution(program.bytecode(), &public_input, &proof).unwrap() }); @@ -111,7 +111,7 @@ mod tests { super::run_fibonacci::( 200_000, stable_lean_vm_core::pcs::TEST_LOG_INV_RATE, - bench::Plan::default(), + bench_harness::Plan::default(), ); } } diff --git a/bins/leanvm/src/main.rs b/bench/src/main.rs similarity index 92% rename from bins/leanvm/src/main.rs rename to bench/src/main.rs index 3f1a30ebaf..55b4c53f96 100644 --- a/bins/leanvm/src/main.rs +++ b/bench/src/main.rs @@ -1,7 +1,7 @@ //! Benchmark CLI for signature and blob proofs, recursion, and the Fibonacci demo. #[global_allocator] -static ALLOCATOR: bench::Jemalloc = bench::Jemalloc; +static ALLOCATOR: allocator::Jemalloc = allocator::Jemalloc; use clap::{Parser, Subcommand}; use experimental_lean_vm_core::ExperimentalProver; @@ -13,10 +13,11 @@ mod report; /// Run the CLI's command on prover `P`. fn run(cli: Cli) { + P::set_num_threads(cli.threads); P::init(); - let plan = bench::Plan::new(cli.repeat, cli.cooldown); + let plan = bench_harness::Plan::new(cli.repeat, cli.cooldown); if cli.tracing && !matches!(&cli.command, Command::Recursion { .. }) { - bench::init_tracing(); + bench_harness::init_tracing(); } match cli.command { Command::Aggregate { xmss, sphincs, blobs } => { @@ -54,7 +55,7 @@ enum Prover { } #[derive(Parser)] -#[command(name = "leanvm")] +#[command(name = "leanvm-bench")] struct Cli { /// WHIR inverse-rate logarithm (1 through 4). #[arg( @@ -78,6 +79,10 @@ struct Cli { )] repeat: usize, + /// Thread count (0: one per core). + #[arg(long, global = true, default_value_t = 0)] + threads: usize, + /// Idle seconds before each measured pass. #[arg(long, global = true, default_value_t = 2)] cooldown: u64, diff --git a/bins/leanvm/src/report.rs b/bench/src/report.rs similarity index 95% rename from bins/leanvm/src/report.rs rename to bench/src/report.rs index f56078bf04..77fd8c8420 100644 --- a/bins/leanvm/src/report.rs +++ b/bench/src/report.rs @@ -17,7 +17,7 @@ pub fn pow(x: usize) -> String { /// Peak resident set size, in GiB. pub fn peak_gib() -> String { - pretty_f64(bench::peak_rss_bytes() as f64 / (1u64 << 30) as f64) + pretty_f64(bench_harness::peak_rss_bytes() as f64 / (1u64 << 30) as f64) } /// One line of per-table instruction counts and shares, largest first, followed by memory, bytecode (`code_len` diff --git a/bins/leanvm/tests/identical_proofs.rs b/bench/tests/identical_proofs.rs similarity index 98% rename from bins/leanvm/tests/identical_proofs.rs rename to bench/tests/identical_proofs.rs index f8e9c9ccfd..c6c1ce417a 100644 --- a/bins/leanvm/tests/identical_proofs.rs +++ b/bench/tests/identical_proofs.rs @@ -76,10 +76,7 @@ fn prove( /// `# witness name:`) and with a wrong public input, through experimental's interpreter and hints too, at every rate. #[test] fn compiler_programs_prove_identically() { - let dir = concat!( - env!("CARGO_MANIFEST_DIR"), - "/../../crates/zkdsl_compiler/tests/programs" - ); + let dir = concat!(env!("CARGO_MANIFEST_DIR"), "/../crates/zkdsl_compiler/tests/programs"); let mut paths: Vec<_> = std::fs::read_dir(dir) .unwrap() .map(|entry| entry.unwrap().path()) diff --git a/bins/leanvm-sidecar/Cargo.toml b/bins/leanvm-sidecar/Cargo.toml new file mode 100644 index 0000000000..b86d719a6a --- /dev/null +++ b/bins/leanvm-sidecar/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "leanvm-sidecar" +version.workspace = true +edition.workspace = true +publish = false + +[[bin]] +name = "leanvm-sidecar" +path = "src/main.rs" +# A binary, nothing to document. +doc = false + +[lints] +workspace = true + +[dependencies] +sidecar = { workspace = true, features = ["server"] } +stable_lean_vm_core = { path = "../../crates/stable_prover/lean_vm_core" } +experimental_lean_vm_core = { path = "../../crates/experimental_prover/lean_vm_core", optional = true } +allocator.workspace = true +clap = { version = "4", features = ["derive"] } + +[features] +# Run the experimental prover rather than the stable one. +experimental_prover = ["dep:experimental_lean_vm_core"] + +[dev-dependencies] +lean_vm = { path = "../../crates/lean_vm" } +aggregation = { workspace = true, features = ["signers-cache"] } diff --git a/bins/leanvm-sidecar/README.md b/bins/leanvm-sidecar/README.md new file mode 100644 index 0000000000..cafd916d43 --- /dev/null +++ b/bins/leanvm-sidecar/README.md @@ -0,0 +1,110 @@ +# leanvm-sidecar + +Generates and verifies leanVM proofs, in a dedicated process, communicating with your client (in any langage) over a Unix socket. + +## Running + +```bash +cargo build --release -p leanvm-sidecar +target/release/leanvm-sidecar --socket /run/leanvm/sidecar.sock +``` + +It runs the stable prover, or, built with `--features experimental_prover`, the experimental one (the same proofs, faster). + +| flag | default | meaning | +| --- | --- | --- | +| `--socket` | required | the socket to listen on | +| `--threads` | 0 | thread count, 0 for one per core | + +- It warms up (two discarded proofs) before listening, so the socket appears once it is ready. It logs to standard error. +- One sidecar per socket: it locks `.lock`, and replaces a stale socket left by one that stopped. +- The socket takes the process's umask, and connecting needs write permission on it: let only trusted clients connect, since any of them can keep it proving. Put it in a directory only the sidecar's user can write (not `/tmp`). +- It serves 16 connections at once; more wait to be accepted. +- Requests are proven one at a time, in the order they arrive. Closing the connection drops a request whose proof has not started. +- Stop it with a signal; requests in flight are lost. + +## Clients + +From Rust, with `lean_vm = { features = ["sidecar"] }`, the client neither proves nor verifies itself: `lean_vm::sidecar::Client` does both, with the library's arguments, blobs as they are and proofs as bytes: + +```rust +let mut sidecar = lean_vm::sidecar::Client::connect("/run/leanvm/sidecar.sock")?; +let (claims, proof) = sidecar.aggregate(&children, &raw_lean_xmss, &[], &[], OutputClaims::All, 2)?; +assert!(sidecar.verify(&claims, &proof)?); +``` + +A call waits as long as the sidecar takes, unless `set_timeout` bounds it; after a timeout, drop the client. + +From Go, `clients/go` (package `leanvmsidecar`) offers the same, `Dial`, then `Aggregate` and `Verify`, with claims, proofs, signatures and blobs in the byte forms below. From another language, implement the protocol below. + +## Protocol, version 1 + +Integers are little-endian. A **frame** and a **bytes** field are a `u64` length, then that many bytes; a **count** is a `u64`. + +On connecting, the client reads a hello frame, then sends request frames, reading each one's response before sending the next. A connection whose writing half is shut down counts as closed. + +### Hello + +| field | type | value | +| --- | --- | --- | +| magic | 8 bytes | `leanvmsc` | +| version | `u32` | 1 | +| max request | `u64` | the longest request body taken, 64 MiB | +| verifier | bytes | the verifier's tag, `leanvm-v1` | +| prover | bytes | `stable` or `experimental` | + +The magic and the version come first in every version. A client checks them and the verifier tag, and sends no request body longer than the max request. + +### Requests + +**Aggregate**, the library's `aggregate`: + +| field | type | meaning | +| --- | --- | --- | +| operation | `u8` | 1 | +| rate | `u8` | `log_inv_rate`, 1 to 4 | +| children | count, then per child: claims (bytes), proof (bytes) | at most 16 | +| leanXMSS signatures | count, then per signature: pubkey (32 bytes), message (32 bytes), signature (2404 bytes) | | +| leanSPHINCS signatures | count, then per signature: pubkey (32 bytes), message (32 bytes), signature (7740 bytes) | | +| blobs | count, then per blob: 8192 symbols, each a `u128` | at most 32 | +| output | `u8`, then for 1 the claims (bytes) | 0: every covered claim; 1: exactly these | + +Keys, signatures and proofs are the libraries' `to_bytes`. Signatures may come in any order, and a repeated claim counts once. Children are not verified: an invalid one fails the proof (status 3 or 4), so verify untrusted children first. + +**Verify**, `AggregateProof::verify`, answered without waiting for proofs in progress: + +| field | type | +| --- | --- | +| operation | `u8`, 2 | +| claims | bytes | +| proof | bytes | + +**Claims**, `AggregateClaims::to_bytes`: + +| field | type | +| --- | --- | +| leanXMSS claims | count, then per claim: pubkey (32 bytes), message (32 bytes) | +| leanSPHINCS claims | count, then per claim: pubkey (32 bytes), message (32 bytes) | +| DA commitment | `u8`: 0, or 1 then the root (32 bytes) | + +A proof verifies only against claim lists strictly sorted by their 64 bytes; a response's claims are. Output claims may come in any order. + +### Response + +| field | type | meaning | +| --- | --- | --- | +| status | `u8` | below | +| for 0, to `aggregate` | claims (bytes), proof (bytes) | what the proof proves, and the proof | +| for 0, to `verify` | `u8` | 1 if the proof proves the claims, else 0 (also for a proof that does not decode) | +| otherwise | bytes | a UTF-8 message | + +| status | name | meaning | +| --- | --- | --- | +| 0 | ok | | +| 1 | invalid input | a rate out of range, too many children or blobs, nothing to publish, an output claim not covered | +| 2 | invalid signature | | +| 3 | invalid child | a child is invalid, or its proof does not decode | +| 4 | proving failed | an invalid input the checks missed, or too large for one proof | +| 5 | malformed | the request does not decode, or exceeds the max request | + +A request with several problems gets the first one found; `verify` gets only 0 or 5. The connection stays usable after any response but to a request over the max request, which is refused unread and closes it. diff --git a/bins/leanvm-sidecar/clients/go/client.go b/bins/leanvm-sidecar/clients/go/client.go new file mode 100644 index 0000000000..637799e24e --- /dev/null +++ b/bins/leanvm-sidecar/clients/go/client.go @@ -0,0 +1,152 @@ +// Package leanvmsidecar proves and verifies leanVM aggregates through leanvm-sidecar, over its Unix socket. Claims, +// proofs, signatures and blobs are byte strings in the forms bins/leanvm-sidecar/README.md specifies. +// +// A Client serves one call at a time. After an error other than a *Rejection, close it and dial again. A request +// longer than the sidecar takes (64 MiB) fails so: the sidecar refuses it unread and closes the connection. +package leanvmsidecar + +import ( + "bytes" + "encoding/binary" + "errors" + "fmt" + "io" + "net" +) + +// The longest response read: a request's claims, at most its 64 MiB, and room for a proof. +const maxResponse = 80 << 20 + +var errMalformed = errors.New("leanvm-sidecar: a response that does not decode") + +// Rejection is the sidecar's refusal of a request (status 1 to 5), which leaves the connection usable. +type Rejection struct { + Status byte + Message string +} + +func (r *Rejection) Error() string { + return fmt.Sprintf("leanvm-sidecar refused the request (status %d): %s", r.Status, r.Message) +} + +// Proven is a proof and the claims it proves. +type Proven struct{ Claims, Proof []byte } + +// Client is a connection to a sidecar. +type Client struct{ conn net.Conn } + +// Dial connects to the sidecar listening on socket, and checks it speaks protocol version 1 for verifier leanvm-v1. +func Dial(socket string) (*Client, error) { + conn, err := net.Dial("unix", socket) + if err != nil { + return nil, err + } + hello, err := readFrame(conn) + if err == nil && (len(hello) < 20 || string(hello[:12]) != "leanvmsc\x01\x00\x00\x00" || + !bytes.HasPrefix(hello[20:], appendField(nil, []byte("leanvm-v1")))) { + err = errors.New("leanvm-sidecar: not a sidecar of protocol version 1 for verifier leanvm-v1") + } + if err != nil { + conn.Close() + return nil, err + } + return &Client{conn}, nil +} + +// Close closes the connection; the sidecar drops a request it has not started proving. +func (c *Client) Close() error { return c.conn.Close() } + +// Aggregate proves the children's claims and the signatures, and commits to the blobs, at the code rate +// 2^-logInvRate (1 to 4). A leanXMSS or leanSPHINCS signature is its pubkey, message and signature, end to end. With +// only nil the proof publishes every claim it covers, else exactly those claims. Children are not verified: verify an +// untrusted one first. +func (c *Client) Aggregate(children []Proven, leanXmss, leanSphincs, blobs [][]byte, only []byte, + logInvRate byte) (Proven, error) { + b := binary.LittleEndian.AppendUint64([]byte{1, logInvRate}, uint64(len(children))) + for _, child := range children { + b = appendField(appendField(b, child.Claims), child.Proof) + } + for _, list := range [][][]byte{leanXmss, leanSphincs, blobs} { + b = binary.LittleEndian.AppendUint64(b, uint64(len(list))) + for _, item := range list { + b = append(b, item...) + } + } + if only == nil { + b = append(b, 0) + } else { + b = appendField(append(b, 1), only) + } + r, err := c.call(b) + if err != nil { + return Proven{}, err + } + claims, r, ok1 := field(r) + proof, r, ok2 := field(r) + if !ok1 || !ok2 || len(r) != 0 { + return Proven{}, errMalformed + } + return Proven{claims, proof}, nil +} + +// Verify says whether proof proves claims, as the library's AggregateProof::verify does. +func (c *Client) Verify(claims, proof []byte) (bool, error) { + r, err := c.call(appendField(appendField([]byte{2}, claims), proof)) + if err != nil { + return false, err + } + if len(r) != 1 || r[0] > 1 { + return false, errMalformed + } + return r[0] == 1, nil +} + +// call sends a request and returns its successful response, past the status. +func (c *Client) call(request []byte) ([]byte, error) { + if _, err := c.conn.Write(appendField(nil, request)); err != nil { + return nil, err + } + r, err := readFrame(c.conn) + if err != nil { + return nil, err + } + if len(r) == 0 { + return nil, errMalformed + } + if r[0] != 0 { + message, rest, ok := field(r[1:]) + if !ok || len(rest) != 0 { + return nil, errMalformed + } + return nil, &Rejection{r[0], string(message)} + } + return r[1:], nil +} + +func readFrame(conn io.Reader) ([]byte, error) { + var prefix [8]byte + if _, err := io.ReadFull(conn, prefix[:]); err != nil { + return nil, err + } + n := binary.LittleEndian.Uint64(prefix[:]) + if n > maxResponse { + return nil, errMalformed + } + body := make([]byte, n) + _, err := io.ReadFull(conn, body) + return body, err +} + +// appendField appends v after its u64 length. +func appendField(b, v []byte) []byte { + return append(binary.LittleEndian.AppendUint64(b, uint64(len(v))), v...) +} + +// field splits a length-prefixed field off the front of b. +func field(b []byte) (v, rest []byte, ok bool) { + if len(b) < 8 || binary.LittleEndian.Uint64(b) > uint64(len(b)-8) { + return nil, nil, false + } + n := 8 + binary.LittleEndian.Uint64(b) + return b[8:n], b[n:], true +} diff --git a/bins/leanvm-sidecar/clients/go/client_test.go b/bins/leanvm-sidecar/clients/go/client_test.go new file mode 100644 index 0000000000..9bc3f6cf59 --- /dev/null +++ b/bins/leanvm-sidecar/clients/go/client_test.go @@ -0,0 +1,55 @@ +package leanvmsidecar + +import ( + "bytes" + "errors" + "os" + "testing" +) + +// Against a running sidecar, whose socket LEANVM_SIDECAR_SOCKET names: a blob-only leaf, which needs no keys, a node +// over it, verifying both ways, and a refusal that leaves the connection usable. +func TestSidecar(t *testing.T) { + socket := os.Getenv("LEANVM_SIDECAR_SOCKET") + if socket == "" { + t.Skip("LEANVM_SIDECAR_SOCKET names no sidecar") + } + c, err := Dial(socket) + if err != nil { + t.Fatal(err) + } + defer c.Close() + + blob := make([]byte, 8192*16) + for i := range blob { + blob[i] = byte(i) + } + leaf, err := c.Aggregate(nil, nil, nil, [][]byte{blob}, nil, 2) + if err != nil { + t.Fatal(err) + } + node, err := c.Aggregate([]Proven{leaf}, nil, nil, nil, nil, 1) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(node.Claims, leaf.Claims) { + t.Fatal("the node publishes other claims than its leaf") + } + noClaims := make([]byte, 17) + for _, check := range []struct { + claims, proof []byte + valid bool + }{{leaf.Claims, leaf.Proof, true}, {node.Claims, node.Proof, true}, {noClaims, node.Proof, false}, {node.Claims, nil, false}} { + if valid, err := c.Verify(check.claims, check.proof); err != nil || valid != check.valid { + t.Fatalf("verify: %v, %v, want %v", valid, err, check.valid) + } + } + + var rejection *Rejection + if _, err := c.Aggregate(nil, nil, nil, [][]byte{blob}, nil, 0); !errors.As(err, &rejection) || rejection.Status != 1 { + t.Fatalf("rate 0: %v", err) + } + if valid, err := c.Verify(leaf.Claims, leaf.Proof); err != nil || !valid { + t.Fatalf("after a refusal: %v, %v", valid, err) + } +} diff --git a/bins/leanvm-sidecar/clients/go/go.mod b/bins/leanvm-sidecar/clients/go/go.mod new file mode 100644 index 0000000000..2afff0845a --- /dev/null +++ b/bins/leanvm-sidecar/clients/go/go.mod @@ -0,0 +1,3 @@ +module github.com/leanEthereum/leanVM/bins/leanvm-sidecar/clients/go + +go 1.22 diff --git a/bins/leanvm-sidecar/src/main.rs b/bins/leanvm-sidecar/src/main.rs new file mode 100644 index 0000000000..1385c3e7b7 --- /dev/null +++ b/bins/leanvm-sidecar/src/main.rs @@ -0,0 +1,33 @@ +//! The leanVM sidecar: proves for one machine's clients over a Unix socket. `README.md` says how to run it and +//! specifies the protocol. + +#[global_allocator] +static ALLOCATOR: allocator::Jemalloc = allocator::Jemalloc; + +use clap::Parser; +use std::path::PathBuf; +use std::process::ExitCode; + +#[cfg(feature = "experimental_prover")] +type P = experimental_lean_vm_core::ExperimentalProver; +#[cfg(not(feature = "experimental_prover"))] +type P = stable_lean_vm_core::StableProver; + +#[derive(Parser)] +#[command(name = "leanvm-sidecar")] +struct Cli { + /// The Unix socket to listen on. It appears once the sidecar is ready. + #[arg(long)] + socket: PathBuf, + + /// Thread count (0: one per core). + #[arg(long, default_value_t = 0)] + threads: usize, +} + +fn main() -> ExitCode { + let cli = Cli::parse(); + let Err(e) = sidecar::serve::

(&cli.socket, cli.threads); + eprintln!("leanvm-sidecar: {}: {e}", cli.socket.display()); + ExitCode::FAILURE +} diff --git a/bins/leanvm-sidecar/tests/sidecar.rs b/bins/leanvm-sidecar/tests/sidecar.rs new file mode 100644 index 0000000000..1db647ec8c --- /dev/null +++ b/bins/leanvm-sidecar/tests/sidecar.rs @@ -0,0 +1,132 @@ +//! The sidecar binary end to end, through the Rust client: a leaf with every kind of input, a narrowed node over +//! it from a second connection, verifying, refusals that leave the connection usable, an unknown operation sent as +//! another language's client would, and the socket's life across a second sidecar and a restart. + +use aggregation::signers_cache; +use lean_vm::{AggregateClaims, OutputClaims, lean_da}; +use sidecar::{Client, Rejection, SidecarError}; +use stable_lean_vm_core::StableProver; +use std::io::{Read, Write}; +use std::os::unix::net::UnixStream; +use std::path::{Path, PathBuf}; +use std::process::{Child, Command}; +use std::time::{Duration, Instant}; + +struct Sidecar(Child); + +impl Drop for Sidecar { + fn drop(&mut self) { + let _ = self.0.kill(); + let _ = self.0.wait(); + } +} + +/// The socket's path, and its lock's, removed at the end whatever happens. +struct Socket(PathBuf); + +impl Drop for Socket { + fn drop(&mut self) { + let _ = std::fs::remove_file(&self.0); + let _ = std::fs::remove_file(self.0.with_extension("sock.lock")); + } +} + +fn command(socket: &Path) -> Command { + let mut command = Command::new(env!("CARGO_BIN_EXE_leanvm-sidecar")); + command.arg("--socket").arg(socket); + command +} + +/// Starts a sidecar and waits until it serves. +fn start(socket: &Path) -> (Sidecar, Client) { + let mut sidecar = Sidecar(command(socket).spawn().unwrap()); + let start = Instant::now(); + loop { + if let Ok(client) = Client::connect(socket) { + return (sidecar, client); + } + assert!(sidecar.0.try_wait().unwrap().is_none(), "the sidecar exited"); + assert!(start.elapsed() < Duration::from_secs(300), "the sidecar never served"); + std::thread::sleep(Duration::from_millis(50)); + } +} + +fn read_frame(stream: &mut UnixStream) -> Vec { + let mut len = [0; 8]; + stream.read_exact(&mut len).unwrap(); + let mut body = vec![0; u64::from_le_bytes(len) as usize]; + stream.read_exact(&mut body).unwrap(); + body +} + +#[test] +fn sidecar_end_to_end() { + let guard = Socket(PathBuf::from(format!( + "/tmp/leanvm-sidecar-test-{}.sock", + std::process::id() + ))); + let socket = &guard.0; + let (first, mut client) = start(socket); + assert_eq!( + client.prover(), + if cfg!(feature = "experimental_prover") { + "experimental" + } else { + "stable" + } + ); + + let xmss = signers_cache::get_signers::(3); + let sphincs = signers_cache::get_lean_sphincs_signers::(1); + let blobs: Vec = vec![std::array::from_fn(|i| i as u128)]; + let leaf = client + .aggregate(&[], &xmss[..2], &sphincs, &blobs, OutputClaims::All, 2) + .unwrap(); + assert!(client.verify(&leaf.0, &leaf.1).unwrap()); + assert_eq!(leaf.0.num_signature_claims(), 3); + assert_eq!( + leaf.0.da_commitment, + Some(lean_da::BlobMatrix::build(&blobs).unwrap().commitment()) + ); + + // A second connection while the first stays open: the leaf and one more signature, keeping only one leanXMSS + // claim of each. + let mut other = Client::connect(socket).unwrap(); + let mut kept: Vec<_> = [&xmss[0], &xmss[2]].iter().map(|(pk, m, _)| (*pk, *m)).collect(); + kept.sort(); + let only = AggregateClaims { + lean_xmss: kept, + ..AggregateClaims::default() + }; + let node = other + .aggregate(&[leaf], &xmss[2..], &[], &[], OutputClaims::Only(&only), 1) + .unwrap(); + assert_eq!(node.0, only); + assert!(client.verify(&only, &node.1).unwrap()); + // Not against other claims. + assert!(!other.verify(&AggregateClaims::default(), &node.1).unwrap()); + + // Refusals leave the connection in step: two in a row. + for rate in [0, 300] { + match client.aggregate(&[], &xmss[..1], &[], &[], OutputClaims::All, rate) { + Err(SidecarError::Rejected(Rejection::InvalidInput, message)) => assert!(message.contains("log_inv_rate")), + other => panic!("rate {rate}: {other:?}"), + } + } + + // A client in another language sends an operation this version does not have, twice: a refusal keeps the + // connection usable. + let mut raw = UnixStream::connect(socket).unwrap(); + assert!(read_frame(&mut raw).starts_with(b"leanvmsc")); + for _ in 0..2 { + raw.write_all(&[&1u64.to_le_bytes()[..], &[7]].concat()).unwrap(); + assert_eq!(read_frame(&mut raw)[0], Rejection::Malformed as u8); + } + + // A second sidecar leaves a live one's socket alone; a restart replaces the stale socket a killed one leaves. + let refused = command(socket).output().unwrap(); + assert!(!refused.status.success()); + drop(first); + assert!(socket.exists()); + let (_sidecar, _client) = start(socket); +} diff --git a/crates/aggregation/Cargo.toml b/crates/aggregation/Cargo.toml index 0c192c0b25..087ca25e81 100644 --- a/crates/aggregation/Cargo.toml +++ b/crates/aggregation/Cargo.toml @@ -13,6 +13,7 @@ verifier.workspace = true keccak256.workspace = true fiat_shamir_prover.workspace = true zkdsl_compiler.workspace = true +claims.workspace = true lean_xmss.workspace = true lean_sphincs.workspace = true lean_da.workspace = true diff --git a/crates/aggregation/src/coverage.rs b/crates/aggregation/src/coverage.rs index f7de07c4ae..3c39e1d676 100644 --- a/crates/aggregation/src/coverage.rs +++ b/crates/aggregation/src/coverage.rs @@ -1,8 +1,8 @@ //! The coverage table: which slot each raw signature and child claim writes. use super::error::AggregateError; -use super::proof::{AggregateClaims, AggregateProof}; -use super::{LeanSphincsClaim, LeanXmssClaim}; +use super::proof::AggregateProof; +use super::{AggregateClaims, LeanSphincsClaim, LeanXmssClaim}; use std::collections::BTreeSet; /// The coverage slot each write in the guest's coverage walk targets, in walk diff --git a/crates/aggregation/src/lib.rs b/crates/aggregation/src/lib.rs index 259adb897c..15377d1672 100644 --- a/crates/aggregation/src/lib.rs +++ b/crates/aggregation/src/lib.rs @@ -54,19 +54,11 @@ mod statement; #[cfg(test)] mod tests; +pub use claims::{AggregateClaims, LeanSphincsClaim, LeanXmssClaim, OutputClaims}; pub use error::AggregateError; pub use guest::unified_guest; -use lean_sphincs::LeanSphincsPublicKey; -use lean_xmss::LeanXmssPublicKey; pub use node::aggregate; -pub use proof::{AggregateClaims, AggregateProof, OutputClaims}; - -/// One leanXMSS claim: a pubkey, and the message it signed at whatever leaf index its -/// signature carries. -pub type LeanXmssClaim = (LeanXmssPublicKey, lean_xmss::Message); - -/// One leanSPHINCS claim: a pubkey, and the message it signed. -pub type LeanSphincsClaim = (LeanSphincsPublicKey, lean_sphincs::Message); +pub use proof::AggregateProof; /// The maximum number of child proofs one [`aggregate`] call can take. pub const MAX_RECURSIONS: usize = 16; diff --git a/crates/aggregation/src/node.rs b/crates/aggregation/src/node.rs index 9b2298e2b6..8df3e452a2 100644 --- a/crates/aggregation/src/node.rs +++ b/crates/aggregation/src/node.rs @@ -9,9 +9,9 @@ use super::guest::{fs_seed, unified_guest}; #[cfg(test)] use super::hints::FORGE; use super::hints::{Hints, lean_sphincs_hints, lean_xmss_hints}; -use super::proof::{AggregateClaims, AggregateProof, OutputClaims, is_sorted}; +use super::proof::{AggregateProof, is_sorted}; use super::statement::{lean_sphincs_claim_cells, lean_xmss_claim_cells, list_split, statement_digest}; -use super::{MAX_LOG_INV_RATE, MAX_RECURSIONS, MIN_LOG_INV_RATE}; +use super::{AggregateClaims, MAX_LOG_INV_RATE, MAX_RECURSIONS, MIN_LOG_INV_RATE, OutputClaims}; use fiat_shamir_prover::merkle::hash_to_scalars; use isa::{Prover, Stats}; use lean_da::CELLS_PER_ROW; diff --git a/crates/aggregation/src/proof.rs b/crates/aggregation/src/proof.rs index dcffb01e05..ceea60c7f4 100644 --- a/crates/aggregation/src/proof.rs +++ b/crates/aggregation/src/proof.rs @@ -3,33 +3,10 @@ use super::deferred::{DeferredBytecodeClaim, bytecode_vars}; use super::guest::unified_guest; use super::statement::statement_digest; -use super::{LeanSphincsClaim, LeanXmssClaim}; +use claims::AggregateClaims; use lean_vm_core::cpu::public_input_bytes; use primitives::field::F128; -/// What an [`AggregateProof`] proves: every `(pubkey, message)` claim of two lists, one of leanXMSS and one of -/// leanSPHINCS, had a valid signature, and, optionally, a commitment is the root of a well-formed matrix of blobs (see -/// [`lean_da::BlobMatrix`]). -/// -/// Each list is strictly sorted (so without duplicates), and may be empty. -#[derive(Clone, Debug, Default, PartialEq, Eq)] -pub struct AggregateClaims { - pub lean_xmss: Vec, - pub lean_sphincs: Vec, - /// `None` for no blobs. - pub da_commitment: Option<[u8; 32]>, -} - -/// Which covered claims [`aggregate`](super::aggregate) proves. -#[derive(Clone, Copy, Debug)] -pub enum OutputClaims<'a> { - /// Every covered signature claim. The DA root is `blobs`' if any, otherwise the first - /// child's that has one, otherwise none. - All, - /// Exactly these, each covered by the inputs. - Only(&'a AggregateClaims), -} - /// A proof of some [`AggregateClaims`], which travel separately: [`Self::verify`] checks it against /// the claims the receiver expects. #[derive(Clone, Debug)] @@ -65,14 +42,6 @@ pub(super) fn is_sorted(claims: &AggregateClaims) -> bool { claims.lean_xmss.windows(2).all(|w| w[0] < w[1]) && claims.lean_sphincs.windows(2).all(|w| w[0] < w[1]) } -impl AggregateClaims { - /// The number of `(pubkey, message)` claims, not of distinct signers: a pubkey may sign - /// several messages. - pub fn num_signature_claims(&self) -> usize { - self.lean_xmss.len() + self.lean_sphincs.len() - } -} - impl AggregateProof { pub fn to_bytes(&self) -> Vec { encode(&self.bytecode_point, &self.proof.to_bytes()) diff --git a/crates/aggregation/src/statement.rs b/crates/aggregation/src/statement.rs index 87daf13854..68c511f8ff 100644 --- a/crates/aggregation/src/statement.rs +++ b/crates/aggregation/src/statement.rs @@ -3,8 +3,7 @@ use super::cells::{cell_bytes, count, pack_16_bytes}; use super::deferred::DeferredBytecodeClaim; use super::guest::fs_seed; -use super::proof::AggregateClaims; -use super::{LeanSphincsClaim, LeanXmssClaim}; +use super::{AggregateClaims, LeanSphincsClaim, LeanXmssClaim}; use fiat_shamir_prover::merkle::hash_to_scalars; use primitives::field::F128; diff --git a/crates/aggregation/src/tests.rs b/crates/aggregation/src/tests.rs index 48b03e5d31..03e3bf6552 100644 --- a/crates/aggregation/src/tests.rs +++ b/crates/aggregation/src/tests.rs @@ -4,7 +4,7 @@ use super::error::AggregateError; use super::guest::{GUEST_SOURCE, compile_guest, unified_guest}; use super::hints::{FORGE, Forgery, Hints}; use super::placeholders::placeholder_map; -use super::proof::{AggregateClaims, AggregateProof, decode, encode}; +use super::proof::{AggregateProof, decode, encode}; use super::statement::{CLAIM_GROUP, claim_list_digest, list_split}; use super::*; use fiat_shamir_prover::merkle::hash_to_scalars; diff --git a/crates/allocator/Cargo.toml b/crates/allocator/Cargo.toml new file mode 100644 index 0000000000..d626e48872 --- /dev/null +++ b/crates/allocator/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "allocator" +version.workspace = true +edition.workspace = true +publish = false + +[lints] +workspace = true + +[dependencies] +tikv-jemallocator.workspace = true diff --git a/crates/bench/src/allocator.rs b/crates/allocator/src/lib.rs similarity index 94% rename from crates/bench/src/allocator.rs rename to crates/allocator/src/lib.rs index cf1d158f59..116f00d702 100644 --- a/crates/bench/src/allocator.rs +++ b/crates/allocator/src/lib.rs @@ -1,4 +1,4 @@ -//! The global allocator of the CLI and the proving benchmarks. +//! The global allocator of the benchmark CLI, the proving benchmarks and the sidecar. //! //! A proof allocates gigabytes of short-lived buffers and frees them before it returns. //! diff --git a/crates/bench/Cargo.toml b/crates/bench_harness/Cargo.toml similarity index 82% rename from crates/bench/Cargo.toml rename to crates/bench_harness/Cargo.toml index 8397ed252c..dbf4d7fff0 100644 --- a/crates/bench/Cargo.toml +++ b/crates/bench_harness/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "bench" +name = "bench_harness" version.workspace = true edition.workspace = true publish = false @@ -10,6 +10,5 @@ workspace = true [dependencies] isa.workspace = true libc.workspace = true -tikv-jemallocator.workspace = true tracing-forest.workspace = true tracing-subscriber.workspace = true diff --git a/crates/bench/src/lib.rs b/crates/bench_harness/src/lib.rs similarity index 99% rename from crates/bench/src/lib.rs rename to crates/bench_harness/src/lib.rs index aa5d11de01..8eaef797a1 100644 --- a/crates/bench/src/lib.rs +++ b/crates/bench_harness/src/lib.rs @@ -5,9 +5,7 @@ use std::io::{IsTerminal, Write}; use std::time::{Duration, Instant}; -mod allocator; mod trace; -pub use allocator::Jemalloc; pub use trace::{TraceSuppressed, init_tracing, init_tracing_from_env, suppress_tracing}; /// One line of live progress on stderr. diff --git a/crates/bench/src/trace.rs b/crates/bench_harness/src/trace.rs similarity index 100% rename from crates/bench/src/trace.rs rename to crates/bench_harness/src/trace.rs diff --git a/crates/claims/Cargo.toml b/crates/claims/Cargo.toml new file mode 100644 index 0000000000..0f063dc544 --- /dev/null +++ b/crates/claims/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "claims" +version.workspace = true +edition.workspace = true +publish = false + +[lints] +workspace = true + +[dependencies] +lean_xmss.workspace = true +lean_sphincs.workspace = true diff --git a/crates/claims/src/lib.rs b/crates/claims/src/lib.rs new file mode 100644 index 0000000000..c1290c3e17 --- /dev/null +++ b/crates/claims/src/lib.rs @@ -0,0 +1,98 @@ +//! The claims an aggregate proof attests, and their byte form: shared by the library, which proves and verifies +//! them, and the sidecar's client, which builds neither. + +use lean_sphincs::LeanSphincsPublicKey; +use lean_xmss::LeanXmssPublicKey; + +/// One leanXMSS claim: a pubkey, and the message it signed at whatever leaf index its +/// signature carries. +pub type LeanXmssClaim = (LeanXmssPublicKey, lean_xmss::Message); + +/// One leanSPHINCS claim: a pubkey, and the message it signed. +pub type LeanSphincsClaim = (LeanSphincsPublicKey, lean_sphincs::Message); + +/// What an aggregate proof proves: every `(pubkey, message)` claim of two lists, one of leanXMSS and one of +/// leanSPHINCS, had a valid signature, and, optionally, a commitment is the root of a well-formed matrix of blobs +/// (`lean_da::BlobMatrix`). +/// +/// Each list is strictly sorted (so without duplicates), and may be empty. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct AggregateClaims { + pub lean_xmss: Vec, + pub lean_sphincs: Vec, + /// `None` for no blobs. + pub da_commitment: Option<[u8; 32]>, +} + +/// Which covered claims an aggregation proves. +#[derive(Clone, Copy, Debug)] +pub enum OutputClaims<'a> { + /// Every covered signature claim. The DA root is `blobs`' if any, otherwise the first + /// child's that has one, otherwise none. + All, + /// Exactly these, each covered by the inputs. + Only(&'a AggregateClaims), +} + +// The claims' byte form is specified in `bins/leanvm-sidecar/README.md` (Claims). Decoding is canonical: it rejects +// any other flag and trailing bytes. Order is kept, sorted or not. + +const _: () = assert!(lean_xmss::PUB_KEY_SIZE == 32 && lean_sphincs::PUB_KEY_SIZE == 32); +const _: () = assert!(lean_xmss::MESSAGE_LEN == 32 && lean_sphincs::MESSAGE_LEN == 32); + +fn encode_claims(out: &mut Vec, claims: &[(K, [u8; 32])], pk_bytes: impl Fn(&K) -> [u8; 32]) { + out.extend((claims.len() as u64).to_le_bytes()); + for (pk, message) in claims { + out.extend(pk_bytes(pk)); + out.extend(message); + } +} + +/// Reads a count and that many `(pubkey, message)` pairs off the front of `bytes`. A forged count allocates nothing. +fn decode_claims(bytes: &mut &[u8], pk: impl Fn(&[u8; 32]) -> K) -> Option> { + let (count, rest) = bytes.split_first_chunk::<8>()?; + let count = usize::try_from(u64::from_le_bytes(*count)).ok()?; + let (pairs, rest) = rest.split_at_checked(count.checked_mul(64)?)?; + *bytes = rest; + let pairs = pairs.as_chunks::<32>().0.as_chunks::<2>().0; + Some(pairs.iter().map(|[key, message]| (pk(key), *message)).collect()) +} + +impl AggregateClaims { + /// The number of `(pubkey, message)` claims, not of distinct signers: a pubkey may sign + /// several messages. + pub fn num_signature_claims(&self) -> usize { + self.lean_xmss.len() + self.lean_sphincs.len() + } + + /// Encode claims, which [`Self::from_bytes`] reads back. + pub fn to_bytes(&self) -> Vec { + let mut out = Vec::new(); + encode_claims(&mut out, &self.lean_xmss, LeanXmssPublicKey::to_bytes); + encode_claims(&mut out, &self.lean_sphincs, LeanSphincsPublicKey::to_bytes); + match self.da_commitment { + None => out.push(0), + Some(root) => { + out.push(1); + out.extend(root); + } + } + out + } + + /// Decode claims, `None` if the bytes are not exactly one encoding. + pub fn from_bytes(mut bytes: &[u8]) -> Option { + let lean_xmss = decode_claims(&mut bytes, LeanXmssPublicKey::from_bytes)?; + let lean_sphincs = decode_claims(&mut bytes, LeanSphincsPublicKey::from_bytes)?; + let da_commitment = match bytes { + [0] => None, + [1, root @ ..] => Some(<[u8; 32]>::try_from(root).ok()?), + _ => return None, + }; + Some(Self { + lean_xmss, + lean_sphincs, + da_commitment, + }) + } +} diff --git a/crates/experimental_prover/keccak_gkr/Cargo.toml b/crates/experimental_prover/keccak_gkr/Cargo.toml index 95e0522c38..435f22f7c5 100644 --- a/crates/experimental_prover/keccak_gkr/Cargo.toml +++ b/crates/experimental_prover/keccak_gkr/Cargo.toml @@ -13,15 +13,16 @@ primitives = { package = "experimental_primitives", path = "../primitives" } fiat_shamir = { package = "fiat_shamir_prover", path = "../../fiat_shamir_prover" } pcs = { package = "experimental_pcs", path = "../pcs" } tracing.workspace = true -bench = { workspace = true, optional = true } +bench_harness = { workspace = true, optional = true } isa = { workspace = true, optional = true } verifier.workspace = true [features] # The standalone batch benchmark (`batch_bench`), for the `hash_batch` target. -bench = ["dep:bench", "dep:isa", "primitives/test-util"] +bench = ["dep:bench_harness", "dep:isa", "primitives/test-util"] [dev-dependencies] +allocator.workspace = true keccak_gkr = { package = "experimental_keccak_gkr", path = ".", features = ["bench"] } [[bench]] diff --git a/crates/experimental_prover/keccak_gkr/benches/hash_batch.rs b/crates/experimental_prover/keccak_gkr/benches/hash_batch.rs index 456280f05b..74f9ccb9cd 100644 --- a/crates/experimental_prover/keccak_gkr/benches/hash_batch.rs +++ b/crates/experimental_prover/keccak_gkr/benches/hash_batch.rs @@ -11,14 +11,14 @@ //! `BENCH_TRACING=1` prints the final pass's span tree (`RUST_LOG` adjusts it). extern crate experimental_keccak_gkr as keccak_gkr; -use bench::{Plan, env_usize}; +use bench_harness::{Plan, env_usize}; use pcs::whir::LOG_INV_RATE_0; #[global_allocator] -static ALLOCATOR: bench::Jemalloc = bench::Jemalloc; +static ALLOCATOR: allocator::Jemalloc = allocator::Jemalloc; fn main() { - bench::init_tracing_from_env(); + bench_harness::init_tracing_from_env(); let n = 1usize << env_usize("KECCAK_N_LOG", 13); let report = keccak_gkr::batch_bench::run(n, env_usize("KECCAK_DEPTH", 12), LOG_INV_RATE_0, Plan::from_env()); if std::env::args().any(|arg| arg == "--json") { diff --git a/crates/experimental_prover/keccak_gkr/src/batch_bench.rs b/crates/experimental_prover/keccak_gkr/src/batch_bench.rs index 6ba1754321..aef8356967 100644 --- a/crates/experimental_prover/keccak_gkr/src/batch_bench.rs +++ b/crates/experimental_prover/keccak_gkr/src/batch_bench.rs @@ -3,7 +3,7 @@ use std::time::Instant; -use bench::{Metric, Plan, Timing, bencher_json}; +use bench_harness::{Metric, Plan, Timing, bencher_json}; use isa::pretty_integer; use pcs::stack_open::StackClaim; use pcs::whir::{ProverConfig, ProverData, commit, config_for_rate}; @@ -82,7 +82,7 @@ pub fn run(n: usize, depth: usize, log_inv_rate: usize, plan: Plan) -> Report { let mut stages: [Timing; 6] = std::array::from_fn(|_| Timing::default()); let mut warm = true; let (proof, _) = plan.warm_then_measure(|final_pass| { - let _quiet = (!final_pass).then(bench::suppress_tracing); + let _quiet = (!final_pass).then(bench_harness::suppress_tracing); let (proof, secs) = prove_pass(); if !std::mem::take(&mut warm) { for (timing, s) in stages.iter_mut().zip(secs) { diff --git a/crates/experimental_prover/lean_vm_core/src/prover.rs b/crates/experimental_prover/lean_vm_core/src/prover.rs index 4ee0518578..464eb1bce1 100644 --- a/crates/experimental_prover/lean_vm_core/src/prover.rs +++ b/crates/experimental_prover/lean_vm_core/src/prover.rs @@ -12,10 +12,16 @@ pub struct ExperimentalProver; const GROUP: usize = 8; impl isa::Prover for ExperimentalProver { + const NAME: &'static str = "experimental"; + fn init() { parallel::init(); } + fn set_num_threads(n: usize) { + parallel::set_num_threads(n) + } + fn prove( program: &isa::Program, witness: &[(String, Vec>)], diff --git a/crates/experimental_prover/lean_vm_core/tests/verifiers/main.rs b/crates/experimental_prover/lean_vm_core/tests/verifiers/main.rs index 25e6aa7375..a3053ea546 100644 --- a/crates/experimental_prover/lean_vm_core/tests/verifiers/main.rs +++ b/crates/experimental_prover/lean_vm_core/tests/verifiers/main.rs @@ -1,5 +1,5 @@ //! Both verifiers' WHIR query table and grinds, pinned against the security search only this tree keeps. The verifier -//! itself is tested on stable's proofs, which `bins/leanvm/tests/identical_proofs.rs` pins to this tree's. +//! itself is tested on stable's proofs, which `bench/tests/identical_proofs.rs` pins to this tree's. extern crate experimental_lean_vm_core as lean_vm_core; mod whir_query_table; diff --git a/crates/experimental_prover/parallel/src/lib.rs b/crates/experimental_prover/parallel/src/lib.rs index c71df8a480..9abfa14d31 100644 --- a/crates/experimental_prover/parallel/src/lib.rs +++ b/crates/experimental_prover/parallel/src/lib.rs @@ -14,9 +14,9 @@ use std::thread::Thread; mod topology; -pub use topology::num_threads; pub(crate) use topology::topology; use topology::{Qos, set_qos}; +pub use topology::{num_threads, set_num_threads}; /// Idle spins before a worker parks: long enough to stay hot across back-to-back /// dispatches, short enough to yield the core during a sequential stretch. diff --git a/crates/experimental_prover/parallel/src/topology.rs b/crates/experimental_prover/parallel/src/topology.rs index 516ce6bea8..117b9a6fa6 100644 --- a/crates/experimental_prover/parallel/src/topology.rs +++ b/crates/experimental_prover/parallel/src/topology.rs @@ -20,30 +20,30 @@ impl Topology { } } +static TOPOLOGY: OnceLock = OnceLock::new(); + /// The pool's shape, resolved once. -/// -/// `LEANVM_NUM_THREADS` sets the total worker count, as in stable: performance -/// workers first, efficiency workers only beyond the performance cores. `1` is -/// strictly sequential. #[must_use] pub(crate) fn topology() -> Topology { - static TOPOLOGY: OnceLock = OnceLock::new(); - *TOPOLOGY.get_or_init(|| { - let requested = std::env::var("LEANVM_NUM_THREADS") - .ok() - .and_then(|v| v.parse::().ok()) - .filter(|&n| n > 0); - match requested { - Some(n) => { - let efficiency = n.saturating_sub(perf_cores()).min(efficiency_cores()); - Topology { - perf: n - efficiency, - efficiency, - } + *TOPOLOGY.get_or_init(default_topology) +} + +/// Fixes the worker count before the pool starts: performance workers first, efficiency workers only beyond the +/// performance cores; `1` is sequential and `0` keeps the default. Panics if it is already another. +pub fn set_num_threads(n: usize) { + if n == 0 { + return; + } + let set = TOPOLOGY + .get_or_init(|| { + let efficiency = n.saturating_sub(perf_cores()).min(efficiency_cores()); + Topology { + perf: n - efficiency, + efficiency, } - None => default_topology(), - } - }) + }) + .total(); + assert_eq!(set, n, "the thread pool already runs with {set} threads"); } /// Worker count including the dispatcher. @@ -185,6 +185,13 @@ pub(crate) fn set_qos(qos: Qos) { mod tests { use super::*; + #[test] + #[should_panic(expected = "already runs")] + fn a_fixed_shape_refuses_another() { + set_num_threads(num_threads()); + set_num_threads(num_threads() + 1); + } + #[test] fn cpu_lists_parse_ranges_and_singles() { assert_eq!(cpu_list("0-3,8,10-11"), Some(vec![0, 1, 2, 3, 8, 10, 11])); diff --git a/crates/experimental_prover/pcs/Cargo.toml b/crates/experimental_prover/pcs/Cargo.toml index b370248474..f92be5e734 100644 --- a/crates/experimental_prover/pcs/Cargo.toml +++ b/crates/experimental_prover/pcs/Cargo.toml @@ -16,7 +16,8 @@ verifier.workspace = true [dev-dependencies] primitives = { package = "experimental_primitives", path = "../primitives", features = ["test-util"] } -bench.workspace = true +allocator.workspace = true +bench_harness.workspace = true isa.workspace = true [[bench]] diff --git a/crates/experimental_prover/pcs/benches/throughput.rs b/crates/experimental_prover/pcs/benches/throughput.rs index a95b3da63a..5bba1a35b4 100644 --- a/crates/experimental_prover/pcs/benches/throughput.rs +++ b/crates/experimental_prover/pcs/benches/throughput.rs @@ -3,7 +3,7 @@ //! Commits a random witness of `2^PCS_LOG_N` GF(2^64) elements at //! inverse-rate `1/2^PCS_LOG_INV_RATE` and opens it through the stacked opener with one point //! claim, after the OOD sample, times each phase, and reports GiB/s over the committed data. The -//! passes follow [`bench::Plan::from_env`] (`BENCH_REPEAT`, `BENCH_COOLDOWN`). +//! passes follow [`bench_harness::Plan::from_env`] (`BENCH_REPEAT`, `BENCH_COOLDOWN`). //! //! ```text //! PCS_LOG_N number of variables = log2(witness length) [default 22] @@ -22,7 +22,7 @@ extern crate experimental_pcs as pcs; use std::hint::black_box; use std::time::Instant; -use bench::{Plan, Timing, env_usize}; +use bench_harness::{Plan, Timing, env_usize}; use isa::pretty_integer; use pcs::ood::ood_sample; use pcs::stack_open::{RingSwitchClaim, RingSwitchOpen, StackClaim, open_batch_mixed_whir_stacked}; @@ -34,10 +34,10 @@ use primitives::{ }; #[global_allocator] -static ALLOCATOR: bench::Jemalloc = bench::Jemalloc; +static ALLOCATOR: allocator::Jemalloc = allocator::Jemalloc; fn main() { - bench::init_tracing_from_env(); + bench_harness::init_tracing_from_env(); let log_n = env_usize("PCS_LOG_N", 22); let log_inv_rate = env_usize("PCS_LOG_INV_RATE", LOG_INV_RATE_0); @@ -79,7 +79,7 @@ fn main() { let (mut commit_t, mut open_t) = (Timing::default(), Timing::default()); plan.warm_then_measure(|last| { - let _quiet = (!last).then(bench::suppress_tracing); + let _quiet = (!last).then(bench_harness::suppress_tracing); let t = Instant::now(); let (cm, pd) = tracing::info_span!("Commit").in_scope(|| commit(&witness, log_n, pc.initial_k, log_inv_rate)); diff --git a/crates/isa/src/prover.rs b/crates/isa/src/prover.rs index c8f5cba41c..050c2c0884 100644 --- a/crates/isa/src/prover.rs +++ b/crates/isa/src/prover.rs @@ -6,9 +6,15 @@ use verifier::{E, layout::N_TABLES}; /// A prover of ISA programs, and the thread pool and field kernels it brings for the work around a proof. pub trait Prover: 'static { + /// The prover's name: `stable` or `experimental`. + const NAME: &'static str; + /// Warm up the prover's thread pool, so that no timed work pays for starting it. Idempotent. fn init(); + /// Fixes the thread pool's size before it starts; `0` keeps the default. Panics if it is already another. + fn set_num_threads(n: usize); + /// Prove an execution of `program` on `public_input` at the code rate `2^-log_inv_rate`, or say why there is none. /// `witness` names the streams of its `hint_witness` calls, each with one entry per call, in the order they are /// popped, and no two with one name. diff --git a/crates/keccak256/Cargo.toml b/crates/keccak256/Cargo.toml index 1a4d58bf61..e62b79ffee 100644 --- a/crates/keccak256/Cargo.toml +++ b/crates/keccak256/Cargo.toml @@ -9,7 +9,7 @@ workspace = true [dev-dependencies] sha3.workspace = true -bench.workspace = true +bench_harness.workspace = true parallel = { package = "stable_parallel", path = "../stable_prover/parallel" } verifier.workspace = true diff --git a/crates/keccak256/benches/hash_throughput.rs b/crates/keccak256/benches/hash_throughput.rs index 77b80e947d..befba1e999 100644 --- a/crates/keccak256/benches/hash_throughput.rs +++ b/crates/keccak256/benches/hash_throughput.rs @@ -1,6 +1,6 @@ //! Batched Keccak-256 throughput across the stable prover's thread pool, to 32-byte digests: 64-byte inputs (a //! Merkle node's two children) and 512-byte ones (a PCS leaf of 64 words, four permutations). The passes follow -//! [`bench::Plan::from_env`] (`BENCH_REPEAT`, `BENCH_COOLDOWN`). +//! [`bench_harness::Plan::from_env`] (`BENCH_REPEAT`, `BENCH_COOLDOWN`). //! //! ```text //! BENCH_REPEAT=5 cargo bench -p keccak256 --bench hash_throughput @@ -14,7 +14,7 @@ fn run() { let data: Vec = (0..TASKS * BYTES).map(|i| (i & 0xff) as u8).collect(); let mut out = vec![0u8; TASKS * k * 32]; - let (_, time) = bench::Plan::from_env().warm_then_measure(|_| { + let (_, time) = bench_harness::Plan::from_env().warm_then_measure(|_| { parallel::chunks_mut(&mut out, k * 32, |i, sub| { let d = &data[i * BYTES..][..BYTES]; for _ in 0..ITERS { diff --git a/crates/lean_vm/Cargo.toml b/crates/lean_vm/Cargo.toml index 01451d2cd9..f46cbe32a8 100644 --- a/crates/lean_vm/Cargo.toml +++ b/crates/lean_vm/Cargo.toml @@ -7,14 +7,19 @@ edition.workspace = true workspace = true [dependencies] +claims.workspace = true +verifier.workspace = true +lean_xmss.workspace = true +lean_sphincs.workspace = true isa.workspace = true aggregation.workspace = true lean_da.workspace = true -lean_xmss.workspace = true -lean_sphincs.workspace = true stable_lean_vm_core = { path = "../stable_prover/lean_vm_core" } experimental_lean_vm_core = { path = "../experimental_prover/lean_vm_core", optional = true } +sidecar = { workspace = true, optional = true } [features] -# Prove with the experimental tree rather than the stable one. +# Prove with the experimental prover rather than the stable one. experimental_prover = ["dep:experimental_lean_vm_core"] +# Prove and verify through the sidecar rather than in this process. +sidecar = ["dep:sidecar"] diff --git a/crates/lean_vm/src/in_process.rs b/crates/lean_vm/src/in_process.rs new file mode 100644 index 0000000000..8cf14a2530 --- /dev/null +++ b/crates/lean_vm/src/in_process.rs @@ -0,0 +1,60 @@ +//! What the library offers without the `sidecar` feature: proving and verifying here, and LeanDA's encoding. + +use crate::{AggregateClaims, OutputClaims}; +use ::lean_sphincs::{LeanSphincsPublicKey, LeanSphincsSignature}; +use ::lean_xmss::{LeanXmssPublicKey, LeanXmssSignature}; +pub use aggregation::{AggregateError, AggregateProof}; +use isa::Prover; + +#[cfg(feature = "experimental_prover")] +type P = experimental_lean_vm_core::ExperimentalProver; +#[cfg(not(feature = "experimental_prover"))] +type P = stable_lean_vm_core::StableProver; + +// TODO: make leanDA a self-contained dependency, so that it can move to lib.rs +pub mod lean_da { + pub use ::lean_da::{ + BLOB_SYMBOLS, Blob, BlobCountError, BlobMatrix, CELL_SYMBOLS, CELLS_PER_ROW, Codeword, DA_MAX_ROWS, + MERKLE_PATH_LEN, Symbol, verify_cell, + }; +} + +/// Call this function before generating / verifying proofs: it starts the prover's thread pool, with `threads` threads +/// (0: one per core), and builds the aggregation program. +/// +/// # Panics +/// If the pool already runs with another thread count. +pub fn setup(threads: usize) { + P::set_num_threads(threads); + P::init(); + aggregation::unified_guest(); +} + +/// Aggregate leanXMSS and leanSPHINCS signatures, blobs and the claims of child proofs. +/// +/// - `children`: at most [`MAX_RECURSIONS`](crate::MAX_RECURSIONS), each with its claims. +/// - `raw_lean_xmss`, `raw_lean_sphincs`: `(public_key, message, signature)`, in any order. +/// - `blobs`: the encoded blobs, if any ([`lean_da::BlobMatrix::build`]). +/// - `output`: which covered claims the proof attests. +/// - `log_inv_rate`: the code rate's log, in [`MIN_LOG_INV_RATE`](crate::MIN_LOG_INV_RATE)..= +/// [`MAX_LOG_INV_RATE`](crate::MAX_LOG_INV_RATE); higher gives a smaller proof but slower proving. +/// +/// # Errors +/// +/// [`AggregateError`] says why there is no proof. +/// +/// Calls run one at a time: a second waits for the first. +/// +/// # WARNING +/// - Signatures and children must be valid (children checked with [`AggregateProof::verify`]): otherwise proving fails. +pub fn aggregate( + children: &[(AggregateClaims, AggregateProof)], + raw_lean_xmss: Vec<(LeanXmssPublicKey, ::lean_xmss::Message, LeanXmssSignature)>, + raw_lean_sphincs: Vec<(LeanSphincsPublicKey, ::lean_sphincs::Message, LeanSphincsSignature)>, + blobs: Option<&lean_da::BlobMatrix>, + output: OutputClaims, + log_inv_rate: usize, +) -> Result<(AggregateClaims, AggregateProof), AggregateError> { + aggregation::aggregate::

(children, raw_lean_xmss, raw_lean_sphincs, blobs, output, log_inv_rate) + .map(|(claims, proof, _)| (claims, proof)) +} diff --git a/crates/lean_vm/src/lib.rs b/crates/lean_vm/src/lib.rs index 13fbe4a545..51b3fa350e 100644 --- a/crates/lean_vm/src/lib.rs +++ b/crates/lean_vm/src/lib.rs @@ -3,30 +3,29 @@ //! - leanSPHINCS (stateless, for the execution layer) //! - leanDA (PQ blobs) //! -//! Proving runs the stable prover, or with the `experimental_prover` feature the experimental one. -//! Both give the same proofs, byte for byte, and verifying needs neither. +//! Where proofs are made and checked: +//! - by default, in this process, on the stable prover; +//! - with the `experimental_prover` feature, in this process, on the experimental prover (the same proofs); +//! - with the `sidecar` feature, in a second process, the sidecar, which you launch with +//! `cargo run --release -p leanvm-sidecar -- --socket ` (`bins/leanvm-sidecar`): this process sends it proving +//! and verification requests through `sidecar::Client`. //! //! Example: [`crates/lean_vm/tests/api.rs`](https://github.com/leanEthereum/leanVM/blob/main/crates/lean_vm/tests/api.rs). -use ::lean_sphincs::{LeanSphincsPublicKey, LeanSphincsSignature}; -use ::lean_xmss::{LeanXmssPublicKey, LeanXmssSignature}; -pub use aggregation::{ - AggregateClaims, AggregateError, AggregateProof, LeanSphincsClaim, LeanXmssClaim, MAX_LOG_INV_RATE, MAX_RECURSIONS, - MIN_LOG_INV_RATE, OutputClaims, -}; -use isa::Prover; +#[cfg(all(feature = "sidecar", feature = "experimental_prover"))] +compile_error!( + "`experimental_prover` picks the prover of this process, and with `sidecar` this process does not prove: build the \ + sidecar with `--features experimental_prover` instead" +); -#[cfg(feature = "experimental_prover")] -type P = experimental_lean_vm_core::ExperimentalProver; -#[cfg(not(feature = "experimental_prover"))] -type P = stable_lean_vm_core::StableProver; +pub use aggregation::MAX_RECURSIONS; +pub use claims::{AggregateClaims, LeanSphincsClaim, LeanXmssClaim, OutputClaims}; +pub use verifier::{MAX_LOG_INV_RATE, MIN_LOG_INV_RATE}; -pub mod lean_da { - pub use ::lean_da::{ - BLOB_SYMBOLS, Blob, BlobCountError, BlobMatrix, CELL_SYMBOLS, CELLS_PER_ROW, Codeword, DA_MAX_ROWS, - MERKLE_PATH_LEN, Symbol, verify_cell, - }; -} +#[cfg(not(feature = "sidecar"))] +mod in_process; +#[cfg(not(feature = "sidecar"))] +pub use in_process::{AggregateError, AggregateProof, aggregate, lean_da, setup}; pub mod lean_xmss { pub use lean_xmss::{ @@ -43,39 +42,5 @@ pub mod lean_sphincs { }; } -/// Starts the prover's thread pool and builds the aggregation program, which [`AggregateProof::verify`] also reads. -/// Optional: both are built on first use otherwise. -pub fn setup() { - P::init(); - aggregation::unified_guest(); -} - -/// Aggregate leanXMSS and leanSPHINCS signatures, blobs and the claims of child proofs. -/// -/// - `children`: at most [`MAX_RECURSIONS`], each with its claims. -/// - `raw_lean_xmss`, `raw_lean_sphincs`: `(public_key, message, signature)`, in any order. -/// - `blobs`: the encoded blobs, if any ([`lean_da::BlobMatrix::build`]). -/// - `output`: which covered claims the proof attests. -/// - `log_inv_rate`: the code rate's log, in [`MIN_LOG_INV_RATE`]..=[`MAX_LOG_INV_RATE`]; higher gives a smaller proof -/// but slower proving. -/// -/// # Errors -/// -/// [`AggregateError`] says why there is no proof. -/// -/// Calls run one at a time: a second waits for the first. -/// -/// # WARNING -/// - Signatures and children must be valid (children checked with [`AggregateProof::verify`]): otherwise proving fails. -/// - Call [`setup`] once before the first. -pub fn aggregate( - children: &[(AggregateClaims, AggregateProof)], - raw_lean_xmss: Vec<(LeanXmssPublicKey, lean_xmss::Message, LeanXmssSignature)>, - raw_lean_sphincs: Vec<(LeanSphincsPublicKey, lean_sphincs::Message, LeanSphincsSignature)>, - blobs: Option<&lean_da::BlobMatrix>, - output: OutputClaims, - log_inv_rate: usize, -) -> Result<(AggregateClaims, AggregateProof), AggregateError> { - aggregation::aggregate::

(children, raw_lean_xmss, raw_lean_sphincs, blobs, output, log_inv_rate) - .map(|(claims, proof, _)| (claims, proof)) -} +#[cfg(feature = "sidecar")] +pub use ::sidecar; diff --git a/crates/lean_vm/tests/api.rs b/crates/lean_vm/tests/api.rs index b79c0bfcb8..5a34f44e6c 100644 --- a/crates/lean_vm/tests/api.rs +++ b/crates/lean_vm/tests/api.rs @@ -1,3 +1,5 @@ +#![cfg(not(feature = "sidecar"))] + use lean_vm::*; const MSG_0: lean_xmss::Message = [0; lean_xmss::MESSAGE_LEN]; @@ -6,7 +8,7 @@ const MSG_2: lean_xmss::Message = [2; lean_xmss::MESSAGE_LEN]; #[test] fn public_api_end_to_end() { - setup(); + setup(0); // 1. Eight leanXMSS signatures over three messages, three of the first, four of the second, one of the third, // by four keys. A key signs each message at its own leaf index, which the statement does not record. diff --git a/crates/sidecar/Cargo.toml b/crates/sidecar/Cargo.toml new file mode 100644 index 0000000000..2dbeacd81d --- /dev/null +++ b/crates/sidecar/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "sidecar" +version.workspace = true +edition.workspace = true +publish = false + +[lints] +workspace = true + +[dependencies] +claims.workspace = true +verifier.workspace = true +lean_xmss.workspace = true +lean_sphincs.workspace = true +isa = { workspace = true, optional = true } +aggregation = { workspace = true, optional = true } +lean_da = { workspace = true, optional = true } +libc = { workspace = true, optional = true } + +[features] +# The server, which proves: what `bins/leanvm-sidecar` runs. The client builds no prover. +server = ["dep:isa", "dep:aggregation", "dep:lean_da", "dep:libc"] diff --git a/crates/sidecar/src/client.rs b/crates/sidecar/src/client.rs new file mode 100644 index 0000000000..9a76233667 --- /dev/null +++ b/crates/sidecar/src/client.rs @@ -0,0 +1,130 @@ +//! The protocol from Rust. + +use crate::wire::{self, Incoming}; +use crate::{Blob, Rejection, SidecarError}; +use claims::{AggregateClaims, OutputClaims}; +use lean_sphincs::{LeanSphincsPublicKey, LeanSphincsSignature}; +use lean_xmss::{LeanXmssPublicKey, LeanXmssSignature}; +use std::io::Write; +use std::os::unix::net::UnixStream; +use std::path::Path; +use std::time::Duration; + +/// The longest response read beyond the request's length, which bounds the claims it carries: room for a hello, a +/// verdict or a proof, far more than any takes. +const ROOM: u64 = 16 << 20; + +/// A connection to a sidecar, for one request at a time. Proofs are their bytes, as the library's +/// `AggregateProof::to_bytes` writes them and `from_bytes` reads them. +/// +/// After an error other than [`SidecarError::Rejected`], the connection is in an unknown state: drop it and connect +/// again. +#[derive(Debug)] +pub struct Client { + stream: UnixStream, + prover: String, + max_request: u64, +} + +impl Client { + /// Connects to the sidecar listening on `socket`, and checks it speaks this protocol version and proves for this + /// verifier. + pub fn connect(socket: impl AsRef) -> Result { + let mut stream = UnixStream::connect(socket)?; + let body = match wire::read_frame(&mut stream, ROOM)? { + Incoming::Body(body) => body, + Incoming::End => return Err(SidecarError::Protocol("closed before its hello".into())), + // Not a hello, as `hello_version` then says. + Incoming::TooLarge(_) => Vec::new(), + }; + let version = + wire::hello_version(&body).ok_or_else(|| SidecarError::Incompatible("not a leanVM sidecar".into()))?; + if version != wire::VERSION { + return Err(SidecarError::Incompatible(format!( + "protocol version {version}, this client speaks {}", + wire::VERSION + ))); + } + let hello = + wire::decode_hello(&body).ok_or_else(|| SidecarError::Protocol("a hello that does not decode".into()))?; + if hello.tag != verifier::TAG { + return Err(SidecarError::Incompatible(format!( + "proves for verifier {}, this client verifies {}", + String::from_utf8_lossy(&hello.tag), + String::from_utf8_lossy(verifier::TAG) + ))); + } + Ok(Self { + stream, + prover: hello.prover, + max_request: hello.max_request, + }) + } + + /// How long a call may wait on the sidecar, reading or writing, before it fails with [`SidecarError::Io`]; `None`, + /// the default, waits as long as it takes (a sidecar that dies closes the connection, failing the call at once). + /// Dropping the client then closes the connection, so the sidecar drops the request unless it has started proving + /// it. + pub fn set_timeout(&self, timeout: Option) -> std::io::Result<()> { + self.stream.set_read_timeout(timeout)?; + self.stream.set_write_timeout(timeout) + } + + /// The prover the sidecar runs: `stable` or `experimental`. + pub fn prover(&self) -> &str { + &self.prover + } + + /// The library's `aggregate`, proven by the sidecar: its arguments, but for the blobs, which go as they are (none + /// for no blobs) and which the sidecar builds the matrix of. + /// + /// As with the library, children must be valid (checked with [`Self::verify`]), or proving fails. + pub fn aggregate( + &mut self, + children: &[(AggregateClaims, Vec)], + raw_lean_xmss: &[(LeanXmssPublicKey, lean_xmss::Message, LeanXmssSignature)], + raw_lean_sphincs: &[(LeanSphincsPublicKey, lean_sphincs::Message, LeanSphincsSignature)], + blobs: &[Blob], + output: OutputClaims, + log_inv_rate: usize, + ) -> Result<(AggregateClaims, Vec), SidecarError> { + let request = wire::encode_request(children, raw_lean_xmss, raw_lean_sphincs, blobs, output, log_inv_rate); + let body = self.call(&request)?; + answer(wire::decode_response(&body)) + } + + /// The library's `AggregateProof::verify`, run by the sidecar: whether `proof` proves `claims`. Bytes that do not + /// decode prove nothing. + pub fn verify(&mut self, claims: &AggregateClaims, proof: &[u8]) -> Result { + let body = self.call(&wire::encode_verify(claims, proof))?; + answer(wire::decode_verdict(&body)) + } + + /// Sends a request frame and reads the response's body. + fn call(&mut self, request: &[u8]) -> Result, SidecarError> { + let len = request.len() as u64 - 8; + if len > self.max_request { + return Err(SidecarError::Rejected( + Rejection::Malformed, + format!( + "a request of {len} bytes, the sidecar takes at most {}", + self.max_request + ), + )); + } + self.stream.write_all(request)?; + match wire::read_frame(&mut self.stream, request.len() as u64 + ROOM)? { + Incoming::Body(body) => Ok(body), + Incoming::End => Err(std::io::Error::from(std::io::ErrorKind::UnexpectedEof).into()), + Incoming::TooLarge(len) => Err(SidecarError::Protocol(format!( + "a response of {len} bytes, longer than any" + ))), + } + } +} + +/// A decoded response as the client's result. +fn answer(decoded: Option>) -> Result { + let decoded = decoded.ok_or_else(|| SidecarError::Protocol("a response that does not decode".into()))?; + decoded.map_err(|(rejection, message)| SidecarError::Rejected(rejection, message)) +} diff --git a/crates/sidecar/src/lib.rs b/crates/sidecar/src/lib.rs new file mode 100644 index 0000000000..6dba61d96b --- /dev/null +++ b/crates/sidecar/src/lib.rs @@ -0,0 +1,88 @@ +//! The leanVM sidecar: a process that proves and verifies for clients on the same machine. +//! +//! A client calls the library's `aggregate` and `verify` over a Unix stream socket, so it builds no prover: the +//! sidecar keeps the prover's allocator, threads and warm state out of the client's process. `bins/leanvm-sidecar` +//! runs `serve`, which the `server` feature brings; its `README.md` specifies the protocol, which any language can +//! speak, and [`Client`] speaks it from Rust. + +mod client; +#[cfg(feature = "server")] +mod server; +mod wire; + +pub use client::Client; +#[cfg(feature = "server")] +pub use server::serve; + +/// One blob, as `lean_da` takes it: its symbols, each 128 bits. +pub type Blob = [u128; 1 << verifier::da::LOG_BLOB_SYMBOLS]; + +/// Why the sidecar made no proof. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[repr(u8)] +pub enum Rejection { + /// `AggregateError::InvalidInput`, or more blobs than one proof takes. + InvalidInput = 1, + /// `AggregateError::InvalidSignature`. + InvalidSignature = 2, + /// `AggregateError::InvalidChild`, or a child proof's bytes that do not decode. + InvalidChild = 3, + /// `AggregateError::ProvingFailed`. + ProvingFailed = 4, + /// The request is not one: its bytes do not decode, or it is longer than the sidecar takes. + Malformed = 5, +} + +impl Rejection { + fn from_code(code: u8) -> Option { + [ + Self::InvalidInput, + Self::InvalidSignature, + Self::InvalidChild, + Self::ProvingFailed, + Self::Malformed, + ] + .into_iter() + .find(|r| *r as u8 == code) + } +} + +/// Why a [`Client`] call failed. +#[derive(Debug)] +pub enum SidecarError { + /// The socket failed, or the sidecar closed it: it stopped, or crashed. Connect again. + Io(std::io::Error), + /// The peer is not a leanVM sidecar, speaks another protocol version, or proves for another verifier. + Incompatible(String), + /// The sidecar's bytes are not the protocol. Connect again. + Protocol(String), + /// The request was refused, by the sidecar or, for one longer than the sidecar takes, by the client, which says + /// why. The connection stays usable. + Rejected(Rejection, String), +} + +impl std::fmt::Display for SidecarError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Io(e) => write!(f, "sidecar connection: {e}"), + Self::Incompatible(why) => write!(f, "incompatible sidecar: {why}"), + Self::Protocol(why) => write!(f, "sidecar protocol: {why}"), + Self::Rejected(rejection, message) => write!(f, "sidecar refused ({rejection:?}): {message}"), + } + } +} + +impl std::error::Error for SidecarError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self { + Self::Io(e) => Some(e), + _ => None, + } + } +} + +impl From for SidecarError { + fn from(e: std::io::Error) -> Self { + Self::Io(e) + } +} diff --git a/crates/sidecar/src/server.rs b/crates/sidecar/src/server.rs new file mode 100644 index 0000000000..94780a7a94 --- /dev/null +++ b/crates/sidecar/src/server.rs @@ -0,0 +1,550 @@ +//! The server: a thread per connection reads and decodes requests and verifies proofs, and the thread that calls +//! [`serve`], the one that started the prover's pool, proves them one at a time. Also the server's half of the +//! protocol: requests in, the hello and responses out. + +use crate::wire::{ + self, Cursor, Frame, Hello, Incoming, OK, OP_AGGREGATE, OP_VERIFY, Outcome, Proven, RawLeanSphincs, RawLeanXmss, +}; +use crate::{Blob, Rejection}; +use aggregation::{AggregateError, AggregateProof}; +use claims::{AggregateClaims, OutputClaims}; +use isa::Prover; +use lean_sphincs::{LeanSphincsPublicKey, LeanSphincsSignature}; +use lean_xmss::{LeanXmssPublicKey, LeanXmssSignature}; +use std::convert::Infallible; +use std::fs::{File, TryLockError}; +use std::io::{self, Write}; +use std::os::fd::AsRawFd; +use std::os::unix::fs::{FileTypeExt, OpenOptionsExt}; +use std::os::unix::net::{UnixListener, UnixStream}; +use std::path::Path; +use std::sync::mpsc::{self, Receiver, Sender}; +use std::sync::{Arc, Condvar, Mutex}; +use std::thread; +use std::time::Duration; + +/// The longest request body taken, in bytes: room for every blob and child a request may have and thousands of +/// signatures, while bounding the memory a connection holds. +const MAX_REQUEST: u64 = 64 << 20; + +/// The most connections served at once; more wait to be accepted. With [`MAX_REQUEST`], it bounds the memory +/// requests hold, queued ones included: each connection has at most one. +const MAX_CONNECTIONS: usize = 16; + +/// A request, its client's connection, and where its outcome goes. +type Job = (Request, UnixStream, Sender); + +/// Starts prover `P` on `threads` threads (0: one per core), builds the aggregation program and warms up, then listens +/// on `socket`, replacing a stale one a sidecar that stopped left, and serves until the process ends, proving on the +/// calling thread. The socket appears once the sidecar is ready to prove. Returns only on an error before serving, +/// which a second sidecar on the same socket gets before it starts anything. +/// +/// Call it from the main thread: a panic while proving, a bug, then ends the process. +pub fn serve(socket: &Path, threads: usize) -> io::Result { + std::os::unix::net::SocketAddr::from_pathname(socket)?; + let _lock = lock(socket)?; + remove_stale(socket)?; + P::set_num_threads(threads); + P::init(); + warm_up::

(); + let listener = UnixListener::bind(socket)?; + let hello = encode_hello(&Hello { + max_request: MAX_REQUEST, + tag: verifier::TAG.to_vec(), + prover: P::NAME.into(), + }); + let (jobs, queue) = mpsc::channel(); + thread::Builder::new() + .name("accept".into()) + .spawn(move || accept(&listener, &jobs, hello.into()))?; + eprintln!("serving {} with the {} prover", socket.display(), P::NAME); + prove_forever::

(&queue) +} + +/// A fresh process's first proofs are slower, touching memory the allocator then keeps: this pays for it before the +/// first request, with a discarded leaf and a full recursion over it, at rate 1/4, the one used in practice (a lower +/// rate, a larger `log_inv_rate`, still pays for its extra memory on its first proof). A blob is the one input that +/// needs no key. +fn warm_up() { + let blobs = lean_da::BlobMatrix::build(&[[0; lean_da::BLOB_SYMBOLS]]).expect("one blob"); + let rate = 2; + let leaf = aggregation::aggregate::

(&[], vec![], vec![], Some(&blobs), OutputClaims::All, rate) + .expect("the warm-up's leaf proves"); + let children = vec![(leaf.0, leaf.1); aggregation::MAX_RECURSIONS]; + aggregation::aggregate::

(&children, vec![], vec![], None, OutputClaims::All, rate) + .expect("the warm-up's recursion proves"); +} + +/// Locks `.lock`, which the process holds until it ends, so that one sidecar at a time owns the socket. A +/// symlink or a FIFO there is refused rather than followed or waited on. +fn lock(socket: &Path) -> io::Result { + let mut path = socket.as_os_str().to_owned(); + path.push(".lock"); + let file = File::options() + .create(true) + .truncate(false) + .write(true) + .custom_flags(libc::O_NOFOLLOW | libc::O_NONBLOCK) + .open(&path)?; + match file.try_lock() { + Ok(()) => Ok(file), + Err(TryLockError::WouldBlock) => Err(io::Error::new( + io::ErrorKind::AddrInUse, + format!("another sidecar serves {}", socket.display()), + )), + Err(TryLockError::Error(e)) => Err(e), + } +} + +/// Removes the socket a sidecar that stopped left, and nothing else: not another file, nor a socket something listens +/// on. Holding the lock, no other sidecar binds meanwhile. +fn remove_stale(socket: &Path) -> io::Result<()> { + let metadata = match std::fs::symlink_metadata(socket) { + Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(()), + metadata => metadata?, + }; + if !metadata.file_type().is_socket() { + return Err(io::Error::new( + io::ErrorKind::AlreadyExists, + format!("{} is not a socket", socket.display()), + )); + } + // Only a refused connection says nothing listens: one we may not open can be another program's. + match UnixStream::connect(socket) { + Err(e) if e.kind() == io::ErrorKind::ConnectionRefused => std::fs::remove_file(socket), + Ok(_) => Err(io::Error::new( + io::ErrorKind::AddrInUse, + format!("something else listens on {}", socket.display()), + )), + Err(e) => Err(e), + } +} + +fn prove_forever(queue: &Receiver) -> ! { + loop { + let (request, client, reply) = queue.recv().expect("the accept thread never ends"); + let summary = format!( + "{} children, {} leanXMSS, {} leanSPHINCS, {} blobs, rate {}", + request.children.len(), + request.raw_lean_xmss.len(), + request.raw_lean_sphincs.len(), + request.blobs.len(), + request.log_inv_rate, + ); + if hung_up(&client) { + eprintln!("{summary}: dropped, its client hung up"); + continue; + } + let outcome = aggregate::

(request); + match &outcome { + Ok(_) => eprintln!("{summary}: proven"), + Err((rejection, message)) => eprintln!("{summary}: refused ({rejection:?}): {message}"), + } + // The connection may have closed meanwhile. + let _ = reply.send(outcome); + } +} + +/// Whether the client closed its connection, so no longer waits for the request it sent: one that timed out, say. A +/// client that only shut down its writing half reads the same, as the protocol says. Nothing else reads the stream +/// meanwhile, its connection thread waiting on the outcome. +fn hung_up(client: &UnixStream) -> bool { + let mut byte = 0u8; + // SAFETY: a one-byte buffer of ours, on an open socket; peeking leaves the stream as it is. + let n = unsafe { + libc::recv( + client.as_raw_fd(), + (&raw mut byte).cast(), + 1, + libc::MSG_PEEK | libc::MSG_DONTWAIT, + ) + }; + match n { + 0 => true, + 1.. => false, + _ => !matches!( + io::Error::last_os_error().kind(), + io::ErrorKind::WouldBlock | io::ErrorKind::Interrupted + ), + } +} + +fn aggregate(request: Request) -> Outcome { + let children = request + .children + .into_iter() + .enumerate() + .map(|(i, (claims, proof))| match AggregateProof::from_bytes(&proof) { + Some(proof) => Ok((claims, proof)), + None => Err((Rejection::InvalidChild, format!("child {i}'s proof does not decode"))), + }) + .collect::, _>>()?; + let blobs = match request.blobs.len() { + 0 => None, + _ => Some(lean_da::BlobMatrix::build(&request.blobs).map_err(|e| (Rejection::InvalidInput, e.to_string()))?), + }; + drop(request.blobs); + let output = request.output.as_ref().map_or(OutputClaims::All, OutputClaims::Only); + aggregation::aggregate::

( + &children, + request.raw_lean_xmss, + request.raw_lean_sphincs, + blobs.as_ref(), + output, + request.log_inv_rate, + ) + .map(|(claims, proof, _)| (claims, proof.to_bytes())) + .map_err(|e| match e { + AggregateError::InvalidInput(reason) => (Rejection::InvalidInput, reason.into()), + AggregateError::InvalidSignature => (Rejection::InvalidSignature, e.to_string()), + AggregateError::InvalidChild => (Rejection::InvalidChild, e.to_string()), + AggregateError::ProvingFailed => (Rejection::ProvingFailed, e.to_string()), + }) +} + +fn accept(listener: &UnixListener, jobs: &Sender, hello: Arc<[u8]>) { + let open = Arc::new((Mutex::new(0), Condvar::new())); + loop { + let slot = Slot::take(&open); + match listener.accept() { + Ok((stream, _)) => { + let (jobs, hello) = (jobs.clone(), hello.clone()); + let spawned = thread::Builder::new().name("connection".into()).spawn(move || { + let _slot = slot; + connection(stream, &jobs, &hello); + }); + if let Err(e) = spawned { + eprintln!("a connection dropped: {e}"); + } + } + Err(e) => { + // Out of file descriptors, say: wait for some to close rather than spin. + eprintln!("accept: {e}"); + thread::sleep(Duration::from_millis(100)); + } + } + } +} + +/// One of the [`MAX_CONNECTIONS`], freed when dropped, even by a connection's thread that panics. +struct Slot(Arc<(Mutex, Condvar)>); + +impl Slot { + /// Waits for a free slot and takes it. + fn take(open: &Arc<(Mutex, Condvar)>) -> Self { + let (count, freed) = &**open; + let mut count = freed + .wait_while(count.lock().unwrap(), |n| *n == MAX_CONNECTIONS) + .unwrap(); + *count += 1; + Self(open.clone()) + } +} + +impl Drop for Slot { + fn drop(&mut self) { + let (count, freed) = &*self.0; + *count.lock().unwrap() -= 1; + freed.notify_one(); + } +} + +/// Serves one connection: its hello, then each request in turn, until the client closes it. +fn connection(mut stream: UnixStream, jobs: &Sender, hello: &[u8]) { + if stream.write_all(hello).is_err() { + return; + } + loop { + let body = match wire::read_frame(&mut stream, MAX_REQUEST) { + Ok(Incoming::Body(body)) => body, + // The body is left unread, so the stream is lost: say why, and close. + Ok(Incoming::TooLarge(len)) => { + let message = format!("a request of {len} bytes, the sidecar takes at most {MAX_REQUEST}"); + eprintln!("a request refused (Malformed): {message}"); + let _ = stream.write_all(&encode_response(&Err((Rejection::Malformed, message)))); + return; + } + Ok(Incoming::End) | Err(_) => return, + }; + let response = match decode_request(&body) { + Ok(Call::Aggregate(request)) => { + drop(body); + let Ok(client) = stream.try_clone() else { return }; + let (reply, outcome) = mpsc::channel(); + jobs.send((request, client, reply)) + .expect("the prover outlives every connection"); + // No outcome: the prover dropped the request, its client having hung up. + let Ok(outcome) = outcome.recv() else { return }; + encode_response(&outcome) + } + // Here rather than behind the proofs: it is fast, and on one thread. + Ok(Call::Verify(claims, proof)) => { + encode_verdict(AggregateProof::from_bytes(&proof).is_some_and(|proof| proof.verify(&claims))) + } + Err((rejection, message)) => { + eprintln!("a request refused ({rejection:?}): {message}"); + encode_response(&Err((rejection, message))) + } + }; + if stream.write_all(&response).is_err() { + return; + } + } +} + +const XMSS_RECORD: usize = lean_xmss::PUB_KEY_SIZE + lean_xmss::MESSAGE_LEN + lean_xmss::SIG_SIZE; +const SPHINCS_RECORD: usize = lean_sphincs::PUB_KEY_SIZE + lean_sphincs::MESSAGE_LEN + lean_sphincs::SIG_SIZE; +const BLOB_BYTES: usize = size_of::(); + +fn encode_hello(hello: &Hello) -> Vec { + let mut f = Frame::new(); + f.raw(&wire::MAGIC); + f.raw(&wire::VERSION.to_le_bytes()); + f.u64(hello.max_request); + f.bytes(&hello.tag); + f.bytes(hello.prover.as_bytes()); + f.finish() +} + +/// A request, as the server decodes it. +enum Call { + Aggregate(Request), + /// Claims, and their proof's bytes. + Verify(AggregateClaims, Vec), +} + +/// An `aggregate` call, its children's proofs still bytes. +struct Request { + children: Vec, + raw_lean_xmss: Vec, + raw_lean_sphincs: Vec, + blobs: Vec, + /// `None` for [`OutputClaims::All`]. + output: Option, + log_inv_rate: usize, +} + +fn decode_request(body: &[u8]) -> Result { + let malformed = |what: &str| (Rejection::Malformed, format!("malformed request: {what}")); + let mut c = Cursor(body); + let call = match c.u8() { + Some(OP_AGGREGATE) => Call::Aggregate(decode_aggregate(&mut c, malformed)?), + Some(OP_VERIFY) => { + let claims = c.claims().ok_or_else(|| malformed("claims"))?; + let proof = c.bytes().ok_or_else(|| malformed("proof"))?; + Call::Verify(claims, proof.to_vec()) + } + Some(op) => return Err((Rejection::Malformed, format!("unknown operation {op}"))), + None => return Err(malformed("empty")), + }; + if !c.is_empty() { + return Err(malformed("trailing bytes")); + } + Ok(call) +} + +/// A count of records of `size` bytes each, all of which must follow: a forged count allocates nothing. +fn count(c: &mut Cursor, size: usize) -> Option { + let n = usize::try_from(c.u64()?).ok()?; + (n.checked_mul(size)? <= c.0.len()).then_some(n) +} + +fn decode_aggregate( + c: &mut Cursor, + malformed: impl Fn(&str) -> (Rejection, String), +) -> Result { + let log_inv_rate = c.u8().ok_or_else(|| malformed("no rate"))?.into(); + + // Each child is at least its two lengths. + let n = count(c, 16).ok_or_else(|| malformed("children"))?; + // `aggregate`'s bound, checked before decoding: a decoded child takes more memory than its bytes. + if n > aggregation::MAX_RECURSIONS { + return Err((Rejection::InvalidInput, "more than MAX_RECURSIONS children".into())); + } + let mut children = Vec::with_capacity(n); + for i in 0..n { + let claims = c.claims().ok_or_else(|| malformed(&format!("child {i}'s claims")))?; + let proof = c.bytes().ok_or_else(|| malformed(&format!("child {i}'s proof")))?; + children.push((claims, proof.to_vec())); + } + + let n = count(c, XMSS_RECORD).ok_or_else(|| malformed("leanXMSS signatures"))?; + let raw_lean_xmss = (0..n) + .map(|_| { + ( + LeanXmssPublicKey::from_bytes(c.array().unwrap()), + *c.array().unwrap(), + LeanXmssSignature::from_bytes(c.array().unwrap()), + ) + }) + .collect(); + let n = count(c, SPHINCS_RECORD).ok_or_else(|| malformed("leanSPHINCS signatures"))?; + let raw_lean_sphincs = (0..n) + .map(|_| { + ( + LeanSphincsPublicKey::from_bytes(c.array().unwrap()), + *c.array().unwrap(), + LeanSphincsSignature::from_bytes(c.array().unwrap()), + ) + }) + .collect(); + + let n = count(c, BLOB_BYTES).ok_or_else(|| malformed("blobs"))?; + let blobs = (0..n) + .map(|_| { + let symbols = c.take(BLOB_BYTES).unwrap().as_chunks::<16>().0; + std::array::from_fn(|i| u128::from_le_bytes(symbols[i])) + }) + .collect(); + + let output = match c.u8() { + Some(0) => None, + Some(1) => Some(c.claims().ok_or_else(|| malformed("output claims"))?), + _ => return Err(malformed("output")), + }; + Ok(Request { + children, + raw_lean_xmss, + raw_lean_sphincs, + blobs, + output, + log_inv_rate, + }) +} + +fn encode_response(result: &Outcome) -> Vec { + let mut f = Frame::new(); + match result { + Ok((claims, proof)) => { + f.u8(OK); + f.bytes(&claims.to_bytes()); + f.bytes(proof); + } + Err((rejection, message)) => { + f.u8(*rejection as u8); + f.bytes(message.as_bytes()); + } + } + f.finish() +} + +/// A verify request's response: whether the proof is valid. +fn encode_verdict(valid: bool) -> Vec { + let mut f = Frame::new(); + f.u8(OK); + f.u8(valid.into()); + f.finish() +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::wire::{decode_hello, decode_response, encode_request, hello_version}; + + fn body(frame: Vec) -> Vec { + assert_eq!( + u64::from_le_bytes(frame[..8].try_into().unwrap()), + frame.len() as u64 - 8 + ); + frame[8..].to_vec() + } + + #[test] + fn hello_round_trips() { + let hello = Hello { + max_request: 1 << 30, + tag: verifier::TAG.to_vec(), + prover: "stable".into(), + }; + let bytes = body(encode_hello(&hello)); + assert_eq!(hello_version(&bytes), Some(wire::VERSION)); + assert_eq!(decode_hello(&bytes), Some(hello)); + assert_eq!(decode_hello(&bytes[..bytes.len() - 1]), None); + assert_eq!(decode_hello(&[&bytes[..], &[0]].concat()), None); + let mut other = bytes.clone(); + other[0] ^= 1; + assert_eq!(hello_version(&other), None); + } + + /// A request decodes to what was encoded; every truncation, and a trailing byte, is refused, never a panic. + #[test] + fn requests_round_trip_and_truncations_are_refused() { + let xmss = ( + LeanXmssPublicKey::from_bytes(&[1; 32]), + [2; 32], + LeanXmssSignature::from_bytes(&std::array::from_fn(|i| i as u8)), + ); + let sphincs = ( + LeanSphincsPublicKey::from_bytes(&[3; 32]), + [4; 32], + LeanSphincsSignature::from_bytes(&std::array::from_fn(|i| (i * 7) as u8)), + ); + let blob: Blob = std::array::from_fn(|i| ((i as u128) << 100) | i as u128); + let only = AggregateClaims { + lean_xmss: vec![(xmss.0, xmss.1)], + lean_sphincs: vec![(sphincs.0, sphincs.1)], + da_commitment: Some([5; 32]), + }; + let child = (only.clone(), vec![6, 7, 8]); + let frame = encode_request( + std::slice::from_ref(&child), + std::slice::from_ref(&xmss), + std::slice::from_ref(&sphincs), + &[blob], + OutputClaims::Only(&only), + 3, + ); + let bytes = body(frame); + let Ok(Call::Aggregate(request)) = decode_request(&bytes) else { + panic!("not an aggregate request") + }; + assert_eq!(request.children, vec![child]); + assert_eq!(request.raw_lean_xmss, vec![xmss]); + assert_eq!(request.raw_lean_sphincs, vec![sphincs]); + assert_eq!(request.blobs, vec![blob]); + assert_eq!(request.output, Some(only)); + assert_eq!(request.log_inv_rate, 3); + for len in 0..bytes.len() { + assert!( + decode_request(&bytes[..len]).is_err(), + "a prefix of {len} bytes decoded" + ); + } + assert!(decode_request(&[&bytes[..], &[0]].concat()).is_err()); + // A count no body could hold. + let mut huge = vec![OP_AGGREGATE, 1]; + huge.extend(u64::MAX.to_le_bytes()); + assert!(decode_request(&huge).is_err()); + let many = vec![(AggregateClaims::default(), Vec::new()); aggregation::MAX_RECURSIONS + 1]; + let frame = encode_request(&many, &[], &[], &[], OutputClaims::All, 1); + assert!(matches!( + decode_request(&body(frame)), + Err((Rejection::InvalidInput, _)) + )); + } + + #[test] + fn connections_beyond_the_cap_wait() { + let open = Arc::new((Mutex::new(0), Condvar::new())); + let mut slots: Vec<_> = (0..MAX_CONNECTIONS).map(|_| Slot::take(&open)).collect(); + let (taken, waiter) = mpsc::channel(); + let open2 = open.clone(); + thread::spawn(move || taken.send(Slot::take(&open2)).unwrap()); + assert!(waiter.recv_timeout(Duration::from_millis(100)).is_err()); + slots.pop(); + waiter.recv().unwrap(); + } + + #[test] + fn responses_round_trip() { + let proven = (AggregateClaims::default(), vec![1, 2, 3]); + assert_eq!( + decode_response(&body(encode_response(&Ok(proven.clone())))), + Some(Ok(proven)) + ); + let rejection = (Rejection::InvalidInput, "log_inv_rate out of range".to_string()); + let bytes = body(encode_response(&Err(rejection.clone()))); + assert_eq!(decode_response(&bytes).unwrap().unwrap_err(), rejection); + assert!(decode_response(&[42, 0, 0, 0, 0, 0, 0, 0, 0]).is_none()); + } +} diff --git a/crates/sidecar/src/wire.rs b/crates/sidecar/src/wire.rs new file mode 100644 index 0000000000..d5c19ced42 --- /dev/null +++ b/crates/sidecar/src/wire.rs @@ -0,0 +1,239 @@ +//! The protocol's bytes, as `bins/leanvm-sidecar/README.md` specifies them: frames, the hello, and the client's half, +//! requests out and responses in (the server's half is beside the server). Every integer is little-endian; every +//! variable-length field is a `u64` length, then its bytes. + +use crate::{Blob, Rejection}; +use claims::{AggregateClaims, OutputClaims}; +use lean_sphincs::{LeanSphincsPublicKey, LeanSphincsSignature}; +use lean_xmss::{LeanXmssPublicKey, LeanXmssSignature}; +use std::io::{self, Read}; + +/// The hello's first bytes. +pub(crate) const MAGIC: [u8; 8] = *b"leanvmsc"; + +/// The protocol's version, which the hello announces right after [`MAGIC`], where every version keeps it. +pub(crate) const VERSION: u32 = 1; + +pub(crate) const OP_AGGREGATE: u8 = 1; +pub(crate) const OP_VERIFY: u8 = 2; +pub(crate) const OK: u8 = 0; + +pub(crate) type RawLeanXmss = (LeanXmssPublicKey, lean_xmss::Message, LeanXmssSignature); +pub(crate) type RawLeanSphincs = (LeanSphincsPublicKey, lean_sphincs::Message, LeanSphincsSignature); +/// Claims, and the bytes of their proof. +pub(crate) type Proven = (AggregateClaims, Vec); +/// A request's proof, or why there is none. +pub(crate) type Outcome = Result; + +/// A frame being built: its length is patched in by [`Self::finish`]. +pub(crate) struct Frame(Vec); + +impl Frame { + pub(crate) fn new() -> Self { + Self(vec![0; 8]) + } + + pub(crate) fn u8(&mut self, x: u8) { + self.0.push(x); + } + + pub(crate) fn u64(&mut self, x: u64) { + self.0.extend(x.to_le_bytes()); + } + + pub(crate) fn raw(&mut self, bytes: &[u8]) { + self.0.extend(bytes); + } + + pub(crate) fn bytes(&mut self, bytes: &[u8]) { + self.u64(bytes.len() as u64); + self.raw(bytes); + } + + pub(crate) fn finish(mut self) -> Vec { + let len = (self.0.len() as u64 - 8).to_le_bytes(); + self.0[..8].copy_from_slice(&len); + self.0 + } +} + +/// What [`read_frame`] read. +pub(crate) enum Incoming { + Body(Vec), + /// The stream ended cleanly, between frames. + End, + /// A frame longer than the reader takes: its announced length. The body is left unread. + TooLarge(u64), +} + +/// Reads one frame. A body is read as it arrives, so a forged length allocates nothing it does not receive. +pub(crate) fn read_frame(r: &mut impl Read, max_len: u64) -> io::Result { + let mut len = [0; 8]; + let mut got = 0; + while got < len.len() { + match r.read(&mut len[got..]) { + Ok(0) if got == 0 => return Ok(Incoming::End), + Ok(0) => return Err(io::ErrorKind::UnexpectedEof.into()), + Ok(n) => got += n, + Err(e) if e.kind() == io::ErrorKind::Interrupted => {} + Err(e) => return Err(e), + } + } + let len = u64::from_le_bytes(len); + if len > max_len { + return Ok(Incoming::TooLarge(len)); + } + let mut body = Vec::new(); + r.take(len).read_to_end(&mut body)?; + if body.len() as u64 != len { + return Err(io::ErrorKind::UnexpectedEof.into()); + } + Ok(Incoming::Body(body)) +} + +/// Reads fields off the front of a body. +pub(crate) struct Cursor<'a>(pub(crate) &'a [u8]); + +impl<'a> Cursor<'a> { + pub(crate) fn take(&mut self, n: usize) -> Option<&'a [u8]> { + let (head, rest) = self.0.split_at_checked(n)?; + self.0 = rest; + Some(head) + } + + pub(crate) fn array(&mut self) -> Option<&'a [u8; N]> { + self.take(N).map(|bytes| bytes.try_into().unwrap()) + } + + pub(crate) fn u8(&mut self) -> Option { + self.array::<1>().map(|[x]| *x) + } + + fn u32(&mut self) -> Option { + self.array().map(|x| u32::from_le_bytes(*x)) + } + + pub(crate) fn u64(&mut self) -> Option { + self.array().map(|x| u64::from_le_bytes(*x)) + } + + pub(crate) fn bytes(&mut self) -> Option<&'a [u8]> { + let len = usize::try_from(self.u64()?).ok()?; + self.take(len) + } + + pub(crate) fn claims(&mut self) -> Option { + AggregateClaims::from_bytes(self.bytes()?) + } + + fn string(&mut self) -> Option { + String::from_utf8(self.bytes()?.to_vec()).ok() + } + + pub(crate) fn is_empty(&self) -> bool { + self.0.is_empty() + } +} + +/// What the server says first on every connection, after [`MAGIC`] and [`VERSION`]. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) struct Hello { + pub max_request: u64, + pub tag: Vec, + pub prover: String, +} + +/// The version a hello announces, `None` unless the body starts with [`MAGIC`]. +pub(crate) fn hello_version(body: &[u8]) -> Option { + let mut c = Cursor(body); + (c.array::<8>()? == &MAGIC).then(|| c.u32())? +} + +/// `None` unless the body is a whole hello of this version's layout. +pub(crate) fn decode_hello(body: &[u8]) -> Option { + let mut c = Cursor(body); + hello_version(c.take(12)?)?; + let hello = Hello { + max_request: c.u64()?, + tag: c.bytes()?.to_vec(), + prover: c.string()?, + }; + c.is_empty().then_some(hello) +} + +pub(crate) fn encode_request( + children: &[Proven], + raw_lean_xmss: &[RawLeanXmss], + raw_lean_sphincs: &[RawLeanSphincs], + blobs: &[Blob], + output: OutputClaims, + log_inv_rate: usize, +) -> Vec { + let mut f = Frame::new(); + f.u8(OP_AGGREGATE); + // A rate past a byte is out of range anyway: the server refuses it as it would any other. + f.u8(u8::try_from(log_inv_rate).unwrap_or(u8::MAX)); + f.u64(children.len() as u64); + for (claims, proof) in children { + f.bytes(&claims.to_bytes()); + f.bytes(proof); + } + f.u64(raw_lean_xmss.len() as u64); + for (pk, message, signature) in raw_lean_xmss { + f.raw(&pk.to_bytes()); + f.raw(message); + f.raw(&signature.to_bytes()); + } + f.u64(raw_lean_sphincs.len() as u64); + for (pk, message, signature) in raw_lean_sphincs { + f.raw(&pk.to_bytes()); + f.raw(message); + f.raw(&signature.to_bytes()); + } + f.u64(blobs.len() as u64); + for blob in blobs { + for symbol in blob { + f.raw(&symbol.to_le_bytes()); + } + } + match output { + OutputClaims::All => f.u8(0), + OutputClaims::Only(claims) => { + f.u8(1); + f.bytes(&claims.to_bytes()); + } + } + f.finish() +} + +pub(crate) fn encode_verify(claims: &AggregateClaims, proof: &[u8]) -> Vec { + let mut f = Frame::new(); + f.u8(OP_VERIFY); + f.bytes(&claims.to_bytes()); + f.bytes(proof); + f.finish() +} + +/// `None` if the body is not a verify request's response. +pub(crate) fn decode_verdict(body: &[u8]) -> Option> { + decode(body, |c| match c.u8()? { + 0 => Some(false), + 1 => Some(true), + _ => None, + }) +} + +/// `None` if the body is not an aggregate request's response. +pub(crate) fn decode_response(body: &[u8]) -> Option { + decode(body, |c| Some((c.claims()?, c.bytes()?.to_vec()))) +} + +/// A response: `ok` reads what follows status 0, any other status is a rejection and its message. +fn decode(body: &[u8], ok: impl FnOnce(&mut Cursor) -> Option) -> Option> { + let mut c = Cursor(body); + let result = match c.u8()? { + OK => Ok(ok(&mut c)?), + status => Err((Rejection::from_code(status)?, c.string()?)), + }; + c.is_empty().then_some(result) +} diff --git a/crates/stable_prover/keccak_gkr/Cargo.toml b/crates/stable_prover/keccak_gkr/Cargo.toml index dc0a0a244f..d7c53062a0 100644 --- a/crates/stable_prover/keccak_gkr/Cargo.toml +++ b/crates/stable_prover/keccak_gkr/Cargo.toml @@ -13,15 +13,16 @@ primitives = { package = "stable_primitives", path = "../primitives" } fiat_shamir = { package = "fiat_shamir_prover", path = "../../fiat_shamir_prover" } pcs = { package = "stable_pcs", path = "../pcs" } tracing.workspace = true -bench = { workspace = true, optional = true } +bench_harness = { workspace = true, optional = true } isa = { workspace = true, optional = true } verifier.workspace = true [features] # The standalone batch benchmark (`batch_bench`), for the `hash_batch` target. -bench = ["dep:bench", "dep:isa", "primitives/test-util"] +bench = ["dep:bench_harness", "dep:isa", "primitives/test-util"] [dev-dependencies] +allocator.workspace = true keccak_gkr = { package = "stable_keccak_gkr", path = ".", features = ["bench"] } [[bench]] diff --git a/crates/stable_prover/keccak_gkr/benches/hash_batch.rs b/crates/stable_prover/keccak_gkr/benches/hash_batch.rs index cb7be20407..92d306ce2a 100644 --- a/crates/stable_prover/keccak_gkr/benches/hash_batch.rs +++ b/crates/stable_prover/keccak_gkr/benches/hash_batch.rs @@ -11,14 +11,14 @@ //! `BENCH_TRACING=1` prints the final pass's span tree (`RUST_LOG` adjusts it). extern crate stable_keccak_gkr as keccak_gkr; -use bench::{Plan, env_usize}; +use bench_harness::{Plan, env_usize}; use verifier::MIN_LOG_INV_RATE; #[global_allocator] -static ALLOCATOR: bench::Jemalloc = bench::Jemalloc; +static ALLOCATOR: allocator::Jemalloc = allocator::Jemalloc; fn main() { - bench::init_tracing_from_env(); + bench_harness::init_tracing_from_env(); let n = 1usize << env_usize("KECCAK_N_LOG", 13); let report = keccak_gkr::batch_bench::run(n, env_usize("KECCAK_DEPTH", 12), MIN_LOG_INV_RATE, Plan::from_env()); if std::env::args().any(|arg| arg == "--json") { diff --git a/crates/stable_prover/keccak_gkr/src/batch_bench.rs b/crates/stable_prover/keccak_gkr/src/batch_bench.rs index d930461fc2..4c38000cec 100644 --- a/crates/stable_prover/keccak_gkr/src/batch_bench.rs +++ b/crates/stable_prover/keccak_gkr/src/batch_bench.rs @@ -1,7 +1,7 @@ //! The standalone batch benchmark of the `hash_batch` bench target and the tests: witness, commitment, reduction and //! opening of `n` random permutations, then verification. Where a pass's time goes is its span tree. -use bench::{Metric, Plan, Timing, bencher_json}; +use bench_harness::{Metric, Plan, Timing, bencher_json}; use isa::pretty_integer; use pcs::Committed; use primitives::field::{F64, F128Unreduced}; @@ -32,7 +32,7 @@ pub fn run(n: usize, depth: usize, log_inv_rate: usize, plan: Plan) -> Report { let inputs: Vec<[u64; STATE_LANES]> = (0..n).map(|_| std::array::from_fn(|_| rng.next_u64())).collect(); let (proof, prove) = plan.warm_then_measure(|final_pass| { - let _quiet = (!final_pass).then(bench::suppress_tracing); + let _quiet = (!final_pass).then(bench_harness::suppress_tracing); let _span = tracing::info_span!("Keccak GKR prove", n).entered(); let mut committed = vec![F64(0); 1 << shape.stack_log()]; let witness = tracing::info_span!("Witness").in_scope(|| Witness::generate(shape, &inputs, &mut committed)); diff --git a/crates/stable_prover/lean_vm_core/src/prover.rs b/crates/stable_prover/lean_vm_core/src/prover.rs index 5d359a600b..d7ed580106 100644 --- a/crates/stable_prover/lean_vm_core/src/prover.rs +++ b/crates/stable_prover/lean_vm_core/src/prover.rs @@ -8,10 +8,16 @@ use verifier::E; pub struct StableProver; impl isa::Prover for StableProver { + const NAME: &'static str = "stable"; + fn init() { parallel::init(); } + fn set_num_threads(n: usize) { + parallel::set_num_threads(n) + } + fn prove( program: &isa::Program, witness: &[(String, Vec>)], diff --git a/crates/stable_prover/parallel/src/lib.rs b/crates/stable_prover/parallel/src/lib.rs index 87b9757eb8..a75593bfcd 100644 --- a/crates/stable_prover/parallel/src/lib.rs +++ b/crates/stable_prover/parallel/src/lib.rs @@ -10,26 +10,29 @@ use std::ptr::NonNull; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Condvar, Mutex, Once, OnceLock}; -/// Worker count including the dispatcher: `LEANVM_NUM_THREADS` when set, `1` being strictly sequential, else one per -/// physical core: the logical CPUs the platform reports, halved where Linux says SMT is active (a core's second -/// hardware thread slows a proof on a many-core machine). +static NUM_THREADS: OnceLock = OnceLock::new(); + +/// Worker count including the dispatcher: [`set_num_threads`]'s, else one per physical core (the logical CPUs, halved +/// where Linux says SMT is active: a second hardware thread per core slows a proof). #[must_use] #[inline] pub fn num_threads() -> usize { - static NUM_THREADS: OnceLock = OnceLock::new(); *NUM_THREADS.get_or_init(|| { - std::env::var("LEANVM_NUM_THREADS") - .ok() - .and_then(|v| v.parse::().ok()) - .filter(|&n| n > 0) - .unwrap_or_else(|| { - let logical = std::thread::available_parallelism().map_or(1, |n| n.get()); - let smt = std::fs::read_to_string("/sys/devices/system/cpu/smt/active").is_ok_and(|s| s.trim() == "1"); - if smt { (logical / 2).max(1) } else { logical } - }) + let logical = std::thread::available_parallelism().map_or(1, |n| n.get()); + let smt = std::fs::read_to_string("/sys/devices/system/cpu/smt/active").is_ok_and(|s| s.trim() == "1"); + if smt { (logical / 2).max(1) } else { logical } }) } +/// Fixes [`num_threads`] before the pool starts; `0` keeps the default. Panics if it is already another. +pub fn set_num_threads(n: usize) { + if n == 0 { + return; + } + let set = *NUM_THREADS.get_or_init(|| n); + assert_eq!(set, n, "the thread pool already runs with {set} threads"); +} + /// How finely guided scheduling divides the remaining work. const CLAIM_DIVISOR: usize = 4; @@ -503,3 +506,15 @@ where ) .unwrap_or_else(identity) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + #[should_panic(expected = "already runs")] + fn a_fixed_count_refuses_another() { + set_num_threads(num_threads()); + set_num_threads(num_threads() + 1); + } +} diff --git a/crates/stable_prover/pcs/Cargo.toml b/crates/stable_prover/pcs/Cargo.toml index 27c6a36b1d..7249807175 100644 --- a/crates/stable_prover/pcs/Cargo.toml +++ b/crates/stable_prover/pcs/Cargo.toml @@ -16,7 +16,8 @@ verifier.workspace = true [dev-dependencies] primitives = { package = "stable_primitives", path = "../primitives", features = ["test-util"] } -bench.workspace = true +allocator.workspace = true +bench_harness.workspace = true isa.workspace = true [[bench]] diff --git a/crates/stable_prover/pcs/benches/throughput.rs b/crates/stable_prover/pcs/benches/throughput.rs index 2f0e615ec2..8036f17a23 100644 --- a/crates/stable_prover/pcs/benches/throughput.rs +++ b/crates/stable_prover/pcs/benches/throughput.rs @@ -1,6 +1,6 @@ //! PCS throughput: commits a random witness of `2^PCS_LOG_N` `K` words at inverse rate `2^PCS_LOG_INV_RATE`, with its //! OOD sample, opens it, times each phase, and reports GiB/s over the committed data. The passes follow -//! [`bench::Plan::from_env`] (`BENCH_REPEAT`, `BENCH_COOLDOWN`). +//! [`bench_harness::Plan::from_env`] (`BENCH_REPEAT`, `BENCH_COOLDOWN`). //! //! ```text //! PCS_LOG_N number of variables = log2(witness length) [default 22] @@ -18,17 +18,17 @@ extern crate stable_pcs as pcs; use std::hint::black_box; use std::time::Instant; -use bench::{Plan, Timing, env_usize}; +use bench_harness::{Plan, Timing, env_usize}; use isa::pretty_integer; use pcs::{RingSwitchOpen, StackClaim, commit, open}; use primitives::{field::F64, test_rng::Rng}; use verifier::MIN_LOG_INV_RATE; #[global_allocator] -static ALLOCATOR: bench::Jemalloc = bench::Jemalloc; +static ALLOCATOR: allocator::Jemalloc = allocator::Jemalloc; fn main() { - bench::init_tracing_from_env(); + bench_harness::init_tracing_from_env(); let log_n = env_usize("PCS_LOG_N", 22); let log_inv_rate = env_usize("PCS_LOG_INV_RATE", MIN_LOG_INV_RATE); @@ -60,7 +60,7 @@ fn main() { let (mut commit_t, mut open_t) = (Timing::default(), Timing::default()); plan.warm_then_measure(|last| { - let _quiet = (!last).then(bench::suppress_tracing); + let _quiet = (!last).then(bench_harness::suppress_tracing); let mut ps = primitives::transcript::ProverState::from_label(b"pcs-throughput"); let t = Instant::now(); diff --git a/formal/lean_vm/scripts/guest-export/Cargo.lock b/formal/lean_vm/scripts/guest-export/Cargo.lock index ce924b30ef..f4344c915a 100644 --- a/formal/lean_vm/scripts/guest-export/Cargo.lock +++ b/formal/lean_vm/scripts/guest-export/Cargo.lock @@ -8,6 +8,14 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" +[[package]] +name = "claims" +version = "0.1.0" +dependencies = [ + "lean_sphincs", + "lean_xmss", +] + [[package]] name = "fiat_shamir_prover" version = "0.1.0" @@ -68,6 +76,7 @@ dependencies = [ name = "leanvm-guest-export" version = "0.0.0" dependencies = [ + "claims", "fiat_shamir_prover", "isa", "keccak256", diff --git a/formal/lean_vm/scripts/guest-export/Cargo.toml b/formal/lean_vm/scripts/guest-export/Cargo.toml index 6f12380eb1..2e3c68ac3b 100644 --- a/formal/lean_vm/scripts/guest-export/Cargo.toml +++ b/formal/lean_vm/scripts/guest-export/Cargo.toml @@ -19,7 +19,7 @@ build = "build.rs" # The overlay's sources (`build.rs` copies `aggregation` into `OUT_DIR`) use these, which # cargo-machete, reading this crate's own sources, cannot see. [package.metadata.cargo-machete] -ignored = ["fiat_shamir_prover", "isa", "keccak256", "parallel", "pcs", "primitives", "tracing"] +ignored = ["claims", "fiat_shamir_prover", "isa", "keccak256", "parallel", "pcs", "primitives", "tracing"] [workspace] @@ -37,6 +37,7 @@ verifier = { path = "../../../../crates/verifier" } keccak256 = { path = "../../../../crates/keccak256" } fiat_shamir_prover = { path = "../../../../crates/fiat_shamir_prover" } zkdsl_compiler = { path = "../../../../crates/zkdsl_compiler" } +claims = { path = "../../../../crates/claims" } lean_xmss = { path = "../../../../crates/lean_xmss" } lean_sphincs = { path = "../../../../crates/lean_sphincs" } lean_da = { path = "../../../../crates/lean_da" } diff --git a/formal/lean_vm/scripts/guest-export/src/export/guest_diff.rs b/formal/lean_vm/scripts/guest-export/src/export/guest_diff.rs index 487112a382..5405724f35 100644 --- a/formal/lean_vm/scripts/guest-export/src/export/guest_diff.rs +++ b/formal/lean_vm/scripts/guest-export/src/export/guest_diff.rs @@ -17,8 +17,8 @@ use isa::Prover; use lean_vm_core::StableProver; use lean_vm_core::cpu::{from_scalar, public_input_bytes}; use primitives::field::F128; -use lean_sphincs::LeanSphincsSignature; -use lean_xmss::LeanXmssSignature; +use lean_sphincs::{LeanSphincsPublicKey, LeanSphincsSignature}; +use lean_xmss::{LeanXmssPublicKey, LeanXmssSignature}; use rand::rngs::StdRng; use rand::{Rng, SeedableRng}; use std::fmt::Write as _; diff --git a/formal/lean_vm/scripts/guest-export/src/export/node_vectors.rs b/formal/lean_vm/scripts/guest-export/src/export/node_vectors.rs index a8f2094c4e..bd86db060e 100644 --- a/formal/lean_vm/scripts/guest-export/src/export/node_vectors.rs +++ b/formal/lean_vm/scripts/guest-export/src/export/node_vectors.rs @@ -7,6 +7,8 @@ //! are hand-written in `tests/LeanVMTests/Spec/Node.lean`. use super::*; +use lean_sphincs::LeanSphincsPublicKey; +use lean_xmss::LeanXmssPublicKey; use lean_vm_core::StableProver; use lean_vm_core::cpu::{from_scalar, to_scalar}; use primitives::field::F128;