diff --git a/Cargo.lock b/Cargo.lock index 5c1b76f..e26b171 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -176,6 +176,15 @@ dependencies = [ "generic-array 0.14.4", ] +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array 0.14.4", +] + [[package]] name = "block-modes" version = "0.7.0" @@ -260,7 +269,7 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4b9434b9a5aa1450faa3f9cb14ea0e8c53bb5d2b3c1bfd1ab4fc03e9f33fbfb0" dependencies = [ - "rustc_version", + "rustc_version 0.2.3", ] [[package]] @@ -275,7 +284,7 @@ dependencies = [ "quote", "serde", "serde_json", - "syn", + "syn 1.0.68", "tempfile", "toml", ] @@ -368,6 +377,15 @@ dependencies = [ "libc", ] +[[package]] +name = "cpufeatures" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a17b76ff3a4162b0b27f354a0c87015ddad39d35f9c0c36607a3bdd175dde1f1" +dependencies = [ + "libc", +] + [[package]] name = "criterion" version = "0.3.4" @@ -455,6 +473,16 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array 0.14.4", + "typenum", +] + [[package]] name = "crypto-mac" version = "0.8.0" @@ -524,6 +552,35 @@ dependencies = [ "zeroize", ] +[[package]] +name = "curve25519-dalek" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f711ade317dd348950a9910f81c5947e3d8907ebd2b83f76203ff1807e6a2bc2" +dependencies = [ + "cfg-if 1.0.0", + "cpufeatures 0.2.9", + "curve25519-dalek-derive", + "digest 0.10.7", + "fiat-crypto", + "platforms", + "rand_core 0.6.4", + "rustc_version 0.4.0", + "subtle", + "zeroize", +] + +[[package]] +name = "curve25519-dalek-derive" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83fdaf97f4804dcebfa5862639bc9ce4121e82140bec2a987ac5140294865b5b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.27", +] + [[package]] name = "digest" version = "0.6.2" @@ -551,6 +608,16 @@ dependencies = [ "generic-array 0.14.4", ] +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer 0.10.4", + "crypto-common", +] + [[package]] name = "ed25519" version = "1.0.3" @@ -694,7 +761,7 @@ checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.68", "synstructure", ] @@ -733,6 +800,29 @@ dependencies = [ "wedpr_l_utils 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ffi_java_ecdh_psi" +version = "0.1.0" +dependencies = [ + "jni", + "psi_utils", +] + +[[package]] +name = "ffi_wasm_ecdh_psi" +version = "0.1.0" +dependencies = [ + "getrandom 0.2.2", + "psi_utils", + "wasm-bindgen", +] + +[[package]] +name = "fiat-crypto" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e825f6987101665dea6ec934c09ec6d721de7bc1bf92248e1d5810c8cd636b77" + [[package]] name = "foreign-types" version = "0.3.2" @@ -808,8 +898,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]] @@ -1176,6 +1268,12 @@ version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" +[[package]] +name = "platforms" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3d7ddaed09e0eb771a79ab0fd64609ba0afb0a8366421957936ad14cbd13630" + [[package]] name = "plotters" version = "0.3.0" @@ -1212,11 +1310,11 @@ checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" [[package]] name = "proc-macro2" -version = "1.0.26" +version = "1.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a152013215dca273577e18d2bf00fa862b89b24169fb78c4c95aeb07992c9cec" +checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" dependencies = [ - "unicode-xid", + "unicode-ident", ] [[package]] @@ -1256,11 +1354,21 @@ dependencies = [ "tempfile", ] +[[package]] +name = "psi_utils" +version = "0.1.0" +dependencies = [ + "curve25519-dalek 4.0.0", + "rand 0.8.5", + "rand_core 0.6.4", + "sha2 0.10.7", +] + [[package]] name = "quote" -version = "1.0.7" +version = "1.0.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" +checksum = "50f3b39ccfb720540debaa0164757101c08ecb8d326b15358ce76a62c7e85965" dependencies = [ "proc-macro2", ] @@ -1322,14 +1430,13 @@ dependencies = [ [[package]] name = "rand" -version = "0.8.3" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ "libc", "rand_chacha 0.3.0", - "rand_core 0.6.2", - "rand_hc 0.3.0", + "rand_core 0.6.4", ] [[package]] @@ -1359,7 +1466,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" dependencies = [ "ppv-lite86", - "rand_core 0.6.2", + "rand_core 0.6.4", ] [[package]] @@ -1388,9 +1495,9 @@ dependencies = [ [[package]] name = "rand_core" -version = "0.6.2" +version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" dependencies = [ "getrandom 0.2.2", ] @@ -1413,15 +1520,6 @@ dependencies = [ "rand_core 0.5.1", ] -[[package]] -name = "rand_hc" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" -dependencies = [ - "rand_core 0.6.2", -] - [[package]] name = "rand_isaac" version = "0.1.1" @@ -1589,7 +1687,16 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" dependencies = [ - "semver", + "semver 0.9.0", +] + +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver 1.0.18", ] [[package]] @@ -1619,7 +1726,7 @@ version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4124a35fe33ae14259c490fd70fa199a32b9ce9502f2ee6bc4f81ec06fa65894" dependencies = [ - "rand 0.8.3", + "rand 0.8.5", "secp256k1-sys", ] @@ -1655,6 +1762,12 @@ dependencies = [ "semver-parser", ] +[[package]] +name = "semver" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0293b4b29daaf487284529cc2f5675b8e57c61f70167ba415a463651fd6a918" + [[package]] name = "semver-parser" version = "0.7.0" @@ -1688,7 +1801,7 @@ checksum = "552954ce79a059ddd5fd68c271592374bd15cab2274970380c000118aeffe1cd" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.68", ] [[package]] @@ -1723,11 +1836,22 @@ checksum = "b362ae5752fd2137731f9fa25fd4d9058af34666ca1966fb969119cc35719f12" dependencies = [ "block-buffer 0.9.0", "cfg-if 1.0.0", - "cpufeatures", + "cpufeatures 0.1.5", "digest 0.9.0", "opaque-debug 0.3.0", ] +[[package]] +name = "sha2" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479fb9d862239e610720565ca91403019f2f00410f1864c5aa7479b950a76ed8" +dependencies = [ + "cfg-if 1.0.0", + "cpufeatures 0.2.9", + "digest 0.10.7", +] + [[package]] name = "sha3" version = "0.6.0" @@ -1821,6 +1945,17 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "syn" +version = "2.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b60f673f44a8255b9c8c657daf66a596d435f2da81a555b06dc644d080ba45e0" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "synstructure" version = "0.12.4" @@ -1829,7 +1964,7 @@ checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.68", "unicode-xid", ] @@ -1873,7 +2008,7 @@ checksum = "9ba20f23e85b10754cd195504aebf6a27e2e6cbe28c17778a0c930724628dd56" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.68", ] [[package]] @@ -1897,9 +2032,15 @@ dependencies = [ [[package]] name = "typenum" -version = "1.12.0" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" + +[[package]] +name = "unicode-ident" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33" +checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" [[package]] name = "unicode-width" @@ -1990,7 +2131,7 @@ dependencies = [ "log", "proc-macro2", "quote", - "syn", + "syn 1.0.68", "wasm-bindgen-shared", ] @@ -2012,7 +2153,7 @@ checksum = "d53739ff08c8a68b0fdbcd54c372b8ab800b1449ab3c9d706503bc7dd1621b2c" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.68", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -2284,7 +2425,7 @@ version = "1.0.0" dependencies = [ "aes", "block-modes", - "rand 0.8.3", + "rand 0.8.5", "wedpr_l_macros 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "wedpr_l_utils 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2297,7 +2438,7 @@ checksum = "a95a60c1f481a6d8303dd052437738720676de98c3771691df67d07e2c1115f2" dependencies = [ "aes", "block-modes", - "rand 0.8.3", + "rand 0.8.5", "wedpr_l_macros 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "wedpr_l_utils 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2307,7 +2448,7 @@ name = "wedpr_l_crypto_block_cipher_sm4" version = "1.0.0" dependencies = [ "block-modes", - "rand 0.8.3", + "rand 0.8.5", "sm4", "wedpr_l_macros 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "wedpr_l_utils 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2320,7 +2461,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b2cb1235aa887c4f65d160d33fab200382edfd596732b86b50334ebe444abdbd" dependencies = [ "block-modes", - "rand 0.8.3", + "rand 0.8.5", "sm4", "wedpr_l_macros 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "wedpr_l_utils 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2714,6 +2855,6 @@ checksum = "c3f369ddb18862aba61aa49bf31e74d29f0f162dec753063200e1dc084345d16" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.68", "synstructure", ] diff --git a/Cargo.toml b/Cargo.toml index 767d4d4..09b917f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,4 +34,7 @@ members = [ "protos", "third_party/fisco_bcos", "third_party/fisco_bcos_java_sdk", + "third_party/ecdh_psi/psi_utils", + "third_party/ecdh_psi/ffi_java_ecdh_psi", + "third_party/ecdh_psi/ffi_wasm_ecdh_psi", ] diff --git a/crypto/signature/secp256k1/benches/secp256k1.rs b/crypto/signature/secp256k1/benches/secp256k1.rs index 7cd7e75..073aafa 100644 --- a/crypto/signature/secp256k1/benches/secp256k1.rs +++ b/crypto/signature/secp256k1/benches/secp256k1.rs @@ -1,16 +1,16 @@ // Copyright 2021 WeDPR Lab Project Authors. Licensed under Apache-2.0. use criterion::Criterion; -use wedpr_l_crypto_signature_secp256k1::{WedprSecp256k1Recover}; -use wedpr_l_utils::traits::Signature; -use wedpr_l_utils::constant::tests::BASE64_ENCODED_TEST_MESSAGE; +use wedpr_l_crypto_signature_secp256k1::WedprSecp256k1Recover; +use wedpr_l_utils::{ + constant::tests::BASE64_ENCODED_TEST_MESSAGE, traits::Signature, +}; #[macro_use] extern crate criterion; fn create_sign_helper(c: &mut Criterion, message_size: usize) { - let label = - format!("create_sign_helper, message_size = {}", message_size); + let label = format!("create_sign_helper, message_size = {}", message_size); let secp256k1 = WedprSecp256k1Recover::default(); let (pk_b, sk_b) = secp256k1.generate_keypair(); @@ -26,8 +26,7 @@ fn create_sign_helper(c: &mut Criterion, message_size: usize) { fn create_verify_helper(c: &mut Criterion, message_size: usize) { let label = format!("create_verify_helper, message_size = {}", message_size); - let label = - format!("create_sign_helper, message_size = {}", message_size); + let label = format!("create_sign_helper, message_size = {}", message_size); let secp256k1 = WedprSecp256k1Recover::default(); let (pk_b, sk_b) = secp256k1.generate_keypair(); @@ -35,11 +34,9 @@ fn create_verify_helper(c: &mut Criterion, message_size: usize) { let sign_obj = secp256k1.sign(&sk_b, &message.to_vec()).unwrap(); - c.bench_function(&label, move |b| { b.iter(|| { - let _ = secp256k1 - .recover_public_key(&message.to_vec(), &sign_obj); + let _ = secp256k1.recover_public_key(&message.to_vec(), &sign_obj); }) }); } diff --git a/crypto/signature/secp256k1/src/lib.rs b/crypto/signature/secp256k1/src/lib.rs index afbe54b..78ad3ad 100644 --- a/crypto/signature/secp256k1/src/lib.rs +++ b/crypto/signature/secp256k1/src/lib.rs @@ -9,11 +9,11 @@ extern crate lazy_static; extern crate secp256k1; use secp256k1::{ + ecdsa::{RecoverableSignature, RecoveryId}, + rand::rngs::OsRng, All, Message, PublicKey, Secp256k1, SecretKey, VerifyOnly, }; -use secp256k1::ecdsa::{RecoverableSignature, RecoveryId}; use wedpr_l_utils::{error::WedprError, traits::Signature}; -use secp256k1::rand::rngs::OsRng; lazy_static! { // Shared secp256k1 instance initialized for verification function only. @@ -22,7 +22,6 @@ lazy_static! { static ref SECP256K1_ALL: Secp256k1 = Secp256k1::new(); } - /// Implements FISCO-BCOS-compatible Secp256k1 as a Signature instance. #[derive(Default, Debug, Clone, Copy)] pub struct WedprSecp256k1Recover {} @@ -99,7 +98,6 @@ impl Signature for WedprSecp256k1Recover { fn generate_keypair(&self) -> (Vec, Vec) { loop { - let (secret_key, public_key) = SECP256K1_ALL.generate_keypair(&mut OsRng); @@ -155,14 +153,15 @@ impl WedprSecp256k1Recover { return Err(WedprError::FormatError); }, }; - let recovered_public_key = - match SECP256K1_VERIFY.recover_ecdsa(&msg_hash_obj, &get_sign_final) { - Ok(v) => v, - Err(_) => { - wedpr_println!("Signature recover failed"); - return Err(WedprError::FormatError); - }, - }; + let recovered_public_key = match SECP256K1_VERIFY + .recover_ecdsa(&msg_hash_obj, &get_sign_final) + { + Ok(v) => v, + Err(_) => { + wedpr_println!("Signature recover failed"); + return Err(WedprError::FormatError); + }, + }; return Ok(recovered_public_key.serialize_uncompressed().to_vec()); } diff --git a/protos/src/generated/mod.rs b/protos/src/generated/mod.rs index 6094c5b..b79093b 100644 --- a/protos/src/generated/mod.rs +++ b/protos/src/generated/mod.rs @@ -2,4 +2,4 @@ pub mod common; pub mod ot; -pub mod zkp; \ No newline at end of file +pub mod zkp; diff --git a/third_party/ecdh_psi/ffi_java_ecdh_psi/Cargo.toml b/third_party/ecdh_psi/ffi_java_ecdh_psi/Cargo.toml new file mode 100644 index 0000000..2e83eac --- /dev/null +++ b/third_party/ecdh_psi/ffi_java_ecdh_psi/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "ffi_java_ecdh_psi" +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_java_ecdh_psi" +crate-type = [ "cdylib", "staticlib" ] + +[dependencies] +jni = "0.13.0" +psi_utils = { path = "../psi_utils"} \ No newline at end of file diff --git a/third_party/ecdh_psi/ffi_java_ecdh_psi/src/lib.rs b/third_party/ecdh_psi/ffi_java_ecdh_psi/src/lib.rs new file mode 100644 index 0000000..1554822 --- /dev/null +++ b/third_party/ecdh_psi/ffi_java_ecdh_psi/src/lib.rs @@ -0,0 +1,95 @@ +extern crate jni; + +use psi_utils::{ + hash_to_curve, point_scalar_multi, random_scalar, scalar_inverse, +}; + +use jni::{objects::JClass, sys::jbyteArray, JNIEnv}; + +// 导出函数给JNI接口调用 + +#[no_mangle] +pub extern "system" fn Java_com_webank_wedpr_crypto_NativeInterface_randomScalar( + env: JNIEnv, + _class: JClass, +) -> jbyteArray { + // 调用原始函数 + let result = random_scalar(); + + // 将 Vec 转换成 jbyteArray 并返回给Java层 + match env.byte_array_from_slice(&result) { + Ok(array) => array, + Err(_) => env.new_byte_array(0).unwrap(), // 返回空的 jbyteArray + } +} + +#[no_mangle] +pub extern "system" fn Java_com_webank_wedpr_crypto_NativeInterface_hashToCurve( + env: JNIEnv, + _class: JClass, + message: jbyteArray, +) -> jbyteArray { + // 将 jbyteArray 转换成 Vec + let message_bytes = match env.convert_byte_array(message) { + Ok(bytes) => bytes, + Err(_) => return env.new_byte_array(0).unwrap(), /* 返回空的 jbyteArray */ + }; + + // 调用原始函数 + let result = hash_to_curve(&message_bytes); + + // 将 Vec 转换成 jbyteArray 并返回给Java层 + match env.byte_array_from_slice(&result) { + Ok(array) => array, + Err(_) => env.new_byte_array(0).unwrap(), // 返回空的 jbyteArray + } +} + +#[no_mangle] +pub extern "system" fn Java_com_webank_wedpr_crypto_NativeInterface_scalarInverse( + env: JNIEnv, + _class: JClass, + scalar: jbyteArray, +) -> jbyteArray { + // 将 jbyteArray 转换成 Vec + let scalar_bytes = match env.convert_byte_array(scalar) { + Ok(bytes) => bytes, + Err(_) => return env.new_byte_array(0).unwrap(), /* 返回空的 jbyteArray */ + }; + + // 调用原始函数 + let result = scalar_inverse(&scalar_bytes); + + // 将 Vec 转换成 jbyteArray 并返回给Java层 + match env.byte_array_from_slice(&result) { + Ok(array) => array, + Err(_) => env.new_byte_array(0).unwrap(), // 返回空的 jbyteArray + } +} + +#[no_mangle] +pub extern "system" fn Java_com_webank_wedpr_crypto_NativeInterface_pointScalarMulti( + env: JNIEnv, + _class: JClass, + point: jbyteArray, + scalar: jbyteArray, +) -> jbyteArray { + // 将 jbyteArray 转换成 Vec + let point_bytes = match env.convert_byte_array(point) { + Ok(bytes) => bytes, + Err(_) => return env.new_byte_array(0).unwrap(), /* 返回空的 jbyteArray */ + }; + let scalar_bytes = match env.convert_byte_array(scalar) { + Ok(bytes) => bytes, + Err(_) => return env.new_byte_array(0).unwrap(), /* 返回空的 jbyteArray */ + }; + + // 调用原始函数 + let result = point_scalar_multi(&point_bytes, &scalar_bytes); + + // 将 Vec 转换成 jbyteArray 并返回给Java层 + match env.byte_array_from_slice(&result) { + Ok(array) => array, + Err(_) => env.new_byte_array(0).unwrap(), // 返回空的 jbyteArray + } +} diff --git a/third_party/ecdh_psi/ffi_wasm_ecdh_psi/Cargo.toml b/third_party/ecdh_psi/ffi_wasm_ecdh_psi/Cargo.toml new file mode 100644 index 0000000..fcf5c79 --- /dev/null +++ b/third_party/ecdh_psi/ffi_wasm_ecdh_psi/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "ffi_wasm_ecdh_psi" +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_ecdh_psi" +crate-type = ["rlib", "cdylib"] + +[dependencies] +wasm-bindgen = "0.2" +getrandom = { version = "0.2", features = ["js"] } +psi_utils = { path = "../psi_utils"} diff --git a/third_party/ecdh_psi/ffi_wasm_ecdh_psi/readme.md b/third_party/ecdh_psi/ffi_wasm_ecdh_psi/readme.md new file mode 100644 index 0000000..0fc75a1 --- /dev/null +++ b/third_party/ecdh_psi/ffi_wasm_ecdh_psi/readme.md @@ -0,0 +1,17 @@ +# 编译生成wasm + +`wasm-pack build`命令可以使用不同的目标参数,用于生成不同平台和环境可用的 WebAssembly(Wasm)模块。以下是`wasm-pack`库当前版本(v0.10.0)中支持的目标选项: + +1. `bundler`(默认):生成可以在现代浏览器和支持 ES6 模块的环境中使用的 Wasm 模块,打包为单个文件。 + +2. `web`:生成可以在现代浏览器中直接使用的 Wasm 模块,打包为单个文件。 + +3. `no-modules`:生成不依赖 ES6 模块的 Wasm 输出,适用于在没有模块系统的环境下使用。 + +4. `nodejs`:生成可以在 Node.js 环境中使用的 Wasm 模块,使用 CommonJS 模块进行导出。 + +5. `webworker`:生成用于 Web Worker 的 Wasm 模块,打包为单个文件。 + +6. `nodejs-esm`:生成可以在支持 ES6 模块的 Node.js 环境中使用的 Wasm 模块。 + +你可以根据你的需求,选择合适的构建目标,以便在不同的环境中正确地使用和部署生成的 Wasm 模块。使用对应的`--target`选项来选择特定的目标。例如:`wasm-pack build --target web`将生成支持现代浏览器的 Wasm 模块。 \ No newline at end of file diff --git a/third_party/ecdh_psi/ffi_wasm_ecdh_psi/src/lib.rs b/third_party/ecdh_psi/ffi_wasm_ecdh_psi/src/lib.rs new file mode 100644 index 0000000..3c1605a --- /dev/null +++ b/third_party/ecdh_psi/ffi_wasm_ecdh_psi/src/lib.rs @@ -0,0 +1,26 @@ +extern crate wasm_bindgen; + +use psi_utils::{ + hash_to_curve, point_scalar_multi, random_scalar, scalar_inverse, +}; +use wasm_bindgen::prelude::wasm_bindgen; + +#[wasm_bindgen] +pub fn wasm_scalar_inverse(scalar: &[u8]) -> Vec { + scalar_inverse(scalar) +} + +#[wasm_bindgen] +pub fn wasm_point_scalar_multi(point: &[u8], scalar: &[u8]) -> Vec { + point_scalar_multi(point, scalar) +} + +#[wasm_bindgen] +pub fn wasm_hash_to_curve(message: &[u8]) -> Vec { + hash_to_curve(message) +} + +#[wasm_bindgen] +pub fn wasm_random_scalar() -> Vec { + random_scalar() +} diff --git a/third_party/ecdh_psi/psi_utils/Cargo.toml b/third_party/ecdh_psi/psi_utils/Cargo.toml new file mode 100644 index 0000000..d0786f0 --- /dev/null +++ b/third_party/ecdh_psi/psi_utils/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "psi_utils" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + + +[dependencies] +sha2 = "0.10.7" +rand = "0.8.4" +rand_core = "0.6.3" +curve25519-dalek = { version = "4", features = [ "digest" , "rand_core"] } diff --git a/third_party/ecdh_psi/psi_utils/src/lib.rs b/third_party/ecdh_psi/psi_utils/src/lib.rs new file mode 100644 index 0000000..243a79a --- /dev/null +++ b/third_party/ecdh_psi/psi_utils/src/lib.rs @@ -0,0 +1,97 @@ +use sha2::Sha512; +// use sha2::Digest; +use rand::rngs::ThreadRng; +// use rand::RngCore; +use curve25519_dalek::{ + edwards::{CompressedEdwardsY, EdwardsPoint}, + Scalar, +}; + +const SCALAR_SIZE: usize = 32; +const POINT_SIZE: usize = 32; + +pub fn random_scalar() -> Vec { + // 创建一个随机数生成器 + let mut rng: ThreadRng = rand::thread_rng(); + + // 生成一个随机的 Scalar + let scalar = Scalar::random(&mut rng); + + // 将 Scalar 转换成 &[u8] + scalar.to_bytes().to_vec() +} + +pub fn hash_to_curve(message: &[u8]) -> Vec { + let hash_scalar = Scalar::hash_from_bytes::(message); + let hash_point = EdwardsPoint::mul_base(&hash_scalar); + return hash_point.compress().to_bytes().to_vec(); +} + +pub fn scalar_inverse(scalar: &[u8]) -> Vec { + // 检查输入切片是否具有正确的大小 + if scalar.len() != SCALAR_SIZE { + return Vec::new(); // 如果大小不正确,返回空的 Vec + } + + // 将输入 &[u8] 转换成 Scalar + let mut scalar_bytes = [0u8; SCALAR_SIZE]; + scalar_bytes.copy_from_slice(scalar); + let scalar = Scalar::from_bytes_mod_order(scalar_bytes); + let inverse_scalar = scalar.invert(); + return inverse_scalar.to_bytes().to_vec(); +} + +pub fn point_scalar_multi(point: &[u8], scalar: &[u8]) -> Vec { + // 检查输入切片是否具有正确的大小 + if point.len() != POINT_SIZE || scalar.len() != SCALAR_SIZE { + return Vec::new(); // 如果大小不正确,返回空的 Vec + } + + // 将输入 &[u8] 转换成 CompressedEdwardsY 表示的点 + let mut point_bytes = [0u8; POINT_SIZE]; + point_bytes.copy_from_slice(point); + let compressed_point = match CompressedEdwardsY(point_bytes).decompress() { + Some(point) => point, + None => return Vec::new(), // 解析点失败,返回空的 Vec + }; + + // 将输入 &[u8] 转换成 Scalar + let mut scalar_bytes = [0u8; SCALAR_SIZE]; + scalar_bytes.copy_from_slice(scalar); + let scalar = Scalar::from_bytes_mod_order(scalar_bytes); + + // 执行点乘操作 + let result_point = compressed_point * scalar; + + // 将结果转换成压缩格式的点 + let compressed_result = result_point.compress(); + + // 将结果转换成 &[u8] + compressed_result.as_bytes().to_vec() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_flow() { + // 生成一个随机的标量 + let random_scalar = random_scalar(); + println!("Random Scalar: {:?}", random_scalar); + + // 定义一个消息,对其进行哈希并生成哈希点 + let message = "To really appreciate architecture, you may even need \ + to commit a murder"; + let hash_point = hash_to_curve(message.as_bytes()); + + // 定义一个标量并计算其逆元 + let inverse_scalar = scalar_inverse(&random_scalar); + + // 定义一个点和标量,并进行点乘操作 + let point_mul_result = point_scalar_multi(&hash_point, &random_scalar); + let point_mul_result2 = + point_scalar_multi(&point_mul_result, &inverse_scalar); + assert_eq!(point_mul_result2, hash_point); + } +}