diff --git a/Cargo.lock b/Cargo.lock index 34a0911..1142c74 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -770,6 +770,16 @@ dependencies = [ "wedpr_l_utils 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ffi_wasm_web_equlity" +version = "0.1.0" +dependencies = [ + "getrandom 0.2.2", + "hex", + "wasm-bindgen", + "wedpr_bls12_381", +] + [[package]] name = "foreign-types" version = "0.3.2" @@ -851,8 +861,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" dependencies = [ "cfg-if 1.0.0", + "js-sys", "libc", "wasi 0.10.2+wasi-snapshot-preview1", + "wasm-bindgen", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 9b22dc4..e656f09 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ members = [ "ffi/ffi_c/ffi_c_fisco_bcos", "ffi/ffi_c/ffi_c_zkp", "ffi/ffi_c/ffi_c_equality", + "ffi/ffi_wasm/ffi_wasm_web_equlity", "ffi/ffi_common", "ffi/ffi_java/ffi_java_crypto", "ffi/ffi_java/ffi_java_crypto_binary", diff --git a/crypto/pairing/bls12_381/src/lib.rs b/crypto/pairing/bls12_381/src/lib.rs index a296b23..6922595 100644 --- a/crypto/pairing/bls12_381/src/lib.rs +++ b/crypto/pairing/bls12_381/src/lib.rs @@ -117,6 +117,19 @@ mod tests { use crate::*; use hex; + #[test] + fn test_complex_word_cm() { + let id1 = hex::decode("f0a59684f0acba93").unwrap(); + let web_cipher = hex::decode("830e9f65febccc7a3baba23b3216b3ee23003b57d95b834afbb0405312ce93306c9011973c4b278f080d62c822bd0bd699dedef98481ad3114de742cbc4c58078b67f56f037b112d4759090cd400e26a97cb14ca4bc03b9f87f8d663bd8f107308b4cfba32849d031bb045db5fb282a1cfa3c9ff3cac1eea4407c89a213fdda7f2528a40268fbd919c9f8abf54734893").unwrap(); + let cipher1_m1_recover = + WedprBls128Cipher::from_bytes(&web_cipher).unwrap(); + let cipher1_m1 = encrypt_message(&id1); + let cipher1_m2 = encrypt_message(&id1); + assert_eq!(equality_test(&cipher1_m1, &cipher1_m2), true); + assert_eq!(equality_test(&cipher1_m1, &cipher1_m1_recover), true); + + } + #[test] fn test_equality() { let message1: &[u8] = b"hello world"; @@ -200,14 +213,15 @@ mod tests { #[test] fn test_from_web() { - let message_hello = b"abc"; + // let message_hello = b"𥖄𬺓"; + let message_hello = &hex::decode("e6ada6e6b189").unwrap(); // let message_hello:Vec = vec![96,97,98,0,0,0,0,0]; - wedpr_println!("message_hello:{:?}", message_hello); + wedpr_println!("message:{:?}", message_hello); // let message_wrong = b"wrong"; let cipher1_hello = encrypt_message(message_hello); // let cipher1_wrong = encrypt_message(message_wrong); - let web_hello1 = "a91c14d9111e95206e5be9e644f68dd6ddb5865da5bfab80d4c7fb7a1a1299db401e200bb401adc87da751283dd8cb2984820a100894e50583daec6261bd8728d6c80689fa02d8f3e7fae23329755229c98456db47f78071e03dd4344e7e8dc3029bf4809a8fc3198dcd4c6810465cbc42fc57329b1d980f70b2a383507c257e35f33f2851e0ab2a40bb24606900c152"; + let web_hello1 = "80f26419aa4ab90def42ef065a1f3ee73fc4b1d7b9a28c3e886082641d1f144378df52b27f47eeb74e275c29e9a04a00ac06154bf0c7c741bdd4f65e22c156c2f5e7a0779e2fa51a461307886c6c6443952e76c54ea46106eef58fe3151aa9f20c52c47263b061b611921cdaabfc686cd79f41b42ab303c74396f60d64287d7e452fe50576e6550a9ff77493446f41ef"; let web_bytes1 = hex::decode(web_hello1).unwrap(); // let web_hello2 = diff --git a/crypto/pairing/bls12_381/src/peks.rs b/crypto/pairing/bls12_381/src/peks.rs index 840d08a..4c6ded4 100644 --- a/crypto/pairing/bls12_381/src/peks.rs +++ b/crypto/pairing/bls12_381/src/peks.rs @@ -229,6 +229,15 @@ mod tests { use wedpr_l_common_coder_base64::WedprBase64; use wedpr_l_utils::traits::Coder; + #[test] + fn test_complex_word() { + let id1 = hex::decode("f0a59684f0acba93").unwrap(); + let key1 = generate_key(); + let cipher_id1 = encrypt_message(&id1, &key1.pk); + let trapdoor1 = trapdoor(&id1, &key1.sk); + assert_eq!(trapdoor_test(&cipher_id1, &trapdoor1), true); + } + #[test] fn test_peks() { let id1 = "zhangsan".as_bytes(); diff --git a/ffi/ffi_wasm/ffi_wasm_web_equlity/Cargo.toml b/ffi/ffi_wasm/ffi_wasm_web_equlity/Cargo.toml new file mode 100644 index 0000000..4f0452f --- /dev/null +++ b/ffi/ffi_wasm/ffi_wasm_web_equlity/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "ffi_wasm_web_equlity" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +name = "ffi_wasm_equality" +crate-type = ["rlib", "cdylib"] + +[dependencies] +wasm-bindgen = "0.2" +hex = "0.4.2" +wedpr_bls12_381 = { path = "../../../crypto/pairing/bls12_381" } +getrandom = { version = "0.2", features = ["js"] } \ No newline at end of file diff --git a/ffi/ffi_wasm/ffi_wasm_web_equlity/src/lib.rs b/ffi/ffi_wasm/ffi_wasm_web_equlity/src/lib.rs new file mode 100644 index 0000000..e4d5980 --- /dev/null +++ b/ffi/ffi_wasm/ffi_wasm_web_equlity/src/lib.rs @@ -0,0 +1,13 @@ +use wasm_bindgen::prelude::*; +extern crate wedpr_bls12_381; + + +#[wasm_bindgen] +pub fn encrypt_message(message: &str) -> String { + let message_bytes = match hex::decode(message) { + Ok(v) => v, + Err(_) => return "".to_string(), + }; + let result = wedpr_bls12_381::encrypt_message(&message_bytes); + hex::encode(result.to_bytes()) +} \ No newline at end of file