Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WASM support #14

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
498 changes: 477 additions & 21 deletions Cargo.lock

Large diffs are not rendered by default.

23 changes: 21 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ license = "GPL-3.0-or-later"
[lib]
crate-type = ["staticlib", "lib"]

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
rand = "0.6.5"

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "0.2"
rand = { version="0.6.5", features = ["wasm-bindgen"] }

[dependencies]
rand = "0.6"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
Expand All @@ -25,10 +30,24 @@ num-bigint = {version = "0.2.2" , features = ["serde","rand"]}
num-integer = "0.1"
num-traits = "0.2.6"


bit-vec = "0.5"
cryptoxide = "0.1.1"
zeroize = "0.5.2"

libsecp256k1 = "0.2.2"

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = "0.2"

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
criterion = "0.2"

[[bench]]
name = "keygen"
path = "tests/keygen.rs"
harness = false

[[bench]]
name = "sign"
path = "tests/sign.rs"
harness = false
1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
theme: jekyll-theme-cayman
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ mod tests {
use curv::arithmetic::traits::Samplable;
use num_traits::{One, Zero};

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn test_bit_length_create_commitment() {
let hex_len = SECURITY_BITS;
Expand All @@ -85,6 +89,7 @@ mod tests {
assert!(ctr_blind_len / sample_size > 0.3);
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn test_bit_length_create_commitment_with_user_defined_randomness() {
let message = BigInt::sample(SECURITY_BITS);
Expand All @@ -94,6 +99,7 @@ mod tests {
assert_eq!(commitment2.to_str_radix(16).len(), SECURITY_BITS / 4);
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn test_random_num_generation_create_commitment_with_user_defined_randomness() {
let message = BigInt::sample(SECURITY_BITS);
Expand All @@ -103,6 +109,7 @@ mod tests {
assert_eq!(commitment, commitment2);
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn test_hashing_create_commitment_with_user_defined_randomness() {
let mut digest = Sha3::sha3_256();
Expand Down
6 changes: 6 additions & 0 deletions src/curv/cryptographic_primitives/hashing/hash_sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ mod tests {
use curv::elliptic::curves::traits::ECPoint;
use curv::elliptic::curves::traits::ECScalar;
use num_traits::{One, Zero};

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
// Very basic test here, TODO: suggest better testing
fn create_hash_test() {
Expand All @@ -60,6 +65,7 @@ mod tests {
assert!(result > BigInt::zero());
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn create_hash_from_ge_test() {
let point = GE::base_point2();
Expand Down
4 changes: 3 additions & 1 deletion src/curv/cryptographic_primitives/proofs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use std::fmt;
pub mod sigma_correct_homomorphic_elgamal_enc;
pub mod sigma_dlog;

pub const PROOF_ERROR_DESCRIPTION: &str = "Error while verifying";

#[derive(Debug, Clone, Copy)]
pub struct ProofError;

Expand All @@ -22,6 +24,6 @@ impl fmt::Display for ProofError {

impl Error for ProofError {
fn description(&self) -> &str {
"Error while verifying"
PROOF_ERROR_DESCRIPTION
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,14 @@ impl HomoELGamalProof {
#[cfg(test)]
mod tests {
use curv::cryptographic_primitives::proofs::sigma_correct_homomorphic_elgamal_enc::*;
use curv::cryptographic_primitives::proofs::PROOF_ERROR_DESCRIPTION;
use curv::elliptic::curves::secp256_k1::{FE, GE};
use std::error::Error;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn test_correct_general_homo_elgamal() {
let witness = HomoElGamalWitness {
Expand All @@ -110,6 +117,7 @@ mod tests {
assert!(proof.verify(&delta).is_ok());
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn test_correct_homo_elgamal() {
let witness = HomoElGamalWitness {
Expand All @@ -132,8 +140,8 @@ mod tests {
assert!(proof.verify(&delta).is_ok());
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
#[should_panic]
fn test_wrong_homo_elgamal() {
// test for E = (r+1)G
let witness = HomoElGamalWitness {
Expand All @@ -149,7 +157,7 @@ mod tests {
let E = &G * &witness.r + G.clone();
let delta = HomoElGamalStatement { G, H, Y, D, E };
let proof = HomoELGamalProof::prove(&witness, &delta);
assert!(proof.verify(&delta).is_ok());
let result = proof.verify(&delta);
assert_eq!(result.unwrap_err().description(), PROOF_ERROR_DESCRIPTION);
}

}
4 changes: 4 additions & 0 deletions src/curv/cryptographic_primitives/proofs/sigma_dlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ mod tests {
use curv::cryptographic_primitives::proofs::sigma_dlog::*;
use curv::elliptic::curves::secp256_k1::FE;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn test_dlog_proof() {
let witness: FE = ECScalar::new_random();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ mod tests {
use curv::cryptographic_primitives::secret_sharing::feldman_vss::*;
use curv::elliptic::curves::secp256_k1::{FE, GE};

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn test_secret_sharing_3_out_of_5_at_indices() {
let secret: FE = ECScalar::new_random();
Expand All @@ -261,6 +265,7 @@ mod tests {
assert_eq!(secret, secret_reconstructed);
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn test_secret_sharing_3_out_of_5() {
let secret: FE = ECScalar::new_random();
Expand Down Expand Up @@ -303,6 +308,7 @@ mod tests {
assert_eq!(w, secret_reconstructed);
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn test_secret_sharing_3_out_of_7() {
let secret: FE = ECScalar::new_random();
Expand Down Expand Up @@ -340,6 +346,7 @@ mod tests {
assert_eq!(w, secret_reconstructed);
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn test_secret_sharing_1_out_of_2() {
let secret: FE = ECScalar::new_random();
Expand Down Expand Up @@ -369,6 +376,7 @@ mod tests {
assert_eq!(w, secret_reconstructed);
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn test_secret_sharing_1_out_of_3() {
let secret: FE = ECScalar::new_random();
Expand Down
26 changes: 18 additions & 8 deletions src/curv/elliptic/curves/secp256_k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@
//

use super::rand::{thread_rng, Rng};
// use super::secp256k1::util::{
// // CURVE_ORDER,
// // GENERATOR_X,
// // GENERATOR_Y,
// SECRET_KEY_SIZE,
// RAW_PUBLIC_KEY_SIZE,
// };
use super::secp256k1::{PublicKey, SecretKey};
use super::traits::{ECPoint, ECScalar};
use curv::arithmetic::num_bigint::from;
Expand Down Expand Up @@ -606,6 +599,11 @@ mod tests {
use curv::elliptic::curves::traits::ECScalar;
use serde_json;
use ErrorKey;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn serialize_sk() {
let scalar: Secp256k1Scalar = ECScalar::from(&BigInt::from(123456 as u32));
Expand All @@ -614,6 +612,7 @@ mod tests {
assert_eq!(s, "\"1e240\"");
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn serialize_rand_pk_verify_pad() {
let vx = BigInt::from_hex(
Expand Down Expand Up @@ -643,6 +642,7 @@ mod tests {
assert_eq!(r.y_coor().unwrap(), r_expected.y_coor().unwrap());
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn deserialize_sk() {
let s = "\"1e240\"";
Expand All @@ -653,6 +653,7 @@ mod tests {
assert_eq!(dummy, sk);
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn serialize_pk() {
let pk = Secp256k1Point::generator();
Expand All @@ -667,6 +668,7 @@ mod tests {
assert_eq!(des_pk.ge, pk.ge);
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn test_serdes_pk() {
let pk = GE::generator();
Expand All @@ -680,8 +682,9 @@ mod tests {
assert_eq!(des_pk, pk);
}

#[cfg_attr(target_arch = "wasm32", ignore)] //TODO: switch `ignore` to `wasm_bindgen_test` once wasm can catch
#[cfg_attr(not(target_arch = "wasm32"), should_panic)]
#[test]
#[should_panic]
fn test_serdes_bad_pk() {
let pk = GE::generator();
let s = serde_json::to_string(&pk).expect("Failed in serialization");
Expand All @@ -691,6 +694,7 @@ mod tests {
assert_eq!(des_pk, pk);
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn test_from_bytes() {
let g = Secp256k1Point::generator();
Expand All @@ -700,6 +704,7 @@ mod tests {
assert_eq!(result.unwrap_err(), ErrorKey::InvalidPublicKey)
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn test_from_bytes_3() {
let test_vec = [
Expand All @@ -710,6 +715,7 @@ mod tests {
assert!(result.is_ok() | result.is_err())
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn test_from_bytes_4() {
let test_vec = [
Expand All @@ -719,6 +725,7 @@ mod tests {
assert!(result.is_ok() | result.is_err())
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn test_from_bytes_5() {
let test_vec = [
Expand All @@ -731,6 +738,7 @@ mod tests {
assert!(result.is_ok() | result.is_err())
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn test_minus_point() {
let a: FE = ECScalar::new_random();
Expand All @@ -749,6 +757,7 @@ mod tests {
assert_eq!(point_ab1.get_element(), point_ab2.get_element());
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn test_invert() {
let a: FE = ECScalar::new_random();
Expand All @@ -759,6 +768,7 @@ mod tests {
assert_eq!(a_inv_bn_1, a_inv_bn_2);
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn test_scalar_mul_scalar() {
let a: FE = ECScalar::new_random();
Expand Down
1 change: 0 additions & 1 deletion src/gg_2018/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@

pub mod mta;
pub mod party_i;
pub mod test;
Loading