Skip to content

Commit

Permalink
WASM unit tests (#53)
Browse files Browse the repository at this point in the history
- Add extensive unit test coverage for WASM libs
- Use optional `getrandom` dependency for the `wasm` feature and turn on
  `getrandom/js`
  (https://docs.rs/getrandom/0.2.3/getrandom/#unsupported-targets)
- Re-add Rangeproof verify WASM (works with `wasm-demo`)
- Add `wasm_bindgen_test` dev-dependency
  • Loading branch information
sdbondi committed Jul 18, 2021
1 parent 262590d commit 48b68d2
Show file tree
Hide file tree
Showing 8 changed files with 900 additions and 107 deletions.
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# For wasm-* targets, the wasm-bindgen-test runner must be used
[target.wasm32-unknown-unknown]
runner = 'wasm-bindgen-test-runner'
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ tari_utilities = "^0.3"
base64 = "0.10.1"
digest = "0.9.0"
rand = { version = "0.8", default-features = false }
getrandom = { version = "0.2.3", default-features = false, optional = true }
clear_on_drop = "=0.2.4"
curve25519-dalek = { package = "curve25519-dalek-ng", version = "4", default-features = false, features = ["u64_backend", "serde", "alloc"] }
bulletproofs = {version = "4.0.0", package="tari_bulletproofs"}
Expand All @@ -34,6 +35,7 @@ wasm-bindgen = { version = "^0.2", features = ["serde-serialize"], optional = tr
criterion = "0.3.4"
bincode = "1.1.4"
blake3 = "0.3"
wasm-bindgen-test = "0.3.24"

[build-dependencies]
cbindgen = "0.17.0"
Expand All @@ -46,7 +48,7 @@ default = ["no_cc"]
# ^^^^^^^^^^^^ feature has been removed
# feature error on subtle-ng
avx2 = ["curve25519-dalek/avx2_backend", "bulletproofs/avx2_backend"]
wasm = ["wasm-bindgen", "rand/getrandom"]
wasm = ["wasm-bindgen", "getrandom/js"]
ffi = ["libc"]
no_cc_nightly = ["clear_on_drop/nightly"]
no_cc = ["clear_on_drop/no_cc"]
Expand Down
67 changes: 67 additions & 0 deletions src/wasm/commitments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,70 @@ pub fn opens(key: &str, value: u64, commitment: &str) -> bool {
let c = PedersenCommitment::from_public_key(&c.unwrap());
factory.open_value(&k.unwrap(), value, &c)
}

#[cfg(test)]
mod test {
use super::*;
use crate::keys::SecretKey;
use rand::rngs::OsRng;
use wasm_bindgen_test::*;

mod commit {
use super::*;

#[wasm_bindgen_test]
fn it_fails_for_invalid_key() {
let val = commit("aa", 123).into_serde::<CommitmentResult>().unwrap();
assert!(!val.error.is_empty());
assert!(val.commitment.is_none());
}

#[wasm_bindgen_test]
fn it_produces_a_commitment_with_given_key() {
let key = RistrettoSecretKey::random(&mut OsRng);
let expected_commit = PedersenCommitmentFactory::default().commit_value(&key, 123);
let commitment = commit(&key.to_hex(), 123).into_serde::<CommitmentResult>().unwrap();
assert!(commitment.error.is_empty());
assert_eq!(commitment.commitment, Some(expected_commit.to_hex()))
}
}

mod commit_private_keys {
use super::*;

#[wasm_bindgen_test]
fn it_fails_for_empty_input() {
let val = commit_private_keys("", "").into_serde::<CommitmentResult>().unwrap();
assert!(!val.error.is_empty());
assert!(val.commitment.is_none());
}

#[wasm_bindgen_test]
fn it_produces_a_commitment_with_given_keys() {
let key1 = RistrettoSecretKey::random(&mut OsRng);
let key2 = RistrettoSecretKey::random(&mut OsRng);
let commitment = commit_private_keys(&key1.to_hex(), &key2.to_hex())
.into_serde::<CommitmentResult>()
.unwrap();
let expected_commit = PedersenCommitmentFactory::default().commit(&key1, &key2);
assert!(commitment.error.is_empty());
assert_eq!(commitment.commitment, Some(expected_commit.to_hex()))
}
}

mod opens {
use super::*;

#[wasm_bindgen_test]
fn it_returns_false_for_zero_length_input() {
assert!(!opens("", 123, &""));
}

#[wasm_bindgen_test]
fn it_returns_true_if_key_value_opens_commitment() {
let key = RistrettoSecretKey::random(&mut OsRng);
let commitment = PedersenCommitmentFactory::default().commit_value(&key, 123);
assert!(opens(&key.to_hex(), 123, &commitment.to_hex()));
}
}
}
Loading

0 comments on commit 48b68d2

Please sign in to comment.