Skip to content

Commit

Permalink
add k1 vrf ffi
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoXuan40404 committed Nov 4, 2024
1 parent f924db9 commit 27c38ec
Show file tree
Hide file tree
Showing 4 changed files with 173 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/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_base64", "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_sha3", "wedpr_f_hash_blake2b", "wedpr_f_signature_ed25519" ]
default = [ "wedpr_f_base64", "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_sha3", "wedpr_f_hash_blake2b", "wedpr_f_signature_ed25519" ]
wedpr_f_base64 = [ "wedpr_ffi_common_base64" ]
wedpr_f_hex = [ "wedpr_ffi_common_hex" ]
wedpr_f_ecies_secp256k1 = [ "wedpr_l_crypto_ecies_secp256k1" ]
Expand All @@ -19,6 +19,7 @@ wedpr_f_hash_ripemd160 = [ "wedpr_l_crypto_hash_ripemd160" ]
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 @@ -46,6 +47,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/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,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.
168 changes: 168 additions & 0 deletions ffi/ffi_c/ffi_c_crypto/src/vrf_secp256k1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// 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 libc::c_char;
use std::{ffi::CString, panic, ptr};

#[cfg(feature = "wedpr_f_base64")]
use wedpr_ffi_common_base64::utils::{
bytes_to_string, c_char_pointer_to_string, string_to_bytes, FAILURE,
SUCCESS,
};

#[cfg(feature = "wedpr_f_hex")]
use wedpr_ffi_common_hex::utils::{
bytes_to_string, c_char_pointer_to_string, string_to_bytes, FAILURE,
SUCCESS,
};

#[cfg(feature = "wedpr_f_vrf_secp256k1")]
use wedpr_l_crypto_vrf_secp256k1::WedprSecp256k1Vrf;
use wedpr_l_utils::{tool::string_to_bytes_utf8, traits::Vrf};

// secp256k1 implementation.

#[no_mangle]
/// C interface for 'wedpr_secp256k1_vrf_derive_public_key'.
pub extern "C" fn wedpr_secp256k1_vrf_derive_public_key(
encoded_private_key: *const c_char,
) -> *mut c_char {
let result = panic::catch_unwind(|| {
let private_key = c_safe_c_char_pointer_to_bytes!(encoded_private_key);

let encrypt_data = WedprSecp256k1Vrf::derive_public_key(&private_key);
c_safe_bytes_to_c_char_pointer!(&encrypt_data)
});
c_safe_return!(result)
}

#[no_mangle]
/// C interface for 'wedpr_secp256k1_vrf_prove_utf8'.
pub extern "C" fn wedpr_secp256k1_vrf_prove_utf8(
encoded_private_key: *const c_char,
utf8_message: *const c_char,
) -> *mut c_char {
let result = panic::catch_unwind(|| {
let private_key = c_safe_c_char_pointer_to_bytes!(encoded_private_key);
let message = c_safe_c_char_pointer_to_bytes_utf8!(utf8_message);

let proof = match WedprSecp256k1Vrf::prove(&private_key, &message) {
Ok(v) => v,
Err(_) => {
return ptr::null_mut();
},
};
c_safe_bytes_to_c_char_pointer!(&proof.encode_proof())
});
c_safe_return!(result)
}

#[no_mangle]
/// C interface for 'wedpr_secp256k1_vrf_prove_fast_utf8'.
pub extern "C" fn wedpr_secp256k1_vrf_prove_fast_utf8(
encoded_private_key: *const c_char,
encoded_public_key: *const c_char,
utf8_message: *const c_char,
) -> *mut c_char {
let result = panic::catch_unwind(|| {
let private_key = c_safe_c_char_pointer_to_bytes!(encoded_private_key);
let public_key = c_safe_c_char_pointer_to_bytes!(encoded_public_key);
let message = c_safe_c_char_pointer_to_bytes_utf8!(utf8_message);

let proof = match WedprSecp256k1Vrf::prove_fast(
&private_key,
&public_key,
&message,
) {
Ok(v) => v,
Err(_) => {
return ptr::null_mut();
},
};
c_safe_bytes_to_c_char_pointer!(&proof.encode_proof())
});
c_safe_return!(result)
}

#[no_mangle]
/// C interface for 'wedpr_secp256k1_vrf_verify_utf8'.
pub extern "C" fn wedpr_secp256k1_vrf_verify_utf8(
encoded_public_key: *const c_char,
utf8_message: *const c_char,
encoded_proof: *const c_char,
) -> i8 {
let result = panic::catch_unwind(|| {
let public_key = c_safe_c_char_pointer_to_bytes_with_error_value!(
encoded_public_key,
FAILURE
);
let message = c_safe_c_char_pointer_to_bytes_utf8_with_error_value!(
utf8_message,
FAILURE
);
let proof_bytes = c_safe_c_char_pointer_to_bytes_with_error_value!(
encoded_proof,
FAILURE
);

let proof = match WedprSecp256k1Vrf::decode_proof(&proof_bytes) {
Ok(v) => v,
Err(_) => {
return FAILURE;
},
};
match proof.verify(&public_key, &message) {
true => SUCCESS,
false => FAILURE,
}
});
c_safe_return_with_error_value!(result, FAILURE)
}

#[no_mangle]
/// C interface for 'wedpr_secp256k1_vrf_proof_to_hash'.
pub extern "C" fn wedpr_secp256k1_vrf_proof_to_hash(
encoded_proof: *const c_char,
) -> *mut c_char {
let result = panic::catch_unwind(|| {
let proof_bytes = c_safe_c_char_pointer_to_bytes!(encoded_proof);
let proof = match WedprSecp256k1Vrf::decode_proof(&proof_bytes) {
Ok(v) => v,
Err(_) => {
return ptr::null_mut();
},
};

let hash_bytes = match proof.proof_to_hash() {
Ok(v) => v,
Err(_) => {
return ptr::null_mut();
},
};
c_safe_bytes_to_c_char_pointer!(&hash_bytes)
});
c_safe_return!(result)
}

#[no_mangle]
/// C interface for 'wedpr_secp256k1_vrf_is_valid_public_key'.
pub extern "C" fn wedpr_secp256k1_vrf_is_valid_public_key(
encoded_public_key: *const c_char,
) -> i8 {
let result = panic::catch_unwind(|| {
let public_key = c_safe_c_char_pointer_to_bytes_with_error_value!(
encoded_public_key,
FAILURE
);

match WedprSecp256k1Vrf::is_valid_public_key(&public_key) {
true => SUCCESS,
false => FAILURE,
}
});
c_safe_return_with_error_value!(result, FAILURE)
}

0 comments on commit 27c38ec

Please sign in to comment.