Skip to content

Commit

Permalink
add jni
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoXuan40404 committed Sep 18, 2020
1 parent bd44429 commit 0a4dd45
Show file tree
Hide file tree
Showing 24 changed files with 1,533 additions and 82 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ members = [
"solution/verifiable_confidential_ledger",
"common/utils",
"common/macros",
"common/protos",
"ffi/ffi_common",
"ffi/ffi_macros",
"ffi/ffi_vcl",
"protos",
]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ cargo doc

## 项目贡献

- 点亮我们的小星星(点击项目左上方Star按钮)
- 点亮我们的小星星(点击项目右上方Star按钮)
- 提交代码(Pull Request),参考我们的代码[贡献流程](./CONTRIBUTING.md)
- [提问和提交BUG](https://github.com/WeBankBlockchain/WeDPR-Lab-Core/issues)
2 changes: 0 additions & 2 deletions common/macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ authors = ["WeDPR <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
6 changes: 3 additions & 3 deletions common/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ macro_rules! wedpr_println {
/// Macros to handle errors and return bool type instead of Result type, which
/// are mainly used to simplify type conversions for Rust FFI.

/// Converts a string into a point if succeeded, otherwise returns false.
/// Converts a string to a point if succeeded, otherwise returns false.
#[macro_export]
macro_rules! string_to_point {
($param:expr) => {
Expand All @@ -34,7 +34,7 @@ macro_rules! string_to_point {
};
}

/// Converts a string into a scalar if succeeded, otherwise returns false.
/// Converts a string to a scalar if succeeded, otherwise returns false.
#[macro_export]
macro_rules! string_to_scalar {
($param:expr) => {
Expand All @@ -48,7 +48,7 @@ macro_rules! string_to_scalar {
};
}

/// Converts a string into a bytes vector if succeeded, otherwise returns false.
/// Converts a string to a bytes vector if succeeded, otherwise returns false.
#[macro_export]
macro_rules! string_to_bytes {
($param:expr) => {
Expand Down
2 changes: 1 addition & 1 deletion crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ rand = "0.6"
secp256k1 = { version = "0.17.2", features = ["recovery"] }
sha3 = "0.8"
wedpr_macros = { path = "../common/macros/"}
wedpr_protos = { path = "../common/protos/" }
wedpr_protos = { path = "../protos/" }
wedpr_utils = { path = "../common/utils" }
2 changes: 1 addition & 1 deletion crypto/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub fn rangeproof_to_string(proof: &RangeProof) -> String {
}

/// Converts an arbitrary string to Scalar.
/// It will hash it first, and transform the numberic value of hash output to
/// It will hash it first, and transform the numeric value of hash output to
/// Scalar.
pub fn hash_to_scalar(value: &str) -> Scalar {
let mut array = [0; 32];
Expand Down
10 changes: 6 additions & 4 deletions crypto/src/zkp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use wedpr_utils::error::WedprError;

/// Uses a smaller value to reduce time cost of using range proofs.
/// Uses a larger value to increase value limit of using range proofs.
/// This is a critical parameter which is recommended to be fixed to
/// prevent unexpected proof validity issues.
const RANGE_SIZE_IN_BITS: usize = 32;
const DEFAULT_BYTES_MESSAGE: &[u8] = b"WeDPR";

Expand Down Expand Up @@ -231,7 +233,7 @@ pub fn verify_value_range_in_batch(
}

/// Proves three commitments satisfying a sum relationship, i.e.
/// the values embeded in them satisfying c1_value + c2_value = c3_value.
/// the values embedded in them satisfying c1_value + c2_value = c3_value.
/// c3_value is not in the argument list, and will be directly computed from
/// c1_value + c2_value.
/// c?_blinding are random blinding values used in the commitments.
Expand Down Expand Up @@ -313,7 +315,7 @@ pub fn prove_sum_relationship(
}

/// Verifies three commitments satisfying a sum relationship, i.e.
/// the values embeded in c1_point, c2_point, c3_point satisfying
/// the values embedded in c1_point, c2_point, c3_point satisfying
/// c1_value + c2_value = c3_value.
pub fn verify_sum_relationship(
c1_point: &RistrettoPoint,
Expand Down Expand Up @@ -362,7 +364,7 @@ pub fn verify_sum_relationship(
}

/// Proves three commitments satisfying a product relationship, i.e.
/// the values embeded in them satisfying c1_value * c2_value = c3_value.
/// the values embedded in them satisfying c1_value * c2_value = c3_value.
/// c3_value is not in the argument list, and will be directly computed from
/// c1_value * c2_value.
/// c?_blinding are random blinding values used in the commitments.
Expand Down Expand Up @@ -451,7 +453,7 @@ pub fn prove_product_relationship(
}

/// Verifies three commitments satisfying a product relationship, i.e.
/// the values embeded in c1_point, c2_point, c3_point satisfying
/// the values embedded in c1_point, c2_point, c3_point satisfying
/// c1_value * c2_value = c3_value.
pub fn verify_product_relationship(
c1_point: &RistrettoPoint,
Expand Down
14 changes: 14 additions & 0 deletions ffi/ffi_common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "wedpr_ffi_common"
version = "1.0.0"
authors = ["WeDPR <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
jni = "0.13.0"
wedpr_crypto = { path = "../../crypto" }
wedpr_ffi_macros = { path = "../ffi_macros/" }
wedpr_macros = { path = "../../common/macros/" }
wedpr_utils = { path = "../../common/utils" }
14 changes: 14 additions & 0 deletions ffi/ffi_common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0.

//! Library of utilities for FFI (adapting Rust to other programming languages).

#[cfg_attr(tarpaulin, skip)]
pub mod utils;

#[allow(unused_imports)]
#[macro_use]
extern crate wedpr_ffi_macros;

#[allow(unused_imports)]
#[macro_use]
extern crate wedpr_macros;
102 changes: 102 additions & 0 deletions ffi/ffi_common/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0.

//! Common utility functions for FFI.

// From Rust to Java.
extern crate jni;
use self::jni::objects::JByteBuffer;
use jni::{
objects::{JObject, JString, JValue},
sys::jobject,
JNIEnv,
};

use wedpr_crypto::utils;
use wedpr_utils::error::WedprError;

// Java FFI functions.

// Default error field name used by WeDPR FFI output data types.
const DEFAULT_ERROR_FIELD: &str = "wedprErrorMessage";

/// Creates a new Java object of a given class specified by java_class_name.
/// Please note that objects::JObject is the wrapper object type used by FFI
/// logic. You will need to later call .into_inner() function to extract the
/// actual object type (sys::jobject), and return it to Java runtime.
pub fn java_new_jobject<'a>(
_env: &'a JNIEnv,
java_class_name: &'a str,
) -> JObject<'a>
{
let java_class = _env
.find_class(java_class_name)
.expect(&format!("Could not find Java class {}", java_class_name));
let java_object = _env.alloc_object(java_class).expect(&format!(
"Could not allocate Java object for class {}",
java_class_name
));
java_object
}

/// Sets the default error message field and extracts actual java object to
/// return in a erroneous condition.
pub fn java_set_error_field_and_extract_jobject(
_env: &JNIEnv,
java_object: &JObject,
error_message: &str,
) -> jobject
{
let java_string;
// Error message should not be empty.
assert!(!error_message.is_empty());
java_string = _env
.new_string(error_message)
.expect("new_string should not fail");
_env.set_field(
*java_object,
DEFAULT_ERROR_FIELD,
"Ljava/lang/String;",
JValue::from(JObject::from(java_string)),
)
.expect("set_field should not fail");

// Extract actual java object.
java_object.into_inner()
}

/// Converts Java String to Rust bytes.
pub fn java_jstring_to_bytes(
_env: &JNIEnv,
java_string: JString,
) -> Result<Vec<u8>, WedprError>
{
let rust_string = java_jstring_to_string(&_env, java_string)?;
match utils::string_to_bytes(&rust_string) {
Ok(rust_bytes) => Ok(rust_bytes),
Err(_) => return Err(WedprError::FormatError),
}
}

/// Converts Java String to Rust String.
pub fn java_jstring_to_string(
_env: &JNIEnv,
java_string: JString,
) -> Result<String, WedprError>
{
match _env.get_string(java_string) {
Ok(java_string_data) => Ok(java_string_data.into()),
Err(_) => return Err(WedprError::FormatError),
}
}

/// Converts Java bytes to Rust bytes.
pub fn java_jbytes_to_bytes(
_env: &JNIEnv,
java_bytes: JByteBuffer,
) -> Result<Vec<u8>, WedprError>
{
match _env.get_direct_buffer_address(java_bytes) {
Ok(rust_bytes_array) => Ok(rust_bytes_array.to_vec()),
Err(_) => return Err(WedprError::FormatError),
}
}
7 changes: 7 additions & 0 deletions ffi/ffi_macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "wedpr_ffi_macros"
version = "1.0.0"
authors = ["WeDPR <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Loading

0 comments on commit 0a4dd45

Please sign in to comment.