Skip to content

Commit

Permalink
fix a lint bug, add support for testing wasm using wasm-pack
Browse files Browse the repository at this point in the history
  • Loading branch information
s0l0ist authored and mikelodder7 committed Mar 4, 2024
1 parent 56e64eb commit da3d259
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ crypto = ["unknown_order/crypto"]
gmp = ["unknown_order/gmp"]
openssl = ["unknown_order/openssl"]
rust = ["unknown_order/rust"]
wasm = ["getrandom", "rand", "wasm-bindgen"]
wasm = ["getrandom", "rand", "wasm-bindgen", "serde-wasm-bindgen"]

[dependencies]
digest = "0.10"
getrandom = { version = "0.2", features = ["js"], optional = true }
rand = { version = "0.8", optional = true }
serde = { version = "1.0", features = ["serde_derive"] }
serde_bare = "0.5"
serde-wasm-bindgen = { version = "0.6", optional = true }
unknown_order = { version = "0.8", default-features = false }
wasm-bindgen = { version = "0.2", default-features = false, features = ["serde-serialize"], optional = true }
zeroize = { version = "1.5", features = ["zeroize_derive"] }
Expand All @@ -34,6 +35,7 @@ zeroize = { version = "1.5", features = ["zeroize_derive"] }
elliptic-curve = "0.13"
hex = "0.4"
k256 = { version = "0.13", features = ["arithmetic"] }
wasm-bindgen-test = "0.3"
rand = "0.8"
multibase = "0.9"
sha2 = "0.10"
Expand Down
6 changes: 2 additions & 4 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ macro_rules! wasm_slice_impl {
}
}

impl std::convert::TryFrom<wasm_bindgen::JsValue> for $name {
impl TryFrom<wasm_bindgen::JsValue> for $name {
type Error = &'static str;

fn try_from(value: wasm_bindgen::JsValue) -> Result<Self, Self::Error> {
value
.into_serde::<$name>()
.map_err(|_| "unable to deserialize value")
serde_wasm_bindgen::from_value(value).map_err(|_| "unable to deserialize value")
}
}
};
Expand Down
49 changes: 42 additions & 7 deletions tests/paillier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ fn b10(s: &str) -> BigNumber {
BigNumber::from_slice(bytes.as_slice())
}

#[test]
fn encrypt() {
let res = DecryptionKey::with_primes_unchecked(&b10(TEST_PRIMES[0]), &b10(TEST_PRIMES[1]));
assert!(res.is_some());
Expand Down Expand Up @@ -46,7 +45,6 @@ fn encrypt() {
}
}

#[test]
fn add() {
let res = DecryptionKey::with_primes_unchecked(&b10(TEST_PRIMES[0]), &b10(TEST_PRIMES[1]));
assert!(res.is_some());
Expand All @@ -73,7 +71,6 @@ fn add() {
assert_eq!(m3, BigNumber::from(13));
}

#[test]
fn mul() {
let res = DecryptionKey::with_primes_unchecked(&b10(TEST_PRIMES[0]), &b10(TEST_PRIMES[1]));
assert!(res.is_some());
Expand All @@ -97,7 +94,6 @@ fn mul() {
assert_eq!(m3, BigNumber::from(42));
}

#[test]
fn serialization() {
let res = DecryptionKey::with_primes_unchecked(&b10(TEST_PRIMES[2]), &b10(TEST_PRIMES[3]));
assert!(res.is_some());
Expand Down Expand Up @@ -131,7 +127,6 @@ fn serialization() {
assert_eq!(sk.n(), sk1.n());
}

#[test]
fn bytes() {
let res = DecryptionKey::with_primes_unchecked(&b10(TEST_PRIMES[2]), &b10(TEST_PRIMES[3]));
assert!(res.is_some());
Expand All @@ -154,7 +149,6 @@ fn bytes() {
assert_eq!(sk.n(), sk1.n());
}

#[test]
fn proof() {
use k256::elliptic_curve::group::prime::PrimeCurveAffine;

Expand Down Expand Up @@ -194,7 +188,6 @@ fn proof() {
assert!(res.is_err());
}

#[test]
fn all() {
let res = DecryptionKey::random();
assert!(res.is_some());
Expand Down Expand Up @@ -222,3 +215,45 @@ fn all() {
assert!(res.is_none());
}
}

#[cfg_attr(feature = "wasm", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn test_encrypt() {
encrypt()
}

#[cfg_attr(feature = "wasm", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn test_add() {
add()
}

#[cfg_attr(feature = "wasm", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn test_mul() {
mul()
}

#[cfg_attr(feature = "wasm", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn test_serialization() {
serialization()
}

#[cfg_attr(feature = "wasm", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn test_bytes() {
bytes()
}

#[cfg_attr(feature = "wasm", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn test_proof() {
proof()
}

#[cfg_attr(feature = "wasm", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn test_all() {
all()
}

0 comments on commit da3d259

Please sign in to comment.