In my last post I described how I implemented the
signal-protocol Python library, which provides Python bindings using Pyo3 to an upstream maintained Rust cryptography crate implementing the Signal protocol. I created the the
signal-protocol library in order to prototype end-to-end encrypted messaging between journalists and their sources through SecureDrop. In the SecureDrop ecosystem, journalists use a Python project,
securedrop-client, hence the need for the Python bindings, and sources use Tor Browser.
In this brief post we’ll cover:
- an intro to WebAssembly
- useful crates you should know of, including
- helpful references to read more
WebAssembly is a binary-code format that runs in a stack-based virtual machine. It’s supported in all modern browsers, and can also be run in other runtimes (e.g. see WASI) that are browser independent.
There are two main approaches to combining Rust and WebAssembly:
wasm-bindgen is a handy tool wherein you can simply add the
It encapsulates a private member
InMemSignalProtocolStore that methods in our impl block in Rust will use when performing crypto operations in our WebAssembly module.
Result<T, JsValue> return type is a common pattern that
Err variant is returned.
Once the WebAssembly module is compiled and loaded, I can now create
SecureDropSourceSession objects (now that I’ve implemented
wasm-pack builds your project along with some autogenerated helper JS to a folder called
pkg. This is useful if you use a bundler like Webpack since you can simply add the path to your WebAssembly package to your dependencies. This is also useful if you want to publish your WebAssembly package to
npm. By publishing to
npm, folks using the package will not need the Rust toolchain installed since you’ll be publishing the built Wasm artifact.
Other useful crates to know of are
This post was a very brief overview. There are great tutorials out there on Rust and WebAssembly, and the main references I found useful while learning enough to implement my project are here:
- Rust and WebAssembly book - an introduction to using Rust and WebAssembly together wherein you implement the Game of Life in the browser
- MDN’s intro to
- Programming WebAssembly with Rust - a short read covering both WebAssembly in the browser as well as WASI (if you want to run WebAssembly independent of web browsers)
wasm-bindgenbook - This is a nice reference text with the details of using