The repo takes a Rust implementation of Shamir Secret Sharing over Finite Field and exposes it as WASM library. Next it showcases the usage using simple HTML page.
Components:
src/lib
- Rust implementationtests
- Rust integration testsindex.html
- simple HTML page that demos the usage.
- Clone the repo
- Prepare your environment according to the description on this page: https://rustwasm.github.io/book/game-of-life/setup.html
- Build the wasm library:
wasm-pack build --release --target web
- Optionally:
- Run Rust tests:
cargo test
- Run wasm tests"
wasm-pack test --node
- Run Rust tests:
- Run HTTP server:
python3 -m http.server
- Go to
http://localhost:8000
- Open a page (
http://localhost:8000
)
- Click Submit to generates shares from provided text and with defined threshold
- Select number of shares below threshold and click Recover
- Select number of shares equal or greater than threshold and click Recover
shamir_secret_sharing package is quite old and its newest version 0.1.1, published to crates.io, supports rand package in version 0.5 which in turn doesn't support wasm-bindgen. As a result, rand doesn't work when compiled to wasm library (the error says that source of entropy is not available).
Support for wasm-bindgen in rand started from version 0.6. Interestingly, the newest code committed to shamir_secret_sharing repo in master branch supports rand 0.6, but it doesn't compile to wasm lib (compilation errors).
The solution was to fork shamir_secret_sharing repo, take the latest working commit (which represents version 0.1.1 published to crates.io) and modify its Cargo.toml to support rand version 0.6. This change has been done in a separate branch. Finally, we can reference the fixed package in our Cargo.toml and enable support for "wasm-bindgen" in rand.
There is an example that shows how to deploy WASM: https://rustwasm.github.io/docs/wasm-bindgen/examples/without-a-bundler.html
In this example, there is an error in this line:
#[wasm_bindgen(start)]
pub fn main() -> Result<(), JsValue> {
Turns out this function can't be called main
(any other name can be used).
Issue is described here in detail:
rustwasm/wasm-bindgen#2206
Running the tests like this wasm-pack test --<browser_name> --headless
fails with the error:
http://127.0.0.1:49410/session/fdeaa5a7b92f04d662568146055c3f51/url: status code 404
It happens not only for the custom code implemented in this repo but also for the code taken from: https://rustwasm.github.io/book/game-of-life/hello-world.html
The reason is currently unknown. This needs to be investigated.
The build without any custom dependencies (just after starting implementation) had a size below 1 KB. Currently, after importing all dependencies, the size is 99KB. There is definitely a space for improvement.
There is a good tutorial on this topic: https://rustwasm.github.io/book/reference/code-size.html