# Weft Weft is a small frontend language that compiles directly to WebAssembly. A component describes its state, views, actions, and permitted messages in one source file. Its build checks behavioral and confidentiality claims, then proves that the emitted binary implements the source semantics. The browser adapter renders the exported view and routes explicit messages. Wasmtime runs the same bytes headlessly. The compiler, source verifier, binary validator, local server and headless host are written in C11. They call Z3 and Wasmtime through their native C APIs. Application logic executes as WASM. A small browser adapter owns the DOM, entropy and transport. ## Try the private ballot The Apple Silicon download includes the executable, source, native libraries and C headers. From the extracted project directory, verify both supplied modules: ```sh bin/weft check examples/ballot.weft bin/weft check examples/vault.weft ``` To rebuild the toolchain and examples in that distribution: ```sh make -j4 bin/weft build examples/ballot.weft bin/weft build examples/vault.weft ``` For a source-only build, install a C11 compiler, make, json-c, the Z3 development package and the Wasmtime **C SDK** for your architecture. On Linux the build also uses libcrypto for SHA-256. Point make at prefixes containing `include/` and `lib/`, for example `make -j4 Z3_PREFIX=/usr WASMTIME_PREFIX=/opt/wasmtime JSON_PREFIX=/usr`. This release was built and exercised on macOS arm64. [Wasmtime C SDK installation](https://docs.wasmtime.dev/c-api/) · [Z3 C API](https://z3prover.github.io/api/html/group__capi.html) Run the browser version from the project directory: ```sh bin/weft serve examples/ballot.weft ``` Open `http://127.0.0.1:8080`. Choose a value from 0 through 3, optionally set your private confidence, review, and confirm. The browser creates two shares using fresh randomness. Two independent loopback processes each receive one share. A confirmation shows how many recipients acknowledged delivery. Each inbox keeps its latest share in process memory for the lifetime of the demo. The local machine and its operator control both processes. To run the same ballot module in a standalone Wasm runtime: ```sh bin/weft run examples/ballot.weft choose=3 annotate=2 review share bin/weft run examples/vault.weft rotate=143 status ``` The headless command prints the **owner's** view and outbox for inspection. XOR the two numbers in the ballot outbox to recover 3. Individual shares vary across runs. The vault initializes its private field at its host. The browser receives only the health value from the vault's modeled output channel. The example web server serves browser-owned components exclusively. ## Write a component This excerpt is from [the executable ballot](examples/ballot.weft). ```text state vote: u2 = 0 private state confidence: u2 = 0 private protect confidence from north, south with input annotate channel north on share only when reviewing and not sealed channel south on share only when reviewing and not sealed action share "Confirm and split" when reviewing and not sealed split vote to north, south set reviewing = 0 set sealed = 1 ensures seals_once = sealed and not enabled(share) ensures keeps_choice = vote == old(vote) end ``` The guard drives both the button and dispatch. A disabled action stutters even if invoked directly. The channel declarations independently specify the action and state in which each recipient may receive anything. Removing the review guard produces a counterexample. Changing the split source to `confidence` preserves secrecy against either recipient alone but fails the explicit coalition policy. | Declaration | Meaning | | --- | --- | | `component "Title" on owner`, `observer peer` | Place this module at one principal and name its outbound recipients. | | `state x: uN = 0 private` | Declare a finite field of 1–8 bits. `external` requests an initializer from the owner. Public fields are included in every peer's observation. | | `view name "Label" = expr` | Render a numeric value at the owner. A claim can refer to it via `shown(name)`. | | `action name "Label" input uN private when guard` | Define an event with an optional typed payload available as `value`. Its guard depends on state. The action's identity is public in the security model. | | `set field = expr` | Simultaneously update fields from the pre-state. | | `send expr to peer` | Emit a value to one recipient. The checker accounts for both its value and its presence. | | `split x to left, right` | Distribute a two-of-two XOR sharing of field or input `x` using one fresh uniform mask. Each action may use one mask and one message per recipient. | | `protect field from a, b with input change` | Allow the private field and the named action's private payload to differ across runs. Prove joint ignorance for these recipients. The input clause is optional. | | `channel peer on act only when expr` | Constrain every possible emission on the channel. | | `claim name = expr` | Require an inductive state or view invariant. | | `ensures name = expr` | Require a postcondition whenever the enclosing action is enabled. Use `old(x)` for a pre-state field. | Expressions support unsigned comparisons, bitwise `&`, `|`, `^`, Boolean `and`, `or`, `not`, and `ite(test, yes, no)`. Literals range from 0 to 255. Fields widen losslessly. Predicates have width one. A full-line comment begins with `#`. ## Check a build Each build emits `app.wasm`, `interface.json`, and a verification receipt containing the source, ABI and binary SHA-256 digests and the list of discharged obligations. To check an existing artifact or export solver queries: ```sh bin/weft check examples/ballot.weft bin/weft smt examples/ballot.weft > build/ballot/obligations.smt2 z3 build/ballot/obligations.smt2 ``` The optional replay command uses the Z3 command-line executable on your PATH. Each query asks for a counterexample. Every result should be `unsat`. The receipt is a reproducibility record. The `check` command reruns the proofs against the bytes on disk independently of the compiler. A counterexample, unsupported opcode, timeout, or unknown solver result fails verification. [Formal semantics, ABI and trust assumptions](SEMANTICS.md) ## Scope This prototype supports finite reactive components, numeric inputs and views, enabled controls, and two-party XOR sharing. The verified UX properties concern view values and interaction states. Browser layout, accessibility-tree behavior, timing, transport, and cryptographic entropy depend on the host. The demo illustrates recipient isolation on one machine. Production custody, authentication, delivery retries, and durable voting would require their own design. ## Regression tests ```sh make test make browser-test ``` The browser smoke test launches Chrome with a temporary profile and uses its built-in DevTools pipe. Set `WEFT_CHROME` to a Chromium executable if needed. Build the ballot first. The tests include deliberately broken UI rules, implicit and direct leaks, coalition leaks, and a valid Wasm binary with an incorrect opcode. ## Performance `make bench` measures parsing and code generation, prints the verification time for a complete build, and times one million hot dispatches per sample in Wasmtime. The C host caches validated export handles and uses the returned state in place. Dispatch performs no heap allocation in the host. Verification runs when admitting an artifact. The browser adapter updates only changed text and enablement properties. [Measurements and workload definitions](PERFORMANCE.md) ## Author a static website in Weft A browser component can include a `page "Description"` block. Indent semantic elements by two spaces per level. `press action` binds a button to a no-argument verified action, `read view` displays a value, `when view` controls visibility using a one-bit view, and `message recipient` inspects that recipient’s last output at the owner. The static page host displays messages locally. It sends no share payloads over the network. Use `style ".selector"` blocks to set properties as `property_name "value"`. An optional `below 780` breakpoint applies the rule to smaller screens. The compiler emits static HTML, CSS, the shared DOM adapter and the component’s WASM. The check command reproduces and compares the page artifacts, then checks the component and its WASM. Presentation and DOM rendering are outside the formal model. The complete language site is included as `examples/site.weft`. Run `bin/weft build examples/site.weft -o build/site` to compile it. Its download and documentation links refer to the accompanying distribution files on the published site.