Talk · 11 slides

wasm_zero and developing with WASM targets

Rust’s WebAssembly targets and what each is for, a crypto wallet that runs entirely in the browser, crossing the FFI boundary by hand, zero-copy serialisation with rkyv, and running WASM as parallel, isolated tasks with Exu.

Daksh Upadhyaydusk-network/exudusk-network/rusk

  1. Title slide: wasm_zero and developing with WASM targets.
    01wasm_zero and developing with WASM targets
  2. WASM targets. Each target has its own use case: wasm32-unknown-unknown; wasm32-unknown-emscripten, similar to above, emits JS glue code; wasm32-wasi / wasm32-wasip1, first preview of components; wasm32-wasip1-threads, on top of wasip1 but threads; wasm32-wasip2, http and async IO on top of wasip1; wasm32-wali-linux-musl, Linux syscalls in WASM; wasm64-unknown-unknown.
    02WASM targets
  3. Problem statement: a crypto wallet that's entirely on the frontend browser. Spawn a web worker in the browser. Instantiate your crypto WebAssembly logic inside the web worker. Call functions like generate_private_key from a bip39 mnemonic and keep the seed in sandboxed memory. Then call functions like get_balance and transfer, which give you blob tx data ready to send to the node network. Drop the worker, and the WASM memory is destroyed along with it.
    03Problem statement
  4. wasm32-unknown-unknown, a limited target. Code: an unsafe no_mangle extern "C" fn balance taking a seed, an index, a notes pointer and a mutable 16-byte balance_info pointer, returning an ErrorCode. It derives the phoenix view key, reads notes from the buffer, calculates the balance, and copies the bytes into balance_info_ptr with copy_nonoverlapping.
    04wasm32-unknown-unknown: a limited targetgithub.com/dusk-network/rusk
  5. Exu: run WebAssembly code as parallel, isolated and abortable functions in the JS engine. Screenshot of the Exu docs showing task-based bulk actions with module.task, and direct shared memory access using malloc, a Uint8Array over memory.buffer, and free.
    05Exu: parallel, isolated, abortable WASM in the JS enginegithub.com/dusk-network/exu
  6. Crossing the wasm to language boundary. This is the FFI boundary. Call malloc in Rust, which returns ptr and len. Use that on the Rust side in the allocated memory region to serialize a string or integer. Decode the string or integer on the JavaScript side.
    06Crossing the wasm → language boundary
  7. Zero copy serialisation on the Rust side. rkyv allows zero copy serialisation and alignment for types that support it, only on the Rust side. Generate the type shape at compile time during WASM bytecode generation so the JavaScript side knows how to deserialise raw bytes using pointer arithmetic and TextDecoder calls, similar to the glue wasm-bindgen emits. We can use FFI on the JavaScript side with rkyv to decrease deserialisation cost (theoretical).
    07Zero copy serialisation on the Rust side
  8. wasm_zero: rkyv zero-copy serialisation on the Rust side, emitting only minimal decoding glue on the JS side. Code: a #[wasm_zero] pub fn get_adult_person returning a Person, and JS that awaits initWasmZero("app.wasm") and calls wasmzero.get_adult_person() to get name, age, email and scores. Less WASM bundle size emitted overall, faster decoding of primitives and complex objects, supports the WASI target, no_std and std both supported, use your own allocator.
    08wasm_zero
  9. Exu plus wasm_zero (rayon with Exu). Code: create an Exu Module from wasm_zero_rayon.wasm, then run module.task, which gets a fresh sandbox and memory dropped when it resolves. Inside, initThreadPool with navigator.hardwareConcurrency spins up a rayon pool over the sandbox's shared memory, and __wasm_zero_parallel_count_primes(1_000_000) returns 78498 primes.
    09Exu + wasm_zero (rayon with Exu)
  10. wasip3 target plus Tokio plus Toasty ORM on Spin. The Spin SDK allows writing http components.
    10wasip3 target + Tokio + Toasty ORM on Spin
  11. Last frontier: engineer your own ABI. ABI compatibility is important for WASM modules. WASM modules are portable. Write them in C, C++, Rust or Go and instantiate them together. No extra code for ABI compatibility, reducing WASM sizes.
    11Last frontier: engineer your own ABI