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

optimize code structure #51

Merged
merged 1 commit into from
Sep 20, 2023
Merged
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
28 changes: 14 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "psi_utils"
name = "ecc_ed25519"
version = "0.1.0"
edition = "2021"

Expand Down
58 changes: 58 additions & 0 deletions crypto/ecc/ecc_ed25519/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<u8> {
// 创建一个随机数生成器
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<u8> {
let hash_scalar = Scalar::hash_from_bytes::<Sha512>(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<u8> {
// 检查输入切片是否具有正确的大小
if point.len() != POINT_SIZE || scalar.len() != SCALAR_SIZE {
return Vec::new(); // 如果大小不正确,返回空的 Vec<u8>
}

// 将输入 &[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>
};

// 将输入 &[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()
}
Original file line number Diff line number Diff line change
@@ -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"}
ecc_ed25519 = { path = "../ecc_ed25519"}
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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<u8>
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<u8> 转换成 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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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"}
Original file line number Diff line number Diff line change
@@ -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<u8> {
scalar_inverse(scalar)
}

#[wasm_bindgen]
pub fn wasm_point_scalar_multi(point: &[u8], scalar: &[u8]) -> Vec<u8> {
point_scalar_multi(point, scalar)
Expand Down
121 changes: 0 additions & 121 deletions third_party/ecdh_psi/psi_utils/src/lib.rs

This file was deleted.

Loading