From ddc11051e20e77321572cdd15d98d0ec2302b83c Mon Sep 17 00:00:00 2001 From: shareong <740310627@qq.com> Date: Wed, 20 Sep 2023 16:01:43 +0800 Subject: [PATCH] optimize code structure --- Cargo.lock | 28 ++-- Cargo.toml | 6 +- .../ecc/ecc_ed25519}/Cargo.toml | 2 +- crypto/ecc/ecc_ed25519/src/lib.rs | 58 +++++++++ .../ecc/ffi_java_ecc_ed25519}/Cargo.toml | 6 +- .../ecc/ffi_java_ecc_ed25519}/src/lib.rs | 26 +--- .../ecc/ffi_wasm_ecc_ed25519}/Cargo.toml | 6 +- .../ecc/ffi_wasm_ecc_ed25519}/readme.md | 0 .../ecc/ffi_wasm_ecc_ed25519}/src/lib.rs | 9 +- third_party/ecdh_psi/psi_utils/src/lib.rs | 121 ------------------ 10 files changed, 86 insertions(+), 176 deletions(-) rename {third_party/ecdh_psi/psi_utils => crypto/ecc/ecc_ed25519}/Cargo.toml (93%) create mode 100644 crypto/ecc/ecc_ed25519/src/lib.rs rename {third_party/ecdh_psi/ffi_java_ecdh_psi => crypto/ecc/ffi_java_ecc_ed25519}/Cargo.toml (68%) rename {third_party/ecdh_psi/ffi_java_ecdh_psi => crypto/ecc/ffi_java_ecc_ed25519}/src/lib.rs (73%) rename {third_party/ecdh_psi/ffi_wasm_ecdh_psi => crypto/ecc/ffi_wasm_ecc_ed25519}/Cargo.toml (72%) rename {third_party/ecdh_psi/ffi_wasm_ecdh_psi => crypto/ecc/ffi_wasm_ecc_ed25519}/readme.md (100%) rename {third_party/ecdh_psi/ffi_wasm_ecdh_psi => crypto/ecc/ffi_wasm_ecc_ed25519}/src/lib.rs (67%) delete mode 100644 third_party/ecdh_psi/psi_utils/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index e26b171..a50c7ec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -618,6 +618,16 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "ecc_ed25519" +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 = "ed25519" version = "1.0.3" @@ -801,19 +811,19 @@ dependencies = [ ] [[package]] -name = "ffi_java_ecdh_psi" +name = "ffi_java_ecc_ed25519" version = "0.1.0" dependencies = [ + "ecc_ed25519", "jni", - "psi_utils", ] [[package]] -name = "ffi_wasm_ecdh_psi" +name = "ffi_wasm_ecc_ed25519" version = "0.1.0" dependencies = [ + "ecc_ed25519", "getrandom 0.2.2", - "psi_utils", "wasm-bindgen", ] @@ -1354,16 +1364,6 @@ 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.32" diff --git a/Cargo.toml b/Cargo.toml index 09b917f..40e4310 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,7 +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", + "crypto/ecc/ecc_ed25519", + "crypto/ecc/ffi_java_ecc_ed25519", + "crypto/ecc/ffi_wasm_ecc_ed25519", ] diff --git a/third_party/ecdh_psi/psi_utils/Cargo.toml b/crypto/ecc/ecc_ed25519/Cargo.toml similarity index 93% rename from third_party/ecdh_psi/psi_utils/Cargo.toml rename to crypto/ecc/ecc_ed25519/Cargo.toml index d0786f0..dbcdf8e 100644 --- a/third_party/ecdh_psi/psi_utils/Cargo.toml +++ b/crypto/ecc/ecc_ed25519/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "psi_utils" +name = "ecc_ed25519" version = "0.1.0" edition = "2021" diff --git a/crypto/ecc/ecc_ed25519/src/lib.rs b/crypto/ecc/ecc_ed25519/src/lib.rs new file mode 100644 index 0000000..73db4f0 --- /dev/null +++ b/crypto/ecc/ecc_ed25519/src/lib.rs @@ -0,0 +1,58 @@ +use sha2::Sha512; +// use sha2::Digest; +use rand::rngs::ThreadRng; +// use rand::RngCore; +use curve25519_dalek::{edwards::CompressedEdwardsY, 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).to_bytes(); + let opt_point = match CompressedEdwardsY::from_slice(&hash_scalar) { + Ok(v) => v, + Err(_) => return Vec::new(), + }; + + return opt_point.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() +} diff --git a/third_party/ecdh_psi/ffi_java_ecdh_psi/Cargo.toml b/crypto/ecc/ffi_java_ecc_ed25519/Cargo.toml similarity index 68% rename from third_party/ecdh_psi/ffi_java_ecdh_psi/Cargo.toml rename to crypto/ecc/ffi_java_ecc_ed25519/Cargo.toml index 2e83eac..e6b9013 100644 --- a/third_party/ecdh_psi/ffi_java_ecdh_psi/Cargo.toml +++ b/crypto/ecc/ffi_java_ecc_ed25519/Cargo.toml @@ -1,14 +1,14 @@ [package] -name = "ffi_java_ecdh_psi" +name = "ffi_java_ecc_ed25519" 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" +name = "ffi_java_ecc_ed25519" crate-type = [ "cdylib", "staticlib" ] [dependencies] jni = "0.13.0" -psi_utils = { path = "../psi_utils"} \ No newline at end of file +ecc_ed25519 = { path = "../ecc_ed25519"} \ No newline at end of file diff --git a/third_party/ecdh_psi/ffi_java_ecdh_psi/src/lib.rs b/crypto/ecc/ffi_java_ecc_ed25519/src/lib.rs similarity index 73% rename from third_party/ecdh_psi/ffi_java_ecdh_psi/src/lib.rs rename to crypto/ecc/ffi_java_ecc_ed25519/src/lib.rs index 1554822..981dc30 100644 --- a/third_party/ecdh_psi/ffi_java_ecdh_psi/src/lib.rs +++ b/crypto/ecc/ffi_java_ecc_ed25519/src/lib.rs @@ -1,7 +1,7 @@ extern crate jni; -use psi_utils::{ - hash_to_curve, point_scalar_multi, random_scalar, scalar_inverse, +use ecc_ed25519::{ + hash_to_curve, point_scalar_multi, random_scalar, }; use jni::{objects::JClass, sys::jbyteArray, JNIEnv}; @@ -45,28 +45,6 @@ pub extern "system" fn Java_com_webank_wedpr_crypto_NativeInterface_hashToCurve( } } -#[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, diff --git a/third_party/ecdh_psi/ffi_wasm_ecdh_psi/Cargo.toml b/crypto/ecc/ffi_wasm_ecc_ed25519/Cargo.toml similarity index 72% rename from third_party/ecdh_psi/ffi_wasm_ecdh_psi/Cargo.toml rename to crypto/ecc/ffi_wasm_ecc_ed25519/Cargo.toml index fcf5c79..89e608a 100644 --- a/third_party/ecdh_psi/ffi_wasm_ecdh_psi/Cargo.toml +++ b/crypto/ecc/ffi_wasm_ecc_ed25519/Cargo.toml @@ -1,14 +1,14 @@ [package] -name = "ffi_wasm_ecdh_psi" +name = "ffi_wasm_ecc_ed25519" 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" +name = "ffi_wasm_ecc_ed25519" crate-type = ["rlib", "cdylib"] [dependencies] wasm-bindgen = "0.2" getrandom = { version = "0.2", features = ["js"] } -psi_utils = { path = "../psi_utils"} +ecc_ed25519 = { path = "../ecc_ed25519"} diff --git a/third_party/ecdh_psi/ffi_wasm_ecdh_psi/readme.md b/crypto/ecc/ffi_wasm_ecc_ed25519/readme.md similarity index 100% rename from third_party/ecdh_psi/ffi_wasm_ecdh_psi/readme.md rename to crypto/ecc/ffi_wasm_ecc_ed25519/readme.md diff --git a/third_party/ecdh_psi/ffi_wasm_ecdh_psi/src/lib.rs b/crypto/ecc/ffi_wasm_ecc_ed25519/src/lib.rs similarity index 67% rename from third_party/ecdh_psi/ffi_wasm_ecdh_psi/src/lib.rs rename to crypto/ecc/ffi_wasm_ecc_ed25519/src/lib.rs index 3c1605a..904f9c8 100644 --- a/third_party/ecdh_psi/ffi_wasm_ecdh_psi/src/lib.rs +++ b/crypto/ecc/ffi_wasm_ecc_ed25519/src/lib.rs @@ -1,15 +1,10 @@ extern crate wasm_bindgen; -use psi_utils::{ - hash_to_curve, point_scalar_multi, random_scalar, scalar_inverse, +use ecc_ed25519::{ + hash_to_curve, point_scalar_multi, random_scalar, }; 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) diff --git a/third_party/ecdh_psi/psi_utils/src/lib.rs b/third_party/ecdh_psi/psi_utils/src/lib.rs deleted file mode 100644 index 22b247e..0000000 --- a/third_party/ecdh_psi/psi_utils/src/lib.rs +++ /dev/null @@ -1,121 +0,0 @@ -use sha2::Sha512; -// use sha2::Digest; -use rand::rngs::ThreadRng; -// use rand::RngCore; -use curve25519_dalek::{edwards::CompressedEdwardsY, 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).to_bytes(); - let opt_point = match CompressedEdwardsY::from_slice(&hash_scalar) { - Ok(v) => v, - Err(_) => return Vec::new(), - }; - - return opt_point.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::*; - use curve25519_dalek::EdwardsPoint; - use rand::Rng; - use std::ops::Mul; - - #[test] - fn test_flow() { - // 生成一个随机的标量 - let random_scalar1 = random_scalar(); - let random_scalar2 = random_scalar(); - println!("Random Scalar: {:?}", random_scalar1); - // 创建一个随机数生成器 - let mut rng = rand::thread_rng(); - - // 定义要生成的字节长度 - let byte_length = 16; // 例如,生成 16 字节的随机数据 - - // 生成随机字节序列 - let random_bytes: Vec = - (0..byte_length).map(|_| rng.gen()).collect(); - - // 定义一个消息,对其进行哈希并生成哈希点 - let hash_point = hash_to_curve(&random_bytes); - println!("hash_point Scalar: {:?}", hash_point); - - // 定义一个标量并计算其逆元 - let inverse_scalar = scalar_inverse(&random_scalar1); - let test_scalar = Scalar::from_bytes_mod_order( - <[u8; 32]>::try_from(random_scalar1.clone()).unwrap(), - ); - let test_scalar2 = Scalar::from_bytes_mod_order( - <[u8; 32]>::try_from(inverse_scalar.clone()).unwrap(), - ); - let test3 = test_scalar.mul(test_scalar2); - println!("test3 Scalar: {:?}", test3); - let point_mul_result = - point_scalar_multi(&hash_point, &test3.to_bytes().to_vec()); - assert_eq!(point_mul_result, hash_point); - - // 定义一个点和标量,并进行点乘操作 - // let point_mul_result = point_scalar_multi(&hash_point, - // &random_scalar1); let point_mul_result2 = - // point_scalar_multi(&point_mul_result, &inverse_scalar); - // assert_eq!(point_mul_result2, hash_point); - } -}