WASM bindings draft

This commit is contained in:
zer0x64 2020-01-25 22:52:54 -05:00
parent 1b35f6d8dd
commit 8609773552
3 changed files with 34 additions and 0 deletions

View File

@ -17,9 +17,18 @@ codecov = { repository = "https://github.com/c0dearm/sharks" }
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies]
rand = "0.7"
[target.'cfg(target_arch="wasm32")'.dependencies]
serde = "1.0.104"
serde_derive = "1.0.104"
rand = { version = "0.7", features = ["wasm-bindgen"] }
wasm-bindgen = { version = "0.2.58", features = ["serde-serialize"] }
[dev-dependencies]
criterion = "0.3"

View File

@ -19,6 +19,9 @@ mod field;
mod math;
mod share;
#[cfg(target_arch = "wasm32")]
pub mod wasm;
use std::collections::HashSet;
use field::GF256;

22
src/wasm.rs Normal file
View File

@ -0,0 +1,22 @@
use wasm_bindgen::prelude::*;
use crate::{ Sharks, Share };
#[wasm_bindgen]
pub fn generate_shares(n_shares: u8, threshold: u8, secret: &[u8]) -> JsValue {
let sharks = Sharks(threshold);
let dealer = sharks.dealer(secret);
let shares: Vec<Vec<u8>> = dealer.take(n_shares as usize).map(|s| (&s).into()).collect();
JsValue::from_serde(&shares).expect("A Vec<Vec<u8>> should always be JSON serializable.")
}
#[wasm_bindgen]
pub fn recover(threshold: u8, shares: JsValue) -> Vec<u8> {
let sharks = Sharks(threshold);
let shares: Vec<Vec<u8>> = shares.into_serde().expect("will implement proper error handling later");
let shares: Vec<Share> = shares.iter().map(|s| s.as_slice().into()).collect();
sharks.recover(&shares).expect("will implement proper error handling later").into()
}