Skip to content

Commit

Permalink
add vrf feature to binary
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoXuan40404 committed Nov 18, 2024
1 parent a250924 commit 029329a
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion ffi/ffi_c/ffi_c_crypto_binary/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = [ "wedpr_f_ecies_secp256k1", "wedpr_f_signature_secp256k1", "wedpr_f_hash_keccak256", "wedpr_f_signature_sm2", "wedpr_f_hash_sm3", "wedpr_f_vrf_curve25519", "wedpr_f_crypto_block_cipher_aes", "wedpr_f_crypto_block_cipher_sm4", "wedpr_f_hash_ripemd160", "wedpr_f_hash_sha2", "wedpr_f_hash_sha3", "wedpr_f_hash_blake2b", "wedpr_f_signature_ed25519" ]
default = [ "wedpr_f_ecies_secp256k1", "wedpr_f_signature_secp256k1", "wedpr_f_hash_keccak256", "wedpr_f_signature_sm2", "wedpr_f_hash_sm3", "wedpr_f_vrf_curve25519", "wedpr_f_vrf_secp256k1", "wedpr_f_crypto_block_cipher_aes", "wedpr_f_crypto_block_cipher_sm4", "wedpr_f_hash_ripemd160", "wedpr_f_hash_sha2", "wedpr_f_hash_sha3", "wedpr_f_hash_blake2b", "wedpr_f_signature_ed25519" ]
wedpr_f_ecies_secp256k1 = [ "wedpr_l_crypto_ecies_secp256k1" ]
wedpr_f_signature_secp256k1 = [ "wedpr_l_crypto_signature_secp256k1" ]
wedpr_f_hash_keccak256 = [ "wedpr_l_crypto_hash_keccak256" ]
Expand All @@ -18,6 +18,7 @@ wedpr_f_hash_sha2 = [ "wedpr_l_crypto_hash_sha2" ]
wedpr_f_hash_sha3 = [ "wedpr_l_crypto_hash_sha3" ]
wedpr_f_hash_blake2b = [ "wedpr_l_crypto_hash_blake2b" ]
wedpr_f_vrf_curve25519 = [ "wedpr_l_crypto_vrf_curve25519" ]
wedpr_f_vrf_secp256k1 = [ "wedpr_l_crypto_vrf_secp256k1" ]
wedpr_f_crypto_block_cipher_aes = [ "wedpr_l_crypto_block_cipher_aes" ]
wedpr_f_crypto_block_cipher_sm4 = [ "wedpr_l_crypto_block_cipher_sm4" ]

Expand Down Expand Up @@ -45,6 +46,7 @@ wedpr_l_crypto_signature_ed25519 = { path = "../../../crypto/signature/ed25519",
wedpr_l_crypto_signature_secp256k1 = { path = "../../../crypto/signature/secp256k1", optional = true }
wedpr_l_crypto_signature_sm2 = { path = "../../../crypto/signature/sm2", optional = true }
wedpr_l_crypto_vrf_curve25519 = { path = "../../../crypto/vrf/curve25519", optional = true }
wedpr_l_crypto_vrf_secp256k1 = { path = "../../../crypto/vrf/secp256k1", optional = true }
wedpr_l_macros = "1.0.0"
wedpr_l_protos = { path = "../../../protos" }
wedpr_l_utils = "1.1.0"
Expand Down
1 change: 1 addition & 0 deletions ffi/ffi_c/ffi_c_crypto_binary/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ pub mod ecies;
pub mod hash;
pub mod signature;
pub mod vrf;
pub mod vrf_secp256k1;

// C/C++ FFI: C-style interfaces will be generated.
150 changes: 150 additions & 0 deletions ffi/ffi_c/ffi_c_crypto_binary/src/vrf_secp256k1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0.

//! VRF function wrappers.
#![cfg(not(tarpaulin_include))]
#![cfg(feature = "wedpr_f_vrf_secp256k1")]

use wedpr_ffi_common::utils::{
c_read_raw_pointer, c_write_raw_pointer, CInputBuffer, COutputBuffer,
FAILURE, SUCCESS,
};

#[cfg(feature = "wedpr_f_vrf_secp256k1")]
use wedpr_l_crypto_vrf_secp256k1::WedprSecp256k1Vrf;
use wedpr_l_utils::traits::Vrf;

// secp256k1 implementation.

#[no_mangle]
/// C interface for 'wedpr_secp256k1_vrf_derive_public_key'.
pub unsafe extern "C" fn wedpr_secp256k1_vrf_derive_public_key(
raw_private_key: &CInputBuffer,
output_public_key: &mut COutputBuffer,
) -> i8 {
let private_key = c_read_raw_pointer(raw_private_key);

let public_key = WedprSecp256k1Vrf::derive_public_key(&private_key);
std::mem::forget(private_key);
c_write_raw_pointer(&public_key, output_public_key);
SUCCESS
}

#[no_mangle]
/// C interface for 'wedpr_secp256k1_vrf_prove_utf8'.
pub unsafe extern "C" fn wedpr_secp256k1_vrf_prove_utf8(
raw_private_key: &CInputBuffer,
raw_utf8_message: &CInputBuffer,
output_proof: &mut COutputBuffer,
) -> i8 {
let private_key = c_read_raw_pointer(raw_private_key);
let message = c_read_raw_pointer(raw_utf8_message);

let result = WedprSecp256k1Vrf::prove(&private_key, &message);
std::mem::forget(private_key);
std::mem::forget(message);
let proof = match result {
Ok(v) => v,
Err(_) => return FAILURE,
};
c_write_raw_pointer(&proof.encode_proof(), output_proof);
SUCCESS
}

#[no_mangle]
/// C interface for 'wedpr_secp256k1_vrf_prove_fast_utf8'.
pub unsafe extern "C" fn wedpr_secp256k1_vrf_prove_fast_utf8(
raw_private_key: &CInputBuffer,
raw_public_key: &CInputBuffer,
raw_utf8_message: &CInputBuffer,
output_proof: &mut COutputBuffer,
) -> i8 {
let private_key = c_read_raw_pointer(raw_private_key);
let public_key = c_read_raw_pointer(raw_public_key);
let message = c_read_raw_pointer(raw_utf8_message);

let result =
WedprSecp256k1Vrf::prove_fast(&private_key, &public_key, &message);
std::mem::forget(private_key);
std::mem::forget(public_key);
std::mem::forget(message);
let proof = match result {
Ok(v) => v,
Err(_) => return FAILURE,
};
c_write_raw_pointer(&proof.encode_proof(), output_proof);
SUCCESS
}

#[no_mangle]
/// C interface for 'wedpr_secp256k1_vrf_verify_utf8'.
pub unsafe extern "C" fn wedpr_secp256k1_vrf_verify_utf8(
raw_public_key: &CInputBuffer,
raw_utf8_message: &CInputBuffer,
raw_proof: &CInputBuffer,
) -> i8 {
let proof_bytes = c_read_raw_pointer(raw_proof);
let public_key = c_read_raw_pointer(raw_public_key);
let message = c_read_raw_pointer(raw_utf8_message);

let proof = match WedprSecp256k1Vrf::decode_proof(&proof_bytes) {
Ok(v) => v,
Err(_) => {
std::mem::forget(proof_bytes);
std::mem::forget(public_key);
std::mem::forget(message);
return FAILURE;
},
};

let result = proof.verify(&public_key, &message);
std::mem::forget(proof_bytes);
std::mem::forget(public_key);
std::mem::forget(message);
match result {
true => SUCCESS,
false => FAILURE,
}
}

#[no_mangle]
/// C interface for 'wedpr_secp256k1_vrf_proof_to_hash'.
pub unsafe extern "C" fn wedpr_secp256k1_vrf_proof_to_hash(
raw_proof: &CInputBuffer,
output_hash: &mut COutputBuffer,
) -> i8 {
let proof_bytes = c_read_raw_pointer(raw_proof);
let proof = match WedprSecp256k1Vrf::decode_proof(&proof_bytes) {
Ok(v) => v,
Err(_) => {
std::mem::forget(proof_bytes);
return FAILURE;
},
};

let result = proof.proof_to_hash();
std::mem::forget(proof_bytes);
let hash = match result {
Ok(v) => v,
Err(_) => {
return FAILURE;
},
};
c_write_raw_pointer(&hash, output_hash);
SUCCESS
}

#[no_mangle]
/// C interface for 'wedpr_secp256k1_vrf_is_valid_public_key'.
pub unsafe extern "C" fn wedpr_secp256k1_vrf_is_valid_public_key(
raw_public_key: &CInputBuffer,
) -> i8 {
let public_key = c_read_raw_pointer(raw_public_key);

let result = WedprSecp256k1Vrf::is_valid_public_key(&public_key);
std::mem::forget(public_key);
match result {
true => SUCCESS,
false => FAILURE,
}
}

0 comments on commit 029329a

Please sign in to comment.