-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3120555
commit 40a2673
Showing
12 changed files
with
478 additions
and
62 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
|
||
pub mod common; | ||
pub mod ot; | ||
pub mod zkp; | ||
pub mod zkp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<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_hashToCurve( | ||
env: JNIEnv, | ||
_class: JClass, | ||
message: jbyteArray, | ||
) -> jbyteArray { | ||
// 将 jbyteArray 转换成 Vec<u8> | ||
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<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_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, | ||
_class: JClass, | ||
point: jbyteArray, | ||
scalar: jbyteArray, | ||
) -> jbyteArray { | ||
// 将 jbyteArray 转换成 Vec<u8> | ||
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<u8> 转换成 jbyteArray 并返回给Java层 | ||
match env.byte_array_from_slice(&result) { | ||
Ok(array) => array, | ||
Err(_) => env.new_byte_array(0).unwrap(), // 返回空的 jbyteArray | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 模块。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<u8> { | ||
scalar_inverse(scalar) | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn wasm_point_scalar_multi(point: &[u8], scalar: &[u8]) -> Vec<u8> { | ||
point_scalar_multi(point, scalar) | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn wasm_hash_to_curve(message: &[u8]) -> Vec<u8> { | ||
hash_to_curve(message) | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn wasm_random_scalar() -> Vec<u8> { | ||
random_scalar() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<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); | ||
let hash_point = EdwardsPoint::mul_base(&hash_scalar); | ||
return hash_point.compress().to_bytes().to_vec(); | ||
} | ||
|
||
pub fn scalar_inverse(scalar: &[u8]) -> Vec<u8> { | ||
// 检查输入切片是否具有正确的大小 | ||
if scalar.len() != SCALAR_SIZE { | ||
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 inverse_scalar = scalar.invert(); | ||
return inverse_scalar.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() | ||
} | ||
|
||
#[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); | ||
} | ||
} |